Skip to content
Open
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
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ Value mlir::arith::getZeroConstant(OpBuilder &builder, Location loc,

OpFoldResult arith::AddIOp::fold(FoldAdaptor adaptor) {
// addi(x, 0) -> x
if (matchPattern(adaptor.getRhs(), m_Zero()))
if (matchPattern(adaptor.getRhs(), m_Zero()) && getLhs() != *this)
return getLhs();

// addi(subi(a, b), b) -> a
Expand Down
3 changes: 2 additions & 1 deletion mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ bool GreedyPatternRewriteDriver::processWorklist() {
replacements.push_back(constOp->getResult(0));
}

if (materializationSucceeded) {
if (materializationSucceeded &&
!llvm::equal(replacements, op->getResults())) {
rewriter.replaceOp(op, replacements);
changed = true;
LLVM_DEBUG(logSuccessfulFolding(dumpRootOp));
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Transforms/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1248,3 +1248,12 @@ func.func @test_materialize_failure() -> i64 {
%u = index.castu %const : index to i64
return %u: i64
}

// -----

// Make sure that the canonicalizer does not fold infinitely.

// CHECK: %[[c0:.*]] = arith.constant 0 : index
%c0 = arith.constant 0 : index
// CHECK: %[[add:.*]] = arith.addi %[[c0]], %[[add]] : index
%0 = arith.addi %c0, %0 : index