-
Notifications
You must be signed in to change notification settings - Fork 30
DONT MERGE Substrait debug upstream casting fix #851
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
Open
lipracer
wants to merge
6
commits into
iree-org:main
Choose a base branch
from
lipracer:substrait-debug-upstream-casting-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78653fb
XXX: Add tracing of typeswitch.
ingomueller-net bbfdfe7
XXX: Use interface argument for typeswitch. This should break on the …
ingomueller-net 4f19bcd
XXX: Apply WIP patch. This should fix CI.
ingomueller-net ab7289a
XXX: Add direct include of OpDefinition.h.
ingomueller-net edb89ea
update upstream mlir patch
lipracer d89388a
Merge branch 'main' into substrait-debug-upstream-casting-fix
lipracer 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| From d50b3d3473fb9a04e1f57797e0d043719a571969 Mon Sep 17 00:00:00 2001 | ||
| From: lipracer <lipracer@gmail.com> | ||
| Date: Fri, 29 Mar 2024 23:25:07 +0800 | ||
| Subject: [PATCH] [mlir] fix Undefined behavior in CastInfo::castFailed with | ||
| From=<MLIR interface> | ||
|
|
||
| Fixes https://github.com/llvm/llvm-project/issues/86647 | ||
|
|
||
| add CastInfo to support cast Interface to Op | ||
| --- | ||
| mlir/include/mlir/IR/OpDefinition.h | 71 ++++++++++++++++++++++ | ||
| mlir/include/mlir/TableGen/Class.h | 2 + | ||
| mlir/tools/mlir-tblgen/OpClass.cpp | 9 +++ | ||
| mlir/tools/mlir-tblgen/OpInterfacesGen.cpp | 3 +- | ||
| mlir/unittests/IR/InterfaceTest.cpp | 48 +++++++++++++++ | ||
| 5 files changed, 132 insertions(+), 1 deletion(-) | ||
|
|
||
| diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h | ||
| index 59f094d66909..52aac19289cf 100644 | ||
| --- a/mlir/include/mlir/IR/OpDefinition.h | ||
| +++ b/mlir/include/mlir/IR/OpDefinition.h | ||
| @@ -22,6 +22,7 @@ | ||
| #include "mlir/IR/Dialect.h" | ||
| #include "mlir/IR/ODSSupport.h" | ||
| #include "mlir/IR/Operation.h" | ||
| +#include "llvm/Support/Casting.h" | ||
| #include "llvm/Support/PointerLikeTypeTraits.h" | ||
|
|
||
| #include <optional> | ||
| @@ -2142,6 +2143,76 @@ struct DenseMapInfo<T, | ||
| } | ||
| static bool isEqual(T lhs, T rhs) { return lhs == rhs; } | ||
| }; | ||
| + | ||
| +template <typename To, typename From> | ||
| +struct CastInfo< | ||
| + To, From, | ||
| + std::enable_if_t< | ||
| + std::is_base_of_v<mlir::OpInterface<To, typename To::InterfaceTraits>, | ||
| + To> && | ||
| + std::is_base_of_v<mlir::OpInterface<std::remove_const_t<From>, | ||
| + typename std::remove_const_t< | ||
| + From>::InterfaceTraits>, | ||
| + std::remove_const_t<From>>, | ||
| + void>> : NullableValueCastFailed<To>, | ||
| + DefaultDoCastIfPossible<To, From, CastInfo<To, From>> { | ||
| + | ||
| + static inline bool isPossible(From &val) { | ||
| + if constexpr (std::is_same_v<To, From>) | ||
| + return true; | ||
| + else | ||
| + return mlir::OpInterface<To, typename To::InterfaceTraits>:: | ||
| + InterfaceBase::classof( | ||
| + const_cast<std::remove_const_t<From> &>(val).getOperation()); | ||
| + } | ||
| + | ||
| + static inline To doCast(From &val) { | ||
| + return To(const_cast<std::remove_const_t<From> &>(val).getOperation()); | ||
| + } | ||
| +}; | ||
| + | ||
| +template <typename OpT, typename = void> | ||
| +struct is_concrete_op_type : public std::false_type {}; | ||
| + | ||
| +template <typename OpT, template <typename T> typename... Traits> | ||
| +constexpr auto concrete_op_base_type_impl(std::tuple<Traits<OpT>...>) { | ||
| + return mlir::Op<OpT, Traits...>(nullptr); | ||
| +} | ||
| + | ||
| +template <typename OpT> | ||
| +using concrete_op_base_type = | ||
| + decltype(concrete_op_base_type_impl<OpT>(typename OpT::traits())); | ||
| + | ||
| +template <typename OpT> | ||
| +struct is_concrete_op_type< | ||
| + OpT, std::enable_if_t<std::is_base_of_v<concrete_op_base_type<OpT>, OpT>>> | ||
| + : public std::true_type {}; | ||
| + | ||
| +template <typename To, typename From> | ||
| +struct CastInfo< | ||
| + To, From, | ||
| + std::enable_if_t< | ||
| + is_concrete_op_type<To>() && | ||
| + std::is_base_of_v<mlir::OpInterface<std::remove_const_t<From>, | ||
| + typename std::remove_const_t< | ||
| + From>::InterfaceTraits>, | ||
| + std::remove_const_t<From>>>> | ||
| + : NullableValueCastFailed<To>, | ||
| + DefaultDoCastIfPossible<To, From, CastInfo<To, From>> { | ||
| + | ||
| + static inline bool isPossible(From &val) { | ||
| + if constexpr (std::is_same_v<To, From>) | ||
| + return true; | ||
| + else | ||
| + return isa<To>( | ||
| + const_cast<std::remove_const_t<From> &>(val).getOperation()); | ||
| + } | ||
| + | ||
| + static inline To doCast(From &val) { | ||
| + return To(const_cast<std::remove_const_t<From> &>(val).getOperation()); | ||
| + } | ||
| +}; | ||
| + | ||
| } // namespace llvm | ||
|
|
||
| #endif | ||
| diff --git a/mlir/include/mlir/TableGen/Class.h b/mlir/include/mlir/TableGen/Class.h | ||
| index 92fec6a3b11d..7616f56aa2e3 100644 | ||
| --- a/mlir/include/mlir/TableGen/Class.h | ||
| +++ b/mlir/include/mlir/TableGen/Class.h | ||
| @@ -520,6 +520,8 @@ public: | ||
| /// Write the parent class declaration. | ||
| void writeTo(raw_indented_ostream &os) const; | ||
|
|
||
| + friend class OpClass; | ||
| + | ||
| private: | ||
| /// The fully resolved C++ name of the parent class. | ||
| std::string name; | ||
| diff --git a/mlir/tools/mlir-tblgen/OpClass.cpp b/mlir/tools/mlir-tblgen/OpClass.cpp | ||
| index 60fa1833ce62..5426302dfed3 100644 | ||
| --- a/mlir/tools/mlir-tblgen/OpClass.cpp | ||
| +++ b/mlir/tools/mlir-tblgen/OpClass.cpp | ||
| @@ -36,7 +36,16 @@ OpClass::OpClass(StringRef name, std::string extraClassDeclaration, | ||
| } | ||
|
|
||
| void OpClass::finalize() { | ||
| + std::string traitList; | ||
| + llvm::raw_string_ostream os(traitList); | ||
| + iterator_range parentTemplateParams(std::begin(parent.templateParams) + 1, | ||
| + std::end(parent.templateParams)); | ||
| + llvm::interleaveComma(parentTemplateParams, os, [&](auto &trait) { | ||
| + os << trait << "<" << getClassName().str() << ">"; | ||
| + }); | ||
| + declare<UsingDeclaration>("traits", "std::tuple<" + traitList + ">"); | ||
| Class::finalize(); | ||
| + | ||
| declare<VisibilityDeclaration>(Visibility::Public); | ||
| declare<ExtraClassDeclaration>(extraClassDeclaration, extraClassDefinition); | ||
| } | ||
| diff --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp | ||
| index 4b06b92fbc8a..a1cae23c1df9 100644 | ||
| --- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp | ||
| +++ b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp | ||
| @@ -544,7 +544,8 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) { | ||
| // Emit the main interface class declaration. | ||
| os << llvm::formatv("class {0} : public ::mlir::{3}<{1}, detail::{2}> {\n" | ||
| "public:\n" | ||
| - " using ::mlir::{3}<{1}, detail::{2}>::{3};\n", | ||
| + " using ::mlir::{3}<{1}, detail::{2}>::{3};\n" | ||
| + " using InterfaceTraits = detail::{2};\n", | ||
| interfaceName, interfaceName, interfaceTraitsName, | ||
| interfaceBaseType); | ||
|
|
||
| diff --git a/mlir/unittests/IR/InterfaceTest.cpp b/mlir/unittests/IR/InterfaceTest.cpp | ||
| index 42196b003e7d..c9ae6938e8b4 100644 | ||
| --- a/mlir/unittests/IR/InterfaceTest.cpp | ||
| +++ b/mlir/unittests/IR/InterfaceTest.cpp | ||
| @@ -17,6 +17,10 @@ | ||
| #include "../../test/lib/Dialect/Test/TestDialect.h" | ||
| #include "../../test/lib/Dialect/Test/TestOps.h" | ||
| #include "../../test/lib/Dialect/Test/TestTypes.h" | ||
| +#include "mlir/Dialect/Arith/IR/Arith.h" | ||
| +#include "mlir/Dialect/SCF/IR/SCF.h" | ||
| +#include "mlir/Parser/Parser.h" | ||
| +#include "llvm/ADT/TypeSwitch.h" | ||
|
|
||
| using namespace mlir; | ||
| using namespace test; | ||
| @@ -84,3 +88,47 @@ TEST(InterfaceTest, TestImplicitConversion) { | ||
| typeA = typeB; | ||
| EXPECT_EQ(typeA, typeB); | ||
| } | ||
| + | ||
| +TEST(OperationInterfaceTest, CastInterfaceToOpOrInterface) { | ||
| + DialectRegistry registry; | ||
| + MLIRContext ctx; | ||
| + | ||
| + const char *ir = R"MLIR( | ||
| + func.func @map(%arg : tensor<1xi64>) { | ||
| + %0 = arith.constant dense<[10]> : tensor<1xi64> | ||
| + %1 = arith.addi %arg, %0 : tensor<1xi64> | ||
| + return | ||
| + } | ||
| + )MLIR"; | ||
| + | ||
| + registry.insert<func::FuncDialect, arith::ArithDialect>(); | ||
| + ctx.appendDialectRegistry(registry); | ||
| + OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(ir, &ctx); | ||
| + Operation &op = cast<func::FuncOp>(module->front()).getBody().front().front(); | ||
| + | ||
| + static_assert(std::is_base_of_v<llvm::concrete_op_base_type<arith::AddIOp>, | ||
| + arith::AddIOp>, | ||
| + ""); | ||
| + static_assert(llvm::is_concrete_op_type<arith::AddIOp>(), ""); | ||
| + static_assert(!llvm::is_concrete_op_type<OpAsmOpInterface>(), ""); | ||
| + | ||
| + OpAsmOpInterface interface = llvm::cast<OpAsmOpInterface>(op); | ||
| + | ||
| + bool constantOp = llvm::TypeSwitch<OpAsmOpInterface, bool>(interface) | ||
| + .Case<arith::AddIOp, arith::ConstantOp>([&](auto op) { | ||
| + bool is_same = | ||
| + std::is_same_v<decltype(op), arith::ConstantOp>; | ||
| + return is_same; | ||
| + }); | ||
| + | ||
| + EXPECT_TRUE(constantOp); | ||
| + | ||
| + EXPECT_FALSE(llvm::isa<VectorUnrollOpInterface>(interface)); | ||
| + EXPECT_FALSE(llvm::dyn_cast<VectorUnrollOpInterface>(interface)); | ||
| + | ||
| + EXPECT_TRUE(llvm::isa<InferTypeOpInterface>(interface)); | ||
| + EXPECT_TRUE(llvm::dyn_cast<InferTypeOpInterface>(interface)); | ||
| + | ||
| + EXPECT_TRUE(llvm::isa<OpAsmOpInterface>(interface)); | ||
| + EXPECT_TRUE(llvm::dyn_cast<OpAsmOpInterface>(interface)); | ||
| +} | ||
| -- | ||
| 2.25.1 | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.