Skip to content

[MLIR] Setting MemorySpace During Bufferization #78484

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ struct BufferizationOptions {
/// Parameters: Value, memory space, bufferization options
using UnknownTypeConverterFn = std::function<BaseMemRefType(
Value, Attribute memorySpace, const BufferizationOptions &)>;
// Produce a MemorySpace attribute from a tensor type
using DefaultMemorySpaceFn =
std::function<std::optional<Attribute>(TensorType t)>;

BufferizationOptions();

Expand Down Expand Up @@ -296,11 +299,6 @@ struct BufferizationOptions {
/// bufferized or not.
bool bufferizeFunctionBoundaries = false;

/// The default memory space that should be used when it cannot be inferred
/// from the context. If case of std::nullopt, bufferization fails when the
/// memory space cannot be inferred at any point.
std::optional<Attribute> defaultMemorySpace = Attribute();

/// Certain ops have aliasing OpOperand/OpResult invariants (e.g., scf.for).
/// If this flag is set to `false`, those invariants are no longer enforced
/// with buffer copies.
Expand Down Expand Up @@ -351,6 +349,13 @@ struct BufferizationOptions {
/// used.
UnknownTypeConverterFn unknownTypeConverterFn = nullptr;

// Use during type conversion to determine the memory space for memref based
// on the original tensor type if the memory space cannot be inferred.
// Returning std::nullopt will cause bufferization to fail (useful to indicate
// failure to determine memory space for a tensor type).
DefaultMemorySpaceFn defaultMemorySpaceFn =
[](TensorType t) -> std::optional<Attribute> { return Attribute(); };

/// Seed for the analysis fuzzer. If set to `0`, the fuzzer is deactivated.
/// Should be used only with `testAnalysisOnly = true`.
unsigned analysisFuzzerSeed = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ struct ConstantOpInterface
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
auto constantOp = cast<arith::ConstantOp>(op);
auto type = constantOp.getType().dyn_cast<RankedTensorType>();

// Only ranked tensors are supported.
if (!type)
return failure();

Attribute memorySpace;
if (options.defaultMemorySpace.has_value())
memorySpace = *options.defaultMemorySpace;
if (auto memSpace = options.defaultMemorySpaceFn(type))
memorySpace = *memSpace;
else
return constantOp->emitError("could not infer memory space");

// Only ranked tensors are supported.
if (!isa<RankedTensorType>(constantOp.getType()))
return failure();

// Only constants inside a module are supported.
auto moduleOp = constantOp->getParentOfType<ModuleOp>();
if (!moduleOp)
Expand Down
14 changes: 8 additions & 6 deletions mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,12 @@ bufferization::getBufferType(Value value, const BufferizationOptions &options,
return bufferizableOp.getBufferType(value, options, invocationStack);

// Op is not bufferizable.
if (!options.defaultMemorySpace.has_value())
auto memSpace =
options.defaultMemorySpaceFn(value.getType().cast<TensorType>());
if (!memSpace.has_value())
return op->emitError("could not infer memory space");

return getMemRefType(value, options, /*layout=*/{},
*options.defaultMemorySpace);
return getMemRefType(value, options, /*layout=*/{}, *memSpace);
}

bool bufferization::hasTensorSemantics(Operation *op) {
Expand Down Expand Up @@ -936,11 +937,12 @@ FailureOr<BaseMemRefType> bufferization::detail::defaultGetBufferType(

// If we do not know the memory space and there is no default memory space,
// report a failure.
if (!options.defaultMemorySpace.has_value())
auto memSpace =
options.defaultMemorySpaceFn(value.getType().cast<TensorType>());
if (!memSpace.has_value())
return op->emitError("could not infer memory space");

return getMemRefType(value, options, /*layout=*/{},
*options.defaultMemorySpace);
return getMemRefType(value, options, /*layout=*/{}, *memSpace);
}

bool bufferization::detail::defaultIsRepetitiveRegion(
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ AllocTensorOp::getBufferType(Value value, const BufferizationOptions &options,
if (failed(copyBufferType))
return failure();
memorySpace = copyBufferType->getMemorySpace();
} else if (options.defaultMemorySpace.has_value()) {
memorySpace = *options.defaultMemorySpace;
} else if (auto ms = options.defaultMemorySpaceFn(getType())) {
memorySpace = *ms;
} else {
return getOperation()->emitError("could not infer memory space");
}
Expand Down
8 changes: 6 additions & 2 deletions mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,12 @@ struct OneShotBufferizePass
opt.dumpAliasSets = dumpAliasSets;
opt.setFunctionBoundaryTypeConversion(
parseLayoutMapOption(functionBoundaryTypeConversion));
if (mustInferMemorySpace)
opt.defaultMemorySpace = std::nullopt;
if (mustInferMemorySpace) {
opt.defaultMemorySpaceFn =
[](TensorType t) -> std::optional<Attribute> {
return std::nullopt;
};
}
opt.printConflicts = printConflicts;
opt.testAnalysisOnly = testAnalysisOnly;
opt.bufferizeFunctionBoundaries = bufferizeFunctionBoundaries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ getBufferizedFunctionArgType(FuncOp funcOp, int64_t index,
assert(tensorType && "expected TensorType");

BaseMemRefType memrefType = options.functionArgTypeConverterFn(
tensorType, *options.defaultMemorySpace, funcOp, options);
tensorType, *options.defaultMemorySpaceFn(tensorType), funcOp, options);

auto layoutAttr = funcOp.getArgAttrOfType<AffineMapAttr>(
index, BufferizationDialect::kBufferLayoutAttrName);
Expand Down Expand Up @@ -443,7 +443,8 @@ struct FuncOpInterface
// Note: If `inferFunctionResultLayout = true`, cast are later folded
// away.
BaseMemRefType resultType = options.functionArgTypeConverterFn(
tensorType, *options.defaultMemorySpace, funcOp, options);
tensorType, *options.defaultMemorySpaceFn(tensorType), funcOp,
options);
Value toMemrefOp = rewriter.create<bufferization::ToMemrefOp>(
loc, resultType, returnVal);
returnValues.push_back(toMemrefOp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,14 @@ struct FromElementsOpInterface
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
auto fromElementsOp = cast<tensor::FromElementsOp>(op);
auto tensorType = cast<RankedTensorType>(fromElementsOp.getType());

// TODO: Implement memory space for this op.
if (options.defaultMemorySpace != Attribute())
if (options.defaultMemorySpaceFn(tensorType) != Attribute())
return op->emitError("memory space not implemented yet");

// Allocate a buffer for the result.
Location loc = op->getLoc();
auto tensorType = cast<RankedTensorType>(fromElementsOp.getType());
auto shape = tensorType.getShape();
// TODO: Create alloc_tensor ops during TensorCopyInsertion.
FailureOr<Value> tensorAlloc = allocateTensorForShapedValue(
Expand Down Expand Up @@ -588,8 +588,10 @@ struct GenerateOpInterface
const BufferizationOptions &options) const {
auto generateOp = cast<tensor::GenerateOp>(op);

auto type = generateOp.getResult().getType();

// TODO: Implement memory space for this op.
if (options.defaultMemorySpace != Attribute())
if (options.defaultMemorySpaceFn(type) != Attribute())
return op->emitError("memory space not implemented yet");

// Allocate memory.
Expand Down Expand Up @@ -1007,10 +1009,6 @@ struct SplatOpInterface
OpBuilder::InsertionGuard g(rewriter);
auto splatOp = cast<tensor::SplatOp>(op);

// TODO: Implement memory space for this op.
if (options.defaultMemorySpace != Attribute())
return op->emitError("memory space not implemented yet");

// Allocate memory.
Location loc = op->getLoc();
FailureOr<Value> tensorAlloc = allocateTensorForShapedValue(
Expand All @@ -1021,6 +1019,11 @@ struct SplatOpInterface

// Create linalg::MapOp.
auto tensorType = cast<RankedTensorType>(tensorAlloc->getType());

// TODO: Implement memory space for this op.
if (options.defaultMemorySpaceFn(tensorType) != Attribute())
return op->emitError("memory space not implemented yet");

auto linalgOp =
rewriter.create<linalg::MapOp>(loc, tensorType, /*inputs=*/ValueRange(),
/*init=*/*tensorAlloc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ struct TestTensorCopyInsertionPass
bufferization::OneShotBufferizationOptions options;
options.allowReturnAllocsFromLoops = allowReturnAllocsFromLoops;
options.bufferizeFunctionBoundaries = bufferizeFunctionBoundaries;
if (mustInferMemorySpace)
options.defaultMemorySpace = std::nullopt;
if (mustInferMemorySpace) {
options.defaultMemorySpaceFn =
[](TensorType t) -> std::optional<Attribute> { return std::nullopt; };
}
if (failed(bufferization::insertTensorCopies(getOperation(), options)))
signalPassFailure();
}
Expand Down