|
| 1 | +//===- TestLowerToLLVM.cpp - Test lowering to LLVM as a sink pass ---------===// |
| 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 | +// This file implements a pass for testing the lowering to LLVM as a generally |
| 10 | +// usable sink pass. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 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/LinalgToLLVM/LinalgToLLVM.h" |
| 18 | +#include "mlir/Conversion/MathToLLVM/MathToLLVM.h" |
| 19 | +#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" |
| 20 | +#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" |
| 21 | +#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" |
| 22 | +#include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h" |
| 23 | +#include "mlir/Conversion/VectorToSCF/VectorToSCF.h" |
| 24 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 25 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 26 | +#include "mlir/Dialect/Linalg/Passes.h" |
| 27 | +#include "mlir/IR/DialectRegistry.h" |
| 28 | +#include "mlir/Pass/Pass.h" |
| 29 | +#include "mlir/Pass/PassManager.h" |
| 30 | +#include "mlir/Transforms/Passes.h" |
| 31 | + |
| 32 | +using namespace mlir; |
| 33 | + |
| 34 | +namespace { |
| 35 | +struct TestLowerToLLVM |
| 36 | + : public PassWrapper<TestLowerToLLVM, OperationPass<ModuleOp>> { |
| 37 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLowerToLLVM) |
| 38 | + |
| 39 | + TestLowerToLLVM() = default; |
| 40 | + TestLowerToLLVM(const TestLowerToLLVM &pass) : PassWrapper(pass) {} |
| 41 | + |
| 42 | + StringRef getArgument() const final { return "test-lower-to-llvm"; } |
| 43 | + StringRef getDescription() const final { |
| 44 | + return "Test lowering to LLVM as a generally usable sink pass"; |
| 45 | + } |
| 46 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 47 | + registry.insert<LLVM::LLVMDialect>(); |
| 48 | + } |
| 49 | + |
| 50 | + Option<bool> reassociateFPReductions{ |
| 51 | + *this, "reassociate-fp-reductions", |
| 52 | + llvm::cl::desc("Allow reassociation og FP reductions"), |
| 53 | + llvm::cl::init(false)}; |
| 54 | + |
| 55 | + void runOnOperation() final; |
| 56 | +}; |
| 57 | +} // namespace |
| 58 | + |
| 59 | +void TestLowerToLLVM::runOnOperation() { |
| 60 | + MLIRContext *context = &this->getContext(); |
| 61 | + RewritePatternSet patterns(context); |
| 62 | + |
| 63 | + // TODO: it is feasible to scope lowering at arbitrary level and introduce |
| 64 | + // unrealized casts, but there needs to be the final module-wise cleanup in |
| 65 | + // the end. Keep module-level for now. |
| 66 | + PassManager pm(&getContext()); |
| 67 | + |
| 68 | + // Blanket-convert any remaining high-level vector ops to loops if any remain. |
| 69 | + pm.addNestedPass<func::FuncOp>(createConvertVectorToSCFPass()); |
| 70 | + // Blanket-convert any remaining linalg ops to loops if any remain. |
| 71 | + pm.addNestedPass<func::FuncOp>(createConvertLinalgToLoopsPass()); |
| 72 | + // Blanket-convert any remaining affine ops if any remain. |
| 73 | + pm.addPass(createLowerAffinePass()); |
| 74 | + // Convert SCF to CF (always needed). |
| 75 | + pm.addPass(createConvertSCFToCFPass()); |
| 76 | + // Sprinkle some cleanups. |
| 77 | + pm.addPass(createCanonicalizerPass()); |
| 78 | + pm.addPass(createCSEPass()); |
| 79 | + // Blanket-convert any remaining linalg ops to LLVM if any remain. |
| 80 | + pm.addPass(createConvertLinalgToLLVMPass()); |
| 81 | + // Convert vector to LLVM (always needed). |
| 82 | + pm.addPass(createConvertVectorToLLVMPass( |
| 83 | + // TODO: add more options on a per-need basis. |
| 84 | + LowerVectorToLLVMOptions().enableReassociateFPReductions( |
| 85 | + reassociateFPReductions))); |
| 86 | + // Convert Math to LLVM (always needed). |
| 87 | + pm.addNestedPass<func::FuncOp>(createConvertMathToLLVMPass()); |
| 88 | + // Convert MemRef to LLVM (always needed). |
| 89 | + pm.addPass(createMemRefToLLVMConversionPass()); |
| 90 | + // Convert Func to LLVM (always needed). |
| 91 | + pm.addPass(createConvertFuncToLLVMPass()); |
| 92 | + // Convert Index to LLVM (always needed). |
| 93 | + pm.addPass(createConvertIndexToLLVMPass()); |
| 94 | + // Convert remaining unrealized_casts (always needed). |
| 95 | + pm.addPass(createReconcileUnrealizedCastsPass()); |
| 96 | + if (failed(pm.run(getOperation()))) { |
| 97 | + getOperation()->dump(); |
| 98 | + return signalPassFailure(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +namespace mlir { |
| 103 | +namespace test { |
| 104 | +void registerTestLowerToLLVM() { PassRegistration<TestLowerToLLVM>(); } |
| 105 | +} // namespace test |
| 106 | +} // namespace mlir |
0 commit comments