100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a pass for testing the lowering to LLVM as a generally10// 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/UBToLLVM/UBToLLVM.h"24#include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVMPass.h"25#include "mlir/Conversion/VectorToSCF/VectorToSCF.h"26#include "mlir/Dialect/Func/IR/FuncOps.h"27#include "mlir/Dialect/Linalg/Passes.h"28#include "mlir/Dialect/MemRef/Transforms/Passes.h"29#include "mlir/Pass/PassManager.h"30#include "mlir/Pass/PassOptions.h"31#include "mlir/Transforms/Passes.h"32 33using namespace mlir;34 35namespace {36struct TestLowerToLLVMOptions37 : 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)};42};43 44void buildTestLowerToLLVM(OpPassManager &pm,45 const TestLowerToLLVMOptions &options) {46 47 // TODO: it is feasible to scope lowering at arbitrary level and introduce48 // unrealized casts, but there needs to be the final module-wise cleanup in49 // the end. Keep module-level for now.50 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(createSCFToControlFlowPass());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 Arith to LLVM (always needed).77 pm.addPass(createArithToLLVMConversionPass());78 // Convert CF to LLVM (always needed).79 pm.addPass(createConvertControlFlowToLLVMPass());80 // Convert Index to LLVM (always needed).81 pm.addPass(createConvertIndexToLLVMPass());82 // Convert UB to LLVM (always needed).83 pm.addPass(createUBToLLVMConversionPass());84 // Convert remaining unrealized_casts (always needed).85 pm.addPass(createReconcileUnrealizedCastsPass());86}87} // namespace88 89namespace mlir {90namespace test {91void 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 test99} // namespace mlir100