Optional<DeallocationFn> deallocationFn;
Optional<MemCpyFn> memCpyFn;
+ /// Create a memref allocation with the given type and dynamic extents.
+ FailureOr<Value> createAlloc(OpBuilder &b, Location loc, MemRefType type,
+ ValueRange dynShape) const;
+
+ /// Creates a memref deallocation. The given memref buffer must have been
+ /// allocated using `createAlloc`.
+ LogicalResult createDealloc(OpBuilder &b, Location loc,
+ Value allocatedBuffer) const;
+
+ /// Creates a memcpy between two given buffers.
+ LogicalResult createMemCpy(OpBuilder &b, Location loc, Value from,
+ Value to) const;
+
/// Specifies whether not bufferizable ops are allowed in the input. If so,
/// bufferization.to_memref and bufferization.to_tensor ops are inserted at
/// the boundaries.
MemRefLayoutAttrInterface layout = {},
Attribute memorySpace = {});
-/// Creates a memref deallocation. The given memref buffer must have been
-/// allocated using `createAlloc`.
-LogicalResult createDealloc(OpBuilder &b, Location loc, Value allocatedBuffer,
- const BufferizationOptions &options);
-
-/// Creates a memcpy between two given buffers.
-LogicalResult createMemCpy(OpBuilder &b, Location loc, Value from, Value to,
- const BufferizationOptions &options);
-
/// Try to hoist all new buffer allocations until the next hoisting barrier.
LogicalResult hoistBufferAllocations(Operation *op,
const BufferizationOptions &options);
// The copy happens right before the op that is bufferized.
rewriter.setInsertionPoint(op);
}
- if (failed(
- createMemCpy(rewriter, loc, operandBuffer, *resultBuffer, options)))
+ if (failed(options.createMemCpy(rewriter, loc, operandBuffer, *resultBuffer)))
return failure();
return resultBuffer;
//===----------------------------------------------------------------------===//
/// Create a memref allocation with the given type and dynamic extents.
-static FailureOr<Value> createAlloc(OpBuilder &b, Location loc, MemRefType type,
- ValueRange dynShape,
- const BufferizationOptions &options) {
- if (options.allocationFn)
- return (*options.allocationFn)(b, loc, type, dynShape,
- options.bufferAlignment);
+FailureOr<Value> BufferizationOptions::createAlloc(OpBuilder &b, Location loc,
+ MemRefType type,
+ ValueRange dynShape) const {
+ if (allocationFn)
+ return (*allocationFn)(b, loc, type, dynShape, bufferAlignment);
// Default bufferallocation via AllocOp.
Value allocated = b.create<memref::AllocOp>(
- loc, type, dynShape, b.getI64IntegerAttr(options.bufferAlignment));
+ loc, type, dynShape, b.getI64IntegerAttr(bufferAlignment));
return allocated;
}
/// Creates a memref deallocation. The given memref buffer must have been
/// allocated using `createAlloc`.
-LogicalResult
-bufferization::createDealloc(OpBuilder &b, Location loc, Value allocatedBuffer,
- const BufferizationOptions &options) {
- if (options.deallocationFn)
- return (*options.deallocationFn)(b, loc, allocatedBuffer);
+LogicalResult BufferizationOptions::createDealloc(OpBuilder &b, Location loc,
+ Value allocatedBuffer) const {
+ if (deallocationFn)
+ return (*deallocationFn)(b, loc, allocatedBuffer);
// Default buffer deallocation via DeallocOp.
b.create<memref::DeallocOp>(loc, allocatedBuffer);
}
/// Create a memory copy between two memref buffers.
-LogicalResult bufferization::createMemCpy(OpBuilder &b, Location loc,
- Value from, Value to,
- const BufferizationOptions &options) {
- if (options.memCpyFn)
- return (*options.memCpyFn)(b, loc, from, to);
+LogicalResult BufferizationOptions::createMemCpy(OpBuilder &b, Location loc,
+ Value from, Value to) const {
+ if (memCpyFn)
+ return (*memCpyFn)(b, loc, from, to);
b.create<memref::CopyOp>(loc, from, to);
return success();
Block *block = allocaOp->getBlock();
rewriter.setInsertionPoint(allocaOp);
FailureOr<Value> alloc =
- createAlloc(rewriter, allocaOp->getLoc(), allocaOp.getType(),
- allocaOp.dynamicSizes(), options);
+ options.createAlloc(rewriter, allocaOp->getLoc(), allocaOp.getType(),
+ allocaOp.dynamicSizes());
if (failed(alloc))
return WalkResult::interrupt();
rewriter.replaceOp(allocaOp, *alloc);
// Create dealloc.
rewriter.setInsertionPoint(block->getTerminator());
- if (failed(createDealloc(rewriter, alloc->getLoc(), *alloc, options)))
+ if (failed(options.createDealloc(rewriter, alloc->getLoc(), *alloc)))
return WalkResult::interrupt();
return WalkResult::advance();
if (!inplace) {
// Do not copy if the copied data is never read.
if (state.getAnalysisState().isValueRead(extractSliceOp.result()))
- if (failed(createMemCpy(rewriter, extractSliceOp.getLoc(), subView,
- alloc, state.getOptions())))
+ if (failed(state.getOptions().createMemCpy(
+ rewriter, extractSliceOp.getLoc(), subView, alloc)))
return failure();
subView = alloc;
}
// tensor.extract_slice, the copy operation will eventually fold away.
auto srcMemref =
state.getBuffer(rewriter, insertSliceOp->getOpOperand(0) /*source*/);
- if (failed(srcMemref) || failed(createMemCpy(rewriter, loc, *srcMemref,
- subView, state.getOptions())))
+ if (failed(srcMemref) || failed(state.getOptions().createMemCpy(
+ rewriter, loc, *srcMemref, subView)))
return failure();
replaceOpWithBufferizedValues(rewriter, op, *dstMemref);