[llvm] r325883 - TableGen: Add !size operation
Nicolai Haehnle via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 23 02:46:07 PST 2018
Author: nha
Date: Fri Feb 23 02:46:07 2018
New Revision: 325883
URL: http://llvm.org/viewvc/llvm-project?rev=325883&view=rev
Log:
TableGen: Add !size operation
Summary:
Returns the size of a list. I have found this to be rather useful in some
development for the AMDGPU backend where we could simplify our .td files
by concatenating list<LLVMType> for complex intrinsics. Doing so requires
us to compute the position argument for LLVMMatchType.
Basically, the usage is in a pattern that looks somewhat like this:
list<LLVMType> argtypes =
!listconcat(base,
[llvm_any_ty, LLVMMatchType<!size(base)>]);
Change-Id: I360a0b000fd488d18bea412228230fd93722bd2c
Reviewers: arsenm, craig.topper, tra, MartinO
Subscribers: wdng, llvm-commits, tpr
Differential Revision: https://reviews.llvm.org/D43553
Added:
llvm/trunk/test/TableGen/size.td
Modified:
llvm/trunk/docs/TableGen/LangIntro.rst
llvm/trunk/docs/TableGen/LangRef.rst
llvm/trunk/include/llvm/TableGen/Record.h
llvm/trunk/lib/TableGen/Record.cpp
llvm/trunk/lib/TableGen/TGLexer.cpp
llvm/trunk/lib/TableGen/TGLexer.h
llvm/trunk/lib/TableGen/TGParser.cpp
Modified: llvm/trunk/docs/TableGen/LangIntro.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/TableGen/LangIntro.rst?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/docs/TableGen/LangIntro.rst (original)
+++ llvm/trunk/docs/TableGen/LangIntro.rst Fri Feb 23 02:46:07 2018
@@ -205,6 +205,9 @@ supported include:
``!empty(a)``
An integer {0,1} indicating whether list 'a' is empty.
+``!size(a)``
+ An integer indicating the number of elements in list 'a'.
+
``!if(a,b,c)``
'b' if the result of 'int' or 'bit' operator 'a' is nonzero, 'c' otherwise.
Modified: llvm/trunk/docs/TableGen/LangRef.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/TableGen/LangRef.rst?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/docs/TableGen/LangRef.rst (original)
+++ llvm/trunk/docs/TableGen/LangRef.rst Fri Feb 23 02:46:07 2018
@@ -98,7 +98,7 @@ wide variety of meanings:
:!eq !if !head !tail !con
:!add !shl !sra !srl !and
:!or !empty !subst !foreach !strconcat
- :!cast !listconcat
+ :!cast !listconcat !size
Syntax
Modified: llvm/trunk/include/llvm/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TableGen/Record.h?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TableGen/Record.h (original)
+++ llvm/trunk/include/llvm/TableGen/Record.h Fri Feb 23 02:46:07 2018
@@ -759,7 +759,7 @@ public:
///
class UnOpInit : public OpInit, public FoldingSetNode {
public:
- enum UnaryOp : uint8_t { CAST, HEAD, TAIL, EMPTY };
+ enum UnaryOp : uint8_t { CAST, HEAD, TAIL, SIZE, EMPTY };
private:
Init *LHS;
Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Fri Feb 23 02:46:07 2018
@@ -722,6 +722,11 @@ Init *UnOpInit::Fold(Record *CurRec, Mul
}
break;
+ case SIZE:
+ if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
+ return IntInit::get(LHSl->size());
+ break;
+
case EMPTY:
if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
return IntInit::get(LHSl->empty());
@@ -746,6 +751,7 @@ std::string UnOpInit::getAsString() cons
case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
case HEAD: Result = "!head"; break;
case TAIL: Result = "!tail"; break;
+ case SIZE: Result = "!size"; break;
case EMPTY: Result = "!empty"; break;
}
return Result + "(" + LHS->getAsString() + ")";
Modified: llvm/trunk/lib/TableGen/TGLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGLexer.cpp?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGLexer.cpp (original)
+++ llvm/trunk/lib/TableGen/TGLexer.cpp Fri Feb 23 02:46:07 2018
@@ -469,6 +469,7 @@ tgtok::TokKind TGLexer::LexExclaim() {
.Case("if", tgtok::XIf)
.Case("head", tgtok::XHead)
.Case("tail", tgtok::XTail)
+ .Case("size", tgtok::XSize)
.Case("con", tgtok::XConcat)
.Case("add", tgtok::XADD)
.Case("and", tgtok::XAND)
Modified: llvm/trunk/lib/TableGen/TGLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGLexer.h?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGLexer.h (original)
+++ llvm/trunk/lib/TableGen/TGLexer.h Fri Feb 23 02:46:07 2018
@@ -48,7 +48,7 @@ namespace tgtok {
// !keywords.
XConcat, XADD, XAND, XOR, XSRA, XSRL, XSHL, XListConcat, XStrConcat, XCast,
- XSubst, XForEach, XHead, XTail, XEmpty, XIf, XEq,
+ XSubst, XForEach, XHead, XTail, XSize, XEmpty, XIf, XEq,
// Integer value.
IntVal,
Modified: llvm/trunk/lib/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.cpp?rev=325883&r1=325882&r2=325883&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGParser.cpp (original)
+++ llvm/trunk/lib/TableGen/TGParser.cpp Fri Feb 23 02:46:07 2018
@@ -781,6 +781,7 @@ Init *TGParser::ParseOperation(Record *C
return nullptr;
case tgtok::XHead:
case tgtok::XTail:
+ case tgtok::XSize:
case tgtok::XEmpty:
case tgtok::XCast: { // Value ::= !unop '(' Value ')'
UnOpInit::UnaryOp Code;
@@ -808,6 +809,11 @@ Init *TGParser::ParseOperation(Record *C
Lex.Lex(); // eat the operation
Code = UnOpInit::TAIL;
break;
+ case tgtok::XSize:
+ Lex.Lex();
+ Code = UnOpInit::SIZE;
+ Type = IntRecTy::get();
+ break;
case tgtok::XEmpty:
Lex.Lex(); // eat the operation
Code = UnOpInit::EMPTY;
@@ -842,12 +848,15 @@ Init *TGParser::ParseOperation(Record *C
}
}
- if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
+ if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
+ Code == UnOpInit::SIZE) {
if (!LHSl && !LHSt) {
TokError("expected list type argument in unary operator");
return nullptr;
}
+ }
+ if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
if (LHSl && LHSl->empty()) {
TokError("empty list argument in unary operator");
return nullptr;
@@ -1453,6 +1462,7 @@ Init *TGParser::ParseSimpleValue(Record
case tgtok::XHead:
case tgtok::XTail:
+ case tgtok::XSize:
case tgtok::XEmpty:
case tgtok::XCast: // Value ::= !unop '(' Value ')'
case tgtok::XConcat:
Added: llvm/trunk/test/TableGen/size.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/size.td?rev=325883&view=auto
==============================================================================
--- llvm/trunk/test/TableGen/size.td (added)
+++ llvm/trunk/test/TableGen/size.td Fri Feb 23 02:46:07 2018
@@ -0,0 +1,34 @@
+// RUN: llvm-tblgen %s | FileCheck %s
+// XFAIL: vg_leak
+
+// CHECK: --- Defs ---
+
+// CHECK: def A1 {
+// CHECK: int Val = 0;
+// CHECK: }
+
+// CHECK: def A2 {
+// CHECK: int Val = 3;
+// CHECK: }
+
+// CHECK: def B1 {
+// CHECK: int Val = 0;
+// CHECK: }
+
+// CHECK: def B2 {
+// CHECK: int Val = 2;
+// CHECK: }
+
+class A<list<int> L> {
+ int Val = !size(L);
+}
+
+class B<list<string> L> {
+ int Val = !size(L);
+}
+
+def A1 : A<[]>;
+def A2 : A<[1, 1, 2]>;
+
+def B1 : B<[]>;
+def B2 : B<["a", "b"]>;
More information about the llvm-commits
mailing list