Skip to content

[mlir] add a way to query non-property attributes #76959

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 2 commits into from
Jan 4, 2024
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
3 changes: 3 additions & 0 deletions mlir/include/mlir/IR/Operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ class alignas(8) Operation final
llvm::to_vector(getDiscardableAttrs()));
}

/// Return all attributes that are not stored as properties.
DictionaryAttr getRawDictionaryAttrs() { return attrs; }

/// Return all of the attributes on this operation.
ArrayRef<NamedAttribute> getAttrs() { return getAttrDictionary().getValue(); }

Expand Down
9 changes: 4 additions & 5 deletions mlir/lib/IR/OperationSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ llvm::hash_code OperationEquivalence::computeHash(
// - Attributes
// - Result Types
llvm::hash_code hash =
llvm::hash_combine(op->getName(), op->getDiscardableAttrDictionary(),
llvm::hash_combine(op->getName(), op->getRawDictionaryAttrs(),
op->getResultTypes(), op->hashProperties());

// - Location if required
Expand Down Expand Up @@ -831,14 +831,13 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,

// 1. Compare the operation properties.
if (lhs->getName() != rhs->getName() ||
lhs->getDiscardableAttrDictionary() !=
rhs->getDiscardableAttrDictionary() ||
lhs->getRawDictionaryAttrs() != rhs->getRawDictionaryAttrs() ||
lhs->getNumRegions() != rhs->getNumRegions() ||
lhs->getNumSuccessors() != rhs->getNumSuccessors() ||
lhs->getNumOperands() != rhs->getNumOperands() ||
lhs->getNumResults() != rhs->getNumResults() ||
!lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
rhs->getPropertiesStorage()))
rhs->getPropertiesStorage()))
return false;
if (!(flags & IgnoreLocations) && lhs->getLoc() != rhs->getLoc())
return false;
Expand Down Expand Up @@ -923,7 +922,7 @@ OperationFingerPrint::OperationFingerPrint(Operation *topOp) {
if (op != topOp)
addDataToHash(hasher, op->getParentOp());
// - Attributes
addDataToHash(hasher, op->getDiscardableAttrDictionary());
addDataToHash(hasher, op->getRawDictionaryAttrs());
// - Properties
addDataToHash(hasher, op->hashProperties());
// - Blocks in Regions
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Interfaces/InferTypeOpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,8 @@ LogicalResult mlir::detail::verifyInferredResultTypes(Operation *op) {
auto retTypeFn = cast<InferTypeOpInterface>(op);
auto result = retTypeFn.refineReturnTypes(
op->getContext(), op->getLoc(), op->getOperands(),
op->getPropertiesStorage() ? op->getDiscardableAttrDictionary()
: op->getAttrDictionary(),
op->getPropertiesStorage(), op->getRegions(), inferredReturnTypes);
op->getRawDictionaryAttrs(), op->getPropertiesStorage(), op->getRegions(),
inferredReturnTypes);
if (failed(result))
op->emitOpError() << "failed to infer returned types";

Expand Down
3 changes: 3 additions & 0 deletions mlir/unittests/Bytecode/BytecodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@ TEST(Bytecode, OpWithoutProperties) {
EXPECT_EQ(roundtripped->getAttrs().size(), 2u);
EXPECT_TRUE(roundtripped->getInherentAttr("inherent_attr") != std::nullopt);
EXPECT_TRUE(roundtripped->getDiscardableAttr("other_attr") != Attribute());

EXPECT_TRUE(OperationEquivalence::computeHash(op.get()) ==
OperationEquivalence::computeHash(roundtripped));
}
10 changes: 10 additions & 0 deletions mlir/unittests/IR/OpPropertiesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "mlir/IR/Attributes.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/Parser/Parser.h"
#include "gtest/gtest.h"
#include <optional>
Expand Down Expand Up @@ -401,6 +402,15 @@ TEST(OpPropertiesTest, withoutPropertiesDiscardableAttrs) {
op->print(os);
EXPECT_TRUE(StringRef(os.str()).contains("inherent_attr = 42"));
EXPECT_TRUE(StringRef(os.str()).contains("other_attr = 56"));

OwningOpRef<Operation *> reparsed = parseSourceString(os.str(), config);
auto trivialHash = [](Value v) { return hash_value(v); };
auto hash = [&](Operation *operation) {
return OperationEquivalence::computeHash(
operation, trivialHash, trivialHash,
OperationEquivalence::Flags::IgnoreLocations);
};
EXPECT_TRUE(hash(op.get()) == hash(reparsed.get()));
}

} // namespace