diff options
-rw-r--r-- | mlir/lib/Transforms/DialectConversion.cpp | 9 | ||||
-rw-r--r-- | mlir/test/Transforms/test-legalizer.mlir | 7 |
2 files changed, 16 insertions, 0 deletions
diff --git a/mlir/lib/Transforms/DialectConversion.cpp b/mlir/lib/Transforms/DialectConversion.cpp index f6d6329900b..be60ada6a43 100644 --- a/mlir/lib/Transforms/DialectConversion.cpp +++ b/mlir/lib/Transforms/DialectConversion.cpp @@ -170,6 +170,15 @@ void ArgConverter::applyRewrites() { continue; } + // If mapping is from type to itself, replace the remaining uses and drop + // the cast operation. + if (op->getNumOperands() == 1 && + op->getResult(0)->getType() == op->getOperand(0)->getType()) { + op->getResult(0)->replaceAllUsesWith(op->getOperand(0)); + op->destroy(); + continue; + } + // Otherwise, if there are any dangling uses then replace the fake // conversion operation with one generated by the type converter. This // is necessary as the cast must persist in the IR after conversion. diff --git a/mlir/test/Transforms/test-legalizer.mlir b/mlir/test/Transforms/test-legalizer.mlir index 8640ef5d24d..87988ea4d4e 100644 --- a/mlir/test/Transforms/test-legalizer.mlir +++ b/mlir/test/Transforms/test-legalizer.mlir @@ -40,6 +40,13 @@ func @remap_input_1_to_N_remaining_use(%arg0: f32) { "work"(%arg0) : (f32) -> () } +// CHECK-LABEL: func @remap_input_to_self +func @remap_input_to_self(%arg0: index) { + // CHECK-NOT: test.cast + // CHECK: "work" + "work"(%arg0) : (index) -> () +} + // CHECK-LABEL: func @remap_multi(%arg0: f64, %arg1: f64) -> (f64, f64) func @remap_multi(%arg0: i64, %unused: i16, %arg1: i64) -> (i64, i64) { // CHECK-NEXT: "test.valid"{{.*}} : (f64, f64) |