Skip to content

Commit 20c926e

Browse files
author
Stephan Herhut
committed
[mlir][DialectConversion] Do not prematurely drop unused cast operations
The rewrite logic has an optimization to drop a cast operation after rewriting block arguments if the cast operation has no users. This is unsafe as there might be a pending rewrite that replaced the cast operation itself and hence would trigger a second free. Instead, do not remove the casts and leave it up to a later canonicalization to do so. Differential Revision: https://reviews.llvm.org/D92184
1 parent 0b20d0a commit 20c926e

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,6 @@ void ArgConverter::applyRewrites(ConversionValueMapping &mapping) {
364364
// If the argument is still used, replace it with the generated cast.
365365
if (!origArg.use_empty())
366366
origArg.replaceAllUsesWith(mapping.lookupOrDefault(castValue));
367-
368-
// If all users of the cast were removed, we can drop it. Otherwise, keep
369-
// the operation alive and let the user handle any remaining usages.
370-
if (castValue.use_empty() && castValue.getDefiningOp())
371-
castValue.getDefiningOp()->erase();
372367
}
373368
}
374369
}

mlir/test/Transforms/test-legalizer.mlir

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ func @remap_call_1_to_1(%arg0: i64) {
3636

3737
// CHECK-LABEL: func @remap_input_1_to_N({{.*}}f16, {{.*}}f16)
3838
func @remap_input_1_to_N(%arg0: f32) -> f32 {
39-
// CHECK-NEXT: "test.return"{{.*}} : (f16, f16) -> ()
40-
"test.return"(%arg0) : (f32) -> ()
39+
// CHECK-NEXT: [[CAST:%.*]] = "test.cast"(%arg0, %arg1) : (f16, f16) -> f32
40+
// CHECK-NEXT: "test.return"{{.*}} : (f16, f16) -> ()
41+
"test.return"(%arg0) : (f32) -> ()
4142
}
4243

4344
// CHECK-LABEL: func @remap_input_1_to_N_remaining_use(%arg0: f16, %arg1: f16)

0 commit comments

Comments
 (0)