[llvm] r198348 - [TableGen] Correctly generate implicit anonymous prototype defs in multiclasses

Hal Finkel hfinkel at anl.gov
Thu Jan 2 12:47:09 PST 2014


Author: hfinkel
Date: Thu Jan  2 14:47:09 2014
New Revision: 198348

URL: http://llvm.org/viewvc/llvm-project?rev=198348&view=rev
Log:
[TableGen] Correctly generate implicit anonymous prototype defs in multiclasses

Even within a multiclass, we had been generating concrete implicit anonymous
defs when parsing values (generally in value lists). This behavior was
incorrect, and led to errors when multiclass parameters were used in the
parameter list of the implicit anonymous def.

If we had some multiclass:

multiclass mc<string n> {

 ... : SomeClass<SomeOtherClass<n> >

The capture of the multiclass parameter 'n' would not work correctly, and
depending on how the implicit SomeOtherClass was used, either TableGen would
ignore something it shouldn't, or would crash.

To fix this problem, when inside a multiclass, we generate prototype anonymous
defs for implicit anonymous defs (just as we do for explicit anonymous defs).
Within the multiclass, the current record prototype is populated with a node
that is essentially: !cast<SomeOtherClass>(!strconcat(NAME, anon_value_name)).
This is then resolved to the correct concrete anonymous def, in the usual way,
when NAME is resolved during multiclass instantiation.

Modified:
    llvm/trunk/lib/TableGen/TGParser.cpp
    llvm/trunk/test/TableGen/MultiClassDefName.td

Modified: llvm/trunk/lib/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.cpp?rev=198348&r1=198347&r2=198348&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGParser.cpp (original)
+++ llvm/trunk/lib/TableGen/TGParser.cpp Thu Jan  2 14:47:09 2014
@@ -1225,8 +1225,36 @@ Init *TGParser::ParseSimpleValue(Record
     // Add info about the subclass to NewRec.
     if (AddSubClass(NewRec, SCRef))
       return 0;
-    NewRec->resolveReferences();
-    Records.addDef(NewRec);
+    if (!CurMultiClass) {
+      NewRec->resolveReferences();
+      Records.addDef(NewRec);
+    } else {
+      // Otherwise, we're inside a multiclass, add it to the multiclass.
+      CurMultiClass->DefPrototypes.push_back(NewRec);
+
+      // Copy the template arguments for the multiclass into the def.
+      const std::vector<Init *> &TArgs =
+                                  CurMultiClass->Rec.getTemplateArgs();
+
+      for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
+        const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
+        assert(RV && "Template arg doesn't exist?");
+        NewRec->addValue(*RV);
+      }
+
+      // We can't return the prototype def here, instead return:
+      // !cast<ItemType>(!strconcat(NAME, AnonName)).
+      const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
+      assert(MCNameRV && "multiclass record must have a NAME");
+
+      return UnOpInit::get(UnOpInit::CAST,
+                           BinOpInit::get(BinOpInit::STRCONCAT,
+                                          VarInit::get(MCNameRV->getName(),
+                                                       MCNameRV->getType()),
+                                          NewRec->getNameInit(),
+                                          StringRecTy::get()),
+                           Class->getDefInit()->getType());
+    }
 
     // The result of the expression is a reference to the new record.
     return DefInit::get(NewRec);
@@ -1962,7 +1990,18 @@ bool TGParser::ParseDef(MultiClass *CurM
       return true;
     }
     Records.addDef(CurRec);
+
+    if (ParseObjectBody(CurRec))
+      return true;
   } else if (CurMultiClass) {
+    // Parse the body before adding this prototype to the DefPrototypes vector.
+    // That way implicit definitions will be added to the DefPrototypes vector
+    // before this object, instantiated prior to defs derived from this object,
+    // and this available for indirect name resolution when defs derived from
+    // this object are instantiated.
+    if (ParseObjectBody(CurRec))
+      return true;
+
     // Otherwise, a def inside a multiclass, add it to the multiclass.
     for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
       if (CurMultiClass->DefPrototypes[i]->getNameInit()
@@ -1972,9 +2011,7 @@ bool TGParser::ParseDef(MultiClass *CurM
         return true;
       }
     CurMultiClass->DefPrototypes.push_back(CurRec);
-  }
-
-  if (ParseObjectBody(CurRec))
+  } else if (ParseObjectBody(CurRec))
     return true;
 
   if (CurMultiClass == 0)  // Def's in multiclasses aren't really defs.

Modified: llvm/trunk/test/TableGen/MultiClassDefName.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/MultiClassDefName.td?rev=198348&r1=198347&r2=198348&view=diff
==============================================================================
--- llvm/trunk/test/TableGen/MultiClassDefName.td (original)
+++ llvm/trunk/test/TableGen/MultiClassDefName.td Thu Jan  2 14:47:09 2014
@@ -29,3 +29,13 @@ multiclass MC<string name> {
 
 defm : MC<"foo">;
 
+multiclass MC2<string name> {
+  def there : Outer<C<name> >;
+}
+
+// Ensure that we've correctly captured the reference to name from the implicit
+// anonymous C def in the template parameter list of Outer.
+// CHECK-NOT: MC2::name
+
+defm : MC2<"bar">;
+





More information about the llvm-commits mailing list