-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[MLIR] Make OneShotModuleBufferize
use OpInterface
#107295
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
[MLIR] Make OneShotModuleBufferize
use OpInterface
#107295
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir-linalg @llvm/pr-subscribers-mlir Author: Tzung-Han Juang (tzunghanjuang) ChangesDescription:
Limitations:
Related Discord Discussion: Link Full diff: https://github.com/llvm/llvm-project/pull/107295.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp b/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
index 0a4072605c265f..2983af0fcbf3f7 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
@@ -75,7 +75,7 @@ using namespace mlir::bufferization;
using namespace mlir::bufferization::func_ext;
/// A mapping of FuncOps to their callers.
-using FuncCallerMap = DenseMap<func::FuncOp, DenseSet<Operation *>>;
+using FuncCallerMap = DenseMap<FunctionOpInterface, DenseSet<Operation *>>;
/// Get or create FuncAnalysisState.
static FuncAnalysisState &
@@ -247,6 +247,15 @@ static func::FuncOp getCalledFunction(func::CallOp callOp) {
SymbolTable::lookupNearestSymbolFrom(callOp, sym));
}
+static FunctionOpInterface getCalledFunction(CallOpInterface callOp) {
+ SymbolRefAttr sym =
+ llvm::dyn_cast_if_present<SymbolRefAttr>(callOp.getCallableForCallee());
+ if (!sym)
+ return nullptr;
+ return dyn_cast_or_null<FunctionOpInterface>(
+ SymbolTable::lookupNearestSymbolFrom(callOp, sym));
+}
+
/// Gather equivalence info of CallOps.
/// Note: This only adds new equivalence info if the called function was already
/// analyzed.
@@ -277,10 +286,10 @@ static void equivalenceAnalysis(func::FuncOp funcOp,
}
/// Return "true" if the given function signature has tensor semantics.
-static bool hasTensorSignature(func::FuncOp funcOp) {
- return llvm::any_of(funcOp.getFunctionType().getInputs(),
+static bool hasTensorSignature(FunctionOpInterface funcOp) {
+ return llvm::any_of(funcOp.getArgumentTypes(),
llvm::IsaPred<TensorType>) ||
- llvm::any_of(funcOp.getFunctionType().getResults(),
+ llvm::any_of(funcOp.getResultTypes(),
llvm::IsaPred<TensorType>);
}
@@ -291,26 +300,30 @@ static bool hasTensorSignature(func::FuncOp funcOp) {
/// retrieve the called FuncOp from any func::CallOp.
static LogicalResult
getFuncOpsOrderedByCalls(ModuleOp moduleOp,
- SmallVectorImpl<func::FuncOp> &orderedFuncOps,
+ SmallVectorImpl<FunctionOpInterface> &orderedFuncOps,
FuncCallerMap &callerMap) {
// For each FuncOp, the set of functions called by it (i.e. the union of
// symbols of all nested func::CallOp).
- DenseMap<func::FuncOp, DenseSet<func::FuncOp>> calledBy;
+ DenseMap<FunctionOpInterface, DenseSet<FunctionOpInterface>> calledBy;
// For each FuncOp, the number of func::CallOp it contains.
- DenseMap<func::FuncOp, unsigned> numberCallOpsContainedInFuncOp;
- WalkResult res = moduleOp.walk([&](func::FuncOp funcOp) -> WalkResult {
- if (!funcOp.getBody().empty()) {
- func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
- if (!returnOp)
- return funcOp->emitError()
- << "cannot bufferize a FuncOp with tensors and "
- "without a unique ReturnOp";
+ DenseMap<FunctionOpInterface, unsigned> numberCallOpsContainedInFuncOp;
+ WalkResult res = moduleOp.walk([&](FunctionOpInterface funcOp) -> WalkResult {
+ // Only handle ReturnOp if funcOp is exactly the FuncOp type.
+ if(isa<FuncOp>(funcOp)) {
+ FuncOp funcOpCasted = cast<FuncOp>(funcOp);
+ if (!funcOpCasted.getBody().empty()) {
+ func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOpCasted);
+ if (!returnOp)
+ return funcOp->emitError()
+ << "cannot bufferize a FuncOp with tensors and "
+ "without a unique ReturnOp";
+ }
}
// Collect function calls and populate the caller map.
numberCallOpsContainedInFuncOp[funcOp] = 0;
- return funcOp.walk([&](func::CallOp callOp) -> WalkResult {
- func::FuncOp calledFunction = getCalledFunction(callOp);
+ return funcOp.walk([&](CallOpInterface callOp) -> WalkResult {
+ FunctionOpInterface calledFunction = getCalledFunction(callOp);
assert(calledFunction && "could not retrieved called func::FuncOp");
// If the called function does not have any tensors in its signature, then
// it is not necessary to bufferize the callee before the caller.
@@ -379,7 +392,7 @@ mlir::bufferization::analyzeModuleOp(ModuleOp moduleOp,
FuncAnalysisState &funcState = getOrCreateFuncAnalysisState(state);
// A list of functions in the order in which they are analyzed + bufferized.
- SmallVector<func::FuncOp> orderedFuncOps;
+ SmallVector<FunctionOpInterface> orderedFuncOps;
// A mapping of FuncOps to their callers.
FuncCallerMap callerMap;
@@ -388,27 +401,33 @@ mlir::bufferization::analyzeModuleOp(ModuleOp moduleOp,
return failure();
// Analyze ops.
- for (func::FuncOp funcOp : orderedFuncOps) {
- if (!state.getOptions().isOpAllowed(funcOp))
+ for (FunctionOpInterface funcOp : orderedFuncOps) {
+
+ // The following analysis is specific to the FuncOp type.
+ if(!isa<FuncOp>(funcOp))
+ continue;
+ FuncOp funcOpCasted = cast<func::FuncOp>(funcOp);
+
+ if (!state.getOptions().isOpAllowed(funcOpCasted))
continue;
// Now analyzing function.
- funcState.startFunctionAnalysis(funcOp);
+ funcState.startFunctionAnalysis(funcOpCasted);
// Gather equivalence info for CallOps.
- equivalenceAnalysis(funcOp, state, funcState);
+ equivalenceAnalysis(funcOpCasted, state, funcState);
// Analyze funcOp.
- if (failed(analyzeOp(funcOp, state, statistics)))
+ if (failed(analyzeOp(funcOpCasted, state, statistics)))
return failure();
// Run some extra function analyses.
- if (failed(aliasingFuncOpBBArgsAnalysis(funcOp, state, funcState)) ||
- failed(funcOpBbArgReadWriteAnalysis(funcOp, state, funcState)))
+ if (failed(aliasingFuncOpBBArgsAnalysis(funcOpCasted, state, funcState)) ||
+ failed(funcOpBbArgReadWriteAnalysis(funcOpCasted, state, funcState)))
return failure();
// Mark op as fully analyzed.
- funcState.analyzedFuncOps[funcOp] = FuncOpAnalysisState::Analyzed;
+ funcState.analyzedFuncOps[funcOpCasted] = FuncOpAnalysisState::Analyzed;
}
return success();
@@ -430,20 +449,20 @@ LogicalResult mlir::bufferization::bufferizeModuleOp(
IRRewriter rewriter(moduleOp.getContext());
// A list of functions in the order in which they are analyzed + bufferized.
- SmallVector<func::FuncOp> orderedFuncOps;
+ SmallVector<FunctionOpInterface> orderedFuncOps;
// A mapping of FuncOps to their callers.
FuncCallerMap callerMap;
if (failed(getFuncOpsOrderedByCalls(moduleOp, orderedFuncOps, callerMap)))
return failure();
+ SmallVector<FunctionOpInterface> ops;
// Bufferize functions.
- for (func::FuncOp funcOp : orderedFuncOps) {
+ for (FunctionOpInterface funcOp : orderedFuncOps) {
// Note: It would be good to apply cleanups here but we cannot as aliasInfo
// would be invalidated.
-
- if (llvm::is_contained(options.noAnalysisFuncFilter, funcOp.getSymName())) {
+ if (llvm::is_contained(options.noAnalysisFuncFilter, funcOp.getName())) {
// This function was not analyzed and RaW conflicts were not resolved.
// Buffer copies must be inserted before every write.
OneShotBufferizationOptions updatedOptions = options;
@@ -456,8 +475,8 @@ LogicalResult mlir::bufferization::bufferizeModuleOp(
}
// Change buffer return types to more precise layout maps.
- if (options.inferFunctionResultLayout)
- foldMemRefCasts(funcOp);
+ if (options.inferFunctionResultLayout && isa<func::FuncOp>(funcOp))
+ foldMemRefCasts(cast<func::FuncOp>(funcOp));
}
// Bufferize all other ops.
|
mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
Outdated
Show resolved
Hide resolved
mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
Outdated
Show resolved
Hide resolved
mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
Outdated
Show resolved
Hide resolved
mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
Outdated
Show resolved
Hide resolved
mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
Outdated
Show resolved
Hide resolved
mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp
Outdated
Show resolved
Hide resolved
…ze.cpp Co-authored-by: erick-xanadu <[email protected]>
…ze.cpp Co-authored-by: erick-xanadu <[email protected]>
…ze.cpp Co-authored-by: erick-xanadu <[email protected]>
…orm/ChH/full.mlir
Hi @matthias-springer , this PR is ready to merge but we do not have any write access. Could you please merge this for us? Thank you very much. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
@tzunghanjuang Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/177/builds/5589 Here is the relevant piece of the build log for the reference
|
…)" This reverts commit f586b1e.
) Reverts #107295 This commit breaks an integration test: ``` build/bin/mlir-opt mlir/test/Integration/Dialect/Complex/CPU/correctness.mlir -one-shot-bufferize="bufferize-function-boundaries" ```
@tzunghanjuang This broke an integration test. Can you take fix it an reopen this PR, then I'm going to merge the fixed PR again. |
Hi @matthias-springer , the error is triggered by |
I do not have the access to reopen this PR. I create a new one with the fix: #110322. |
**Description:** This PR replaces a part of `FuncOp` and `CallOp` with `FunctionOpInterface` and `CallOpInterface` in `OneShotModuleBufferize`. Also fix the error from an integration test in the a previous PR attempt. (#107295) The below fixes skip `CallOpInterface` so that the assertions are not triggered. https://github.com/llvm/llvm-project/blob/8d780007625108a7f34e40efb8604b858e04c60c/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp#L254-L259 https://github.com/llvm/llvm-project/blob/8d780007625108a7f34e40efb8604b858e04c60c/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp#L311-L315 **Related Discord Discussion:** [Link](https://discord.com/channels/636084430946959380/642426447167881246/1280556809911799900) --------- Co-authored-by: erick-xanadu <[email protected]>
**Description:** This PR replaces a part of `FuncOp` and `CallOp` with `FunctionOpInterface` and `CallOpInterface` in `OneShotModuleBufferize`. Also fix the error from an integration test in the a previous PR attempt. (llvm/llvm-project#107295) The below fixes skip `CallOpInterface` so that the assertions are not triggered. https://github.com/llvm/llvm-project/blob/8d780007625108a7f34e40efb8604b858e04c60c/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp#L254-L259 https://github.com/llvm/llvm-project/blob/8d780007625108a7f34e40efb8604b858e04c60c/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp#L311-L315 **Related Discord Discussion:** [Link](https://discord.com/channels/636084430946959380/642426447167881246/1280556809911799900) --------- Co-authored-by: erick-xanadu <[email protected]>
**Description:** This PR replaces a part of `FuncOp` and `CallOp` with `FunctionOpInterface` and `CallOpInterface` in `OneShotModuleBufferize`. Also fix the error from an integration test in the a previous PR attempt. (llvm#107295) The below fixes skip `CallOpInterface` so that the assertions are not triggered. https://github.com/llvm/llvm-project/blob/8d780007625108a7f34e40efb8604b858e04c60c/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp#L254-L259 https://github.com/llvm/llvm-project/blob/8d780007625108a7f34e40efb8604b858e04c60c/mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp#L311-L315 **Related Discord Discussion:** [Link](https://discord.com/channels/636084430946959380/642426447167881246/1280556809911799900) --------- Co-authored-by: erick-xanadu <[email protected]>
Description:
OneShotModuleBufferize
deals with the bufferization ofFuncOp
,CallOp
andReturnOp
but they are hard-coded. Any custom function-like operations will not be handled. The PR replaces a part ofFuncOp
andCallOp
withFunctionOpInterface
andCallOpInterface
inOneShotModuleBufferize
so that custom function ops and call ops can be bufferized.Related Discord Discussion: Link