llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir][emitc] Missing parentheses when translate emitc.expression

Open jacquesguan opened this issue 1 year ago • 1 comments

In some case, translation generates wrong cpp code for emitc.expression.

By example, the following case in mlir/test/Target/Cpp/expressions.mlir

func.func @parentheses_for_low_precedence(%arg0: i32, %arg1: i32, %arg2: i32) -> f32 {
  %e = emitc.expression : f32 {
    %a = emitc.add %arg0, %arg1 : (i32, i32) -> i32
    %b = emitc.mul %a, %arg2 : (i32, i32) -> i32
    %d = emitc.cast %b : i32 to f32
    emitc.yield %d : f32
  }
  return %e : f32
}

It will be translate to

float parentheses_for_low_precedence(int32_t v1, int32_t v2, int32_t v3) {
  return (float) (v1 + v2 * v3);
}

which misses the parentheses rounding the addition.

jacquesguan avatar Jan 23 '25 08:01 jacquesguan

@llvm/issue-subscribers-mlir

Author: Jianjian Guan (jacquesguan)

In some case, translation generates wrong cpp code for `emitc.expression`.

By example, the following case in mlir/test/Target/Cpp/expressions.mlir

func.func @<!-- -->parentheses_for_low_precedence(%arg0: i32, %arg1: i32, %arg2: i32) -&gt; f32 {
  %e = emitc.expression : f32 {
    %a = emitc.add %arg0, %arg1 : (i32, i32) -&gt; i32
    %b = emitc.mul %a, %arg2 : (i32, i32) -&gt; i32
    %d = emitc.cast %b : i32 to f32
    emitc.yield %d : f32
  }
  return %e : f32
}

It will be translate to

float parentheses_for_low_precedence(int32_t v1, int32_t v2, int32_t v3) {
  return (float) (v1 + v2 * v3);
}

which misses the parentheses rounding the addition.

llvmbot avatar Jan 23 '25 08:01 llvmbot