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