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/Vector/VectorOps.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->enableAVX512 = options.enableAVX512;
40   }
41   // Override explicitly to allow conditional dialect dependence.
42   void getDependentDialects(DialectRegistry &registry) const override {
43     registry.insert<LLVM::LLVMDialect>();
44     if (enableArmNeon)
45       registry.insert<LLVM::LLVMArmNeonDialect>();
46     if (enableArmSVE)
47       registry.insert<LLVM::LLVMArmSVEDialect>();
48     if (enableAVX512)
49       registry.insert<LLVM::LLVMAVX512Dialect>();
50   }
51   void runOnOperation() override;
52 };
53 } // namespace
54 
55 void LowerVectorToLLVMPass::runOnOperation() {
56   // Perform progressive lowering of operations on slices and
57   // all contraction operations. Also applies folding and DCE.
58   {
59     OwningRewritePatternList patterns;
60     populateVectorToVectorCanonicalizationPatterns(patterns, &getContext());
61     populateVectorSlicesLoweringPatterns(patterns, &getContext());
62     populateVectorContractLoweringPatterns(patterns, &getContext());
63     applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
64   }
65 
66   // Convert to the LLVM IR dialect.
67   LLVMTypeConverter converter(&getContext());
68   OwningRewritePatternList patterns;
69   populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
70   populateVectorToLLVMConversionPatterns(
71       converter, patterns, reassociateFPReductions, enableIndexOptimizations);
72   populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
73   populateStdToLLVMConversionPatterns(converter, patterns);
74 
75   // Architecture specific augmentations.
76   LLVMConversionTarget target(getContext());
77   if (enableArmNeon) {
78     target.addLegalDialect<LLVM::LLVMArmNeonDialect>();
79     target.addIllegalDialect<arm_neon::ArmNeonDialect>();
80     populateArmNeonToLLVMConversionPatterns(converter, patterns);
81   }
82   if (enableArmSVE) {
83     target.addLegalDialect<LLVM::LLVMArmSVEDialect>();
84     target.addIllegalDialect<arm_sve::ArmSVEDialect>();
85     populateArmSVEToLLVMConversionPatterns(converter, patterns);
86   }
87   if (enableAVX512) {
88     target.addLegalDialect<LLVM::LLVMAVX512Dialect>();
89     target.addIllegalDialect<avx512::AVX512Dialect>();
90     populateAVX512ToLLVMConversionPatterns(converter, patterns);
91   }
92 
93   if (failed(
94           applyPartialConversion(getOperation(), target, std::move(patterns))))
95     signalPassFailure();
96 }
97 
98 std::unique_ptr<OperationPass<ModuleOp>>
99 mlir::createConvertVectorToLLVMPass(const LowerVectorToLLVMOptions &options) {
100   return std::make_unique<LowerVectorToLLVMPass>(options);
101 }
102