1 //===- TestLowerToLLVM.cpp - Test lowering to LLVM as a sink pass ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a pass for testing the lowering to LLVM as a generally
12 //===----------------------------------------------------------------------===//
14 #include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
15 #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
16 #include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
17 #include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
18 #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
19 #include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
20 #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
21 #include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVMPass.h"
22 #include "mlir/Conversion/VectorToSCF/VectorToSCF.h"
23 #include "mlir/Dialect/Func/IR/FuncOps.h"
24 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
25 #include "mlir/Dialect/Linalg/Passes.h"
26 #include "mlir/Dialect/MemRef/Transforms/Passes.h"
27 #include "mlir/IR/DialectRegistry.h"
28 #include "mlir/Pass/Pass.h"
29 #include "mlir/Pass/PassManager.h"
30 #include "mlir/Pass/PassOptions.h"
31 #include "mlir/Transforms/Passes.h"
36 struct TestLowerToLLVMOptions
37 : public PassPipelineOptions
<TestLowerToLLVMOptions
> {
38 PassOptions::Option
<bool> reassociateFPReductions
{
39 *this, "reassociate-fp-reductions",
40 llvm::cl::desc("Allow reassociation og FP reductions"),
41 llvm::cl::init(false)};
44 void buildTestLowerToLLVM(OpPassManager
&pm
,
45 const TestLowerToLLVMOptions
&options
) {
47 // TODO: it is feasible to scope lowering at arbitrary level and introduce
48 // unrealized casts, but there needs to be the final module-wise cleanup in
49 // the end. Keep module-level for now.
51 // Blanket-convert any remaining high-level vector ops to loops if any remain.
52 pm
.addNestedPass
<func::FuncOp
>(createConvertVectorToSCFPass());
53 // Blanket-convert any remaining linalg ops to loops if any remain.
54 pm
.addNestedPass
<func::FuncOp
>(createConvertLinalgToLoopsPass());
55 // Blanket-convert any remaining affine ops if any remain.
56 pm
.addPass(createLowerAffinePass());
57 // Convert SCF to CF (always needed).
58 pm
.addPass(createConvertSCFToCFPass());
59 // Sprinkle some cleanups.
60 pm
.addPass(createCanonicalizerPass());
61 pm
.addPass(createCSEPass());
62 // Convert vector to LLVM (always needed).
63 pm
.addPass(createConvertVectorToLLVMPass(
64 // TODO: add more options on a per-need basis.
65 ConvertVectorToLLVMPassOptions
{options
.reassociateFPReductions
}));
66 // Convert Math to LLVM (always needed).
67 pm
.addNestedPass
<func::FuncOp
>(createConvertMathToLLVMPass());
68 // Expand complicated MemRef operations before lowering them.
69 pm
.addPass(memref::createExpandStridedMetadataPass());
70 // The expansion may create affine expressions. Get rid of them.
71 pm
.addPass(createLowerAffinePass());
72 // Convert MemRef to LLVM (always needed).
73 pm
.addPass(createFinalizeMemRefToLLVMConversionPass());
74 // Convert Func to LLVM (always needed).
75 pm
.addPass(createConvertFuncToLLVMPass());
76 // Convert Index to LLVM (always needed).
77 pm
.addPass(createConvertIndexToLLVMPass());
78 // Convert remaining unrealized_casts (always needed).
79 pm
.addPass(createReconcileUnrealizedCastsPass());
85 void registerTestLowerToLLVM() {
86 PassPipelineRegistration
<TestLowerToLLVMOptions
>(
88 "An example of pipeline to lower the main dialects (arith, linalg, "
89 "memref, scf, vector) down to LLVM.",
90 buildTestLowerToLLVM
);