Index: mlir/lib/Interfaces/SideEffectInterfaces.cpp =================================================================== --- mlir/lib/Interfaces/SideEffectInterfaces.cpp +++ mlir/lib/Interfaces/SideEffectInterfaces.cpp @@ -63,10 +63,10 @@ SmallVector effects; effectInterface.getEffects(effects); if (!llvm::all_of(effects, [op](const MemoryEffects::EffectInstance &it) { - // We can drop allocations if the value is a result of the + // We can drop effects if the value is a result of the // operation. - if (isa(it.getEffect())) - return it.getValue() && it.getValue().getDefiningOp() == op; + if (it.getValue() && it.getValue().getDefiningOp() == op) + return true; // Otherwise, the effect must be a read. return isa(it.getEffect()); })) { Index: mlir/test/Transforms/canonicalize-dce.mlir =================================================================== --- mlir/test/Transforms/canonicalize-dce.mlir +++ mlir/test/Transforms/canonicalize-dce.mlir @@ -173,3 +173,17 @@ } return } + +// ----- + + +// Test case: Delete ops that only have side-effects on the result. + +// CHECK: func @f() +// CHECK-NOT: test_effects_result +// CHECK-NEXT: return + +func.func @f() { + %0 = "test.test_effects_result"() : () -> i32 + return +} Index: mlir/test/lib/Dialect/Test/TestOps.td =================================================================== --- mlir/test/lib/Dialect/Test/TestOps.td +++ mlir/test/lib/Dialect/Test/TestOps.td @@ -2861,6 +2861,10 @@ def TestEffectsWrite : TEST_Op<"op_with_memwrite", [MemoryEffects<[MemWrite]>]>; +def TestEffectsResult : TEST_Op<"test_effects_result"> { + let results = (outs Res); +} + //===----------------------------------------------------------------------===// // Test Ops with verifiers //===----------------------------------------------------------------------===//