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 &registry) 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     populateVectorTransposeLoweringPatterns(patterns);
68     (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
69   }
70 
71   // Convert to the LLVM IR dialect.
72   LLVMTypeConverter converter(&getContext());
73   RewritePatternSet patterns(&getContext());
74   populateVectorMaskMaterializationPatterns(patterns, enableIndexOptimizations);
75   populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
76   populateVectorToLLVMConversionPatterns(converter, patterns,
77                                          reassociateFPReductions);
78   populateVectorToLLVMMatrixConversionPatterns(converter, patterns);
79 
80   // Architecture specific augmentations.
81   LLVMConversionTarget target(getContext());
82   target.addLegalOp<LLVM::DialectCastOp>();
83   target.addLegalDialect<memref::MemRefDialect>();
84   target.addLegalDialect<StandardOpsDialect>();
85   target.addLegalOp<UnrealizedConversionCastOp>();
86   if (enableArmNeon) {
87     // TODO: we may or may not want to include in-dialect lowering to
88     // LLVM-compatible operations here. So far, all operations in the dialect
89     // can be translated to LLVM IR so there is no conversion necessary.
90     target.addLegalDialect<arm_neon::ArmNeonDialect>();
91   }
92   if (enableArmSVE) {
93     configureArmSVELegalizeForExportTarget(target);
94     populateArmSVELegalizeForLLVMExportPatterns(converter, patterns);
95   }
96   if (enableAMX) {
97     configureAMXLegalizeForExportTarget(target);
98     populateAMXLegalizeForLLVMExportPatterns(converter, patterns);
99   }
100   if (enableX86Vector) {
101     configureX86VectorLegalizeForExportTarget(target);
102     populateX86VectorLegalizeForLLVMExportPatterns(converter, patterns);
103   }
104 
105   if (failed(
106           applyPartialConversion(getOperation(), target, std::move(patterns))))
107     signalPassFailure();
108 }
109 
110 std::unique_ptr<OperationPass<ModuleOp>>
111 mlir::createConvertVectorToLLVMPass(const LowerVectorToLLVMOptions &options) {
112   return std::make_unique<LowerVectorToLLVMPass>(options);
113 }
114