-
Notifications
You must be signed in to change notification settings - Fork 14k
[mlir]: Added properties/attributes ignore flags to OperationEquivalence #141664
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
Conversation
@llvm/pr-subscribers-mlir Author: Aviad Cohen (AviadCo) ChangesThose flags are useful for cases and operation which we may consider equivalent even when their attributes/properties are not the same. Full diff: https://github.com/llvm/llvm-project/pull/141664.diff 3 Files Affected:
diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index 0046d977c68f4..c9d902514a66b 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -1322,7 +1322,14 @@ struct OperationEquivalence {
// When provided, the location attached to the operation are ignored.
IgnoreLocations = 1,
- LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ IgnoreLocations)
+ // When provided, the dictionary attributes attached to the operation are
+ // ignored.
+ IgnoreDictionaryAttrs = 2,
+
+ // When provided, the properties attached to the operation are ignored.
+ IgnoreProperties = 4,
+
+ LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ IgnoreProperties)
};
/// Compute a hash for the given operation.
diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 7c9e6c89d4d8e..cd0611e741eb3 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -680,9 +680,14 @@ llvm::hash_code OperationEquivalence::computeHash(
// - Operation Name
// - Attributes
// - Result Types
- llvm::hash_code hash =
- llvm::hash_combine(op->getName(), op->getRawDictionaryAttrs(),
- op->getResultTypes(), op->hashProperties());
+ DictionaryAttr dictAttrs;
+ if (!(flags & Flags::IgnoreDictionaryAttrs))
+ dictAttrs = op->getRawDictionaryAttrs();
+ llvm::hash_code hashProperties;
+ if (!(flags & Flags::IgnoreProperties))
+ hashProperties = op->hashProperties();
+ llvm::hash_code hash = llvm::hash_combine(
+ op->getName(), dictAttrs, op->getResultTypes(), hashProperties);
// - Location if required
if (!(flags & Flags::IgnoreLocations))
@@ -836,14 +841,19 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,
return true;
// 1. Compare the operation properties.
+ if (!(flags & IgnoreDictionaryAttrs) &&
+ lhs->getRawDictionaryAttrs() != rhs->getRawDictionaryAttrs())
+ return false;
+
if (lhs->getName() != rhs->getName() ||
- 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()))
+ lhs->getNumResults() != rhs->getNumResults())
+ return false;
+ if (!(flags & IgnoreProperties) &&
+ !(lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
+ rhs->getPropertiesStorage())))
return false;
if (!(flags & IgnoreLocations) && lhs->getLoc() != rhs->getLoc())
return false;
diff --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp
index bac2b72b68deb..30fe44d49f5cb 100644
--- a/mlir/unittests/IR/OperationSupportTest.cpp
+++ b/mlir/unittests/IR/OperationSupportTest.cpp
@@ -314,6 +314,7 @@ TEST(OperandStorageTest, PopulateDefaultAttrs) {
TEST(OperationEquivalenceTest, HashWorksWithFlags) {
MLIRContext context;
+ Builder b(&context);
context.getOrLoadDialect<test::TestDialect>();
auto *op1 = createOp(&context);
@@ -325,10 +326,24 @@ TEST(OperationEquivalenceTest, HashWorksWithFlags) {
op, OperationEquivalence::ignoreHashValue,
OperationEquivalence::ignoreHashValue, flags);
};
+ // Check ignore location.
EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreLocations),
getHash(op2, OperationEquivalence::IgnoreLocations));
EXPECT_NE(getHash(op1, OperationEquivalence::None),
getHash(op2, OperationEquivalence::None));
+ // Check ignore dictionary attributes.
+ op1->setLoc(NameLoc::get(StringAttr::get(&context, "foo")));
+ EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreDictionaryAttrs),
+ getHash(op2, OperationEquivalence::IgnoreDictionaryAttrs));
+ SmallVector<NamedAttribute> newAttrs = {
+ b.getNamedAttr("foo", b.getStringAttr("f"))};
+ op1->setAttrs(newAttrs);
+ EXPECT_NE(getHash(op1, OperationEquivalence::None),
+ getHash(op2, OperationEquivalence::None));
+ op2->setAttrs(newAttrs);
+ // Check ignore properties.
+ EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreProperties),
+ getHash(op2, OperationEquivalence::IgnoreProperties));
op1->destroy();
op2->destroy();
}
|
@llvm/pr-subscribers-mlir-core Author: Aviad Cohen (AviadCo) ChangesThose flags are useful for cases and operation which we may consider equivalent even when their attributes/properties are not the same. Full diff: https://github.com/llvm/llvm-project/pull/141664.diff 3 Files Affected:
diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index 0046d977c68f4..c9d902514a66b 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -1322,7 +1322,14 @@ struct OperationEquivalence {
// When provided, the location attached to the operation are ignored.
IgnoreLocations = 1,
- LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ IgnoreLocations)
+ // When provided, the dictionary attributes attached to the operation are
+ // ignored.
+ IgnoreDictionaryAttrs = 2,
+
+ // When provided, the properties attached to the operation are ignored.
+ IgnoreProperties = 4,
+
+ LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ IgnoreProperties)
};
/// Compute a hash for the given operation.
diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 7c9e6c89d4d8e..cd0611e741eb3 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -680,9 +680,14 @@ llvm::hash_code OperationEquivalence::computeHash(
// - Operation Name
// - Attributes
// - Result Types
- llvm::hash_code hash =
- llvm::hash_combine(op->getName(), op->getRawDictionaryAttrs(),
- op->getResultTypes(), op->hashProperties());
+ DictionaryAttr dictAttrs;
+ if (!(flags & Flags::IgnoreDictionaryAttrs))
+ dictAttrs = op->getRawDictionaryAttrs();
+ llvm::hash_code hashProperties;
+ if (!(flags & Flags::IgnoreProperties))
+ hashProperties = op->hashProperties();
+ llvm::hash_code hash = llvm::hash_combine(
+ op->getName(), dictAttrs, op->getResultTypes(), hashProperties);
// - Location if required
if (!(flags & Flags::IgnoreLocations))
@@ -836,14 +841,19 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,
return true;
// 1. Compare the operation properties.
+ if (!(flags & IgnoreDictionaryAttrs) &&
+ lhs->getRawDictionaryAttrs() != rhs->getRawDictionaryAttrs())
+ return false;
+
if (lhs->getName() != rhs->getName() ||
- 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()))
+ lhs->getNumResults() != rhs->getNumResults())
+ return false;
+ if (!(flags & IgnoreProperties) &&
+ !(lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
+ rhs->getPropertiesStorage())))
return false;
if (!(flags & IgnoreLocations) && lhs->getLoc() != rhs->getLoc())
return false;
diff --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp
index bac2b72b68deb..30fe44d49f5cb 100644
--- a/mlir/unittests/IR/OperationSupportTest.cpp
+++ b/mlir/unittests/IR/OperationSupportTest.cpp
@@ -314,6 +314,7 @@ TEST(OperandStorageTest, PopulateDefaultAttrs) {
TEST(OperationEquivalenceTest, HashWorksWithFlags) {
MLIRContext context;
+ Builder b(&context);
context.getOrLoadDialect<test::TestDialect>();
auto *op1 = createOp(&context);
@@ -325,10 +326,24 @@ TEST(OperationEquivalenceTest, HashWorksWithFlags) {
op, OperationEquivalence::ignoreHashValue,
OperationEquivalence::ignoreHashValue, flags);
};
+ // Check ignore location.
EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreLocations),
getHash(op2, OperationEquivalence::IgnoreLocations));
EXPECT_NE(getHash(op1, OperationEquivalence::None),
getHash(op2, OperationEquivalence::None));
+ // Check ignore dictionary attributes.
+ op1->setLoc(NameLoc::get(StringAttr::get(&context, "foo")));
+ EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreDictionaryAttrs),
+ getHash(op2, OperationEquivalence::IgnoreDictionaryAttrs));
+ SmallVector<NamedAttribute> newAttrs = {
+ b.getNamedAttr("foo", b.getStringAttr("f"))};
+ op1->setAttrs(newAttrs);
+ EXPECT_NE(getHash(op1, OperationEquivalence::None),
+ getHash(op2, OperationEquivalence::None));
+ op2->setAttrs(newAttrs);
+ // Check ignore properties.
+ EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreProperties),
+ getHash(op2, OperationEquivalence::IgnoreProperties));
op1->destroy();
op2->destroy();
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main comment is test coverage
Those flags are useful for cases and operation which we may consider equivalent even when their attributes/properties are not the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved, thank you. Do you need a merge?
Thanks for the review! I will land it. |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/24/builds/9000 Here is the relevant piece of the build log for the reference
|
There are multiple bots broken on this patch like this Even with #142210 |
…Equivalence" (#142319) Reverts #141664 See #141664 (comment)
…o OperationEquivalence" (#142319) Reverts llvm/llvm-project#141664 See llvm/llvm-project#141664 (comment)
llvm#142318) Reverts llvm#142210 This is not enough, see llvm#141664
…Equivalence" (llvm#142319) Reverts llvm#141664 See llvm#141664 (comment)
Those flags are useful for cases and operation which we may consider equivalent even when their attributes/properties are not the same.