Skip to content

Commit 144ae25

Browse files
nemanjaiNemanja Ivanovic
andauthored
[TableGen] Improve handling for dag op names (#149248)
There are currently no ways to add names to dag operators other than when defining them. Furthermore a !con operation as well as some others, drops the operator names. This patch propagates the name from the LHS dag for !con and adds a way to get and set the operator name for a dag (!getdagopname, !setdagopname). --------- Co-authored-by: Nemanja Ivanovic <[email protected]>
1 parent 8c87496 commit 144ae25

File tree

8 files changed

+112
-32
lines changed

8 files changed

+112
-32
lines changed

llvm/docs/TableGen/ProgRef.rst

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,17 @@ TableGen provides "bang operators" that have a wide variety of uses:
219219

220220
.. productionlist::
221221
BangOperator: one of
222-
: !add !and !cast !con !dag
223-
: !div !empty !eq !exists !filter
224-
: !find !foldl !foreach !ge !getdagarg
225-
: !getdagname !getdagop !gt !head !if
226-
: !initialized !instances !interleave !isa !le
227-
: !listconcat !listflatten !listremove !listsplat !logtwo
228-
: !lt !match !mul !ne !not
229-
: !or !range !repr !setdagarg !setdagname
230-
: !setdagop !shl !size !sra !srl
231-
: !strconcat !sub !subst !substr !tail
232-
: !tolower !toupper !xor
222+
: !add !and !cast !con !dag
223+
: !div !empty !eq !exists !filter
224+
: !find !foldl !foreach !ge !getdagarg
225+
: !getdagname !getdagop !getdagopname !gt !head
226+
: !if !initialized !instances !interleave !isa
227+
: !le !listconcat !listflatten !listremove !listsplat
228+
: !logtwo !lt !match !mul !ne
229+
: !not !or !range !repr !setdagarg
230+
: !setdagname !setdagop !setdagopname !shl !size
231+
: !sra !srl !strconcat !sub !subst
232+
: !substr !tail !tolower !toupper !xor
233233

234234
The ``!cond`` operator has a slightly different
235235
syntax compared to other bang operators, so it is defined separately:
@@ -1443,7 +1443,8 @@ DAG.
14431443

14441444
The following bang operators are useful for working with DAGs:
14451445
``!con``, ``!dag``, ``!empty``, ``!foreach``, ``!getdagarg``, ``!getdagname``,
1446-
``!getdagop``, ``!setdagarg``, ``!setdagname``, ``!setdagop``, ``!size``.
1446+
``!getdagop``, ``!getdagopname``, ``!setdagarg``, ``!setdagname``, ``!setdagop``,
1447+
``!setdagopname``, ``!size``.
14471448

14481449
Defvar in a record body
14491450
-----------------------
@@ -1695,9 +1696,11 @@ and non-0 as true.
16951696
This operator concatenates the DAG nodes *a*, *b*, etc. Their operations
16961697
must equal.
16971698

1698-
``!con((op a1:$name1, a2:$name2), (op b1:$name3))``
1699+
``!con((op:$lhs a1:$name1, a2:$name2), (op:$rhs b1:$name3))``
16991700

1700-
results in the DAG node ``(op a1:$name1, a2:$name2, b1:$name3)``.
1701+
results in the DAG node ``(op:$lhs a1:$name1, a2:$name2, b1:$name3)``.
1702+
The name of the dag operator is derived from the LHS DAG node if it is
1703+
set, otherwise from the RHS DAG node.
17011704

17021705
``!cond(``\ *cond1* ``:`` *val1*\ ``,`` *cond2* ``:`` *val2*\ ``, ...,`` *condn* ``:`` *valn*\ ``)``
17031706
This operator tests *cond1* and returns *val1* if the result is true.
@@ -1819,6 +1822,10 @@ and non-0 as true.
18191822

18201823
dag d = !dag(!getdagop(someDag), args, names);
18211824

1825+
``!getdagopname(``\ *dag*\ ``)``
1826+
This operator retrieves the name of the given *dag* operator. If the operator
1827+
has no name associated, ``?`` is returned.
1828+
18221829
``!gt(``\ *a*\ `,` *b*\ ``)``
18231830
This operator produces 1 if *a* is greater than *b*; 0 otherwise.
18241831
The arguments must be ``bit``, ``bits``, ``int``, or ``string`` values.
@@ -1949,6 +1956,10 @@ and non-0 as true.
19491956

19501957
Example: ``!setdagop((foo 1, 2), bar)`` results in ``(bar 1, 2)``.
19511958

1959+
``!setdagopname(``\ *dag*\ ``,``\ *name*\ ``)``
1960+
This operator produces a DAG node with the same operator and arguments as
1961+
*dag*, but replacing the name of the operator with *name*.
1962+
19521963
``!shl(``\ *a*\ ``,`` *count*\ ``)``
19531964
This operator shifts *a* left logically by *count* bits and produces the resulting
19541965
value. The operation is performed on a 64-bit integer; the result

llvm/include/llvm/TableGen/Record.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ class UnOpInit final : public OpInit, public FoldingSetNode {
841841
SIZE,
842842
EMPTY,
843843
GETDAGOP,
844+
GETDAGOPNAME,
844845
LOG2,
845846
REPR,
846847
LISTFLATTEN,
@@ -910,6 +911,7 @@ class BinOpInit final : public OpInit, public FoldingSetNode {
910911
GETDAGARG,
911912
GETDAGNAME,
912913
SETDAGOP,
914+
SETDAGOPNAME
913915
};
914916

915917
private:

llvm/lib/TableGen/Record.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,12 @@ const Init *UnOpInit::Fold(const Record *CurRec, bool IsFinal) const {
985985
}
986986
break;
987987

988+
case GETDAGOPNAME:
989+
if (const auto *Dag = dyn_cast<DagInit>(LHS)) {
990+
return Dag->getName();
991+
}
992+
break;
993+
988994
case LOG2:
989995
if (const auto *LHSi = dyn_cast_or_null<IntInit>(
990996
LHS->convertInitializerTo(IntRecTy::get(RK)))) {
@@ -1050,6 +1056,9 @@ std::string UnOpInit::getAsString() const {
10501056
case SIZE: Result = "!size"; break;
10511057
case EMPTY: Result = "!empty"; break;
10521058
case GETDAGOP: Result = "!getdagop"; break;
1059+
case GETDAGOPNAME:
1060+
Result = "!getdagopname";
1061+
break;
10531062
case LOG2 : Result = "!logtwo"; break;
10541063
case LISTFLATTEN:
10551064
Result = "!listflatten";
@@ -1310,7 +1319,11 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
13101319
SmallVector<std::pair<const Init *, const StringInit *>, 8> Args;
13111320
llvm::append_range(Args, LHSs->getArgAndNames());
13121321
llvm::append_range(Args, RHSs->getArgAndNames());
1313-
return DagInit::get(Op, Args);
1322+
// Use the name of the LHS DAG if it's set, otherwise the name of the RHS.
1323+
const auto *NameInit = LHSs->getName();
1324+
if (!NameInit)
1325+
NameInit = RHSs->getName();
1326+
return DagInit::get(Op, NameInit, Args);
13141327
}
13151328
break;
13161329
}
@@ -1508,6 +1521,14 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
15081521
return DagInit::get(Op, Dag->getArgs(), Dag->getArgNames());
15091522
break;
15101523
}
1524+
case SETDAGOPNAME: {
1525+
const auto *Dag = dyn_cast<DagInit>(LHS);
1526+
const auto *Op = dyn_cast<StringInit>(RHS);
1527+
if (Dag && Op)
1528+
return DagInit::get(Dag->getOperator(), Op, Dag->getArgs(),
1529+
Dag->getArgNames());
1530+
break;
1531+
}
15111532
case ADD:
15121533
case SUB:
15131534
case MUL:
@@ -1620,6 +1641,9 @@ std::string BinOpInit::getAsString() const {
16201641
case STRCONCAT: Result = "!strconcat"; break;
16211642
case INTERLEAVE: Result = "!interleave"; break;
16221643
case SETDAGOP: Result = "!setdagop"; break;
1644+
case SETDAGOPNAME:
1645+
Result = "!setdagopname";
1646+
break;
16231647
case GETDAGARG:
16241648
Result = "!getdagarg<" + getType()->getAsString() + ">";
16251649
break;

llvm/lib/TableGen/TGLexer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ tgtok::TokKind TGLexer::LexExclaim() {
680680
.Case("find", tgtok::XFind)
681681
.Cases("setdagop", "setop", tgtok::XSetDagOp) // !setop is deprecated.
682682
.Cases("getdagop", "getop", tgtok::XGetDagOp) // !getop is deprecated.
683+
.Case("setdagopname", tgtok::XSetDagOpName)
684+
.Case("getdagopname", tgtok::XGetDagOpName)
683685
.Case("getdagarg", tgtok::XGetDagArg)
684686
.Case("getdagname", tgtok::XGetDagName)
685687
.Case("setdagarg", tgtok::XSetDagArg)

llvm/lib/TableGen/TGLexer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ enum TokKind {
150150
XGt,
151151
XSetDagOp,
152152
XGetDagOp,
153+
XSetDagOpName,
154+
XGetDagOpName,
153155
XExists,
154156
XListRemove,
155157
XToLower,

llvm/lib/TableGen/TGParser.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "TGParser.h"
14+
#include "TGLexer.h"
1415
#include "llvm/ADT/SmallVector.h"
1516
#include "llvm/ADT/StringExtras.h"
1617
#include "llvm/ADT/Twine.h"
@@ -1199,6 +1200,7 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
11991200
case tgtok::XCast:
12001201
case tgtok::XRepr:
12011202
case tgtok::XGetDagOp:
1203+
case tgtok::XGetDagOpName:
12021204
case tgtok::XInitialized: { // Value ::= !unop '(' Value ')'
12031205
UnOpInit::UnaryOp Code;
12041206
const RecTy *Type = nullptr;
@@ -1287,6 +1289,11 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
12871289
}
12881290
Code = UnOpInit::GETDAGOP;
12891291
break;
1292+
case tgtok::XGetDagOpName:
1293+
Lex.Lex(); // eat the operation
1294+
Type = StringRecTy::get(Records);
1295+
Code = UnOpInit::GETDAGOPNAME;
1296+
break;
12901297
case tgtok::XInitialized:
12911298
Lex.Lex(); // eat the operation
12921299
Code = UnOpInit::INITIALIZED;
@@ -1514,7 +1521,8 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
15141521
case tgtok::XInterleave:
15151522
case tgtok::XGetDagArg:
15161523
case tgtok::XGetDagName:
1517-
case tgtok::XSetDagOp: { // Value ::= !binop '(' Value ',' Value ')'
1524+
case tgtok::XSetDagOp:
1525+
case tgtok::XSetDagOpName: { // Value ::= !binop '(' Value ',' Value ')'
15181526
tgtok::TokKind OpTok = Lex.getCode();
15191527
SMLoc OpLoc = Lex.getLoc();
15201528
Lex.Lex(); // eat the operation
@@ -1550,6 +1558,9 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
15501558
case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
15511559
case tgtok::XInterleave: Code = BinOpInit::INTERLEAVE; break;
15521560
case tgtok::XSetDagOp: Code = BinOpInit::SETDAGOP; break;
1561+
case tgtok::XSetDagOpName:
1562+
Code = BinOpInit::SETDAGOPNAME;
1563+
break;
15531564
case tgtok::XGetDagArg:
15541565
Code = BinOpInit::GETDAGARG;
15551566
break;
@@ -1580,6 +1591,10 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
15801591
}
15811592
ArgType = DagRecTy::get(Records);
15821593
break;
1594+
case tgtok::XSetDagOpName:
1595+
Type = DagRecTy::get(Records);
1596+
ArgType = DagRecTy::get(Records);
1597+
break;
15831598
case tgtok::XGetDagName:
15841599
Type = StringRecTy::get(Records);
15851600
ArgType = DagRecTy::get(Records);
@@ -1773,22 +1788,26 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
17731788
// Deal with BinOps whose arguments have different types, by
17741789
// rewriting ArgType in between them.
17751790
switch (Code) {
1776-
case BinOpInit::SETDAGOP:
1777-
// After parsing the first dag argument, switch to expecting
1778-
// a record, with no restriction on its superclasses.
1779-
ArgType = RecordRecTy::get(Records, {});
1780-
break;
1781-
case BinOpInit::GETDAGARG:
1782-
// After parsing the first dag argument, expect an index integer or a
1783-
// name string.
1784-
ArgType = nullptr;
1785-
break;
1786-
case BinOpInit::GETDAGNAME:
1787-
// After parsing the first dag argument, expect an index integer.
1788-
ArgType = IntRecTy::get(Records);
1789-
break;
1790-
default:
1791-
break;
1791+
case BinOpInit::SETDAGOPNAME:
1792+
// After parsing the first dag argument, expect a string.
1793+
ArgType = StringRecTy::get(Records);
1794+
break;
1795+
case BinOpInit::SETDAGOP:
1796+
// After parsing the first dag argument, switch to expecting
1797+
// a record, with no restriction on its superclasses.
1798+
ArgType = RecordRecTy::get(Records, {});
1799+
break;
1800+
case BinOpInit::GETDAGARG:
1801+
// After parsing the first dag argument, expect an index integer or a
1802+
// name string.
1803+
ArgType = nullptr;
1804+
break;
1805+
case BinOpInit::GETDAGNAME:
1806+
// After parsing the first dag argument, expect an index integer.
1807+
ArgType = IntRecTy::get(Records);
1808+
break;
1809+
default:
1810+
break;
17921811
}
17931812

17941813
if (!consume(tgtok::comma))

llvm/test/TableGen/getsetop.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def bob : Super;
2828
def test {
2929
dag orig = (foo 1, 2:$a, $b);
3030
dag another = (qux "hello", $world);
31+
dag named = (foo:$root 1, 2:$a, $b);
3132

3233
// CHECK: dag replaceWithBar = (bar 1, 2:$a, ?:$b);
3334
dag replaceWithBar = !setop(orig, bar);
@@ -41,6 +42,19 @@ def test {
4142
// CHECK: dag getopToSetop = (foo "hello", ?:$world);
4243
dag getopToSetop = !setdagop(another, !getdagop(orig));
4344

45+
// CHECK: dag setOpName = (foo:$baz 1, 2:$a, ?:$b);
46+
dag setOpName = !setdagopname(orig, "baz");
47+
48+
// CHECK: dag getopNameToSetOpName = (foo:$root 1, 2:$a, ?:$b);
49+
dag getopNameToSetOpName = !setdagopname(orig, !getdagopname(named));
50+
51+
// CHECK: dag setOpNameExpl = (foo:$baz 1, 2:$a, ?:$b);
52+
dag setOpNameExpl = !setdagopname((foo 1, 2:$a, $b), "baz");
53+
54+
// CHECK: dag getopNameToSetOpNameExpl = (foo:$root 1, 2:$a, ?:$b);
55+
dag getopNameToSetOpNameExpl =
56+
!setdagopname(orig, !getdagopname((foo:$root 1, 2:$a, $b)));
57+
4458
// CHECK: dag getopToBangDag = (foo 1:$a, 2:$b, 3:$c);
4559
dag getopToBangDag = !dag(!getdagop(orig), [1, 2, 3], ["a", "b", "c"]);
4660

llvm/test/TableGen/unsetop.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def test {
1616
dag undefSecond = !con((op 1), (? 2));
1717
// CHECK: dag undefBoth = (? 1, 2);
1818
dag undefBoth = !con((? 1), (? 2));
19+
// CHECK: dag namedLHS = (op:$lhs 1, 2);
20+
dag namedLHS = !con((op:$lhs 1), (op 2));
21+
// CHECK: dag namedRHS = (op:$rhs 1, 2);
22+
dag namedRHS = !con((op 1), (op:$rhs 2));
23+
// CHECK: dag namedBoth = (op:$lhs 1, 2);
24+
dag namedBoth = !con((op:$lhs 1), (op:$rhs 2));
1925

2026
#ifdef ERROR
2127
// ERROR: Concatenated Dag operators do not match: '(op 1)' vs. '(otherop 2)'

0 commit comments

Comments
 (0)