23 #define GEN_PASS_DEF_INLINER
24 #include "mlir/Transforms/Passes.h.inc"
27 #define DEBUG_TYPE "inliner-pass"
41 class InlinerPass :
public impl::InlinerBase<InlinerPass> {
44 InlinerPass(
const InlinerPass &) =
default;
45 InlinerPass(std::function<
void(
OpPassManager &)> defaultPipeline);
46 InlinerPass(std::function<
void(
OpPassManager &)> defaultPipeline,
47 llvm::StringMap<OpPassManager> opPipelines);
48 void runOnOperation()
override;
59 return mlir::cast<InlinerPass>(pass).runPipeline(pipeline, op);
67 LogicalResult initializeOptions(
69 function_ref<LogicalResult(
const Twine &)> errorHandler)
override;
78 InlinerPass::InlinerPass(
80 : InlinerPass(std::move(defaultPipelineArg),
83 InlinerPass::InlinerPass(std::function<
void(
OpPassManager &)> defaultPipeline,
84 llvm::StringMap<OpPassManager> opPipelines)
85 :
config(std::move(defaultPipeline), maxInliningIterations) {
86 if (opPipelines.empty())
90 for (
auto &it : opPipelines)
91 opPipelineList.addValue(it.second);
92 config.setOpPipelines(std::move(opPipelines));
97 unsigned inliningThreshold) {
99 if (inliningThreshold == 0U)
102 if (inliningThreshold == -1U)
108 assert(calleeRegion && callerRegion &&
"unexpected external node");
110 auto countOps = [](
Region *region) {
112 region->walk([&](
Operation *) { ++count; });
116 unsigned callerOps = countOps(callerRegion);
122 unsigned ratio = countOps(calleeRegion) * 100 / callerOps;
123 LLVM_DEBUG(llvm::dbgs() <<
"Callee / caller operation ratio (max: "
124 << inliningThreshold <<
"%): " << ratio <<
"%\n");
125 return ratio <= inliningThreshold;
128 void InlinerPass::runOnOperation() {
129 CallGraph &cg = getAnalysis<CallGraph>();
135 op->
emitOpError() <<
" was scheduled to run under the inliner, but does "
136 "not define a symbol table";
137 return signalPassFailure();
146 Inliner inliner(op, cg, *
this, getAnalysisManager(), runPipelineHelper,
150 if (failed(inliner.doInlining()))
155 LogicalResult InlinerPass::initializeOptions(
157 function_ref<LogicalResult(
const Twine &)> errorHandler) {
158 if (failed(Pass::initializeOptions(
options, errorHandler)))
165 if (!defaultPipelineStr.empty()) {
166 std::string defaultPipelineCopy = defaultPipelineStr;
170 }
else if (defaultPipelineStr.getNumOccurrences()) {
171 config.setDefaultPipeline(
nullptr);
175 llvm::StringMap<OpPassManager> pipelines;
177 if (!pipeline.empty())
178 pipelines.try_emplace(pipeline.getOpAnchorName(), pipeline);
179 config.setOpPipelines(std::move(pipelines));
181 config.setMaxInliningIterations(maxInliningIterations);
187 return std::make_unique<InlinerPass>();
189 std::unique_ptr<Pass>
192 std::move(opPipelines));
195 llvm::StringMap<OpPassManager> opPipelines,
196 std::function<
void(
OpPassManager &)> defaultPipelineBuilder) {
197 return std::make_unique<InlinerPass>(std::move(defaultPipelineBuilder),
198 std::move(opPipelines));
static void defaultInlinerOptPipeline(OpPassManager &pm)
This function implements the inliner optimization pipeline.
static bool isProfitableToInline(const Inliner::ResolvedCall &resolvedCall, unsigned inliningThreshold)
static llvm::ManagedStatic< PassManagerOptions > options
Region * getCallableRegion() const
Returns the callable region this node represents.
This is an implementation of the inliner that operates bottom up over the Strongly Connected Componen...
This class represents a pass manager that runs passes on either a specific operation type,...
void addPass(std::unique_ptr< Pass > pass)
Add the given pass to this pass manager.
A trait used to provide symbol table functionalities to a region operation.
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
The abstract base pass class.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig config
std::unique_ptr< Pass > createInlinerPass()
Creates a pass which inlines calls and callable operations as defined by the CallGraph.
std::unique_ptr< Pass > createCanonicalizerPass()
Creates an instance of the Canonicalizer pass, configured with default settings (which can be overrid...
LogicalResult parsePassPipeline(StringRef pipeline, OpPassManager &pm, raw_ostream &errorStream=llvm::errs())
Parse the textual representation of a pass pipeline, adding the result to 'pm' on success.
This struct represents a resolved call to a given callgraph node.
CallGraphNode * sourceNode
CallGraphNode * targetNode