diff options
author | Mehdi Amini <joker.eph@gmail.com> | 2025-05-23 15:55:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-23 15:55:42 +0200 |
commit | 50304b0806e4f9c8001c024925566c9b88e7176e (patch) | |
tree | 180503dcd277e5d35d7efe097f0098d7dc930512 | |
parent | 6bd3543a4d2977cb63f7212594ea0f2ac6e8ab12 (diff) | |
download | llvm-50304b0806e4f9c8001c024925566c9b88e7176e.zip llvm-50304b0806e4f9c8001c024925566c9b88e7176e.tar.gz llvm-50304b0806e4f9c8001c024925566c9b88e7176e.tar.bz2 |
[MLIR][ODS] Fix properties tablegen wrapper to allow ADL lookup for hash_value (#141023)
llvm::hash_value() is meant to be redefined in the relevant namespace
and looked up through ADL. However this can't work with a fully
qualified call.
-rw-r--r-- | mlir/include/mlir/IR/Properties.td | 4 | ||||
-rw-r--r-- | mlir/test/lib/Dialect/Test/TestOps.h | 5 | ||||
-rw-r--r-- | mlir/test/lib/Dialect/Test/TestOps.td | 14 | ||||
-rw-r--r-- | mlir/test/mlir-tblgen/op-properties.td | 5 | ||||
-rw-r--r-- | mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 6 |
5 files changed, 27 insertions, 7 deletions
diff --git a/mlir/include/mlir/IR/Properties.td b/mlir/include/mlir/IR/Properties.td index 15b7296..15337ed9 100644 --- a/mlir/include/mlir/IR/Properties.td +++ b/mlir/include/mlir/IR/Properties.td @@ -548,7 +548,7 @@ class ArrayProp<Property elem = Property<>, string newSummary = ""> : // In the non-trivial case, we define a mapped range to get internal hash // codes. let hashProperty = !if(!empty(elem.hashProperty), - [{::llvm::hash_value(::llvm::ArrayRef<}] # elem.storageType # [{>{$_storage})}], + [{hash_value(::llvm::ArrayRef<}] # elem.storageType # [{>{$_storage})}], [{[&]() -> ::llvm::hash_code { auto getElemHash = [](const auto& propStorage) -> ::llvm::hash_code { return }] # !subst("$_storage", "propStorage", elem.hashProperty) # [{; @@ -762,7 +762,7 @@ class OptionalProp<Property p, bit canDelegateParsing = 1> }] # !subst("$_storage", "(*($_storage))", p.writeToMlirBytecode); let hashProperty = !if(!empty(p.hashProperty), p.hashProperty, - [{ ::llvm::hash_value($_storage.has_value() ? std::optional<::llvm::hash_code>{}] # + [{ hash_value($_storage.has_value() ? std::optional<::llvm::hash_code>{}] # !subst("$_storage", "(*($_storage))", p.hashProperty) #[{} : std::nullopt) }]); assert !or(!not(delegatesParsing), !eq(defaultValue, "std::nullopt")), "For delegated parsing to be used, the default value must be nullopt. " # diff --git a/mlir/test/lib/Dialect/Test/TestOps.h b/mlir/test/lib/Dialect/Test/TestOps.h index 9d5b225..c2ee5f9 100644 --- a/mlir/test/lib/Dialect/Test/TestOps.h +++ b/mlir/test/lib/Dialect/Test/TestOps.h @@ -86,7 +86,7 @@ mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser, //===----------------------------------------------------------------------===// // MyPropStruct //===----------------------------------------------------------------------===// - +namespace test_properties { class MyPropStruct { public: std::string content; @@ -101,6 +101,9 @@ public: return content == rhs.content; } }; +inline llvm::hash_code hash_value(const MyPropStruct &S) { return S.hash(); } +} // namespace test_properties +using test_properties::MyPropStruct; llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader, MyPropStruct &prop); diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td index 3161d2d..59330fd 100644 --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -3197,6 +3197,20 @@ def TestOpWithWrappedProperties : TEST_Op<"with_wrapped_properties"> { ); } +// Same as above, but without a custom `hashProperty` field, checking +// that ADL is correctly working. +def MyStructProperty2 : Property<"MyPropStruct"> { + let convertToAttribute = "return $_storage.asAttribute($_ctxt);"; + let convertFromAttribute = "return MyPropStruct::setFromAttr($_storage, $_attr, $_diag);"; +} + +def TestOpWithWrappedProperties2 : TEST_Op<"with_wrapped_properties2"> { + let assemblyFormat = "prop-dict attr-dict"; + let arguments = (ins + MyStructProperty2:$prop + ); +} + def TestOpWithEmptyProperties : TEST_Op<"empty_properties"> { let assemblyFormat = "prop-dict attr-dict"; let arguments = (ins); diff --git a/mlir/test/mlir-tblgen/op-properties.td b/mlir/test/mlir-tblgen/op-properties.td index 66888e6..a9c784c 100644 --- a/mlir/test/mlir-tblgen/op-properties.td +++ b/mlir/test/mlir-tblgen/op-properties.td @@ -115,8 +115,9 @@ def OpWithOptionalPropsAndAttrs : // DEFS-LABEL: OpWithProps::computePropertiesHash // DEFS: hash_intArray -// DEFS-NEXT: return ::llvm::hash_value(::llvm::ArrayRef<int64_t>{propStorage}) -// DEFS: ::llvm::hash_value(prop.optional) +// DEFS: using ::llvm::hash_value; +// DEFS-NEXT: return hash_value(::llvm::ArrayRef<int64_t>{propStorage}) +// DEFS: hash_value(prop.optional) // DEFS: hash_intArray(prop.intArray) // ----- diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index 373d376..06247b3 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -1588,6 +1588,7 @@ void OpEmitter::genPropertiesSupport() { const char *propHashFmt = R"decl( auto hash_{0} = [] (const auto &propStorage) -> llvm::hash_code { + using ::llvm::hash_value; return {1}; }; )decl"; @@ -1605,6 +1606,7 @@ void OpEmitter::genPropertiesSupport() { } } } + hashMethod << " using llvm::hash_value;\n"; hashMethod << " return llvm::hash_combine("; llvm::interleaveComma( attrOrProperties, hashMethod, [&](const ConstArgument &attrOrProp) { @@ -1614,8 +1616,8 @@ void OpEmitter::genPropertiesSupport() { hashMethod << "\n hash_" << namedProperty->name << "(prop." << namedProperty->name << ")"; } else { - hashMethod << "\n ::llvm::hash_value(prop." - << namedProperty->name << ")"; + hashMethod << "\n hash_value(prop." << namedProperty->name + << ")"; } return; } |