Repository navigation
[mlir][bufferization] Drop TensorLikeType::getBufferType() - #201350
Conversation
|
@llvm/pr-subscribers-mlir-arith @llvm/pr-subscribers-mlir-tensor Author: Andrei Golubev (andrey-golubev) ChangesReplace TensorLikeType::getBufferType() with Historically, an attempt to support custom types within the one-shot bufferization framework was made. As part of it, It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook manually). On the positive side, this unites builtin and custom tensors implementation as they both feed into unknown type conversion. On the negative side, default one-shot bufferization no longer supports custom types directly - instead, one has to implement their own "pass", overwriting the default bufferization options / hooks to customize the behaviour. As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Patch is 54.36 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/201350.diff 19 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h b/mlir/include/mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h
index 1d959dae56180..bf94f56497160 100644
--- a/mlir/include/mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h
+++ b/mlir/include/mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h
@@ -267,11 +267,11 @@ struct BufferizationOptions {
func::FuncOp, const BufferizationOptions &)>;
/// Tensor -> MemRef type conversion.
/// Parameters: tensor type, memory space, bufferization options
- using UnknownTypeConverterFn = std::function<BaseMemRefType(
- TensorType, Attribute memorySpace, const BufferizationOptions &)>;
+ using UnknownTypeConverterFn = std::function<BufferLikeType(
+ TensorLikeType, Attribute memorySpace, const BufferizationOptions &)>;
// Produce a MemorySpace attribute from a tensor type
using DefaultMemorySpaceFn =
- std::function<std::optional<Attribute>(TensorType t)>;
+ std::function<std::optional<Attribute>(TensorLikeType t)>;
BufferizationOptions();
@@ -340,7 +340,7 @@ struct BufferizationOptions {
///
/// By default, if tensor is a (builtin) tensor type, it is converted to a
/// memref type with a fully dynamic layout map; if tensor is a (generic)
- /// tensor-like type, it is converted using TensorLikeType::getBufferType().
+ /// tensor-like type, it is converted using unknownTypeConverterFn.
///
/// If `bufferizeFunctionBoundaries` is not set, this function isn't used.
FunctionArgTypeConverterFn functionArgTypeConverterFn = nullptr;
@@ -362,7 +362,7 @@ struct BufferizationOptions {
// 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(); };
+ [](TensorLikeType t) -> std::optional<Attribute> { return Attribute(); };
/// If set to `true`, the analysis is skipped. A buffer is copied before every
/// write. This flag cannot be used together with `testAnalysisOnly = true`.
diff --git a/mlir/include/mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.td b/mlir/include/mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.td
index fb6fc4f5ad964..6bbc6cb517766 100644
--- a/mlir/include/mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.td
+++ b/mlir/include/mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.td
@@ -24,19 +24,9 @@ def Bufferization_TensorLikeTypeInterface
}];
let methods = [
- InterfaceMethod<[{
- Returns a BufferLike type for this TensorLike type.
- }],
- /*retTy=*/"::mlir::FailureOr<::mlir::bufferization::BufferLikeType>",
- /*methodName=*/"getBufferType",
- /*args=*/(ins
- "const ::mlir::bufferization::BufferizationOptions &":$options,
- "::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError
- )
- >,
InterfaceMethod<[{
Returns whether a BufferLike type is compatible to this TensorLike type.
- The BufferLike type is assumed to be created by getBufferType().
+ The BufferLike type is assumed to be created by unknown type converter.
}],
/*retTy=*/"::mlir::LogicalResult",
/*methodName=*/"verifyCompatibleBufferType",
diff --git a/mlir/lib/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.cpp b/mlir/lib/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.cpp
index b073a31850678..91d03a0356de1 100644
--- a/mlir/lib/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.cpp
@@ -34,7 +34,8 @@ struct ConstantOpInterface
return failure();
Attribute memorySpace;
- if (auto memSpace = options.defaultMemorySpaceFn(type))
+ if (auto memSpace =
+ options.defaultMemorySpaceFn(cast<TensorLikeType>(type)))
memorySpace = *memSpace;
else
return constantOp->emitError("could not infer memory space");
diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp
index 55ec23aaf40c8..e60736a20dc3b 100644
--- a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp
+++ b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp
@@ -225,7 +225,8 @@ FailureOr<Value> bufferization::allocateTensorForShapedValue(
return failure();
std::optional<Attribute> memorySpace = copyBufferType->getMemorySpace();
if (!memorySpace)
- memorySpace = options.defaultMemorySpaceFn(tensorType);
+ memorySpace =
+ options.defaultMemorySpaceFn(cast<TensorLikeType>(tensorType));
if (memorySpace.has_value())
allocTensorOp.setMemorySpaceAttr(memorySpace.value());
return allocTensorOp.getResult();
@@ -356,18 +357,15 @@ defaultFunctionArgTypeConverter(TensorLikeType type, Attribute memorySpace,
getMemRefTypeWithFullyDynamicLayout(tensorType, memorySpace));
}
- // If not builtin, fallback to TensorLikeType::getBufferType()
- auto bufferType =
- type.getBufferType(options, [&]() { return funcOp->emitError(); });
- assert(succeeded(bufferType) &&
- "a valid buffer is always expected at function boundary");
- return *bufferType;
+ // If not builtin, fallback to unknown type conversion.
+ return options.unknownTypeConverterFn(type, memorySpace, options);
}
/// Default unknown type converter: Use a fully dynamic layout map.
-BaseMemRefType
-defaultUnknownTypeConverter(TensorType tensorType, Attribute memorySpace,
+BufferLikeType
+defaultUnknownTypeConverter(TensorLikeType tensorType, Attribute memorySpace,
const BufferizationOptions &options) {
- return getMemRefTypeWithFullyDynamicLayout(tensorType, memorySpace);
+ return cast<BufferLikeType>(getMemRefTypeWithFullyDynamicLayout(
+ cast<TensorType>(tensorType), memorySpace));
}
} // namespace
@@ -417,12 +415,8 @@ void BufferizationOptions::setFunctionBoundaryTypeConversion(
memorySpace));
}
- // If not builtin, fallback to TensorLikeType::getBufferType()
- auto bufferType =
- type.getBufferType(options, [&]() { return funcOp->emitError(); });
- assert(succeeded(bufferType) &&
- "a valid buffer is always expected at function boundary");
- return *bufferType;
+ // If not builtin, fallback to unknown type conversion.
+ return options.unknownTypeConverterFn(type, memorySpace, options);
};
inferFunctionResultLayout =
layoutMapOption == LayoutMapOption::InferLayoutMap;
@@ -739,9 +733,13 @@ bufferization::getBufferType(Value value, const BufferizationOptions &options,
return bufferizableOp.getBufferType(value, options, state, invocationStack);
// Op is not bufferizable.
- return cast<TensorLikeType>(value.getType()).getBufferType(options, [&]() {
- return op->emitError();
- });
+ auto memSpace =
+ options.defaultMemorySpaceFn(cast<TensorLikeType>(value.getType()));
+ if (!memSpace.has_value())
+ return op->emitError("could not infer memory space");
+
+ return options.unknownTypeConverterFn(cast<TensorLikeType>(value.getType()),
+ *memSpace, options);
}
bool bufferization::hasTensorSemantics(Operation *op) {
@@ -957,8 +955,8 @@ FailureOr<BufferLikeType> bufferization::detail::defaultGetBufferType(
// No further analysis is possible for a block argument.
if (llvm::isa<BlockArgument>(value)) {
- return cast<BufferLikeType>(options.unknownTypeConverterFn(
- tensorType, /*memorySpace=*/nullptr, options));
+ return options.unknownTypeConverterFn(cast<TensorLikeType>(tensorType),
+ /*memorySpace=*/nullptr, options);
}
// Value is an OpResult.
@@ -978,12 +976,12 @@ FailureOr<BufferLikeType> bufferization::detail::defaultGetBufferType(
// If we do not know the memory space and there is no default memory space,
// report a failure.
auto memSpace =
- options.defaultMemorySpaceFn(cast<TensorType>(value.getType()));
+ options.defaultMemorySpaceFn(cast<TensorLikeType>(tensorType));
if (!memSpace.has_value())
return op->emitError("could not infer memory space");
- return cast<BufferLikeType>(
- options.unknownTypeConverterFn(tensorType, *memSpace, options));
+ return options.unknownTypeConverterFn(cast<TensorLikeType>(tensorType),
+ *memSpace, options);
}
bool bufferization::detail::defaultIsRepetitiveRegion(
diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizationDialect.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizationDialect.cpp
index b36aac8c68d36..3bf2df7a3eae9 100644
--- a/mlir/lib/Dialect/Bufferization/IR/BufferizationDialect.cpp
+++ b/mlir/lib/Dialect/Bufferization/IR/BufferizationDialect.cpp
@@ -39,18 +39,6 @@ template <typename Tensor>
struct BuiltinTensorExternalModel
: TensorLikeType::ExternalModel<BuiltinTensorExternalModel<Tensor>,
Tensor> {
- llvm::FailureOr<BufferLikeType> getBufferType(
- mlir::Type tensor, const BufferizationOptions &options,
- llvm::function_ref<mlir::InFlightDiagnostic()> emitError) const {
- auto tensorType = cast<TensorType>(tensor);
- auto memSpace = options.defaultMemorySpaceFn(tensorType);
- if (!memSpace.has_value())
- return emitError() << "could not infer memory space";
-
- return cast<BufferLikeType>(
- options.unknownTypeConverterFn(tensorType, *memSpace, options));
- }
-
mlir::LogicalResult verifyCompatibleBufferType(
mlir::Type tensor, BufferLikeType bufferType,
llvm::function_ref<mlir::InFlightDiagnostic()> emitError) const {
diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
index c525ec116f699..2a1dcc89a83ed 100644
--- a/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
+++ b/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
@@ -240,7 +240,8 @@ AllocTensorOp::getBufferType(Value value, const BufferizationOptions &options,
if (failed(copyBufferType))
return failure();
memorySpace = copyBufferType->getMemorySpace();
- } else if (auto ms = options.defaultMemorySpaceFn(getType())) {
+ } else if (auto ms = options.defaultMemorySpaceFn(
+ cast<TensorLikeType>(getType()))) {
memorySpace = *ms;
} else {
return getOperation()->emitError("could not infer memory space");
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp
index 84d5bb2d713c1..8bdee09d0d6e7 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp
@@ -79,14 +79,14 @@ struct OneShotBufferizePass
if (mustInferMemorySpace) {
opt.defaultMemorySpaceFn =
- [](TensorType t) -> std::optional<Attribute> {
+ [](TensorLikeType t) -> std::optional<Attribute> {
return std::nullopt;
};
}
if (useEncodingForMemorySpace) {
opt.defaultMemorySpaceFn =
- [](TensorType t) -> std::optional<Attribute> {
+ [](TensorLikeType t) -> std::optional<Attribute> {
if (auto rtt = dyn_cast<RankedTensorType>(t))
return rtt.getEncoding();
return std::nullopt;
@@ -108,17 +108,20 @@ struct OneShotBufferizePass
"'unknown-type-conversion'");
return signalPassFailure();
}
- opt.unknownTypeConverterFn = [=](TensorType tensorType,
+ opt.unknownTypeConverterFn = [=](TensorLikeType type,
Attribute memorySpace,
const BufferizationOptions &options) {
+ const auto tensorType = cast<TensorType>(type);
if (unknownTypeConversionOption == LayoutMapOption::IdentityLayoutMap)
- return bufferization::getMemRefTypeWithStaticIdentityLayout(
- tensorType, memorySpace);
+ return cast<bufferization::BufferLikeType>(
+ bufferization::getMemRefTypeWithStaticIdentityLayout(
+ tensorType, memorySpace));
assert(unknownTypeConversionOption ==
LayoutMapOption::FullyDynamicLayoutMap &&
"invalid layout map option");
- return bufferization::getMemRefTypeWithFullyDynamicLayout(tensorType,
- memorySpace);
+ return cast<bufferization::BufferLikeType>(
+ bufferization::getMemRefTypeWithFullyDynamicLayout(tensorType,
+ memorySpace));
};
// Configure op filter.
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp b/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp
index 3aaa38272935d..8ca968367b026 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp
@@ -54,8 +54,8 @@ void FuncAnalysisState::startFunctionAnalysis(FuncOp funcOp) {
static mlir::Attribute
getDefaultMemorySpace(const BufferizationOptions &options,
TensorLikeType type) {
- if (auto tensorType = dyn_cast<TensorType>(type)) {
- return *options.defaultMemorySpaceFn(tensorType);
+ if (isa<TensorType>(type)) {
+ return *options.defaultMemorySpaceFn(type);
}
return nullptr;
}
@@ -73,7 +73,7 @@ getBufferizedFunctionArgType(FuncOp funcOp, int64_t index,
// Note: For builtin tensors there is additional logic related to layout.
if (auto tensorType = dyn_cast<TensorType>(type)) {
BufferLikeType memrefType = options.functionArgTypeConverterFn(
- type, *options.defaultMemorySpaceFn(tensorType), funcOp, options);
+ type, *options.defaultMemorySpaceFn(type), funcOp, options);
auto layoutAttr = funcOp.getArgAttrOfType<MemRefLayoutAttrInterface>(
index, BufferizationDialect::kBufferLayoutAttrName);
diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparsificationAndBufferizationPass.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparsificationAndBufferizationPass.cpp
index 7e8d3600293f8..b38d43476efb7 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/SparsificationAndBufferizationPass.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparsificationAndBufferizationPass.cpp
@@ -220,10 +220,12 @@ mlir::getBufferizationOptionsForSparsification(bool analysisOnly) {
OneShotBufferizationOptions options;
options.bufferizeFunctionBoundaries = true;
options.setFunctionBoundaryTypeConversion(LayoutMapOption::IdentityLayoutMap);
- options.unknownTypeConverterFn = [](TensorType tensorType,
+ options.unknownTypeConverterFn = [](TensorLikeType tensorType,
Attribute memorySpace,
const BufferizationOptions &options) {
- return getMemRefTypeWithStaticIdentityLayout(tensorType, memorySpace);
+ return cast<bufferization::BufferLikeType>(
+ getMemRefTypeWithStaticIdentityLayout(cast<TensorType>(tensorType),
+ memorySpace));
};
if (analysisOnly) {
options.testAnalysisOnly = true;
diff --git a/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp b/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp
index 310e72587eb81..41a3d96b832d1 100644
--- a/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp
@@ -610,7 +610,7 @@ struct GenerateOpInterface
auto type = generateOp.getResult().getType();
// TODO: Implement memory space for this op.
- if (options.defaultMemorySpaceFn(type) != Attribute())
+ if (options.defaultMemorySpaceFn(cast<TensorLikeType>(type)) != Attribute())
return op->emitError("memory space not implemented yet");
// Allocate memory.
@@ -1062,7 +1062,8 @@ struct SplatOpInterface
auto tensorType = cast<RankedTensorType>(tensorAlloc->getType());
// TODO: Implement memory space for this op.
- if (options.defaultMemorySpaceFn(tensorType) != Attribute())
+ if (options.defaultMemorySpaceFn(cast<TensorLikeType>(tensorType)) !=
+ Attribute())
return op->emitError("memory space not implemented yet");
auto linalgOp = linalg::MapOp::create(rewriter, loc, tensorType,
@@ -1120,7 +1121,8 @@ struct ConcatOpInterface
auto tensorType = cast<RankedTensorType>(tensorAlloc->getType());
// TODO: Implement memory space for this op.
- if (options.defaultMemorySpaceFn(tensorType) != Attribute())
+ if (options.defaultMemorySpaceFn(cast<TensorLikeType>(tensorType)) !=
+ Attribute())
return op->emitError("memory space not implemented yet");
MemRefLayoutAttrInterface layout;
diff --git a/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize.mlir b/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize.mlir
index 8031732011839..4cb08b68fffa0 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize.mlir
@@ -269,41 +269,3 @@ func.func @materialize_in_dest_raw(%f: f32, %f2: f32, %idx: index) -> (tensor<5x
return %0, %r : tensor<5xf32>, f32
}
-
-// -----
-
-// CHECK: func.func @custom_op(
-// CHECK-SAME: %[[ARG:.*]]: !test.test_tensor<[32, 64], f64>
-// CHECK-SAME: ) -> !test.test_tensor<[32, 128], f64> {
-func.func @custom_op(%arg: !test.test_tensor<[32, 64], f64>)
- -> !test.test_tensor<[32, 128], f64> {
- // CHECK: %[[MEMREF:.*]] = bufferization.to_buffer %[[ARG]]
- // CHECK: %[[DUMMY:.*]] = "test.dummy_memref_op"(%[[MEMREF]])
- // CHECK-SAME: : (!test.test_memref<[32, 64], f64>)
- // CHECK-SAME: -> !test.test_memref<[32, 128], f64>
- // CHECK: %[[OUT:.*]] = bufferization.to_tensor %[[DUMMY]]
- %out = "test.dummy_tensor_op"(%arg) : (!test.test_tensor<[32, 64], f64>)
- -> !test.test_tensor<[32, 128], f64>
-
- // CHECK: return %[[OUT]]
- return %out : !test.test_tensor<[32, 128], f64>
-}
-
-// -----
-
-// CHECK: func.func @custom_origin_op()
-// CHECK-SAME: -> !test.test_tensor<[42], f64> {
-func.func @custom_origin_op() -> !test.test_tensor<[42], f64> {
- // CHECK: %[[MEMREF:.*]] = "test.create_memref_op"() : ()
- // CHECK-SAME: -> !test.test_memref<[21], f64>
- // CHECK: %[[DUMMY:.*]] = "test.dummy_memref_op"(%[[MEMREF]])
- // CHECK-SAME: : (!test.test_memref<[21], f64>)
- // CHECK-SAME: -> !test.test_memref<[42], f64>
- %in = "test.create_tensor_op"() : () -> !test.test_tensor<[21], f64>
- %out = "test.dummy_tensor_op"(%in) : (!test.test_tensor<[21], f64>)
- -> !test.test_tensor<[42], f64>
-
- // CHECK: %[[OUT:.*]] = bufferization.to_tensor %[[DUMMY]]
- // CHECK: return %[[OUT]]
- return %out : !test.test_tensor<[42], f64>
-}
diff --git a/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize.mlir b/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize.mlir
index f8d5a1310ebdf..fee3c23367d9c 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize.mlir
@@ -831,62 +831,6 @@ module @inner_module {
// -----
-// CHECK: func.func @custom_types(
-// CHECK-SAME: %[[arg:.*]]: !test.test_memref<[4, 4], f64>
-// CHECK-SAME: ) -> (!test.test_memref<[4, 8], f64>,
-// CHECK-SAME: !test.test_memref<[4, 8], f64>)
-func.func @custom_types(%arg: !test.test_tensor<[4, 4], f64>)
- -> (!test.test_tensor<[4, 8], f64>, !test.test_tensor<[4, 8], f64>) {
- // CHECK: %[[out1:.*]] = "test.dummy_memref_op"(%[[arg]]) :
- // CHECK-SAME: (!test.test_memref<[4, 4], f64>) -> !test.test_memref<[4, 8], f64>
- %out1 = "test.dummy_tensor_op"(%arg) : (!test.test_tensor<[4, 4], f64>)
- -> !test.test_tensor<[4, 8], f64>
-
- // CHECK: %[[alloc:.*]] = "test.create_memref_op"
- // CHECK: %[[out2:.*]] = "test.dummy_memref_op"(%[[alloc]])
- // CHECK-SAME: (!test....
[truncated]
|
|
@matthias-springer gentle ping. |
matthias-springer
left a comment
There was a problem hiding this comment.
Nice cleanup! Can you rephrase the commit message a bit: A paragraph like Note for LLVM integration: with some hints on what to change would be useful.
| def TestTensorType : Test_Type<"TestTensor", | ||
| [Bufferization_TensorLikeTypeInterface, ShapedTypeInterface]> { | ||
| [DeclareTypeInterfaceMethods<Bufferization_TensorLikeTypeInterface, | ||
| ["getBufferType", "verifyCompatibleBufferType"]>, |
There was a problem hiding this comment.
getBufferType should be removed here?
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow custom types to be properly supported. Historically, an attempt to support custom types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow custom types to provide custom bufferization. However, the whole affair still proved to be challenging: there is a completely custom API implementation for custom types, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + custom options in order to achieve correct bufferization. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook manually). On the positive side, this unites builtin and custom tensors implementation as they both feed into unknown type conversion. On the negative side, default one-shot bufferization no longer supports custom types directly - instead, one has to implement their own "pass", overwriting the default bufferization options / hooks to customize the behaviour. As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion.
d6fcd04 to
efcabaa
Compare
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually.
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually.
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually.
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually.
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually.
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually. Signed-off-by: Gladilov, Gleb <[email protected]>
* [mlir][RFC] Allow to skip operands type specification in ASM form * Add FlatSymbolRefAttr and ArrayAttr for memory space representation (llvm#44/llvm#45) * [mlir] Relax ASM parser requirements about operands Type presence * [mlir] Remove LIT tests affected by ASM simplification * Add method getInlineBlockAndPoint for DialectInlinerInterface * Initialize pass options using pipeline options * Fix compilation issue with MSVC * Port Ubunu-24 warning fixes to npu/release/18.x (llvm#45) * Extend Quant dialect with Quantile Quantization type (llvm#53) * Extending QuantileQuantizedType with quantileType mlir::Type member (llvm#60) * Relax negative scales conditions in Quant Dialect (llvm#61) * Adding missing check in QuantileQuantizedPerAxisType::verify (llvm#66) * Adding new quantileType types to mlir test (llvm#85) * Add Value back to SideEffectInterfaces * EISW-130780: remove all wd flag to enable warnings when LLVM_ENABLE_WARNINGS is ON (llvm#99) * EISW-176770 Fix argument parsing issue in mlir-src-sharder after command-line parser reset * Enable quantization for FP4 type * [MLIR] Make printing Value to a stream thread-safe (llvm#185762) * [MLIR] Fix mlir-doc build failures by adding -dialect to add_mlir_doc calls * [MLIR] Add getter for the action handler of MLIR context (llvm#197230) * Silence deprecation warning for OpBuilder::create * [MLIR] make One-Shot and SCF bufferization TensorLikeType-aware (llvm#189073) * [NFC][mlir][bufferization] Remove getMemRefType() helper (llvm#199034) * [mlir][bufferization] Drop TensorLikeType::getBufferType() (llvm#201350) --------- Signed-off-by: Gladilov, Gleb <[email protected]> Signed-off-by: Zhu, Shaojie <[email protected]> Signed-off-by: Dmitrii Makarenko <[email protected]> Co-authored-by: Vladislav Vinogradov <[email protected]> Co-authored-by: Nikita Kudriavtsev <[email protected]> Co-authored-by: Schondube, Jorn <[email protected]> Co-authored-by: Daria Mityagina <[email protected]> Co-authored-by: Luca Sarti <[email protected]> Co-authored-by: Zoran Zomborat <[email protected]> Co-authored-by: Ngun, Raymond <[email protected]> Co-authored-by: Shaojie Zhu <[email protected]> Co-authored-by: Surendar Rama Sitaraman <[email protected]> Co-authored-by: Andruszkiewicz, Jacenty <[email protected]> Co-authored-by: Mehdi Amini <[email protected]> Co-authored-by: Kigyosi Alexandru <[email protected]> Co-authored-by: Dmitrii Makarenko <[email protected]> Co-authored-by: Andrei Golubev <[email protected]>
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported. Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it, TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks. It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook). As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion. Note for LLVM integration: * Implementation(s) of `TensorLikeType::getBufferType()` for (user-provided) tensor types should now be moved into a user-specified `BufferizationOptions::unknownTypeConverterFn()` hook * In case upstream one-shot-bufferization pass was used to bufferize IR with user types, such a setup would likely fail now, since a fallback to type-based bufferization can now only be set through bufferization options, and cannot be configured at the upstream pass level. One needs to create a new pass that sets up the necessary bufferization hooks and calls one-shot-bufferization infrastructure manually. Signed-off-by: Gladilov, Gleb <[email protected]>
…emrefs The copies that One-Shot Bufferize inserts for out-of-place operands and for loop yields are built by `allocateTensorForShapedValue`, which handled only builtin ranked tensors and memrefs: any other tensor-like value (llvm#134220) reached an `llvm_unreachable`. `resolveTensorOpOperandConflicts` skipped every operand that is not a builtin tensor, so an out-of-place operand of another tensor-like type silently got no copy. Both spots carry the TODO "Properly support with options, for now it is hardcoded to builtin Tensor/MemRef types based approach" from llvm#189073, which continued the custom-type support of llvm#142986. This patch narrows that TODO: - `bufferization.alloc_tensor` takes and produces any `TensorLikeType`. The size helpers keep their signatures and additionally assert a ranked tensor result. The verifier accepts dynamic sizes and a size hint only for a ranked tensor result; shape reification and both canonicalization patterns check for one. The builders route through one that takes the declared `TensorLikeType`. The buffer type of a result that is not a builtin ranked tensor comes from `unknownTypeConverterFn` (as in llvm#201350). - `allocateTensorForShapedValue` copies any tensor-like value, and `resolveTensorOpOperandConflicts` resolves every tensor-like operand. What remains, each a diagnostic with a TODO: the allocation hook still takes a `MemRefType`, so a buffer type that is not a memref is reported rather than allocated; and a value that is not a builtin tensor is allocated only as a copy, because its dynamic extents cannot be read before bufferization. Tests: test-one-shot-module-bufferize-copies.mlir runs `-test-one-shot-module-bufferize` with two new options, `allow-return-allocs-from-loops` and `test-tensor-to-memref` (buffers of `!test.test_tensor` become builtin memrefs), on a `scf.for` that yields a value defined outside the loop and on an operand conflict that needs a copy. invalid.mlir checks the new verifier message. Note for LLVM integration: `AllocTensorOp::getType()` is now the generated accessor and returns `TensorLikeType` instead of `RankedTensorType`. No hook signature changes. Code and tests in this change were written with Claude Code. Carried on integration-23 from the branch bufferization-alloc-tensor-tensorlike (b45b943 on upstream main). At 23.1.4 the alloc_tensor model is op methods in IR/BufferizationOps.cpp rather than an external model in Transforms/BufferizableOpInterfaceImpl.cpp; the model's hunks land there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oundaries (#248055) Building on top of #201350, which made `defaultMemorySpaceFn` and `unknownTypeConverterFn` support tensor-like types, this PR removes the restriction that `defaultMemorySpaceFn` is only queried for builtin tensor types at function boundaries. If `defaultMemorySpaceFn` returns `std::nullopt` for a custom tensor type, an error is now emitted, as for builtin tensors. Assisted-by: Claude Code
…emrefs The copies that One-Shot Bufferize inserts for out-of-place operands and for loop yields are built by `allocateTensorForShapedValue`, which handled only builtin ranked tensors and memrefs: any other tensor-like value (llvm#134220) reached an `llvm_unreachable`. `resolveTensorOpOperandConflicts` skipped every operand that is not a builtin tensor, so an out-of-place operand of another tensor-like type silently got no copy. Both spots carry the TODO "Properly support with options, for now it is hardcoded to builtin Tensor/MemRef types based approach" from llvm#189073, which continued the custom-type support of llvm#142986. This patch narrows that TODO: - `bufferization.alloc_tensor` takes and produces any `TensorLikeType`. The size helpers keep their signatures and additionally assert a ranked tensor result. The verifier accepts dynamic sizes and a size hint only for a ranked tensor result; shape reification and both canonicalization patterns check for one. The builders route through one that takes the declared `TensorLikeType`. The buffer type of a result that is not a builtin ranked tensor comes from `unknownTypeConverterFn` (as in llvm#201350). - `allocateTensorForShapedValue` copies any tensor-like value, and `resolveTensorOpOperandConflicts` resolves every tensor-like operand. What remains, each a diagnostic with a TODO: the allocation hook still takes a `MemRefType`, so a buffer type that is not a memref is reported rather than allocated; and a value that is not a builtin tensor is allocated only as a copy, because its dynamic extents cannot be read before bufferization. Tests: test-one-shot-module-bufferize-copies.mlir runs `-test-one-shot-module-bufferize` with two new options, `allow-return-allocs-from-loops` and `test-tensor-to-memref` (buffers of `!test.test_tensor` become builtin memrefs), on a `scf.for` that yields a value defined outside the loop and on an operand conflict that needs a copy. invalid.mlir checks the new verifier message. Note for LLVM integration: `AllocTensorOp::getType()` is now the generated accessor and returns `TensorLikeType` instead of `RankedTensorType`. No hook signature changes. Code and tests in this change were written with Claude Code. Carried on integration-23 from the branch bufferization-alloc-tensor-tensorlike (b45b943 on upstream main). At 23.1.4 the alloc_tensor model is op methods in IR/BufferizationOps.cpp rather than an external model in Transforms/BufferizableOpInterfaceImpl.cpp; the model's hunks land there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace TensorLikeType::getBufferType() with options.unknownTypeConverterFn() hook. Make the hook work with tensor-like and buffer-like types (instead of builtins) to maintain the same behaviour at the API boundary level and still allow user types to be properly supported.
Historically, an attempt to support user types within the one-shot bufferization framework was made. As part of it,
TensorLikeType::getBufferType() was introduced to allow user-provided types to customize bufferization. However, the whole affair proved to be overly complex: there is an interface with customization points for user-provided tensors, and options-based (not sufficient) implementation for builtin tensors. On top of this, there was always a function-specific hook to customize function-level behaviour further. As a result of this, users would need to implement two different mechanisms on their end: interface implementation + option hooks.
It seems more reasonable at this stage to thus drop the interface part and just allow unknown type conversion hook to be used directly. That way, users would only need to supply that hook themselves (with the caveat of having to perform a type dispatch inside of the hook).
As a drive by, multiple tests that exercise the usage of custom tensor type (e.g. !test.test_tensor) are moved to work on top of a test pass. Default memory space inference hook is also updated to work on top of tensor-like type to align the signature to the unknown type conversion.
Note for LLVM integration:
TensorLikeType::getBufferType()for (user-provided) tensor types should now be moved into a user-specifiedBufferizationOptions::unknownTypeConverterFn()hook