1 //===- VectorToLLVM.cpp - Conversion from Vector to the LLVM dialect ------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h" 10 11 #include "../PassDetail.h" 12 13 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h" 14 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h" 15 #include "mlir/Dialect/AMX/AMXDialect.h" 16 #include "mlir/Dialect/AMX/Transforms.h" 17 #include "mlir/Dialect/ArmNeon/ArmNeonDialect.h" 18 #include "mlir/Dialect/ArmSVE/ArmSVEDialect.h" 19 #include "mlir/Dialect/ArmSVE/Transforms.h" 20 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 21 #include "mlir/Dialect/MemRef/IR/MemRef.h" 22 #include "mlir/Dialect/StandardOps/IR/Ops.h" 23 #include "mlir/Dialect/Vector/VectorOps.h" 24 #include "mlir/Dialect/X86Vector/Transforms.h" 25 #include "mlir/Dialect/X86Vector/X86VectorDialect.h" 26 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 27 28 using namespace mlir; 29 using namespace mlir::vector; 30 31 namespace { 32 struct LowerVectorToLLVMPass 33 : public ConvertVectorToLLVMBase<LowerVectorToLLVMPass> { 34 LowerVectorToLLVMPass(const LowerVectorToLLVMOptions &options) { 35 this->reassociateFPReductions = options.reassociateFPReductions; 36 this->enableIndexOptimizations = options.enableIndexOptimizations; 37 this->enableArmNeon = options.enableArmNeon; 38 this->enableArmSVE = options.enableArmSVE; 39 this->enableAMX = options.enableAMX; 40 this->enableX86Vector = options.enableX86Vector; 41 } 42 // Override explicitly to allow conditional dialect dependence. 43 void getDependentDialects(DialectRegistry ®istry) const override { 44 registry.insert<LLVM::LLVMDialect>(); 45 registry.insert<memref::MemRefDialect>(); 46 if (enableArmNeon) 47 registry.insert<arm_neon::ArmNeonDialect>(); 48 if (enableArmSVE) 49 registry.insert<arm_sve::ArmSVEDialect>(); 50 if (enableAMX) 51 registry.insert<amx::AMXDialect>(); 52 if (enableX86Vector) 53 registry.insert<x86vector::X86VectorDialect>(); 54 } 55 void runOnOperation() override; 56 }; 57 } // namespace 58 59 void LowerVectorToLLVMPass::runOnOperation() { 60 // Perform progressive lowering of operations on slices and 61 // all contraction operations. Also applies folding and DCE. 62 { 63 RewritePatternSet patterns(&getContext()); 64 populateVectorToVectorCanonicalizationPatterns(patterns); 65 populateVectorSlicesLoweringPatterns(patterns); 66 populateVectorContractLoweringPatterns(patterns); 67 (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); 68 } 69 70 // Convert to the LLVM IR dialect. 71 LLVMTypeConverter converter(&getContext()); 72 RewritePatternSet patterns(&getContext()); 73 populateVectorMaskMaterializationPatterns(patterns, enableIndexOptimizations); 74 populateVectorToLLVMMatrixConversionPatterns(converter, patterns); 75 populateVectorToLLVMConversionPatterns(converter, patterns, 76 reassociateFPReductions); 77 populateVectorToLLVMMatrixConversionPatterns(converter, patterns); 78 79 // Architecture specific augmentations. 80 LLVMConversionTarget target(getContext()); 81 target.addLegalOp<LLVM::DialectCastOp>(); 82 target.addLegalDialect<memref::MemRefDialect>(); 83 target.addLegalDialect<StandardOpsDialect>(); 84 target.addLegalOp<UnrealizedConversionCastOp>(); 85 if (enableArmNeon) { 86 // TODO: we may or may not want to include in-dialect lowering to 87 // LLVM-compatible operations here. So far, all operations in the dialect 88 // can be translated to LLVM IR so there is no conversion necessary. 89 target.addLegalDialect<arm_neon::ArmNeonDialect>(); 90 } 91 if (enableArmSVE) { 92 configureArmSVELegalizeForExportTarget(target); 93 populateArmSVELegalizeForLLVMExportPatterns(converter, patterns); 94 } 95 if (enableAMX) { 96 configureAMXLegalizeForExportTarget(target); 97 populateAMXLegalizeForLLVMExportPatterns(converter, patterns); 98 } 99 if (enableX86Vector) { 100 configureX86VectorLegalizeForExportTarget(target); 101 populateX86VectorLegalizeForLLVMExportPatterns(converter, patterns); 102 } 103 104 if (failed( 105 applyPartialConversion(getOperation(), target, std::move(patterns)))) 106 signalPassFailure(); 107 } 108 109 std::unique_ptr<OperationPass<ModuleOp>> 110 mlir::createConvertVectorToLLVMPass(const LowerVectorToLLVMOptions &options) { 111 return std::make_unique<LowerVectorToLLVMPass>(options); 112 } 113