Skip to content

Commit 50304b0

Browse files
authored
[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.
1 parent 6bd3543 commit 50304b0

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

mlir/include/mlir/IR/Properties.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class ArrayProp<Property elem = Property<>, string newSummary = ""> :
548548
// In the non-trivial case, we define a mapped range to get internal hash
549549
// codes.
550550
let hashProperty = !if(!empty(elem.hashProperty),
551-
[{::llvm::hash_value(::llvm::ArrayRef<}] # elem.storageType # [{>{$_storage})}],
551+
[{hash_value(::llvm::ArrayRef<}] # elem.storageType # [{>{$_storage})}],
552552
[{[&]() -> ::llvm::hash_code {
553553
auto getElemHash = [](const auto& propStorage) -> ::llvm::hash_code {
554554
return }] # !subst("$_storage", "propStorage", elem.hashProperty) # [{;
@@ -762,7 +762,7 @@ class OptionalProp<Property p, bit canDelegateParsing = 1>
762762
}] # !subst("$_storage", "(*($_storage))", p.writeToMlirBytecode);
763763

764764
let hashProperty = !if(!empty(p.hashProperty), p.hashProperty,
765-
[{ ::llvm::hash_value($_storage.has_value() ? std::optional<::llvm::hash_code>{}] #
765+
[{ hash_value($_storage.has_value() ? std::optional<::llvm::hash_code>{}] #
766766
!subst("$_storage", "(*($_storage))", p.hashProperty) #[{} : std::nullopt) }]);
767767
assert !or(!not(delegatesParsing), !eq(defaultValue, "std::nullopt")),
768768
"For delegated parsing to be used, the default value must be nullopt. " #

mlir/test/lib/Dialect/Test/TestOps.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser,
8686
//===----------------------------------------------------------------------===//
8787
// MyPropStruct
8888
//===----------------------------------------------------------------------===//
89-
89+
namespace test_properties {
9090
class MyPropStruct {
9191
public:
9292
std::string content;
@@ -101,6 +101,9 @@ class MyPropStruct {
101101
return content == rhs.content;
102102
}
103103
};
104+
inline llvm::hash_code hash_value(const MyPropStruct &S) { return S.hash(); }
105+
} // namespace test_properties
106+
using test_properties::MyPropStruct;
104107

105108
llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader,
106109
MyPropStruct &prop);

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,6 +3197,20 @@ def TestOpWithWrappedProperties : TEST_Op<"with_wrapped_properties"> {
31973197
);
31983198
}
31993199

3200+
// Same as above, but without a custom `hashProperty` field, checking
3201+
// that ADL is correctly working.
3202+
def MyStructProperty2 : Property<"MyPropStruct"> {
3203+
let convertToAttribute = "return $_storage.asAttribute($_ctxt);";
3204+
let convertFromAttribute = "return MyPropStruct::setFromAttr($_storage, $_attr, $_diag);";
3205+
}
3206+
3207+
def TestOpWithWrappedProperties2 : TEST_Op<"with_wrapped_properties2"> {
3208+
let assemblyFormat = "prop-dict attr-dict";
3209+
let arguments = (ins
3210+
MyStructProperty2:$prop
3211+
);
3212+
}
3213+
32003214
def TestOpWithEmptyProperties : TEST_Op<"empty_properties"> {
32013215
let assemblyFormat = "prop-dict attr-dict";
32023216
let arguments = (ins);

mlir/test/mlir-tblgen/op-properties.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ def OpWithOptionalPropsAndAttrs :
115115

116116
// DEFS-LABEL: OpWithProps::computePropertiesHash
117117
// DEFS: hash_intArray
118-
// DEFS-NEXT: return ::llvm::hash_value(::llvm::ArrayRef<int64_t>{propStorage})
119-
// DEFS: ::llvm::hash_value(prop.optional)
118+
// DEFS: using ::llvm::hash_value;
119+
// DEFS-NEXT: return hash_value(::llvm::ArrayRef<int64_t>{propStorage})
120+
// DEFS: hash_value(prop.optional)
120121
// DEFS: hash_intArray(prop.intArray)
121122

122123
// -----

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,7 @@ void OpEmitter::genPropertiesSupport() {
15881588

15891589
const char *propHashFmt = R"decl(
15901590
auto hash_{0} = [] (const auto &propStorage) -> llvm::hash_code {
1591+
using ::llvm::hash_value;
15911592
return {1};
15921593
};
15931594
)decl";
@@ -1605,6 +1606,7 @@ void OpEmitter::genPropertiesSupport() {
16051606
}
16061607
}
16071608
}
1609+
hashMethod << " using llvm::hash_value;\n";
16081610
hashMethod << " return llvm::hash_combine(";
16091611
llvm::interleaveComma(
16101612
attrOrProperties, hashMethod, [&](const ConstArgument &attrOrProp) {
@@ -1614,8 +1616,8 @@ void OpEmitter::genPropertiesSupport() {
16141616
hashMethod << "\n hash_" << namedProperty->name << "(prop."
16151617
<< namedProperty->name << ")";
16161618
} else {
1617-
hashMethod << "\n ::llvm::hash_value(prop."
1618-
<< namedProperty->name << ")";
1619+
hashMethod << "\n hash_value(prop." << namedProperty->name
1620+
<< ")";
16191621
}
16201622
return;
16211623
}

0 commit comments

Comments
 (0)