-
Notifications
You must be signed in to change notification settings - Fork 15.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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -21,6 +22,7 @@ namespace bufferization { | |
| } // namespace mlir | ||
|
|
||
| using namespace mlir; | ||
| using AllocationFn = bufferization::BufferResultsToOutParamsOpts::AllocationFn; | ||
| using MemCpyFn = bufferization::BufferResultsToOutParamsOpts::MemCpyFn; | ||
|
|
||
| /// Return `true` if the given MemRef type has a fully dynamic layout. | ||
|
|
@@ -121,7 +123,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(); | ||
|
|
@@ -139,9 +142,8 @@ static LogicalResult updateReturnOps(func::FuncOp func, | |
|
|
||
| // Updates all CallOps in the scope of the given ModuleOp by allocating | ||
| // temporary buffers for newly introduced out params. | ||
| static LogicalResult | ||
| updateCalls(ModuleOp module, | ||
| const bufferization::BufferResultsToOutParamsOpts &options) { | ||
| static LogicalResult updateCalls(ModuleOp module, AllocationFn allocationFn, | ||
| std::function<bool(func::FuncOp *)> filterFn) { | ||
| bool didFail = false; | ||
| SymbolTable symtab(module); | ||
| module.walk([&](func::CallOp op) { | ||
|
|
@@ -152,7 +154,7 @@ updateCalls(ModuleOp module, | |
| didFail = true; | ||
| return; | ||
| } | ||
| if (!options.filterFn(&callee)) | ||
| if (!filterFn(&callee)) | ||
| return; | ||
| SmallVector<Value, 6> replaceWithNewCallResults; | ||
| SmallVector<Value, 6> replaceWithOutParams; | ||
|
|
@@ -175,7 +177,13 @@ updateCalls(ModuleOp module, | |
| auto allocType = | ||
| MemRefType::get(memrefType.getShape(), memrefType.getElementType(), | ||
| AffineMap(), memrefType.getMemorySpace()); | ||
| Value outParam = builder.create<memref::AllocOp>(op.getLoc(), allocType); | ||
| auto maybeOutParam = allocationFn(builder, op.getLoc(), allocType); | ||
| if (failed(maybeOutParam)) { | ||
| op.emitError() << "failed to create allocation op"; | ||
| didFail = true; | ||
| return; | ||
| } | ||
| Value outParam = maybeOutParam.value(); | ||
| if (!hasStaticIdentityLayout(memrefType)) { | ||
| // Layout maps are already checked in `updateFuncOp`. | ||
| assert(hasFullyDynamicLayoutMap(memrefType) && | ||
|
|
@@ -224,7 +232,13 @@ LogicalResult mlir::bufferization::promoteBufferResultsToOutParams( | |
| return failure(); | ||
| } | ||
| } | ||
| if (failed(updateCalls(module, options))) | ||
| auto defaultAllocationFn = [](OpBuilder &builder, Location loc, | ||
|
||
| MemRefType type) { | ||
| return builder.create<memref::AllocOp>(loc, type).getResult(); | ||
| }; | ||
| if (failed(updateCalls(module, | ||
| options.allocationFn.value_or(defaultAllocationFn), | ||
| options.filterFn))) | ||
| return failure(); | ||
| return success(); | ||
| } | ||
|
|
||
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
buildAllocinterface method toAllocationOpInterface? 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
AllocationOpInterfaceto 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
updateCallsfor allocating at the call site and uses the arg types to allocate