|
| 1 | +//===- InlinerPass.cpp - Pass to inline function calls --------------------===// |
| 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 basic inlining algorithm that operates bottom up over |
| 10 | +// the Strongly Connect Components(SCCs) of the CallGraph. This enables a more |
| 11 | +// incremental propagation of inlining decisions from the leafs to the roots of |
| 12 | +// the callgraph. |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "mlir/Transforms/Passes.h" |
| 17 | + |
| 18 | +#include "mlir/Analysis/CallGraph.h" |
| 19 | +#include "mlir/Pass/PassManager.h" |
| 20 | +#include "mlir/Transforms/Inliner.h" |
| 21 | + |
| 22 | +namespace mlir { |
| 23 | +#define GEN_PASS_DEF_INLINER |
| 24 | +#include "mlir/Transforms/Passes.h.inc" |
| 25 | +} // namespace mlir |
| 26 | + |
| 27 | +using namespace mlir; |
| 28 | + |
| 29 | +/// This function implements the inliner optimization pipeline. |
| 30 | +static void defaultInlinerOptPipeline(OpPassManager &pm) { |
| 31 | + pm.addPass(createCanonicalizerPass()); |
| 32 | +} |
| 33 | + |
| 34 | +//===----------------------------------------------------------------------===// |
| 35 | +// InlinerPass |
| 36 | +//===----------------------------------------------------------------------===// |
| 37 | + |
| 38 | +namespace { |
| 39 | +class InlinerPass : public impl::InlinerBase<InlinerPass> { |
| 40 | +public: |
| 41 | + InlinerPass(); |
| 42 | + InlinerPass(const InlinerPass &) = default; |
| 43 | + InlinerPass(std::function<void(OpPassManager &)> defaultPipeline); |
| 44 | + InlinerPass(std::function<void(OpPassManager &)> defaultPipeline, |
| 45 | + llvm::StringMap<OpPassManager> opPipelines); |
| 46 | + void runOnOperation() override; |
| 47 | + |
| 48 | + /// A callback provided to the inliner driver to execute |
| 49 | + /// the specified pass pipeline on the given operation |
| 50 | + /// within the context of the current inliner pass, |
| 51 | + /// which is passed as the first argument. |
| 52 | + /// runPipeline API is protected within the Pass class, |
| 53 | + /// so this helper is required to call it from the foreign |
| 54 | + /// inliner driver. |
| 55 | + static LogicalResult runPipelineHelper(Pass &pass, OpPassManager &pipeline, |
| 56 | + Operation *op) { |
| 57 | + return mlir::cast<InlinerPass>(pass).runPipeline(pipeline, op); |
| 58 | + } |
| 59 | + |
| 60 | +private: |
| 61 | + /// Attempt to initialize the options of this pass from the given string. |
| 62 | + /// Derived classes may override this method to hook into the point at which |
| 63 | + /// options are initialized, but should generally always invoke this base |
| 64 | + /// class variant. |
| 65 | + LogicalResult initializeOptions(StringRef options) override; |
| 66 | + |
| 67 | + /// Inliner configuration parameters created from the pass options. |
| 68 | + InlinerConfig config; |
| 69 | +}; |
| 70 | +} // namespace |
| 71 | + |
| 72 | +InlinerPass::InlinerPass() : InlinerPass(defaultInlinerOptPipeline) {} |
| 73 | + |
| 74 | +InlinerPass::InlinerPass( |
| 75 | + std::function<void(OpPassManager &)> defaultPipelineArg) |
| 76 | + : InlinerPass(std::move(defaultPipelineArg), |
| 77 | + llvm::StringMap<OpPassManager>{}) {} |
| 78 | + |
| 79 | +InlinerPass::InlinerPass(std::function<void(OpPassManager &)> defaultPipeline, |
| 80 | + llvm::StringMap<OpPassManager> opPipelines) |
| 81 | + : config(std::move(defaultPipeline), maxInliningIterations) { |
| 82 | + if (opPipelines.empty()) |
| 83 | + return; |
| 84 | + |
| 85 | + // Update the option for the op specific optimization pipelines. |
| 86 | + for (auto &it : opPipelines) |
| 87 | + opPipelineList.addValue(it.second); |
| 88 | + config.setOpPipelines(std::move(opPipelines)); |
| 89 | +} |
| 90 | + |
| 91 | +void InlinerPass::runOnOperation() { |
| 92 | + CallGraph &cg = getAnalysis<CallGraph>(); |
| 93 | + |
| 94 | + // The inliner should only be run on operations that define a symbol table, |
| 95 | + // as the callgraph will need to resolve references. |
| 96 | + Operation *op = getOperation(); |
| 97 | + if (!op->hasTrait<OpTrait::SymbolTable>()) { |
| 98 | + op->emitOpError() << " was scheduled to run under the inliner, but does " |
| 99 | + "not define a symbol table"; |
| 100 | + return signalPassFailure(); |
| 101 | + } |
| 102 | + |
| 103 | + // Get an instance of the inliner. |
| 104 | + Inliner inliner(op, cg, *this, getAnalysisManager(), runPipelineHelper, |
| 105 | + config); |
| 106 | + |
| 107 | + // Run the inlining. |
| 108 | + if (failed(inliner.doInlining())) |
| 109 | + signalPassFailure(); |
| 110 | + return; |
| 111 | +} |
| 112 | + |
| 113 | +LogicalResult InlinerPass::initializeOptions(StringRef options) { |
| 114 | + if (failed(Pass::initializeOptions(options))) |
| 115 | + return failure(); |
| 116 | + |
| 117 | + // Initialize the pipeline builder for operations without the dedicated |
| 118 | + // optimization pipeline in opPipelineList to use the option string. |
| 119 | + // TODO: Use a generic pass manager for the pre-inline pipeline, and remove |
| 120 | + // this. |
| 121 | + if (!defaultPipelineStr.empty()) { |
| 122 | + std::string defaultPipelineCopy = defaultPipelineStr; |
| 123 | + config.setDefaultPipeline([=](OpPassManager &pm) { |
| 124 | + (void)parsePassPipeline(defaultPipelineCopy, pm); |
| 125 | + }); |
| 126 | + } else if (defaultPipelineStr.getNumOccurrences()) { |
| 127 | + config.setDefaultPipeline(nullptr); |
| 128 | + } |
| 129 | + |
| 130 | + // Initialize the op specific pass pipelines. |
| 131 | + llvm::StringMap<OpPassManager> pipelines; |
| 132 | + for (OpPassManager pipeline : opPipelineList) |
| 133 | + if (!pipeline.empty()) |
| 134 | + pipelines.try_emplace(pipeline.getOpAnchorName(), pipeline); |
| 135 | + config.setOpPipelines(std::move(pipelines)); |
| 136 | + |
| 137 | + config.setMaxInliningIterations(maxInliningIterations); |
| 138 | + |
| 139 | + return success(); |
| 140 | +} |
| 141 | + |
| 142 | +std::unique_ptr<Pass> mlir::createInlinerPass() { |
| 143 | + return std::make_unique<InlinerPass>(); |
| 144 | +} |
| 145 | +std::unique_ptr<Pass> |
| 146 | +mlir::createInlinerPass(llvm::StringMap<OpPassManager> opPipelines) { |
| 147 | + return std::make_unique<InlinerPass>(defaultInlinerOptPipeline, |
| 148 | + std::move(opPipelines)); |
| 149 | +} |
| 150 | +std::unique_ptr<Pass> mlir::createInlinerPass( |
| 151 | + llvm::StringMap<OpPassManager> opPipelines, |
| 152 | + std::function<void(OpPassManager &)> defaultPipelineBuilder) { |
| 153 | + return std::make_unique<InlinerPass>(std::move(defaultPipelineBuilder), |
| 154 | + std::move(opPipelines)); |
| 155 | +} |
0 commit comments