[llvm] [TableGen][NFC] Add record kind to `Record` class (PR #69919)

Wang Pengcheng via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 20:45:00 PDT 2023


https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/69919

>From 6e693052c88c706c71139429877e685a09869683 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Mon, 23 Oct 2023 19:35:05 +0800
Subject: [PATCH 1/2] [TableGen][NFC] Add record kind to `Record` class

enum `RecordKind` is added to indicate the kind of Record (which
can be a normal record, anonymous record, class or multiclass).

Some arguments like `IsMC` and `IsDefm` are removed since we can
get the information from `RecordKind`.
---
 llvm/include/llvm/TableGen/Record.h | 27 +++++++++++-----------
 llvm/lib/TableGen/Record.cpp        |  4 ++--
 llvm/lib/TableGen/TGParser.cpp      | 36 ++++++++++++++---------------
 llvm/lib/TableGen/TGParser.h        |  5 ++--
 4 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index d1023a73d606847..fe105c8486f756d 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -1650,6 +1650,8 @@ class Record {
     DumpInfo(SMLoc Loc, Init *Message) : Loc(Loc), Message(Message) {}
   };
 
+  enum RecordKind { RK_Normal, RK_Anonymous, RK_Class, RK_MultiClass };
+
 private:
   Init *Name;
   // Location where record was instantiated, followed by the location of
@@ -1676,24 +1678,22 @@ class Record {
   // Unique record ID.
   unsigned ID;
 
-  bool IsAnonymous;
-  bool IsClass;
+  RecordKind Kind;
 
   void checkName();
 
 public:
   // Constructs a record.
   explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
-                  bool Anonymous = false, bool Class = false)
+                  RecordKind Kind = RK_Normal)
       : Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records),
-        ID(getNewUID(N->getRecordKeeper())), IsAnonymous(Anonymous),
-        IsClass(Class) {
+        ID(getNewUID(N->getRecordKeeper())), Kind(Kind) {
     checkName();
   }
 
   explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records,
-                  bool Class = false)
-      : Record(StringInit::get(records, N), locs, records, false, Class) {}
+                  RecordKind Kind = RK_Normal)
+      : Record(StringInit::get(records, N), locs, records, Kind) {}
 
   // When copy-constructing a Record, we must still guarantee a globally unique
   // ID number. Don't copy CorrespondingDefInit either, since it's owned by the
@@ -1702,8 +1702,7 @@ class Record {
       : Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
         Values(O.Values), Assertions(O.Assertions),
         SuperClasses(O.SuperClasses), TrackedRecords(O.TrackedRecords),
-        ID(getNewUID(O.getRecords())), IsAnonymous(O.IsAnonymous),
-        IsClass(O.IsClass) {}
+        ID(getNewUID(O.getRecords())), Kind(O.Kind) {}
 
   static unsigned getNewUID(RecordKeeper &RK);
 
@@ -1743,7 +1742,11 @@ class Record {
   /// get the corresponding DefInit.
   DefInit *getDefInit();
 
-  bool isClass() const { return IsClass; }
+  bool isClass() const { return Kind == RK_Class; }
+
+  bool isMultiClass() const { return Kind == RK_MultiClass; }
+
+  bool isAnonymous() const { return Kind == RK_Anonymous; }
 
   ArrayRef<Init *> getTemplateArgs() const {
     return TemplateArgs;
@@ -1871,10 +1874,6 @@ class Record {
     return TrackedRecords;
   }
 
-  bool isAnonymous() const {
-    return IsAnonymous;
-  }
-
   void dump() const;
 
   //===--------------------------------------------------------------------===//
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 675969003ab3cb4..fd07d18e99236e2 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2264,8 +2264,8 @@ DefInit *VarDefInit::instantiate() {
   if (!Def) {
     RecordKeeper &Records = Class->getRecords();
     auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
-                                           Class->getLoc(), Records,
-                                           /*IsAnonymous=*/true);
+                                                Class->getLoc(), Records,
+                                                /*Kind=*/Record::RK_Anonymous);
     Record *NewRec = NewRecOwner.get();
 
     // Copy values from class to instance
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index 16854c7d1f05ab2..53223d284c8582e 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -111,10 +111,11 @@ static void checkConcrete(Record &R) {
 
 /// Return an Init with a qualifier prefix referring
 /// to CurRec's name.
-static Init *QualifyName(Record &CurRec, Init *Name, bool IsMC = false) {
+static Init *QualifyName(Record &CurRec, Init *Name) {
   RecordKeeper &RK = CurRec.getRecords();
   Init *NewName = BinOpInit::getStrConcat(
-      CurRec.getNameInit(), StringInit::get(RK, IsMC ? "::" : ":"));
+      CurRec.getNameInit(),
+      StringInit::get(RK, CurRec.isMultiClass() ? "::" : ":"));
   NewName = BinOpInit::getStrConcat(NewName, Name);
 
   if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
@@ -123,16 +124,16 @@ static Init *QualifyName(Record &CurRec, Init *Name, bool IsMC = false) {
 }
 
 static Init *QualifyName(MultiClass *MC, Init *Name) {
-  return QualifyName(MC->Rec, Name, /*IsMC=*/true);
+  return QualifyName(MC->Rec, Name);
 }
 
 /// Return the qualified version of the implicit 'NAME' template argument.
-static Init *QualifiedNameOfImplicitName(Record &Rec, bool IsMC = false) {
-  return QualifyName(Rec, StringInit::get(Rec.getRecords(), "NAME"), IsMC);
+static Init *QualifiedNameOfImplicitName(Record &Rec) {
+  return QualifyName(Rec, StringInit::get(Rec.getRecords(), "NAME"));
 }
 
 static Init *QualifiedNameOfImplicitName(MultiClass *MC) {
-  return QualifiedNameOfImplicitName(MC->Rec, /*IsMC=*/true);
+  return QualifiedNameOfImplicitName(MC->Rec);
 }
 
 Init *TGVarScope::getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,
@@ -143,11 +144,10 @@ Init *TGVarScope::getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,
   if (It != Vars.end())
     return It->second;
 
-  auto FindValueInArgs = [&](Record *Rec, StringInit *Name,
-                             bool IsMC) -> Init * {
+  auto FindValueInArgs = [&](Record *Rec, StringInit *Name) -> Init * {
     if (!Rec)
       return nullptr;
-    Init *ArgName = QualifyName(*Rec, Name, IsMC);
+    Init *ArgName = QualifyName(*Rec, Name);
     if (Rec->isTemplateArg(ArgName)) {
       RecordVal *RV = Rec->getValue(ArgName);
       assert(RV && "Template arg doesn't exist??");
@@ -177,7 +177,7 @@ Init *TGVarScope::getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,
 
       // The variable is a class template argument?
       if (CurRec->isClass())
-        if (auto *V = FindValueInArgs(CurRec, Name, /*IsMC=*/false))
+        if (auto *V = FindValueInArgs(CurRec, Name))
           return V;
     }
     break;
@@ -194,7 +194,7 @@ Init *TGVarScope::getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,
   case SK_MultiClass: {
     // The variable is a multiclass template argument?
     if (CurMultiClass)
-      if (auto *V = FindValueInArgs(&CurMultiClass->Rec, Name, /*IsMC=*/true))
+      if (auto *V = FindValueInArgs(&CurMultiClass->Rec, Name))
         return V;
     break;
   }
@@ -772,8 +772,7 @@ ParseSubClassReference(Record *CurRec, bool isDefm) {
     return Result;
   }
 
-  if (ParseTemplateArgValueList(Result.TemplateArgs, CurRec, Result.Rec,
-                                isDefm)) {
+  if (ParseTemplateArgValueList(Result.TemplateArgs, CurRec, Result.Rec)) {
     Result.Rec = nullptr; // Error parsing value list.
     return Result;
   }
@@ -810,7 +809,7 @@ ParseSubMultiClassReference(MultiClass *CurMC) {
   }
 
   if (ParseTemplateArgValueList(Result.TemplateArgs, &CurMC->Rec,
-                                &Result.MC->Rec, true)) {
+                                &Result.MC->Rec)) {
     Result.MC = nullptr; // Error parsing value list.
     return Result;
   }
@@ -3160,8 +3159,7 @@ void TGParser::ParseValueList(SmallVectorImpl<Init *> &Result, Record *CurRec,
 //   PostionalArgValueList ::= [Value {',' Value}*]
 //   NamedArgValueList ::= [NameValue '=' Value {',' NameValue '=' Value}*]
 bool TGParser::ParseTemplateArgValueList(
-    SmallVectorImpl<ArgumentInit *> &Result, Record *CurRec, Record *ArgsRec,
-    bool IsDefm) {
+    SmallVectorImpl<ArgumentInit *> &Result, Record *CurRec, Record *ArgsRec) {
   assert(Result.empty() && "Result vector is not empty");
   ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
 
@@ -3192,7 +3190,7 @@ bool TGParser::ParseTemplateArgValueList(
                      "The name of named argument should be a valid identifier");
 
       auto *Name = cast<StringInit>(Value);
-      Init *QualifiedName = QualifyName(*ArgsRec, Name, /*IsMC=*/IsDefm);
+      Init *QualifiedName = QualifyName(*ArgsRec, Name);
       auto *NamedArg = ArgsRec->getValue(QualifiedName);
       if (!NamedArg)
         return Error(ValueLoc,
@@ -3612,7 +3610,7 @@ bool TGParser::ParseDef(MultiClass *CurMultiClass) {
   if (isa<UnsetInit>(Name)) {
     CurRec =
         std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
-                                 /*Anonymous=*/true);
+                                 /*Kind=*/Record::RK_Anonymous);
   } else {
     CurRec = std::make_unique<Record>(Name, NameLoc, Records);
   }
@@ -3932,7 +3930,7 @@ bool TGParser::ParseClass() {
     // If this is the first reference to this class, create and add it.
     auto NewRec =
         std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
-                                  /*Class=*/true);
+                                 /*Kind=*/Record::RK_Class);
     CurRec = NewRec.get();
     Records.addClass(std::move(NewRec));
   }
diff --git a/llvm/lib/TableGen/TGParser.h b/llvm/lib/TableGen/TGParser.h
index c5365ff2709243f..394cf980e528b5f 100644
--- a/llvm/lib/TableGen/TGParser.h
+++ b/llvm/lib/TableGen/TGParser.h
@@ -85,7 +85,7 @@ struct MultiClass {
   void dump() const;
 
   MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)
-      : Rec(Name, Loc, Records) {}
+      : Rec(Name, Loc, Records, /*Kind=*/Record::RK_MultiClass) {}
 };
 
 class TGVarScope {
@@ -293,8 +293,7 @@ class TGParser {
   void ParseValueList(SmallVectorImpl<llvm::Init*> &Result,
                       Record *CurRec, RecTy *ItemType = nullptr);
   bool ParseTemplateArgValueList(SmallVectorImpl<llvm::ArgumentInit *> &Result,
-                                 Record *CurRec, Record *ArgsRec,
-                                 bool IsDefm = false);
+                                 Record *CurRec, Record *ArgsRec);
   void ParseDagArgList(
       SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
       Record *CurRec);

>From dd84a1661067674207fdd6485ea22731da4f1823 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Tue, 24 Oct 2023 11:44:32 +0800
Subject: [PATCH 2/2] fixup! [TableGen][NFC] Add record kind to `Record` class

Apply renaming suggestion and remove argument comments
---
 llvm/include/llvm/TableGen/Record.h |  8 ++++----
 llvm/lib/TableGen/Record.cpp        |  6 +++---
 llvm/lib/TableGen/TGParser.cpp      | 10 ++++------
 llvm/lib/TableGen/TGParser.h        |  2 +-
 4 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index fe105c8486f756d..5a47a79d8f8fff9 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -1650,7 +1650,7 @@ class Record {
     DumpInfo(SMLoc Loc, Init *Message) : Loc(Loc), Message(Message) {}
   };
 
-  enum RecordKind { RK_Normal, RK_Anonymous, RK_Class, RK_MultiClass };
+  enum RecordKind { RK_Def, RK_AnonymousDef, RK_Class, RK_MultiClass };
 
 private:
   Init *Name;
@@ -1685,14 +1685,14 @@ class Record {
 public:
   // Constructs a record.
   explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
-                  RecordKind Kind = RK_Normal)
+                  RecordKind Kind = RK_Def)
       : Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records),
         ID(getNewUID(N->getRecordKeeper())), Kind(Kind) {
     checkName();
   }
 
   explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records,
-                  RecordKind Kind = RK_Normal)
+                  RecordKind Kind = RK_Def)
       : Record(StringInit::get(records, N), locs, records, Kind) {}
 
   // When copy-constructing a Record, we must still guarantee a globally unique
@@ -1746,7 +1746,7 @@ class Record {
 
   bool isMultiClass() const { return Kind == RK_MultiClass; }
 
-  bool isAnonymous() const { return Kind == RK_Anonymous; }
+  bool isAnonymous() const { return Kind == RK_AnonymousDef; }
 
   ArrayRef<Init *> getTemplateArgs() const {
     return TemplateArgs;
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index fd07d18e99236e2..aa981fdab4b3e69 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2263,9 +2263,9 @@ void VarDefInit::Profile(FoldingSetNodeID &ID) const {
 DefInit *VarDefInit::instantiate() {
   if (!Def) {
     RecordKeeper &Records = Class->getRecords();
-    auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
-                                                Class->getLoc(), Records,
-                                                /*Kind=*/Record::RK_Anonymous);
+    auto NewRecOwner =
+        std::make_unique<Record>(Records.getNewAnonymousName(), Class->getLoc(),
+                                 Records, Record::RK_AnonymousDef);
     Record *NewRec = NewRecOwner.get();
 
     // Copy values from class to instance
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index 53223d284c8582e..21d6ca0e7b8d302 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -3608,9 +3608,8 @@ bool TGParser::ParseDef(MultiClass *CurMultiClass) {
     return true;
 
   if (isa<UnsetInit>(Name)) {
-    CurRec =
-        std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
-                                 /*Kind=*/Record::RK_Anonymous);
+    CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
+                                      Records, Record::RK_AnonymousDef);
   } else {
     CurRec = std::make_unique<Record>(Name, NameLoc, Records);
   }
@@ -3928,9 +3927,8 @@ bool TGParser::ParseClass() {
     CurRec->updateClassLoc(Lex.getLoc());
   } else {
     // If this is the first reference to this class, create and add it.
-    auto NewRec =
-        std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
-                                 /*Kind=*/Record::RK_Class);
+    auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
+                                           Records, Record::RK_Class);
     CurRec = NewRec.get();
     Records.addClass(std::move(NewRec));
   }
diff --git a/llvm/lib/TableGen/TGParser.h b/llvm/lib/TableGen/TGParser.h
index 394cf980e528b5f..0929154fed3d414 100644
--- a/llvm/lib/TableGen/TGParser.h
+++ b/llvm/lib/TableGen/TGParser.h
@@ -85,7 +85,7 @@ struct MultiClass {
   void dump() const;
 
   MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)
-      : Rec(Name, Loc, Records, /*Kind=*/Record::RK_MultiClass) {}
+      : Rec(Name, Loc, Records, Record::RK_MultiClass) {}
 };
 
 class TGVarScope {



More information about the llvm-commits mailing list