Skip to content

[MLIR][ODS] Fix properties tablegen wrapper to allow ADL lookup for hash_value #141023

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 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mlir/include/mlir/IR/Properties.td
Original file line number Diff line number Diff line change
Expand Up @@ -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) # [{;
Expand Down Expand Up @@ -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. " #
Expand Down
5 changes: 4 additions & 1 deletion mlir/test/lib/Dialect/Test/TestOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser,
//===----------------------------------------------------------------------===//
// MyPropStruct
//===----------------------------------------------------------------------===//

namespace test_properties {
class MyPropStruct {
public:
std::string content;
Expand All @@ -101,6 +101,9 @@ class MyPropStruct {
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);
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions mlir/test/mlir-tblgen/op-properties.td
Original file line number Diff line number Diff line change
Expand Up @@ -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)

// -----
Expand Down
6 changes: 4 additions & 2 deletions mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down