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/AVX512ToLLVM/ConvertAVX512ToLLVM.h" 14 #include "mlir/Conversion/ArmNeonToLLVM/ArmNeonToLLVM.h" 15 #include "mlir/Conversion/ArmSVEToLLVM/ArmSVEToLLVM.h" 16 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h" 17 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h" 18 #include "mlir/Dialect/AVX512/AVX512Dialect.h" 19 #include "mlir/Dialect/ArmNeon/ArmNeonDialect.h" 20 #include "mlir/Dialect/ArmSVE/ArmSVEDialect.h" 21 #include "mlir/Dialect/LLVMIR/LLVMAVX512Dialect.h" 22 #include "mlir/Dialect/LLVMIR/LLVMArmNeonDialect.h" 23 #include "mlir/Dialect/LLVMIR/LLVMArmSVEDialect.h" 24 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 25 #include "mlir/Dialect/StandardOps/IR/Ops.h" 26 #include "mlir/Dialect/Vector/VectorOps.h" 27 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 28 29 using namespace mlir; 30 using namespace mlir::vector; 31 32 namespace { 33 struct LowerVectorToLLVMPass 34 : public ConvertVectorToLLVMBase<LowerVectorToLLVMPass> { 35 LowerVectorToLLVMPass(const LowerVectorToLLVMOptions &options) { 36 this->reassociateFPReductions = options.reassociateFPReductions; 37 this->enableIndexOptimizations = options.enableIndexOptimizations; 38 this->enableArmNeon = options.enableArmNeon; 39 this->enableArmSVE = options.enableArmSVE; 40 this->enableAVX512 = options.enableAVX512; 41 } 42 // Override explicitly to allow conditional dialect dependence. 43 void getDependentDialects(DialectRegistry ®istry) const override { 44 registry.insert<LLVM::LLVMDialect>(); 45 if (enableArmNeon) 46 registry.insert<LLVM::LLVMArmNeonDialect>(); 47 if (enableArmSVE) 48 registry.insert<LLVM::LLVMArmSVEDialect>(); 49 if (enableAVX512) 50 registry.insert<LLVM::LLVMAVX512Dialect>(); 51 } 52 void runOnOperation() override; 53 }; 54 } // namespace 55 56 void LowerVectorToLLVMPass::runOnOperation() { 57 // Perform progressive lowering of operations on slices and 58 // all contraction operations. Also applies folding and DCE. 59 { 60 OwningRewritePatternList patterns; 61 populateVectorToVectorCanonicalizationPatterns(patterns, &getContext()); 62 populateVectorSlicesLoweringPatterns(patterns, &getContext()); 63 populateVectorContractLoweringPatterns(patterns, &getContext()); 64 applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); 65 } 66 67 // Convert to the LLVM IR dialect. 68 LLVMTypeConverter converter(&getContext()); 69 OwningRewritePatternList patterns; 70 populateVectorToLLVMMatrixConversionPatterns(converter, patterns); 71 populateVectorToLLVMConversionPatterns( 72 converter, patterns, reassociateFPReductions, enableIndexOptimizations); 73 populateVectorToLLVMMatrixConversionPatterns(converter, patterns); 74 75 // Architecture specific augmentations. 76 LLVMConversionTarget target(getContext()); 77 target.addLegalOp<LLVM::DialectCastOp>(); 78 target.addLegalDialect<StandardOpsDialect>(); 79 target.addLegalOp<UnrealizedConversionCastOp>(); 80 if (enableArmNeon) { 81 target.addLegalDialect<LLVM::LLVMArmNeonDialect>(); 82 target.addIllegalDialect<arm_neon::ArmNeonDialect>(); 83 populateArmNeonToLLVMConversionPatterns(converter, patterns); 84 } 85 if (enableArmSVE) { 86 target.addLegalDialect<LLVM::LLVMArmSVEDialect>(); 87 target.addIllegalDialect<arm_sve::ArmSVEDialect>(); 88 auto hasScalableVectorType = [](TypeRange types) { 89 for (Type type : types) 90 if (type.isa<arm_sve::ScalableVectorType>()) 91 return true; 92 return false; 93 }; 94 // Remove any ArmSVE-specific types from function signatures and results. 95 populateFuncOpTypeConversionPattern(patterns, &getContext(), converter); 96 target.addDynamicallyLegalOp<FuncOp>([hasScalableVectorType](FuncOp op) { 97 return !hasScalableVectorType(op.getType().getInputs()) && 98 !hasScalableVectorType(op.getType().getResults()); 99 }); 100 target.addDynamicallyLegalOp<CallOp, CallIndirectOp, ReturnOp>( 101 [hasScalableVectorType](Operation *op) { 102 return !hasScalableVectorType(op->getOperandTypes()) && 103 !hasScalableVectorType(op->getResultTypes()); 104 }); 105 populateArmSVEToLLVMConversionPatterns(converter, patterns); 106 } 107 if (enableAVX512) { 108 target.addLegalDialect<LLVM::LLVMAVX512Dialect>(); 109 target.addIllegalDialect<avx512::AVX512Dialect>(); 110 populateAVX512ToLLVMConversionPatterns(converter, patterns); 111 } 112 113 if (failed( 114 applyPartialConversion(getOperation(), target, std::move(patterns)))) 115 signalPassFailure(); 116 } 117 118 std::unique_ptr<OperationPass<ModuleOp>> 119 mlir::createConvertVectorToLLVMPass(const LowerVectorToLLVMOptions &options) { 120 return std::make_unique<LowerVectorToLLVMPass>(options); 121 } 122