Skip to content

Commit b336ab4

Browse files
authored
[mlir] add a way to query non-property attributes (#76959)
This helps support generic manipulation of operations that don't (yet) use properties to store inherent attributes. Use this mechanism in type inference and operation equivalence. Note that only minimal unit tests are introduced as all the upstream dialects seem to have been updated to use properties and the non-property behavior is essentially deprecated and untested.
1 parent 2aff7f3 commit b336ab4

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

mlir/include/mlir/IR/Operation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ class alignas(8) Operation final
500500
llvm::to_vector(getDiscardableAttrs()));
501501
}
502502

503+
/// Return all attributes that are not stored as properties.
504+
DictionaryAttr getRawDictionaryAttrs() { return attrs; }
505+
503506
/// Return all of the attributes on this operation.
504507
ArrayRef<NamedAttribute> getAttrs() { return getAttrDictionary().getValue(); }
505508

mlir/lib/IR/OperationSupport.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ llvm::hash_code OperationEquivalence::computeHash(
675675
// - Attributes
676676
// - Result Types
677677
llvm::hash_code hash =
678-
llvm::hash_combine(op->getName(), op->getDiscardableAttrDictionary(),
678+
llvm::hash_combine(op->getName(), op->getRawDictionaryAttrs(),
679679
op->getResultTypes(), op->hashProperties());
680680

681681
// - Location if required
@@ -831,14 +831,13 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,
831831

832832
// 1. Compare the operation properties.
833833
if (lhs->getName() != rhs->getName() ||
834-
lhs->getDiscardableAttrDictionary() !=
835-
rhs->getDiscardableAttrDictionary() ||
834+
lhs->getRawDictionaryAttrs() != rhs->getRawDictionaryAttrs() ||
836835
lhs->getNumRegions() != rhs->getNumRegions() ||
837836
lhs->getNumSuccessors() != rhs->getNumSuccessors() ||
838837
lhs->getNumOperands() != rhs->getNumOperands() ||
839838
lhs->getNumResults() != rhs->getNumResults() ||
840839
!lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
841-
rhs->getPropertiesStorage()))
840+
rhs->getPropertiesStorage()))
842841
return false;
843842
if (!(flags & IgnoreLocations) && lhs->getLoc() != rhs->getLoc())
844843
return false;
@@ -923,7 +922,7 @@ OperationFingerPrint::OperationFingerPrint(Operation *topOp) {
923922
if (op != topOp)
924923
addDataToHash(hasher, op->getParentOp());
925924
// - Attributes
926-
addDataToHash(hasher, op->getDiscardableAttrDictionary());
925+
addDataToHash(hasher, op->getRawDictionaryAttrs());
927926
// - Properties
928927
addDataToHash(hasher, op->hashProperties());
929928
// - Blocks in Regions

mlir/lib/Interfaces/InferTypeOpInterface.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ LogicalResult mlir::detail::verifyInferredResultTypes(Operation *op) {
240240
auto retTypeFn = cast<InferTypeOpInterface>(op);
241241
auto result = retTypeFn.refineReturnTypes(
242242
op->getContext(), op->getLoc(), op->getOperands(),
243-
op->getPropertiesStorage() ? op->getDiscardableAttrDictionary()
244-
: op->getAttrDictionary(),
245-
op->getPropertiesStorage(), op->getRegions(), inferredReturnTypes);
243+
op->getRawDictionaryAttrs(), op->getPropertiesStorage(), op->getRegions(),
244+
inferredReturnTypes);
246245
if (failed(result))
247246
op->emitOpError() << "failed to infer returned types";
248247

mlir/unittests/Bytecode/BytecodeTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@ TEST(Bytecode, OpWithoutProperties) {
144144
EXPECT_EQ(roundtripped->getAttrs().size(), 2u);
145145
EXPECT_TRUE(roundtripped->getInherentAttr("inherent_attr") != std::nullopt);
146146
EXPECT_TRUE(roundtripped->getDiscardableAttr("other_attr") != Attribute());
147+
148+
EXPECT_TRUE(OperationEquivalence::computeHash(op.get()) ==
149+
OperationEquivalence::computeHash(roundtripped));
147150
}

mlir/unittests/IR/OpPropertiesTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "mlir/IR/Attributes.h"
1010
#include "mlir/IR/OpDefinition.h"
11+
#include "mlir/IR/OperationSupport.h"
1112
#include "mlir/Parser/Parser.h"
1213
#include "gtest/gtest.h"
1314
#include <optional>
@@ -401,6 +402,15 @@ TEST(OpPropertiesTest, withoutPropertiesDiscardableAttrs) {
401402
op->print(os);
402403
EXPECT_TRUE(StringRef(os.str()).contains("inherent_attr = 42"));
403404
EXPECT_TRUE(StringRef(os.str()).contains("other_attr = 56"));
405+
406+
OwningOpRef<Operation *> reparsed = parseSourceString(os.str(), config);
407+
auto trivialHash = [](Value v) { return hash_value(v); };
408+
auto hash = [&](Operation *operation) {
409+
return OperationEquivalence::computeHash(
410+
operation, trivialHash, trivialHash,
411+
OperationEquivalence::Flags::IgnoreLocations);
412+
};
413+
EXPECT_TRUE(hash(op.get()) == hash(reparsed.get()));
404414
}
405415

406416
} // namespace

0 commit comments

Comments
 (0)