Skip to content

[TableGen] Make !and and !or short-circuit #113963

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 3 commits into from
Nov 7, 2024
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: 4 additions & 2 deletions llvm/docs/TableGen/ProgRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,8 @@ and non-0 as true.
``!and(``\ *a*\ ``,`` *b*\ ``, ...)``
This operator does a bitwise AND on *a*, *b*, etc., and produces the
result. A logical AND can be performed if all the arguments are either
0 or 1.
0 or 1. This operator is short-circuit to 0 when the left-most operand
is 0.

``!cast<``\ *type*\ ``>(``\ *a*\ ``)``
This operator performs a cast on *a* and produces the result.
Expand Down Expand Up @@ -1872,7 +1873,8 @@ and non-0 as true.
``!or(``\ *a*\ ``,`` *b*\ ``, ...)``
This operator does a bitwise OR on *a*, *b*, etc., and produces the
result. A logical OR can be performed if all the arguments are either
0 or 1.
0 or 1. This operator is short-circuit to -1 (all ones) the left-most
operand is -1.

``!range([``\ *start*\ ``,]`` *end*\ ``[,``\ *step*\ ``])``
This operator produces half-open range sequence ``[start : end : step)`` as
Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,23 @@ 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) {
// Short-circuit. Regardless whether this is a logical or bitwise
// AND/OR.
// Ideally we could also short-circuit `!or(true, ...)`, but it's
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment about !or now seems stale.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment about !or now seems stale.

It is up-to-date: in the case of !or, TableGen first casts every operands into int, which is a 64 integer underlying. A true is casted to 1 (rather than all ones) and because we only support short-circuit against all ones -- namely -1 -- a true is actually not short circuited. The comment that follows also clarifies why we can't short circuit against 1.

// difficult to do it right without knowing if rest of the operands
// are all `bit` or not. Therefore, we're only implementing a relatively
// limited version of short-circuit against all ones (`true` is casted
// to 1 rather than all ones before we evaluate `!or`).
if (const auto *LHSi = dyn_cast_or_null<IntInit>(
lhs->convertInitializerTo(IntRecTy::get(getRecordKeeper())))) {
if ((Opc == AND && !LHSi->getValue()) ||
(Opc == OR && LHSi->getValue() == -1))
return LHSi;
}
}

if (LHS != lhs || RHS != rhs)
return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
->Fold(R.getCurrentRecord());
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/TableGen/true-false.td
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ 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.
// CHECK: def rec8
// CHECK: list<int> newSeq = [];
// CHECK: list<int> newSeq2 = [];

class Foo <list<int> 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));
}

def rec8 : Foo<>;

#ifdef ERROR1
// ERROR1: Record name '1' is not a string

Expand Down
Loading