[Mlir-commits] [mlir] [mlir][emitc] Add op modelling C expressions (PR #71631)

Simon Camphausen llvmlistbot at llvm.org
Thu Nov 9 06:21:44 PST 2023


================
@@ -0,0 +1,212 @@
+// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
+// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
+
+// CPP-DEFAULT:      int32_t single_use(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]], int32_t [[VAL_4:v[0-9]+]]) {
+// CPP-DEFAULT-NEXT:   bool [[VAL_5:v[0-9]+]] = bar([[VAL_1]] * M_PI, [[VAL_3]]) - [[VAL_4]] < [[VAL_2]];
+// CPP-DEFAULT-NEXT:   int32_t [[VAL_6:v[0-9]+]];
+// CPP-DEFAULT-NEXT:   if ([[VAL_5]]) {
+// CPP-DEFAULT-NEXT:     [[VAL_6]] = [[VAL_1]];
+// CPP-DEFAULT-NEXT:   } else {
+// CPP-DEFAULT-NEXT:     [[VAL_6]] = [[VAL_1]];
+// CPP-DEFAULT-NEXT:   }
+// CPP-DEFAULT-NEXT:   return [[VAL_6]];
+// CPP-DEFAULT-NEXT: }
+
+// CPP-DECLTOP:      int32_t single_use(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]], int32_t [[VAL_4:v[0-9]+]]) {
+// CPP-DECLTOP-NEXT:   bool [[VAL_5:v[0-9]+]];
+// CPP-DECLTOP-NEXT:   int32_t [[VAL_6:v[0-9]+]];
+// CPP-DECLTOP-NEXT:   [[VAL_5]] = bar([[VAL_1]] * M_PI, [[VAL_3]]) - [[VAL_4]] < [[VAL_2]];
+// CPP-DECLTOP-NEXT:   ;
+// CPP-DECLTOP-NEXT:   if ([[VAL_5]]) {
+// CPP-DECLTOP-NEXT:     [[VAL_6]] = [[VAL_1]];
+// CPP-DECLTOP-NEXT:   } else {
+// CPP-DECLTOP-NEXT:     [[VAL_6]] = [[VAL_1]];
+// CPP-DECLTOP-NEXT:   }
+// CPP-DECLTOP-NEXT:   return [[VAL_6]];
+// CPP-DECLTOP-NEXT: }
+
+func.func @single_use(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32) -> i32 {
+  %p0 = emitc.literal "M_PI" : i32
+  %e = emitc.expression : i1 {
+    %a = emitc.mul %arg0, %p0 : (i32, i32) -> i32
+    %b = emitc.call "bar" (%a, %arg2) : (i32, i32) -> (i32)
+    %c = emitc.sub %b, %arg3 : (i32, i32) -> i32
+    %d = emitc.cmp lt, %c, %arg1 :(i32, i32) -> i1
+    emitc.yield %d : i1
+  }
+  %v = "emitc.variable"(){value = #emitc.opaque<"">} : () -> i32
+  emitc.if %e {
+    emitc.assign %arg0 : i32 to %v : i32
+    emitc.yield
+  } else {
+    emitc.assign %arg0 : i32 to %v : i32
+    emitc.yield
+  }
+  return %v : i32
+}
+
+// CPP-DEFAULT: int32_t do_not_inline(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
+// CPP-DEFAULT-NEXT: int32_t [[VAL_4:v[0-9]+]] = ([[VAL_1]] + [[VAL_2]]) * [[VAL_3]];
+// CPP-DEFAULT-NEXT: return [[VAL_4]];
+// CPP-DEFAULT-NEXT:}
+
+// CPP-DECLTOP: int32_t do_not_inline(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
+// CPP-DECLTOP-NEXT: int32_t [[VAL_4:v[0-9]+]];
+// CPP-DECLTOP-NEXT: [[VAL_4]] = ([[VAL_1]] + [[VAL_2]]) * [[VAL_3]];
+// CPP-DECLTOP-NEXT: return [[VAL_4]];
+// CPP-DECLTOP-NEXT:}
+
+func.func @do_not_inline(%arg0: i32, %arg1: i32, %arg2 : i32) -> i32 {
+  %e = emitc.expression {do_not_inline} : i32 {
----------------
simon-camp wrote:

```suggestion
  %e = emitc.expression noinline : i32 {
```

https://github.com/llvm/llvm-project/pull/71631


More information about the Mlir-commits mailing list