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/StandardToLLVM/ConvertStandardToLLVM.h" 15 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h" 16 #include "mlir/Dialect/AVX512/AVX512Dialect.h" 17 #include "mlir/Dialect/LLVMIR/LLVMAVX512Dialect.h" 18 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 19 #include "mlir/Dialect/Vector/VectorOps.h" 20 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 21 22 using namespace mlir; 23 using namespace mlir::vector; 24 25 namespace { 26 struct LowerVectorToLLVMPass 27 : public ConvertVectorToLLVMBase<LowerVectorToLLVMPass> { 28 LowerVectorToLLVMPass(const LowerVectorToLLVMOptions &options) { 29 this->reassociateFPReductions = options.reassociateFPReductions; 30 this->enableIndexOptimizations = options.enableIndexOptimizations; 31 this->enableAVX512 = options.enableAVX512; 32 } 33 void runOnOperation() override; 34 }; 35 } // namespace 36 37 void LowerVectorToLLVMPass::runOnOperation() { 38 // Perform progressive lowering of operations on slices and 39 // all contraction operations. Also applies folding and DCE. 40 { 41 OwningRewritePatternList patterns; 42 populateVectorToVectorCanonicalizationPatterns(patterns, &getContext()); 43 populateVectorSlicesLoweringPatterns(patterns, &getContext()); 44 populateVectorContractLoweringPatterns(patterns, &getContext()); 45 applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); 46 } 47 48 // Convert to the LLVM IR dialect. 49 LLVMTypeConverter converter(&getContext()); 50 OwningRewritePatternList patterns; 51 populateVectorToLLVMMatrixConversionPatterns(converter, patterns); 52 populateVectorToLLVMConversionPatterns( 53 converter, patterns, reassociateFPReductions, enableIndexOptimizations); 54 populateVectorToLLVMMatrixConversionPatterns(converter, patterns); 55 populateStdToLLVMConversionPatterns(converter, patterns); 56 57 // Architecture specific augmentations. 58 LLVMConversionTarget target(getContext()); 59 if (enableAVX512) { 60 target.addLegalDialect<LLVM::LLVMAVX512Dialect>(); 61 target.addIllegalDialect<avx512::AVX512Dialect>(); 62 populateAVX512ToLLVMConversionPatterns(converter, patterns); 63 } 64 65 if (failed( 66 applyPartialConversion(getOperation(), target, std::move(patterns)))) 67 signalPassFailure(); 68 } 69 70 std::unique_ptr<OperationPass<ModuleOp>> 71 mlir::createConvertVectorToLLVMPass(const LowerVectorToLLVMOptions &options) { 72 return std::make_unique<LowerVectorToLLVMPass>(options); 73 } 74