Skip to content

Commit 3925663

Browse files
author
Aviad Cohen
committed
[mlir]: Added properties/attributes ignore flags to OperationEquivalence
Those flags are useful for cases and operation which we may consider equivalent even when their attributes/properties are not the same.
1 parent 904d0c2 commit 3925663

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,14 @@ struct OperationEquivalence {
13221322
// When provided, the location attached to the operation are ignored.
13231323
IgnoreLocations = 1,
13241324

1325-
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ IgnoreLocations)
1325+
// When provided, the discardable attributes attached to the operation are
1326+
// ignored.
1327+
IgnoreDiscardableAttrs = 2,
1328+
1329+
// When provided, the properties attached to the operation are ignored.
1330+
IgnoreProperties = 4,
1331+
1332+
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ IgnoreProperties)
13261333
};
13271334

13281335
/// Compute a hash for the given operation.

mlir/lib/IR/OperationSupport.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,14 @@ llvm::hash_code OperationEquivalence::computeHash(
680680
// - Operation Name
681681
// - Attributes
682682
// - Result Types
683-
llvm::hash_code hash =
684-
llvm::hash_combine(op->getName(), op->getRawDictionaryAttrs(),
685-
op->getResultTypes(), op->hashProperties());
683+
DictionaryAttr dictAttrs;
684+
if (!(flags & Flags::IgnoreDiscardableAttrs))
685+
dictAttrs = op->getRawDictionaryAttrs();
686+
llvm::hash_code hashProperties;
687+
if (!(flags & Flags::IgnoreProperties))
688+
hashProperties = op->hashProperties();
689+
llvm::hash_code hash = llvm::hash_combine(
690+
op->getName(), dictAttrs, op->getResultTypes(), hashProperties);
686691

687692
// - Location if required
688693
if (!(flags & Flags::IgnoreLocations))
@@ -836,14 +841,19 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,
836841
return true;
837842

838843
// 1. Compare the operation properties.
844+
if (!(flags & IgnoreDiscardableAttrs) &&
845+
lhs->getRawDictionaryAttrs() != rhs->getRawDictionaryAttrs())
846+
return false;
847+
839848
if (lhs->getName() != rhs->getName() ||
840-
lhs->getRawDictionaryAttrs() != rhs->getRawDictionaryAttrs() ||
841849
lhs->getNumRegions() != rhs->getNumRegions() ||
842850
lhs->getNumSuccessors() != rhs->getNumSuccessors() ||
843851
lhs->getNumOperands() != rhs->getNumOperands() ||
844-
lhs->getNumResults() != rhs->getNumResults() ||
845-
!lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
846-
rhs->getPropertiesStorage()))
852+
lhs->getNumResults() != rhs->getNumResults())
853+
return false;
854+
if (!(flags & IgnoreProperties) &&
855+
!(lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
856+
rhs->getPropertiesStorage())))
847857
return false;
848858
if (!(flags & IgnoreLocations) && lhs->getLoc() != rhs->getLoc())
849859
return false;

mlir/unittests/IR/OperationSupportTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ TEST(OperandStorageTest, PopulateDefaultAttrs) {
315315
TEST(OperationEquivalenceTest, HashWorksWithFlags) {
316316
MLIRContext context;
317317
context.getOrLoadDialect<test::TestDialect>();
318+
Builder b(&context);
318319

319320
auto *op1 = createOp(&context);
320321
// `op1` has an unknown loc.
@@ -325,12 +326,37 @@ TEST(OperationEquivalenceTest, HashWorksWithFlags) {
325326
op, OperationEquivalence::ignoreHashValue,
326327
OperationEquivalence::ignoreHashValue, flags);
327328
};
329+
// Check ignore location.
328330
EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreLocations),
329331
getHash(op2, OperationEquivalence::IgnoreLocations));
330332
EXPECT_NE(getHash(op1, OperationEquivalence::None),
331333
getHash(op2, OperationEquivalence::None));
334+
op1->setLoc(NameLoc::get(StringAttr::get(&context, "foo")));
335+
// Check ignore discardable dictionary attributes.
336+
SmallVector<NamedAttribute> newAttrs = {
337+
b.getNamedAttr("foo", b.getStringAttr("f"))};
338+
op1->setAttrs(newAttrs);
339+
EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreDiscardableAttrs),
340+
getHash(op2, OperationEquivalence::IgnoreDiscardableAttrs));
341+
EXPECT_NE(getHash(op1, OperationEquivalence::None),
342+
getHash(op2, OperationEquivalence::None));
332343
op1->destroy();
333344
op2->destroy();
345+
346+
// Check ignore properties.
347+
OpBuilder opBuilder(&context);
348+
auto req1 = opBuilder.getI32IntegerAttr(10);
349+
Operation *opWithProperty1 = opBuilder.create<test::OpAttrMatch1>(
350+
opBuilder.getUnknownLoc(), req1, nullptr, nullptr, req1);
351+
auto req2 = opBuilder.getI32IntegerAttr(60);
352+
Operation *opWithProperty2 = opBuilder.create<test::OpAttrMatch1>(
353+
opBuilder.getUnknownLoc(), req2, nullptr, nullptr, req2);
354+
EXPECT_NE(getHash(op1, OperationEquivalence::None),
355+
getHash(op2, OperationEquivalence::None));
356+
EXPECT_EQ(getHash(opWithProperty1, OperationEquivalence::IgnoreProperties),
357+
getHash(opWithProperty2, OperationEquivalence::IgnoreProperties));
358+
opWithProperty1->destroy();
359+
opWithProperty2->destroy();
334360
}
335361

336362
} // namespace

0 commit comments

Comments
 (0)