<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/93470>93470</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Parenthesization issue in TranslateToCpp
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
cferry-AMD
</td>
</tr>
</table>
<pre>
This MLIR:
```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`:
```c++
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:
```c++
int32_t v4 = (int32_t)(v3 ? v1 >> v2 : v1);
```
or
```c++
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 :)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVV2P6jYQ_TXOywiUOF_kIQ98LGqlXqmq9n1lkoG4DXZkT2Dpr6_sJJBdbbl3JZTg8ZwzM4djI6yVJ4VYsnTD0l0gemq0KasjGnNbrH_sgoOub-VrIy38-OP3v1i8BhbuWLgen1k4fM6tNEPorOu-RWD5ZljDsVfV0j2AJaFt5JEYXzFezFIAgPE0BBbvgHGOZ0nVstLKklDEOJ8A8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