Skip to content

Commit 375df71

Browse files
authored
[emitc] Fix precedence when emit emit.expression (#124087)
Fixes #124086.
1 parent c6eef00 commit 375df71

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,11 +1387,9 @@ LogicalResult CppEmitter::emitOperand(Value value) {
13871387
// as they might be evaluated in the wrong order depending on the shape of
13881388
// the expression tree.
13891389
bool encloseInParenthesis = precedence.value() <= getExpressionPrecedence();
1390-
if (encloseInParenthesis) {
1390+
if (encloseInParenthesis)
13911391
os << "(";
1392-
pushExpressionPrecedence(lowestPrecedence());
1393-
} else
1394-
pushExpressionPrecedence(precedence.value());
1392+
pushExpressionPrecedence(precedence.value());
13951393

13961394
if (failed(emitOperation(*def, /*trailingSemicolon=*/false)))
13971395
return failure();

mlir/test/Target/Cpp/expressions.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ func.func @do_not_inline(%arg0: i32, %arg1: i32, %arg2 : i32) -> i32 {
7070
}
7171

7272
// CPP-DEFAULT: float parentheses_for_low_precedence(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
73-
// CPP-DEFAULT-NEXT: return (float) ([[VAL_1]] + [[VAL_2]] * [[VAL_3]]);
73+
// CPP-DEFAULT-NEXT: return (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
7474
// CPP-DEFAULT-NEXT: }
7575

7676
// CPP-DECLTOP: float parentheses_for_low_precedence(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t [[VAL_3:v[0-9]+]]) {
77-
// CPP-DECLTOP-NEXT: return (float) ([[VAL_1]] + [[VAL_2]] * [[VAL_3]]);
77+
// CPP-DECLTOP-NEXT: return (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
7878
// CPP-DECLTOP-NEXT: }
7979

8080
func.func @parentheses_for_low_precedence(%arg0: i32, %arg1: i32, %arg2: i32) -> f32 {

0 commit comments

Comments
 (0)