Skip to content

[mlir][emitc] Parenthesization issue in TranslateToCpp #93470

Closed
@cferry-AMD

Description

@cferry-AMD

This MLIR:

module {
  func.func @shift() {
    %0 = "emitc.constant"() <{value = 160 : ui32}> : () -> ui32
    %1 = "emitc.constant"() <{value = 7 : ui32}> : () -> ui32
    %2 = emitc.cmp lt, %0, %1 : (ui32, ui32) -> i1
    %3 = emitc.expression : ui32 {
      %4 = emitc.bitwise_right_shift %0, %1 : (ui32, ui32) -> ui32
      %5 = emitc.conditional %2, %4, %0 : ui32
      emitc.yield %5 : ui32
    }
    %6 = emitc.cast %3 : ui32 to i32
    return
  }
}

gets translated into this C++ code using mlir-translate --mlir-to-cpp:

void shift() {
  uint32_t v1 = 160;
  uint32_t v2 = 7;
  bool v3 = v1 < v2;
  int32_t v4 = (int32_t) v3 ? v1 >> v2 : v1;
  return;
}

where we would expect:

  int32_t v4 = (int32_t)(v3 ? v1 >> v2 : v1);

or

  uint32_t v4 = v3 ? v1 >> v2 : v1;
  int32_t v5 = (int32_t) v4;

The CppEmitter needs to emit parentheses around casted values. Here, it doesn't and the cast happens on the predicate, which is incorrect. Fortunately, this looks pretty easy to fix :)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions