diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp --- a/mlir/lib/IR/OperationSupport.cpp +++ b/mlir/lib/IR/OperationSupport.cpp @@ -656,6 +656,10 @@ llvm::hash_combine(op->getName(), op->getDiscardableAttrDictionary(), op->getResultTypes(), op->hashProperties()); + // - Location if required + if(!(flags & Flags::IgnoreLocations)) + hash = llvm::hash_combine(hash, op->getLoc()); + // - Operands ValueRange operands = op->getOperands(); SmallVector operandStorage; diff --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp --- a/mlir/unittests/IR/OperationSupportTest.cpp +++ b/mlir/unittests/IR/OperationSupportTest.cpp @@ -290,4 +290,26 @@ op->destroy(); } + +TEST(OperationEquivalenceTest, HashWorksWithFlags) { + MLIRContext context; + context.getOrLoadDialect(); + + auto op1 = createOp(&context); + // `op1` has an unknown loc. + auto op2 = createOp(&context); + op2->setLoc(NameLoc::get(StringAttr::get(&context, "foo"))); + auto getHash = [](Operation *op, OperationEquivalence::Flags flags) { + return OperationEquivalence::computeHash( + op, OperationEquivalence::ignoreHashValue, + OperationEquivalence::ignoreHashValue, flags); + }; + EXPECT_EQ(getHash(op1, OperationEquivalence::IgnoreLocations), + getHash(op2, OperationEquivalence::IgnoreLocations)); + EXPECT_NE(getHash(op1, OperationEquivalence::None), + getHash(op2, OperationEquivalence::None)); + op1->destroy(); + op2->destroy(); +} + } // namespace