-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][bufferization] Add tensor-like and buffer-like interfaces #134220
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
33e03b3
[mlir][bufferization] Add tensor-like and memref-like interfaces
andrey-golubev cb5892f
Address code review feedback (simpler fixes)
andrey-golubev 911096e
Address code review feedback (part 2): rewrite tests, simplify code
andrey-golubev 253296f
Rename MemRefLike to BufferLike
andrey-golubev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
mlir/include/mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //===- BufferizationTypeInterfaces.h - Type Interfaces ----------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATIONTYPEINTERFACES_H_ | ||
| #define MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATIONTYPEINTERFACES_H_ | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Bufferization Type Interfaces | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.h.inc" | ||
|
|
||
| #endif // MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATIONTYPEINTERFACES_H_ |
42 changes: 42 additions & 0 deletions
42
mlir/include/mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.td
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //===- BufferizationTypeInterfaces.td - Type Interfaces ----*- tablegen -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This is the definition file for type interfaces used in Bufferization. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef BUFFERIZATION_TYPE_INTERFACES | ||
| #define BUFFERIZATION_TYPE_INTERFACES | ||
|
|
||
| include "mlir/IR/OpBase.td" | ||
|
|
||
| def Bufferization_TensorLikeTypeInterface | ||
| : TypeInterface<"TensorLikeType"> { | ||
| let cppNamespace = "::mlir::bufferization"; | ||
| let description = [{ | ||
| Indicates that this type is a tensor type (similarly to a MLIR builtin | ||
| tensor) for bufferization purposes. | ||
|
|
||
| The interface currently has no methods as it is used by types to opt into | ||
| being supported by the bufferization procedures. | ||
| }]; | ||
| } | ||
|
|
||
| def Bufferization_MemRefLikeTypeInterface | ||
| : TypeInterface<"MemRefLikeType"> { | ||
| let cppNamespace = "::mlir::bufferization"; | ||
| let description = [{ | ||
| Indicates that this type is a memref type (similarly to a MLIR builtin | ||
| memref) for bufferization purposes. | ||
|
|
||
| The interface currently has no methods as it is used by types to opt into | ||
| being supported by the bufferization procedures. | ||
| }]; | ||
| } | ||
|
|
||
| #endif // BUFFERIZATION_TYPE_INTERFACES | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
mlir/test/Dialect/Bufferization/Transforms/tensorlike-memreflike.mlir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // RUN: mlir-opt %s -test-tensorlike-memreflike -split-input-file | FileCheck %s | ||
|
|
||
| // CHECK: func.func @builtin_unranked | ||
| // CHECK-SAME: {found = {operand_0 = "is_tensor_like", result_0 = "is_memref_like"}} | ||
| func.func @builtin_unranked(%t: tensor<*xf32>) -> (memref<*xf32>) | ||
| { | ||
| %0 = bufferization.to_memref %t : tensor<*xf32> to memref<*xf32> | ||
| return %0 : memref<*xf32> | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK: func.func @builtin_ranked | ||
| // CHECK-SAME: {found = {operand_0 = "is_tensor_like", result_0 = "is_memref_like"}} | ||
| func.func @builtin_ranked(%t: tensor<42xf32>) -> (memref<42xf32>) | ||
| { | ||
| %0 = bufferization.to_memref %t : tensor<42xf32> to memref<42xf32> | ||
| return %0 : memref<42xf32> | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK: func.func @custom_tensor | ||
| // CHECK-SAME: {found = {operand_0 = "is_tensor_like"}} | ||
| func.func @custom_tensor(%t: !test.test_tensor<[42], f32>) -> () | ||
| { | ||
| return | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK: func.func @custom_memref | ||
| // CHECK-SAME: {found = {operand_0 = "is_memref_like"}} | ||
| func.func @custom_memref(%t: !test.test_memref<[42], f32>) -> () | ||
| { | ||
| return | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
mlir/test/lib/Dialect/Bufferization/TestTensorLikeAndMemRefLike.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| //===- TestTensorLikeAndMemRefLike.cpp - Bufferization Test -----*- c++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "TestDialect.h" | ||
| #include "mlir/Dialect/Bufferization/IR/Bufferization.h" | ||
| #include "mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/IR/Attributes.h" | ||
| #include "mlir/IR/BuiltinAttributes.h" | ||
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
| std::string getImplementationStatus(Type type) { | ||
| if (isa<bufferization::TensorLikeType>(type)) { | ||
| return "is_tensor_like"; | ||
| } | ||
| if (isa<bufferization::MemRefLikeType>(type)) { | ||
| return "is_memref_like"; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| DictionaryAttr findAllImplementeesOfTensorOrMemRefLike(func::FuncOp funcOp) { | ||
| llvm::SmallVector<NamedAttribute> attributes; | ||
|
|
||
| const auto funcType = funcOp.getFunctionType(); | ||
| for (auto [index, inputType] : llvm::enumerate(funcType.getInputs())) { | ||
| const auto status = getImplementationStatus(inputType); | ||
| if (status.empty()) { | ||
| continue; | ||
| } | ||
|
|
||
| attributes.push_back( | ||
| NamedAttribute(StringAttr::get(funcOp.getContext(), | ||
| "operand_" + std::to_string(index)), | ||
| StringAttr::get(funcOp.getContext(), status))); | ||
| } | ||
|
|
||
| for (auto [index, resultType] : llvm::enumerate(funcType.getResults())) { | ||
| const auto status = getImplementationStatus(resultType); | ||
| if (status.empty()) { | ||
| continue; | ||
| } | ||
|
|
||
| attributes.push_back(NamedAttribute( | ||
| StringAttr::get(funcOp.getContext(), "result_" + std::to_string(index)), | ||
| StringAttr::get(funcOp.getContext(), status))); | ||
| } | ||
|
|
||
| return mlir::DictionaryAttr::get(funcOp.getContext(), attributes); | ||
| } | ||
|
|
||
| /// This pass tests whether specified types implement TensorLike and (or) | ||
| /// MemRefLike type interfaces defined in bufferization. | ||
| /// | ||
| /// The pass analyses operation signature. When the aforementioned interface | ||
| /// implementation found, an attribute is added to the operation, signifying the | ||
| /// associated operand / result. | ||
| struct TestTensorLikeAndMemRefLikePass | ||
| : public PassWrapper<TestTensorLikeAndMemRefLikePass, | ||
| OperationPass<ModuleOp>> { | ||
| MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTensorLikeAndMemRefLikePass) | ||
|
|
||
| void getDependentDialects(DialectRegistry ®istry) const override { | ||
| registry.insert<bufferization::BufferizationDialect, test::TestDialect>(); | ||
| } | ||
| StringRef getArgument() const final { return "test-tensorlike-memreflike"; } | ||
| StringRef getDescription() const final { | ||
| return "Module pass to test custom types that implement TensorLike / " | ||
| "MemRefLike interfaces"; | ||
| } | ||
|
|
||
| void runOnOperation() override { | ||
| auto op = getOperation(); | ||
|
|
||
| op.walk([](func::FuncOp funcOp) { | ||
| const auto dict = findAllImplementeesOfTensorOrMemRefLike(funcOp); | ||
| if (!dict.empty()) { | ||
| funcOp->setAttr("found", dict); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| namespace mlir::test { | ||
| void registerTestTensorLikeAndMemRefLikePass() { | ||
| PassRegistration<TestTensorLikeAndMemRefLikePass>(); | ||
| } | ||
| } // namespace mlir::test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can ask for one more change? The term used in the bufferization is "buffer", not memref. Can we change this to
BufferLikeTypeInterface? E.g., we havegetBufferTypeinBufferizableOpInterface.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 have thought of this as well but wasn't sure it's better than memref. If you prefer "buffer", sure.
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.
done!