-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[TableGen] Avoid evaluating RHS of a BinOp until short-circuit is complete #144021
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
mshockwave
merged 2 commits into
llvm:main
from
mshockwave:patch/tblgen-binop-short-circuit
Jun 13, 2025
Merged
[TableGen] Avoid evaluating RHS of a BinOp until short-circuit is complete #144021
mshockwave
merged 2 commits into
llvm:main
from
mshockwave:patch/tblgen-binop-short-circuit
Jun 13, 2025
Conversation
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
@llvm/pr-subscribers-tablegen Author: Min-Yih Hsu (mshockwave) ChangesThis patch adds an even more aggressive short-circuit on Context: https://discourse.llvm.org/t/how-to-do-short-circuit-evaluation-in-tablegen/86847 Full diff: https://github.com/llvm/llvm-project/pull/144021.diff 2 Files Affected:
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 4c8b41237c604..d0bf8323511c2 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -1558,7 +1558,6 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
const Init *BinOpInit::resolveReferences(Resolver &R) const {
const Init *lhs = LHS->resolveReferences(R);
- const Init *rhs = RHS->resolveReferences(R);
unsigned Opc = getOpcode();
if (Opc == AND || Opc == OR) {
@@ -1577,6 +1576,8 @@ const Init *BinOpInit::resolveReferences(Resolver &R) const {
}
}
+ const Init *rhs = RHS->resolveReferences(R);
+
if (LHS != lhs || RHS != rhs)
return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
->Fold(R.getCurrentRecord());
diff --git a/llvm/test/TableGen/true-false.td b/llvm/test/TableGen/true-false.td
index 5a59f20b21d25..8a1949327e610 100644
--- a/llvm/test/TableGen/true-false.td
+++ b/llvm/test/TableGen/true-false.td
@@ -67,13 +67,18 @@ def rec7 {
bits<3> flags = { true, false, true };
}
-// `!and` and `!or` should be short-circuit such that `!tail` on empty list will never
-// be evaluated.
+// `!and` and `!or` should be short-circuit such that any of the `!head` or
+// `!tail` on empty list below will never be evaluated.
// CHECK: def rec8
+// CHECK: bit v = 0;
+// CHECK: int v2 = -1;
// CHECK: list<int> newSeq = [];
// CHECK: list<int> newSeq2 = [];
class Foo <list<int> seq = []> {
+ bit v = !and(false, !head(seq));
+ int v2 = !or(-1, !head(seq));
+
bit unresolved = !ne(!find(NAME, "BAR"), -1);
list<int> newSeq = !if(!and(false, unresolved), !tail(seq), seq);
list<int> newSeq2 = !if(!or(-1, unresolved), seq, !tail(seq));
|
jurahul
approved these changes
Jun 13, 2025
wecing
approved these changes
Jun 13, 2025
nvjle
approved these changes
Jun 13, 2025
nvjle
reviewed
Jun 13, 2025
tomtor
pushed a commit
to tomtor/llvm-project
that referenced
this pull request
Jun 14, 2025
…plete (llvm#144021) This patch adds an even more aggressive short-circuit on `!and` and `!or` that completely avoids the evaluation of RHS operand until short circuiting decisions are made.
akuhlens
pushed a commit
to akuhlens/llvm-project
that referenced
this pull request
Jun 24, 2025
…plete (llvm#144021) This patch adds an even more aggressive short-circuit on `!and` and `!or` that completely avoids the evaluation of RHS operand until short circuiting decisions are made.
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 adds an even more aggressive short-circuit on
!and
and!or
that completely avoids the evaluation of RHS operand until short circuiting decisions are made.Context: https://discourse.llvm.org/t/how-to-do-short-circuit-evaluation-in-tablegen/86847