[mlir][emitc] Refactor brackets in expressions#168267
Merged
Conversation
This patch is a minor NFC-intended refactoring to the way emitting redundant parentheses is prevented. The current implementation pushes and later pops a fake low precedence into the precedence stack when emitting function calls. The new implementation adds a boolean argument to `emitOperand()` that explicity guarantees that the operand is being emitted between some kind of brackets, exempting the method from enforcing correct evaluation order w.r.t precedence and associativity up the expression tree.
Member
|
@llvm/pr-subscribers-mlir-emitc @llvm/pr-subscribers-mlir Author: Gil Rapaport (aniragil) ChangesThis patch is a minor NFC-intended refactoring to the way emitting redundant parentheses is prevented. Full diff: https://github.com/llvm/llvm-project/pull/168267.diff 1 Files Affected:
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 6bd76bb1ffc4b..56f81b0bea9e2 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -173,8 +173,11 @@ struct CppEmitter {
/// Emits the operands of the operation. All operands are emitted in order.
LogicalResult emitOperands(Operation &op);
- /// Emits value as an operands of an operation
- LogicalResult emitOperand(Value value);
+ /// Emits value as an operand of some operation. Unless \p isInBrackets is
+ /// true, operands emitted as sub-expressions will be parenthesized if needed
+ /// in order to enforce correct evaluation based on precedence and
+ /// associativity.
+ LogicalResult emitOperand(Value value, bool isInBrackets = false);
/// Emit an expression as a C expression.
LogicalResult emitExpression(ExpressionOp expressionOp);
@@ -1578,7 +1581,7 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {
return success();
}
-LogicalResult CppEmitter::emitOperand(Value value) {
+LogicalResult CppEmitter::emitOperand(Value value, bool isInBrackets) {
if (isPartOfCurrentExpression(value)) {
Operation *def = value.getDefiningOp();
assert(def && "Expected operand to be defined by an operation");
@@ -1586,10 +1589,12 @@ LogicalResult CppEmitter::emitOperand(Value value) {
if (failed(precedence))
return failure();
- // Sub-expressions with equal or lower precedence need to be parenthesized,
- // as they might be evaluated in the wrong order depending on the shape of
- // the expression tree.
- bool encloseInParenthesis = precedence.value() <= getExpressionPrecedence();
+ // Unless already in brackets, sub-expressions with equal or lower
+ // precedence need to be parenthesized as they might be evaluated in the
+ // wrong order depending on the shape of the expression tree.
+ bool encloseInParenthesis =
+ !isInBrackets && precedence.value() <= getExpressionPrecedence();
+
if (encloseInParenthesis)
os << "(";
pushExpressionPrecedence(precedence.value());
@@ -1628,15 +1633,9 @@ LogicalResult CppEmitter::emitOperand(Value value) {
LogicalResult CppEmitter::emitOperands(Operation &op) {
return interleaveCommaWithError(op.getOperands(), os, [&](Value operand) {
- // If an expression is being emitted, push lowest precedence as these
- // operands are either wrapped by parenthesis.
- if (getEmittedExpression())
- pushExpressionPrecedence(lowestPrecedence());
- if (failed(emitOperand(operand)))
- return failure();
- if (getEmittedExpression())
- popExpressionPrecedence();
- return success();
+ // Emit operand under guarantee that if it's part of an expression then it
+ // is being emitted within brackets.
+ return emitOperand(operand, /*isInBrackets=*/true);
});
}
|
This was referenced Nov 24, 2025
aadeshps-mcw
pushed a commit
to aadeshps-mcw/llvm-project
that referenced
this pull request
Nov 26, 2025
This patch is a minor NFC-intended refactoring to the way emitting redundant parentheses is prevented. The current implementation pushes and later pops a fake low precedence into the precedence stack when emitting function calls. The new implementation adds a boolean argument to `emitOperand()` that explicity guarantees that the operand is being emitted between some kind of brackets, exempting the method from enforcing correct evaluation order w.r.t precedence and associativity up the expression tree.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch is a minor NFC-intended refactoring to the way emitting redundant parentheses is prevented.
The current implementation pushes and later pops a fake low precedence into the precedence stack when emitting function calls. The new implementation adds a boolean argument to
emitOperand()that explicity guarantees that the operand is being emitted between some kind of brackets, exempting the method from enforcing correct evaluation order w.r.t precedence and associativity up the expression tree.