[Mlir-commits] [mlir] [mlir][emitc] Add 'emitc.while' and 'emitc.do' ops to the dialect (PR #143008)

Vlad Lazar llvmlistbot at llvm.org
Fri Sep 26 07:22:55 PDT 2025


================
@@ -0,0 +1,238 @@
+// RUN: mlir-opt -allow-unregistered-dialect -convert-scf-to-emitc %s | FileCheck %s
+// RUN: mlir-opt -allow-unregistered-dialect -convert-to-emitc="filter-dialects=scf" %s | FileCheck %s
+
+emitc.func @payload_one_result(%arg: i32) -> i32 {
+  %result = add %arg, %arg : (i32, i32) -> i32
+  return %result : i32
+}
+
+func.func @one_result() -> i32 {
----------------
Vladislave0-0 wrote:

Test:
```mlir
func.func @one_result() -> i32 {
  %init = emitc.literal "1.0" : i32
  %var  = emitc.literal "1.0" : i32
  %exit = emitc.literal "10.0" : i32

  %res = scf.while (%arg1 = %init) : (i32) -> i32 {
    %sum = emitc.add %arg1, %var : (i32, i32) -> i32
    %condition = emitc.cmp lt, %sum, %exit : (i32, i32) -> i1
    %next = emitc.add %arg1, %arg1 : (i32, i32) -> i32
    scf.condition(%condition) %next : i32
  } do {
  ^bb0(%arg2: i32):
    %next_arg1 = emitc.call @payload_one_result(%arg2) : (i32) -> i32
    scf.yield %next_arg1 : i32
  }
  
  return %res : i32
}
```

At the moment, lowering looks like this:
```mlir
func.func @one_result() -> i32 {
  %0 = emitc.literal "1.0" : i32
  %1 = emitc.literal "1.0" : i32
  %2 = emitc.literal "10.0" : i32
  %3 = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.lvalue<i32> // results
  %4 = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.lvalue<i32> // arguments
  emitc.assign %0 : i32 to %4 : <i32>
  %5 = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.lvalue<i1>   // condition
  emitc.do {
    %7 = load %4 : <i32>
    %8 = add %7, %1 : (i32, i32) -> i32
    %9 = cmp lt, %8, %2 : (i32, i32) -> i1
    %10 = add %7, %7 : (i32, i32) -> i32
    assign %10 : i32 to %3 : <i32>    // new results
    assign %9 : i1 to %5 : <i1>       // new condition
    if %9 {
      %11 = call @payload_one_result(%10) : (i32) -> i32
      assign %11 : i32 to %4 : <i32>  // new arguments
    }
  } while {
    %7 = expression %5 : (!emitc.lvalue<i1>) -> i1 {
      %8 = load %5 : <i1>
      yield %8 : i1
    }
    yield %7 : i1
  }
  %6 = emitc.load %3 : <i32>
  return %6 : i32
}
```

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


More information about the Mlir-commits mailing list