Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC][mlir] Add profitability callback to the Inliner. #84258

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions mlir/include/mlir/Transforms/Inliner.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ class InlinerConfig {
/// of inlining decisions from the leafs to the roots of the callgraph.
class Inliner {
public:
using RunPipelineHelperTy = std::function<LogicalResult(
Pass &pass, OpPassManager &pipeline, Operation *op)>;

Inliner(Operation *op, CallGraph &cg, Pass &pass, AnalysisManager am,
RunPipelineHelperTy runPipelineHelper, const InlinerConfig &config)
: op(op), cg(cg), pass(pass), am(am),
runPipelineHelper(std::move(runPipelineHelper)), config(config) {}
Inliner(Inliner &) = delete;
void operator=(const Inliner &) = delete;

/// Perform inlining on a OpTrait::SymbolTable operation.
LogicalResult doInlining();

/// This struct represents a resolved call to a given callgraph node. Given
/// that the call does not actually contain a direct reference to the
/// Region(CallGraphNode) that it is dispatching to, we need to resolve them
Expand All @@ -94,7 +81,29 @@ class Inliner {
CallGraphNode *sourceNode, *targetNode;
};

protected:
using RunPipelineHelperTy = std::function<LogicalResult(
Pass &pass, OpPassManager &pipeline, Operation *op)>;

/// Type of the callback answering if it is profitable
/// to inline a callable operation at a call site.
/// It might be the case that the ResolvedCall does not provide
/// enough context to make the profitability decision, so
/// this hook's interface might need to be extended in future.
vzakhari marked this conversation as resolved.
Show resolved Hide resolved
using ProfitabilityCallbackTy = std::function<bool(ResolvedCall &)>;
vzakhari marked this conversation as resolved.
Show resolved Hide resolved

Inliner(Operation *op, CallGraph &cg, Pass &pass, AnalysisManager am,
RunPipelineHelperTy runPipelineHelper, const InlinerConfig &config,
ProfitabilityCallbackTy isProfitableToInline)
: op(op), cg(cg), pass(pass), am(am),
runPipelineHelper(std::move(runPipelineHelper)), config(config),
isProfitableToInline(std::move(isProfitableToInline)) {}
Inliner(Inliner &) = delete;
void operator=(const Inliner &) = delete;

/// Perform inlining on a OpTrait::SymbolTable operation.
LogicalResult doInlining();

private:
/// An OpTrait::SymbolTable operation to run the inlining on.
Operation *op;
/// A CallGraph analysis for the given operation.
Expand All @@ -108,12 +117,12 @@ class Inliner {
const RunPipelineHelperTy runPipelineHelper;
/// The inliner configuration parameters.
const InlinerConfig &config;
/// Returns true, if it is profitable to inline the callable operation
/// at the call site.
ProfitabilityCallbackTy isProfitableToInline;

private:
/// Forward declaration of the class providing the actual implementation.
class Impl;

public:
};
} // namespace mlir

Expand Down
5 changes: 4 additions & 1 deletion mlir/lib/Transforms/InlinerPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ void InlinerPass::runOnOperation() {
return signalPassFailure();
}

// By default, assume that any inlining is profitable.
auto profitabilityCb = [](Inliner::ResolvedCall &) { return true; };
vzakhari marked this conversation as resolved.
Show resolved Hide resolved

// Get an instance of the inliner.
Inliner inliner(op, cg, *this, getAnalysisManager(), runPipelineHelper,
config);
config, profitabilityCb);

// Run the inlining.
if (failed(inliner.doInlining()))
Expand Down
3 changes: 3 additions & 0 deletions mlir/lib/Transforms/Utils/Inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ bool Inliner::Impl::shouldInline(ResolvedCall &resolvedCall) {
if (calleeHasMultipleBlocks && !callerRegionSupportsMultipleBlocks())
return false;

if (!inliner.isProfitableToInline(resolvedCall))
return false;

// Otherwise, inline.
return true;
}
Expand Down
Loading