21#include "llvm/Support/DebugLog.h"
24#define GEN_PASS_DEF_INLINER
25#include "mlir/Transforms/Passes.h.inc"
28#define DEBUG_TYPE "inliner-pass"
45 InlinerPass(
const InlinerPass &) =
default;
46 InlinerPass(std::function<
void(OpPassManager &)> defaultPipeline);
47 InlinerPass(std::function<
void(OpPassManager &)> defaultPipeline,
48 llvm::StringMap<OpPassManager> opPipelines);
49 void runOnOperation()
override;
58 static LogicalResult runPipelineHelper(Pass &pass, OpPassManager &pipeline,
60 return mlir::cast<InlinerPass>(pass).runPipeline(pipeline, op);
68 LogicalResult initializeOptions(
70 function_ref<LogicalResult(
const Twine &)> errorHandler)
override;
79InlinerPass::InlinerPass(
81 : InlinerPass(std::move(defaultPipelineArg),
84InlinerPass::InlinerPass(std::function<
void(
OpPassManager &)> defaultPipeline,
85 llvm::StringMap<OpPassManager> opPipelines)
86 :
config(std::move(defaultPipeline), maxInliningIterations) {
87 if (opPipelines.empty())
91 for (
auto &it : opPipelines)
92 opPipelineList.addValue(it.second);
93 config.setOpPipelines(std::move(opPipelines));
98 unsigned inliningThreshold) {
100 if (inliningThreshold == 0U)
103 if (inliningThreshold == -1U)
109 assert(calleeRegion && callerRegion &&
"unexpected external node");
111 auto countOps = [](
Region *region) {
113 region->walk([&](
Operation *) { ++count; });
117 unsigned callerOps = countOps(callerRegion);
123 unsigned ratio = countOps(calleeRegion) * 100 / callerOps;
124 LDBG() <<
"Callee / caller operation ratio (max: " << inliningThreshold
125 <<
"%): " << ratio <<
"%";
126 return ratio <= inliningThreshold;
129void InlinerPass::runOnOperation() {
130 CallGraph &cg = getAnalysis<CallGraph>();
134 Operation *op = getOperation();
135 if (!op->
hasTrait<OpTrait::SymbolTable>()) {
136 op->
emitOpError() <<
" was scheduled to run under the inliner, but does "
137 "not define a symbol table";
138 return signalPassFailure();
142 auto profitabilityCb = [
this](
const Inliner::ResolvedCall &call) {
147 Inliner inliner(op, cg, *
this, getAnalysisManager(), runPipelineHelper,
151 if (
failed(inliner.doInlining()))
155LogicalResult InlinerPass::initializeOptions(
157 function_ref<LogicalResult(
const Twine &)> errorHandler) {
165 if (!defaultPipelineStr.empty()) {
166 std::string defaultPipelineCopy = defaultPipelineStr;
167 config.setDefaultPipeline([=](OpPassManager &pm) {
170 }
else if (defaultPipelineStr.getNumOccurrences()) {
171 config.setDefaultPipeline(
nullptr);
175 llvm::StringMap<OpPassManager> pipelines;
176 for (OpPassManager pipeline : opPipelineList)
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>();
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 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.
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.
virtual LogicalResult initializeOptions(StringRef options, function_ref< LogicalResult(const Twine &)> errorHandler)
Attempt to initialize the options of this pass from the given string.
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.
llvm::function_ref< Fn > function_ref
This struct represents a resolved call to a given callgraph node.
CallGraphNode * sourceNode
CallGraphNode * targetNode