-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Enable custom alloc-like ops in promoteBufferResultsToOutParams
#120288
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
Enable custom alloc-like ops in promoteBufferResultsToOutParams
#120288
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-bufferization Author: None (srcarroll) ChangesIn Full diff: https://github.com/llvm/llvm-project/pull/120288.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/BufferResultsToOutParams.cpp b/mlir/lib/Dialect/Bufferization/Transforms/BufferResultsToOutParams.cpp
index b7755b2be8483b..b4d2d6b0c5da8f 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/BufferResultsToOutParams.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/BufferResultsToOutParams.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
#include "mlir/Dialect/Bufferization/Transforms/Passes.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
@@ -121,7 +122,8 @@ static LogicalResult updateReturnOps(func::FuncOp func,
OpBuilder builder(op);
for (auto [orig, arg] : llvm::zip(copyIntoOutParams, appendedEntryArgs)) {
if (hoistStaticAllocs &&
- isa_and_nonnull<memref::AllocOp>(orig.getDefiningOp()) &&
+ isa_and_nonnull<bufferization::AllocationOpInterface>(
+ orig.getDefiningOp()) &&
mlir::cast<MemRefType>(orig.getType()).hasStaticShape()) {
orig.replaceAllUsesWith(arg);
orig.getDefiningOp()->erase();
|
i think in addition to this, we should also enable custom alloc op in llvm-project/mlir/lib/Dialect/Bufferization/Transforms/BufferResultsToOutParams.cpp Line 180 in 1db8e79
|
AllocationOpInterface
with hoistStaticAllocs
optionpromoteBufferResultsToOutParams
@matthias-springer i ended up doing this, but if you disagree i can always revert. |
@@ -167,6 +174,10 @@ struct BufferResultsToOutParamsOpts { | |||
return true; | |||
}; | |||
|
|||
/// Allocation function; used to allocate a memref. | |||
/// If this is empty, memref.alloc is used | |||
std::optional<AllocationFn> allocationFn; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be avoided by adding a buildAlloc
interface method to AllocationOpInterface
? Maybe the transformation could also supported mixed alloc ops then>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that would work actually. wouldn't you need an instance of an AllocationOpInterface
to call the method? In this context there is no such instance, since we are creating a new one. Maybe i'm missing something. Also I do think I prefer this since it mirrors alloc and copy customization in bufferization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you only have a memref SSA value, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yah, this is used in updateCalls
for allocating at the call site and uses the arg types to allocate
@@ -224,7 +232,13 @@ LogicalResult mlir::bufferization::promoteBufferResultsToOutParams( | |||
return failure(); | |||
} | |||
} | |||
if (failed(updateCalls(module, options))) | |||
auto defaultAllocationFn = [](OpBuilder &builder, Location loc, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could give keep this as a default value in the struct, then the field doesn't have to be optional and you can pass the options
to updateCalls
. Not sure if it's possible without including MemRefOps.h
in Passes.h
though. (Maybe we can split the header?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure that sounds good to me. i think i would want to do that with the copy op too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was just kinda following what had already been done with copy. i think they should be treated the same in this context either way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's possible without including MemRefOps.h in Passes.h though. (Maybe we can split the header?)
If you are opposed to including MemRefOps.h
I could just forward declare the appropriate ops if you prefer. I have no particular preference and honestly don't know what's best practice in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, no. i don't think forward declaration would work here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i went ahead and just included MemRef.h
for now. Passes.h
was already included. Naively, I think this is ok since the default use of this would have to link the memref dialect anyway if I'm not mistaken. But let me know if this is no good
f868c2a
to
cc788e0
Compare
@matthias-springer i barely noticed you approved, but I think it was before my latest commit so just wanted to make sure it's still good to go. thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/94/builds/3216 Here is the relevant piece of the build log for the reference
|
In
buffer-results-to-out-params
, whenhoist-static-allocs
option is enabled the pass was looking formemref.alloc
s in order to attempt to avoid copies when it can. Which makes it not extensible to external ops that have allocation like properties. This patch simply changesmemref::AllocOp
toAllocationOpInterface
in the check to enable for any allocation op.Moreover, for function call updates, we enable setting an allocation function callback in
BufferResultsToOutParamsOpts
to allow users to emit their own alloc-like op.