[PATCH] D80865: [TableGen] defm in a loop is not final (bug fix)

Hal Finkel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat May 30 05:48:29 PDT 2020


hfinkel created this revision.
hfinkel added reviewers: simon_tatham, greened, nhaehnle, JDevlieghere.
Herald added subscribers: bollu, hiraditya, mcrosier.
Herald added a project: LLVM.

The TableGen parsing code has a bug: when parsing a defm statement, it tries to do a final resolution even if inside a top-level loop. This doesn't work, however, if things inside the multiclass being resolved need access to the loop-iteration variable. These defm statements need to be delayed until the top-level loop itself is evaluated.

As a quick example, given this:

  multiclass D<bit b> {
    def A;
  
    foreach _ = !if(b, [0], []<int>) in
    def B;
  }
  
  foreach ShouldI = 0-1 in
  defm MD#ShouldI : D<ShouldI>;

TableGen would provide a parsing error:

  foreach-multiclass.td:93:3: error: attempting to loop over '!if(ShouldI, [0], [])', expected a list
    foreach _ = !if(b, [0], []<int>) in
    ^

When the defm resolution is delayed, the evaluation works as expected.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80865

Files:
  llvm/lib/TableGen/TGParser.cpp
  llvm/test/TableGen/foreach-multiclass.td


Index: llvm/test/TableGen/foreach-multiclass.td
===================================================================
--- llvm/test/TableGen/foreach-multiclass.td
+++ llvm/test/TableGen/foreach-multiclass.td
@@ -57,6 +57,11 @@
 // CHECK:     def F2_2_1
 // CHECK-NOT: def F2_2_2
 
+// CHECK:     def MD0A
+// CHECK-NOT: def MD0B
+// CHECK:     def MD1A
+// CHECK:     def MD1B
+
 multiclass A<int x> {
   foreach i = [0, 1] in {
     def NAME#i {
@@ -116,3 +121,7 @@
 defm F0 : F<[]>;
 defm F1 : F<[0]>;
 defm F2 : F<[0, 1, 2]>;
+
+foreach ShouldI = 0-1 in
+defm MD#ShouldI : D<ShouldI>;
+
Index: llvm/lib/TableGen/TGParser.cpp
===================================================================
--- llvm/lib/TableGen/TGParser.cpp
+++ llvm/lib/TableGen/TGParser.cpp
@@ -3254,8 +3254,8 @@
 
     Substs.emplace_back(QualifiedNameOfImplicitName(MC), DefmName);
 
-    if (resolve(MC->Entries, Substs, CurMultiClass == nullptr, &NewEntries,
-                &SubClassLoc))
+    if (resolve(MC->Entries, Substs, CurMultiClass == nullptr && Loops.empty(),
+                &NewEntries, &SubClassLoc))
       return true;
 
     if (!consume(tgtok::comma))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80865.267452.patch
Type: text/x-patch
Size: 1149 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200530/e2efc346/attachment.bin>


More information about the llvm-commits mailing list