From 5eef726bc8cf1e4b9d1067e52f9ad9f43bc1dec1 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Mon, 1 Jul 2019 09:52:53 -0700 Subject: [PATCH] TypeConversion: do not materialize conversion of the type to itself Type conversion does not necessarily affect all types, some of them may remain untouched. The type conversion tool from the dialect conversion framework will unconditionally insert a temporary cast operation from the type to itself anyway, and will try to materialize it to a real conversion operation if there are remaining uses. Simply use the original value instead. PiperOrigin-RevId: 255975450 --- mlir/lib/Transforms/DialectConversion.cpp | 9 +++++++++ mlir/test/Transforms/test-legalizer.mlir | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/mlir/lib/Transforms/DialectConversion.cpp b/mlir/lib/Transforms/DialectConversion.cpp index f6d6329..be60ada 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 8640ef5..87988ea 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) -- 2.7.4