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