Skip to content

[emitc] Fix precedence when emit emit.expression #124087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,11 +1387,9 @@ LogicalResult CppEmitter::emitOperand(Value value) {
// as they might be evaluated in the wrong order depending on the shape of
// the expression tree.
bool encloseInParenthesis = precedence.value() <= getExpressionPrecedence();
if (encloseInParenthesis) {
if (encloseInParenthesis)
os << "(";
pushExpressionPrecedence(lowestPrecedence());
} else
pushExpressionPrecedence(precedence.value());
pushExpressionPrecedence(precedence.value());

if (failed(emitOperation(*def, /*trailingSemicolon=*/false)))
return failure();
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Target/Cpp/expressions.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func.func @do_not_inline(%arg0: i32, %arg1: i32, %arg2 : i32) -> i32 {
}

// 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]+]]) {
// CPP-DEFAULT-NEXT: return (float) ([[VAL_1]] + [[VAL_2]] * [[VAL_3]]);
// CPP-DEFAULT-NEXT: return (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
// CPP-DEFAULT-NEXT: }

// 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]+]]) {
// CPP-DECLTOP-NEXT: return (float) ([[VAL_1]] + [[VAL_2]] * [[VAL_3]]);
// CPP-DECLTOP-NEXT: return (float) (([[VAL_1]] + [[VAL_2]]) * [[VAL_3]]);
// CPP-DECLTOP-NEXT: }

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