[Mlir-commits] [mlir] [mlir][emitc] Fix hasSideEffects of emitc.cast (PR #208607)

Longsheng Mou llvmlistbot at llvm.org
Thu Jul 9 20:04:39 PDT 2026


https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/208607

>From 668951e33b3a96beabaaef4db1359805d0afe858 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Fri, 10 Jul 2026 10:38:38 +0800
Subject: [PATCH 1/3] [mlir][emitc] Fix `hasSideEffects` of emitc.cast

When `pure` attribute is set, the cast op is side-effect-free, so we should return false in `hasSideEffects` method.
---
 mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 1b776519b1428..d42c74e963769 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -404,7 +404,7 @@ def EmitC_CastOp : EmitC_Op<"cast",
     bool hasSideEffects() {
       // Use the "pure" attribute to see whether this CastOp has side effects.
       // Note that by default, `pure` is not set.
-      return getPure();
+      return !getPure();
     }
   }];
 }

>From 771aadd206ba4dd18e752b7242e41f3bec43d4bd Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Fri, 10 Jul 2026 10:43:19 +0800
Subject: [PATCH 2/3] fix test

---
 mlir/test/Target/Cpp/expressions.mlir | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mlir/test/Target/Cpp/expressions.mlir b/mlir/test/Target/Cpp/expressions.mlir
index d447f9a38f3d3..b1c024f9ec98c 100644
--- a/mlir/test/Target/Cpp/expressions.mlir
+++ b/mlir/test/Target/Cpp/expressions.mlir
@@ -89,11 +89,14 @@ func.func @do_not_inline(%arg0: i32, %arg1: i32, %arg2 : i32) -> i32 {
 }
 
 // CPP-DEFAULT:      float parentheses_for_low_precedence(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
-// CPP-DEFAULT-NEXT:   return (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
+// CPP-DEFAULT-NEXT:   float [[VAL_4:v[0-9]+]] = (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
+// CPP-DEFAULT-NEXT:   return [[VAL_4]];
 // CPP-DEFAULT-NEXT: }
 
 // CPP-DECLTOP:      float parentheses_for_low_precedence(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
-// CPP-DECLTOP-NEXT:   return (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
+// CPP-DECLTOP-NEXT:   float [[VAL_4:v[0-9]+]];
+// CPP-DECLTOP-NEXT:   [[VAL_4]] = (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
+// CPP-DECLTOP-NEXT:   return [[VAL_4]];
 // CPP-DECLTOP-NEXT: }
 
 func.func @parentheses_for_low_precedence(%arg0: i32, %arg1: i32, %arg2: i32) -> f32 {

>From 5b1638114d0df1afa28a659366dab851a7f1d08c Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Fri, 10 Jul 2026 11:04:29 +0800
Subject: [PATCH 3/3] add cast tests

---
 mlir/test/Target/Cpp/expressions.mlir | 35 +++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/mlir/test/Target/Cpp/expressions.mlir b/mlir/test/Target/Cpp/expressions.mlir
index b1c024f9ec98c..5e76f61933d4d 100644
--- a/mlir/test/Target/Cpp/expressions.mlir
+++ b/mlir/test/Target/Cpp/expressions.mlir
@@ -109,6 +109,41 @@ func.func @parentheses_for_low_precedence(%arg0: i32, %arg1: i32, %arg2: i32) ->
   return %e : f32
 }
 
+// CPP-DEFAULT:      float inline_cast_pure(int32_t [[VAL_1:v[0-9]+]]) {
+// CPP-DEFAULT-NEXT:   return (float) [[VAL_1]];
+// CPP-DEFAULT-NEXT: }
+
+// CPP-DECLTOP:      float inline_cast_pure(int32_t [[VAL_1:v[0-9]+]]) {
+// CPP-DECLTOP-NEXT:   return (float) [[VAL_1]];
+// CPP-DECLTOP-NEXT: }
+
+func.func @inline_cast_pure(%arg0: i32) -> f32 {
+  %0 = emitc.expression : f32 {
+    %1 = cast %arg0 {pure} : i32 to f32
+    yield %1 : f32
+  }
+  return %0 : f32
+}
+
+// CPP-DEFAULT:      float do_not_inline_cast_without_pure(int32_t [[VAL_1:v[0-9]+]]) {
+// CPP-DEFAULT-NEXT:   float [[VAL_2:v[0-9]+]] = (float) [[VAL_1]]
+// CPP-DEFAULT-NEXT:   return [[VAL_2]];
+// CPP-DEFAULT-NEXT: }
+
+// CPP-DECLTOP:      float do_not_inline_cast_without_pure(int32_t [[VAL_1:v[0-9]+]]) {
+// CPP-DECLTOP-NEXT:   float [[VAL_2:v[0-9]+]];
+// CPP-DECLTOP-NEXT:   [[VAL_2]] = (float) [[VAL_1]];
+// CPP-DECLTOP-NEXT:   return [[VAL_2]];
+// CPP-DECLTOP-NEXT: }
+
+func.func @do_not_inline_cast_without_pure(%arg0: i32) -> f32 {
+  %0 = emitc.expression : f32 {
+    %1 = cast %arg0 : i32 to f32
+    yield %1 : f32
+  }
+  return %0 : f32
+}
+
 // CPP-DEFAULT:      int32_t parentheses_for_same_precedence(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
 // CPP-DEFAULT-NEXT:   return [[VAL_3]] / ([[VAL_1]] * [[VAL_2]]);
 // CPP-DEFAULT-NEXT: }



More information about the Mlir-commits mailing list