diff --git a/mlir/lib/Dialect/Mesh/Transforms/Simplifications.cpp b/mlir/lib/Dialect/Mesh/Transforms/Simplifications.cpp index c9275ad5ad4551..67e1bf6320dbf3 100644 --- a/mlir/lib/Dialect/Mesh/Transforms/Simplifications.cpp +++ b/mlir/lib/Dialect/Mesh/Transforms/Simplifications.cpp @@ -103,13 +103,14 @@ struct ClusterShapeFolder : OpRewritePattern { } // Leave only the dynamic mesh axes to be queried. - ClusterShapeOp newShapeOp = - builder.create(mesh.getSymName(), newShapeOpMeshAxes); - for (size_t i = 0; i < newShapeOp->getResults().size(); ++i) { - newResults[newToOldResultsIndexMap[i]] = newShapeOp->getResults()[i]; + if (!newShapeOpMeshAxes.empty()) { + ClusterShapeOp newShapeOp = + builder.create(mesh.getSymName(), newShapeOpMeshAxes); + for (size_t i = 0; i < newShapeOp->getResults().size(); ++i) { + newResults[newToOldResultsIndexMap[i]] = newShapeOp->getResults()[i]; + } } - - rewriter.replaceAllUsesWith(op.getResults(), newResults); + rewriter.replaceOp(op, newResults); return success(); }