Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVM][TableGen] Change all Init pointers to const #112705

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Oct 17, 2024

@jurahul jurahul marked this pull request as ready for review October 17, 2024 15:17
@jurahul jurahul requested a review from arsenm October 17, 2024 15:17
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 mlir:core MLIR Core Infrastructure tablegen llvm:globalisel mlir backend:DirectX llvm:SelectionDAG SelectionDAGISel as well labels Oct 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-tablegen
@llvm/pr-subscribers-backend-directx
@llvm/pr-subscribers-mlir-core
@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-mlir

Author: Rahul Joshi (jurahul)

Changes

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089


Patch is 275.42 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/112705.diff

41 Files Affected:

  • (modified) clang/utils/TableGen/ClangOptionDocEmitter.cpp (+2-2)
  • (modified) llvm/include/llvm/TableGen/Error.h (+2-2)
  • (modified) llvm/include/llvm/TableGen/Record.h (+270-256)
  • (modified) llvm/lib/TableGen/DetailedRecordsBackend.cpp (+1-1)
  • (modified) llvm/lib/TableGen/Error.cpp (+2-2)
  • (modified) llvm/lib/TableGen/Record.cpp (+445-428)
  • (modified) llvm/lib/TableGen/TGParser.cpp (+220-208)
  • (modified) llvm/lib/TableGen/TGParser.h (+42-40)
  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+22-22)
  • (modified) llvm/utils/TableGen/AsmWriterEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/Attributes.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp (+1-1)
  • (modified) llvm/utils/TableGen/CodeEmitterGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/CodeGenMapTable.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+31-30)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.h (+5-5)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.h (+1-1)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.cpp (+11-11)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.h (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenRegisters.cpp (+8-8)
  • (modified) llvm/utils/TableGen/Common/CodeGenSchedule.cpp (+5-4)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+5-5)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+6-5)
  • (modified) llvm/utils/TableGen/CompressInstEmitter.cpp (+4-3)
  • (modified) llvm/utils/TableGen/DAGISelMatcherGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DFAEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DXILEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DecoderEmitter.cpp (+18-18)
  • (modified) llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/InstrInfoEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/OptionParserEmitter.cpp (+5-5)
  • (modified) llvm/utils/TableGen/RegisterInfoEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/SearchableTableEmitter.cpp (+5-4)
  • (modified) llvm/utils/TableGen/X86FoldTablesEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/X86InstrMappingEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/X86RecognizableInstr.cpp (+3-5)
  • (modified) mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp (+1-2)
diff --git a/clang/utils/TableGen/ClangOptionDocEmitter.cpp b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
index b67c5d1d1146c6..ba8840c1bdca78 100644
--- a/clang/utils/TableGen/ClangOptionDocEmitter.cpp
+++ b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
@@ -367,13 +367,13 @@ void emitOption(const DocumentedOption &Option, const Record *DocInfo,
   for (const Record *VisibilityHelp :
        R->getValueAsListOfDefs("HelpTextsForVariants")) {
     // This is a list of visibilities.
-    ArrayRef<Init *> Visibilities =
+    ArrayRef<const Init *> Visibilities =
         VisibilityHelp->getValueAsListInit("Visibilities")->getValues();
 
     // See if any of the program's visibilities are in the list.
     for (StringRef DocInfoMask :
          DocInfo->getValueAsListOfStrings("VisibilityMask")) {
-      for (Init *Visibility : Visibilities) {
+      for (const Init *Visibility : Visibilities) {
         if (Visibility->getAsUnquotedString() == DocInfoMask) {
           // Use the first one we find.
           Description = escapeRST(VisibilityHelp->getValueAsString("Text"));
diff --git a/llvm/include/llvm/TableGen/Error.h b/llvm/include/llvm/TableGen/Error.h
index 512249b0160c24..b963dcba9869fb 100644
--- a/llvm/include/llvm/TableGen/Error.h
+++ b/llvm/include/llvm/TableGen/Error.h
@@ -49,8 +49,8 @@ void PrintError(const RecordVal *RecVal, const Twine &Msg);
 [[noreturn]] void PrintFatalError(function_ref<void(raw_ostream &OS)> PrintMsg);
 
 // Returns true if the assert failed.
-bool CheckAssert(SMLoc Loc, Init *Condition, Init *Message);
-void dumpMessage(SMLoc Loc, Init *Message);
+bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message);
+void dumpMessage(SMLoc Loc, const Init *Message);
 
 extern SourceMgr SrcMgr;
 extern unsigned ErrorsPrinted;
diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index f856ff4cbd34b5..63267b7633f6cf 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -374,25 +374,26 @@ class Init {
   /// If this value is convertible to type \p Ty, return a value whose
   /// type is \p Ty, generating a !cast operation if required.
   /// Otherwise, return null.
-  virtual Init *getCastTo(const RecTy *Ty) const = 0;
+  virtual const Init *getCastTo(const RecTy *Ty) const = 0;
 
   /// Convert to a value whose type is \p Ty, or return null if this
   /// is not possible. This can happen if the value's type is convertible
   /// to \p Ty, but there are unresolved references.
-  virtual Init *convertInitializerTo(const RecTy *Ty) const = 0;
+  virtual const Init *convertInitializerTo(const RecTy *Ty) const = 0;
 
   /// This function is used to implement the bit range
   /// selection operator. Given a value, it selects the specified bits,
   /// returning them as a new \p Init of type \p bits. If it is not legal
   /// to use the bit selection operator on this value, null is returned.
-  virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
+  virtual const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
     return nullptr;
   }
 
   /// This function is used to implement the FieldInit class.
   /// Implementors of this method should return the type of the named
   /// field if they are of type record.
-  virtual const RecTy *getFieldType(StringInit *FieldName) const {
+  virtual const RecTy *getFieldType(const StringInit *FieldName) const {
     return nullptr;
   }
 
@@ -400,12 +401,12 @@ class Init {
   /// variables which may not be defined at the time the expression is formed.
   /// If a value is set for the variable later, this method will be called on
   /// users of the value to allow the value to propagate out.
-  virtual Init *resolveReferences(Resolver &R) const {
+  virtual const Init *resolveReferences(Resolver &R) const {
     return const_cast<Init *>(this);
   }
 
   /// Get the \p Init value of the specified bit.
-  virtual Init *getBit(unsigned Bit) const = 0;
+  virtual const Init *getBit(unsigned Bit) const = 0;
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
@@ -436,15 +437,16 @@ class TypedInit : public Init {
   /// Get the record keeper that initialized this Init.
   RecordKeeper &getRecordKeeper() const { return ValueTy->getRecordKeeper(); }
 
-  Init *getCastTo(const RecTy *Ty) const override;
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *getCastTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
 
   /// This method is used to implement the FieldInit class.
   /// Implementors of this method should return the type of the named field if
   /// they are of type record.
-  const RecTy *getFieldType(StringInit *FieldName) const override;
+  const RecTy *getFieldType(const StringInit *FieldName) const override;
 };
 
 /// '?' - Represents an uninitialized value.
@@ -470,10 +472,10 @@ class UnsetInit : public Init {
   /// Get the record keeper that initialized this Init.
   RecordKeeper &getRecordKeeper() const { return RK; }
 
-  Init *getCastTo(const RecTy *Ty) const override;
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *getCastTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     return const_cast<UnsetInit*>(this);
   }
 
@@ -487,7 +489,7 @@ class UnsetInit : public Init {
 };
 
 // Represent an argument.
-using ArgAuxType = std::variant<unsigned, Init *>;
+using ArgAuxType = std::variant<unsigned, const Init *>;
 class ArgumentInit : public Init, public FoldingSetNode {
 public:
   enum Kind {
@@ -496,11 +498,11 @@ class ArgumentInit : public Init, public FoldingSetNode {
   };
 
 private:
-  Init *Value;
+  const Init *Value;
   ArgAuxType Aux;
 
 protected:
-  explicit ArgumentInit(Init *Value, ArgAuxType Aux)
+  explicit ArgumentInit(const Init *Value, ArgAuxType Aux)
       : Init(IK_ArgumentInit), Value(Value), Aux(Aux) {}
 
 public:
@@ -511,25 +513,27 @@ class ArgumentInit : public Init, public FoldingSetNode {
 
   RecordKeeper &getRecordKeeper() const { return Value->getRecordKeeper(); }
 
-  static ArgumentInit *get(Init *Value, ArgAuxType Aux);
+  static const ArgumentInit *get(const Init *Value, ArgAuxType Aux);
 
   bool isPositional() const { return Aux.index() == Positional; }
   bool isNamed() const { return Aux.index() == Named; }
 
-  Init *getValue() const { return Value; }
+  const Init *getValue() const { return Value; }
   unsigned getIndex() const {
     assert(isPositional() && "Should be positional!");
     return std::get<Positional>(Aux);
   }
-  Init *getName() const {
+  const Init *getName() const {
     assert(isNamed() && "Should be named!");
     return std::get<Named>(Aux);
   }
-  ArgumentInit *cloneWithValue(Init *Value) const { return get(Value, Aux); }
+  const ArgumentInit *cloneWithValue(const Init *Value) const {
+    return get(Value, Aux);
+  }
 
   void Profile(FoldingSetNodeID &ID) const;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
   std::string getAsString() const override {
     if (isPositional())
       return utostr(getIndex()) + ": " + Value->getAsString();
@@ -541,11 +545,11 @@ class ArgumentInit : public Init, public FoldingSetNode {
 
   bool isComplete() const override { return false; }
   bool isConcrete() const override { return false; }
-  Init *getBit(unsigned Bit) const override { return Value->getBit(Bit); }
-  Init *getCastTo(const RecTy *Ty) const override {
+  const Init *getBit(unsigned Bit) const override { return Value->getBit(Bit); }
+  const Init *getCastTo(const RecTy *Ty) const override {
     return Value->getCastTo(Ty);
   }
-  Init *convertInitializerTo(const RecTy *Ty) const override {
+  const Init *convertInitializerTo(const RecTy *Ty) const override {
     return Value->convertInitializerTo(Ty);
   }
 };
@@ -571,9 +575,9 @@ class BitInit final : public TypedInit {
 
   bool getValue() const { return Value; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     assert(Bit < 1 && "Bit index out of range!");
     return const_cast<BitInit*>(this);
   }
@@ -584,8 +588,9 @@ class BitInit final : public TypedInit {
 
 /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value.
 /// It contains a vector of bits, whose size is determined by the type.
-class BitsInit final : public TypedInit, public FoldingSetNode,
-                       public TrailingObjects<BitsInit, Init *> {
+class BitsInit final : public TypedInit,
+                       public FoldingSetNode,
+                       public TrailingObjects<BitsInit, const Init *> {
   unsigned NumBits;
 
   BitsInit(RecordKeeper &RK, unsigned N)
@@ -602,14 +607,15 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
     return I->getKind() == IK_BitsInit;
   }
 
-  static BitsInit *get(RecordKeeper &RK, ArrayRef<Init *> Range);
+  static BitsInit *get(RecordKeeper &RK, ArrayRef<const Init *> Range);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   unsigned getNumBits() const { return NumBits; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
   std::optional<int64_t> convertInitializerToInt() const;
 
   bool isComplete() const override {
@@ -627,11 +633,11 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
   bool isConcrete() const override;
   std::string getAsString() const override;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     assert(Bit < NumBits && "Bit index out of range!");
-    return getTrailingObjects<Init *>()[Bit];
+    return getTrailingObjects<const Init *>()[Bit];
   }
 };
 
@@ -654,13 +660,14 @@ class IntInit : public TypedInit {
 
   int64_t getValue() const { return Value; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
 
   bool isConcrete() const override { return true; }
   std::string getAsString() const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     return BitInit::get(getRecordKeeper(), (Value & (1ULL << Bit)) != 0);
   }
 };
@@ -684,13 +691,13 @@ class AnonymousNameInit : public TypedInit {
 
   unsigned getValue() const { return Value; }
 
-  StringInit *getNameInit() const;
+  const StringInit *getNameInit() const;
 
   std::string getAsString() const override;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off string");
   }
 };
@@ -718,8 +725,8 @@ class StringInit : public TypedInit {
     return I->getKind() == IK_StringInit;
   }
 
-  static StringInit *get(RecordKeeper &RK, StringRef,
-                         StringFormat Fmt = SF_String);
+  static const StringInit *get(RecordKeeper &RK, StringRef,
+                               StringFormat Fmt = SF_String);
 
   static StringFormat determineFormat(StringFormat Fmt1, StringFormat Fmt2) {
     return (Fmt1 == SF_Code || Fmt2 == SF_Code) ? SF_Code : SF_String;
@@ -729,7 +736,7 @@ class StringInit : public TypedInit {
   StringFormat getFormat() const { return Format; }
   bool hasCodeFormat() const { return Format == SF_Code; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
   bool isConcrete() const override { return true; }
 
@@ -744,19 +751,20 @@ class StringInit : public TypedInit {
     return std::string(Value);
   }
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off string");
   }
 };
 
 /// [AL, AH, CL] - Represent a list of defs
 ///
-class ListInit final : public TypedInit, public FoldingSetNode,
-                       public TrailingObjects<ListInit, Init *> {
+class ListInit final : public TypedInit,
+                       public FoldingSetNode,
+                       public TrailingObjects<ListInit, const Init *> {
   unsigned NumValues;
 
 public:
-  using const_iterator = Init *const *;
+  using const_iterator = const Init *const *;
 
 private:
   explicit ListInit(unsigned N, const RecTy *EltTy)
@@ -772,13 +780,13 @@ class ListInit final : public TypedInit, public FoldingSetNode,
   static bool classof(const Init *I) {
     return I->getKind() == IK_ListInit;
   }
-  static ListInit *get(ArrayRef<Init *> Range, const RecTy *EltTy);
+  static const ListInit *get(ArrayRef<const Init *> Range, const RecTy *EltTy);
 
   void Profile(FoldingSetNodeID &ID) const;
 
-  Init *getElement(unsigned i) const {
+  const Init *getElement(unsigned i) const {
     assert(i < NumValues && "List element index out of range!");
-    return getTrailingObjects<Init *>()[i];
+    return getTrailingObjects<const Init *>()[i];
   }
   const RecTy *getElementType() const {
     return cast<ListRecTy>(getType())->getElementType();
@@ -786,30 +794,30 @@ class ListInit final : public TypedInit, public FoldingSetNode,
 
   const Record *getElementAsRecord(unsigned i) const;
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
   /// This method is used by classes that refer to other
   /// variables which may not be defined at the time they expression is formed.
   /// If a value is set for the variable later, this method will be called on
   /// users of the value to allow the value to propagate out.
   ///
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
   bool isComplete() const override;
   bool isConcrete() const override;
   std::string getAsString() const override;
 
-  ArrayRef<Init*> getValues() const {
-    return ArrayRef(getTrailingObjects<Init *>(), NumValues);
+  ArrayRef<const Init *> getValues() const {
+    return ArrayRef(getTrailingObjects<const Init *>(), NumValues);
   }
 
-  const_iterator begin() const { return getTrailingObjects<Init *>(); }
+  const_iterator begin() const { return getTrailingObjects<const Init *>(); }
   const_iterator end  () const { return begin() + NumValues; }
 
   size_t         size () const { return NumValues;  }
   bool           empty() const { return NumValues == 0; }
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off list");
   }
 };
@@ -831,12 +839,12 @@ class OpInit : public TypedInit {
   }
 
   // Clone - Clone this operator, replacing arguments with the new list
-  virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0;
+  virtual const OpInit *clone(ArrayRef<const Init *> Operands) const = 0;
 
   virtual unsigned getNumOperands() const = 0;
-  virtual Init *getOperand(unsigned i) const = 0;
+  virtual const Init *getOperand(unsigned i) const = 0;
 
-  Init *getBit(unsigned Bit) const override;
+  const Init *getBit(unsigned Bit) const override;
 };
 
 /// !op (X) - Transform an init.
@@ -859,9 +867,9 @@ class UnOpInit : public OpInit, public FoldingSetNode {
   };
 
 private:
-  Init *LHS;
+  const Init *LHS;
 
-  UnOpInit(UnaryOp opc, Init *lhs, const RecTy *Type)
+  UnOpInit(UnaryOp opc, const Init *lhs, const RecTy *Type)
       : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {}
 
 public:
@@ -872,12 +880,12 @@ class UnOpInit : public OpInit, public FoldingSetNode {
     return I->getKind() == IK_UnOpInit;
   }
 
-  static UnOpInit *get(UnaryOp opc, Init *lhs, const RecTy *Type);
+  static const UnOpInit *get(UnaryOp opc, const Init *lhs, const RecTy *Type);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   // Clone - Clone this operator, replacing arguments with the new list
-  OpInit *clone(ArrayRef<Init *> Operands) const override {
+  const OpInit *clone(ArrayRef<const Init *> Operands) const override {
     assert(Operands.size() == 1 &&
            "Wrong number of operands for unary operation");
     return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
@@ -885,19 +893,19 @@ class UnOpInit : public OpInit, public FoldingSetNode {
 
   unsigned getNumOperands() const override { return 1; }
 
-  Init *getOperand(unsigned i) const override {
+  const Init *getOperand(unsigned i) const override {
     assert(i == 0 && "Invalid operand id for unary operator");
     return getOperand();
   }
 
   UnaryOp getOpcode() const { return (UnaryOp)Opc; }
-  Init *getOperand() const { return LHS; }
+  const Init *getOperand() const { return LHS; }
 
   // Fold - If possible, fold this to a simpler init.  Return this if not
   // possible to fold.
-  Init *Fold(Record *CurRec, bool IsFinal = false) const;
+  const Init *Fold(const Record *CurRec, bool IsFinal = false) const;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
   std::string getAsString() const override;
 };
@@ -937,9 +945,9 @@ class BinOpInit : public OpInit, public FoldingSetNode {
   };
 
 private:
-  Init *LHS, *RHS;
+  const Init *LHS, *RHS;
 
-  BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, const RecTy *Type)
+  BinOpInit(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
       : OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {}
 
 public:
@@ -950,21 +958,22 @@ class BinOpInit : public OpInit, public FoldingSetNode {
     return I->getKind() == IK_BinOpInit;
   }
 
-  static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, const RecTy *Type);
-  static Init *getStrConcat(Init *lhs, Init *rhs);
-  static Init *getListConcat(TypedInit *lhs, Init *rhs);
+  static const BinOpInit *get(BinaryOp opc, const Init *lhs, const Init *rhs,
+                              const RecTy *Type);
+  static const Init *getStrConcat(const Init *lhs, const Init *rhs);
+  static const Init *getListConcat(const TypedInit *lhs, const Init *rhs);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   // Clone - Clone this operator, replacing arguments with the new list
-  OpInit *clone(ArrayRef<Init *> Operands) const override {
+  const OpInit *clone(ArrayRef<const Init *> Operands) const override {
     assert(Operands.size() == 2 &&
            "Wrong number of operands for binary operation");
     return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
   }
 
   unsigned getNumOperands() const override { return 2; }
-  Init *getOperand(unsigned i) const override {
+  const Init *getOperand(unsigned i) const override {
     switch (i) {
     default: llvm_unreachable("Invalid operand id for binary operator");
     case 0: return getLHS();
@@ -973,16 +982,17 @@ class BinOpInit : public OpInit, public FoldingSetNode {
   }
 
   BinaryOp getOpcode() const { return (BinaryOp)Opc; }
-  Init *getLHS() const { return LHS; }
-  Init *getRHS() const { return RHS; }
+  const Init *getLHS() const { return LHS; }
+  const Init *getRHS() const { return RHS; }
 
-  std::optional<bool> CompareInit(unsigned Opc, Init *LHS, Init *RHS) const;
+  std::optional<bool> CompareInit(unsigned Opc, const Init *LHS,
+                                  const Init *RHS) const;
 
   // Fold - If possible, fold this to a simpler init.  Return this if not
   // possi...
[truncated]

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-llvm-globalisel

Author: Rahul Joshi (jurahul)

Changes

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089


Patch is 275.42 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/112705.diff

41 Files Affected:

  • (modified) clang/utils/TableGen/ClangOptionDocEmitter.cpp (+2-2)
  • (modified) llvm/include/llvm/TableGen/Error.h (+2-2)
  • (modified) llvm/include/llvm/TableGen/Record.h (+270-256)
  • (modified) llvm/lib/TableGen/DetailedRecordsBackend.cpp (+1-1)
  • (modified) llvm/lib/TableGen/Error.cpp (+2-2)
  • (modified) llvm/lib/TableGen/Record.cpp (+445-428)
  • (modified) llvm/lib/TableGen/TGParser.cpp (+220-208)
  • (modified) llvm/lib/TableGen/TGParser.h (+42-40)
  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+22-22)
  • (modified) llvm/utils/TableGen/AsmWriterEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/Attributes.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp (+1-1)
  • (modified) llvm/utils/TableGen/CodeEmitterGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/CodeGenMapTable.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+31-30)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.h (+5-5)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.h (+1-1)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.cpp (+11-11)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.h (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenRegisters.cpp (+8-8)
  • (modified) llvm/utils/TableGen/Common/CodeGenSchedule.cpp (+5-4)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+5-5)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+6-5)
  • (modified) llvm/utils/TableGen/CompressInstEmitter.cpp (+4-3)
  • (modified) llvm/utils/TableGen/DAGISelMatcherGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DFAEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DXILEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DecoderEmitter.cpp (+18-18)
  • (modified) llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/InstrInfoEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/OptionParserEmitter.cpp (+5-5)
  • (modified) llvm/utils/TableGen/RegisterInfoEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/SearchableTableEmitter.cpp (+5-4)
  • (modified) llvm/utils/TableGen/X86FoldTablesEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/X86InstrMappingEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/X86RecognizableInstr.cpp (+3-5)
  • (modified) mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp (+1-2)
diff --git a/clang/utils/TableGen/ClangOptionDocEmitter.cpp b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
index b67c5d1d1146c6..ba8840c1bdca78 100644
--- a/clang/utils/TableGen/ClangOptionDocEmitter.cpp
+++ b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
@@ -367,13 +367,13 @@ void emitOption(const DocumentedOption &Option, const Record *DocInfo,
   for (const Record *VisibilityHelp :
        R->getValueAsListOfDefs("HelpTextsForVariants")) {
     // This is a list of visibilities.
-    ArrayRef<Init *> Visibilities =
+    ArrayRef<const Init *> Visibilities =
         VisibilityHelp->getValueAsListInit("Visibilities")->getValues();
 
     // See if any of the program's visibilities are in the list.
     for (StringRef DocInfoMask :
          DocInfo->getValueAsListOfStrings("VisibilityMask")) {
-      for (Init *Visibility : Visibilities) {
+      for (const Init *Visibility : Visibilities) {
         if (Visibility->getAsUnquotedString() == DocInfoMask) {
           // Use the first one we find.
           Description = escapeRST(VisibilityHelp->getValueAsString("Text"));
diff --git a/llvm/include/llvm/TableGen/Error.h b/llvm/include/llvm/TableGen/Error.h
index 512249b0160c24..b963dcba9869fb 100644
--- a/llvm/include/llvm/TableGen/Error.h
+++ b/llvm/include/llvm/TableGen/Error.h
@@ -49,8 +49,8 @@ void PrintError(const RecordVal *RecVal, const Twine &Msg);
 [[noreturn]] void PrintFatalError(function_ref<void(raw_ostream &OS)> PrintMsg);
 
 // Returns true if the assert failed.
-bool CheckAssert(SMLoc Loc, Init *Condition, Init *Message);
-void dumpMessage(SMLoc Loc, Init *Message);
+bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message);
+void dumpMessage(SMLoc Loc, const Init *Message);
 
 extern SourceMgr SrcMgr;
 extern unsigned ErrorsPrinted;
diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index f856ff4cbd34b5..63267b7633f6cf 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -374,25 +374,26 @@ class Init {
   /// If this value is convertible to type \p Ty, return a value whose
   /// type is \p Ty, generating a !cast operation if required.
   /// Otherwise, return null.
-  virtual Init *getCastTo(const RecTy *Ty) const = 0;
+  virtual const Init *getCastTo(const RecTy *Ty) const = 0;
 
   /// Convert to a value whose type is \p Ty, or return null if this
   /// is not possible. This can happen if the value's type is convertible
   /// to \p Ty, but there are unresolved references.
-  virtual Init *convertInitializerTo(const RecTy *Ty) const = 0;
+  virtual const Init *convertInitializerTo(const RecTy *Ty) const = 0;
 
   /// This function is used to implement the bit range
   /// selection operator. Given a value, it selects the specified bits,
   /// returning them as a new \p Init of type \p bits. If it is not legal
   /// to use the bit selection operator on this value, null is returned.
-  virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
+  virtual const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
     return nullptr;
   }
 
   /// This function is used to implement the FieldInit class.
   /// Implementors of this method should return the type of the named
   /// field if they are of type record.
-  virtual const RecTy *getFieldType(StringInit *FieldName) const {
+  virtual const RecTy *getFieldType(const StringInit *FieldName) const {
     return nullptr;
   }
 
@@ -400,12 +401,12 @@ class Init {
   /// variables which may not be defined at the time the expression is formed.
   /// If a value is set for the variable later, this method will be called on
   /// users of the value to allow the value to propagate out.
-  virtual Init *resolveReferences(Resolver &R) const {
+  virtual const Init *resolveReferences(Resolver &R) const {
     return const_cast<Init *>(this);
   }
 
   /// Get the \p Init value of the specified bit.
-  virtual Init *getBit(unsigned Bit) const = 0;
+  virtual const Init *getBit(unsigned Bit) const = 0;
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
@@ -436,15 +437,16 @@ class TypedInit : public Init {
   /// Get the record keeper that initialized this Init.
   RecordKeeper &getRecordKeeper() const { return ValueTy->getRecordKeeper(); }
 
-  Init *getCastTo(const RecTy *Ty) const override;
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *getCastTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
 
   /// This method is used to implement the FieldInit class.
   /// Implementors of this method should return the type of the named field if
   /// they are of type record.
-  const RecTy *getFieldType(StringInit *FieldName) const override;
+  const RecTy *getFieldType(const StringInit *FieldName) const override;
 };
 
 /// '?' - Represents an uninitialized value.
@@ -470,10 +472,10 @@ class UnsetInit : public Init {
   /// Get the record keeper that initialized this Init.
   RecordKeeper &getRecordKeeper() const { return RK; }
 
-  Init *getCastTo(const RecTy *Ty) const override;
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *getCastTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     return const_cast<UnsetInit*>(this);
   }
 
@@ -487,7 +489,7 @@ class UnsetInit : public Init {
 };
 
 // Represent an argument.
-using ArgAuxType = std::variant<unsigned, Init *>;
+using ArgAuxType = std::variant<unsigned, const Init *>;
 class ArgumentInit : public Init, public FoldingSetNode {
 public:
   enum Kind {
@@ -496,11 +498,11 @@ class ArgumentInit : public Init, public FoldingSetNode {
   };
 
 private:
-  Init *Value;
+  const Init *Value;
   ArgAuxType Aux;
 
 protected:
-  explicit ArgumentInit(Init *Value, ArgAuxType Aux)
+  explicit ArgumentInit(const Init *Value, ArgAuxType Aux)
       : Init(IK_ArgumentInit), Value(Value), Aux(Aux) {}
 
 public:
@@ -511,25 +513,27 @@ class ArgumentInit : public Init, public FoldingSetNode {
 
   RecordKeeper &getRecordKeeper() const { return Value->getRecordKeeper(); }
 
-  static ArgumentInit *get(Init *Value, ArgAuxType Aux);
+  static const ArgumentInit *get(const Init *Value, ArgAuxType Aux);
 
   bool isPositional() const { return Aux.index() == Positional; }
   bool isNamed() const { return Aux.index() == Named; }
 
-  Init *getValue() const { return Value; }
+  const Init *getValue() const { return Value; }
   unsigned getIndex() const {
     assert(isPositional() && "Should be positional!");
     return std::get<Positional>(Aux);
   }
-  Init *getName() const {
+  const Init *getName() const {
     assert(isNamed() && "Should be named!");
     return std::get<Named>(Aux);
   }
-  ArgumentInit *cloneWithValue(Init *Value) const { return get(Value, Aux); }
+  const ArgumentInit *cloneWithValue(const Init *Value) const {
+    return get(Value, Aux);
+  }
 
   void Profile(FoldingSetNodeID &ID) const;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
   std::string getAsString() const override {
     if (isPositional())
       return utostr(getIndex()) + ": " + Value->getAsString();
@@ -541,11 +545,11 @@ class ArgumentInit : public Init, public FoldingSetNode {
 
   bool isComplete() const override { return false; }
   bool isConcrete() const override { return false; }
-  Init *getBit(unsigned Bit) const override { return Value->getBit(Bit); }
-  Init *getCastTo(const RecTy *Ty) const override {
+  const Init *getBit(unsigned Bit) const override { return Value->getBit(Bit); }
+  const Init *getCastTo(const RecTy *Ty) const override {
     return Value->getCastTo(Ty);
   }
-  Init *convertInitializerTo(const RecTy *Ty) const override {
+  const Init *convertInitializerTo(const RecTy *Ty) const override {
     return Value->convertInitializerTo(Ty);
   }
 };
@@ -571,9 +575,9 @@ class BitInit final : public TypedInit {
 
   bool getValue() const { return Value; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     assert(Bit < 1 && "Bit index out of range!");
     return const_cast<BitInit*>(this);
   }
@@ -584,8 +588,9 @@ class BitInit final : public TypedInit {
 
 /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value.
 /// It contains a vector of bits, whose size is determined by the type.
-class BitsInit final : public TypedInit, public FoldingSetNode,
-                       public TrailingObjects<BitsInit, Init *> {
+class BitsInit final : public TypedInit,
+                       public FoldingSetNode,
+                       public TrailingObjects<BitsInit, const Init *> {
   unsigned NumBits;
 
   BitsInit(RecordKeeper &RK, unsigned N)
@@ -602,14 +607,15 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
     return I->getKind() == IK_BitsInit;
   }
 
-  static BitsInit *get(RecordKeeper &RK, ArrayRef<Init *> Range);
+  static BitsInit *get(RecordKeeper &RK, ArrayRef<const Init *> Range);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   unsigned getNumBits() const { return NumBits; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
   std::optional<int64_t> convertInitializerToInt() const;
 
   bool isComplete() const override {
@@ -627,11 +633,11 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
   bool isConcrete() const override;
   std::string getAsString() const override;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     assert(Bit < NumBits && "Bit index out of range!");
-    return getTrailingObjects<Init *>()[Bit];
+    return getTrailingObjects<const Init *>()[Bit];
   }
 };
 
@@ -654,13 +660,14 @@ class IntInit : public TypedInit {
 
   int64_t getValue() const { return Value; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
 
   bool isConcrete() const override { return true; }
   std::string getAsString() const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     return BitInit::get(getRecordKeeper(), (Value & (1ULL << Bit)) != 0);
   }
 };
@@ -684,13 +691,13 @@ class AnonymousNameInit : public TypedInit {
 
   unsigned getValue() const { return Value; }
 
-  StringInit *getNameInit() const;
+  const StringInit *getNameInit() const;
 
   std::string getAsString() const override;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off string");
   }
 };
@@ -718,8 +725,8 @@ class StringInit : public TypedInit {
     return I->getKind() == IK_StringInit;
   }
 
-  static StringInit *get(RecordKeeper &RK, StringRef,
-                         StringFormat Fmt = SF_String);
+  static const StringInit *get(RecordKeeper &RK, StringRef,
+                               StringFormat Fmt = SF_String);
 
   static StringFormat determineFormat(StringFormat Fmt1, StringFormat Fmt2) {
     return (Fmt1 == SF_Code || Fmt2 == SF_Code) ? SF_Code : SF_String;
@@ -729,7 +736,7 @@ class StringInit : public TypedInit {
   StringFormat getFormat() const { return Format; }
   bool hasCodeFormat() const { return Format == SF_Code; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
   bool isConcrete() const override { return true; }
 
@@ -744,19 +751,20 @@ class StringInit : public TypedInit {
     return std::string(Value);
   }
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off string");
   }
 };
 
 /// [AL, AH, CL] - Represent a list of defs
 ///
-class ListInit final : public TypedInit, public FoldingSetNode,
-                       public TrailingObjects<ListInit, Init *> {
+class ListInit final : public TypedInit,
+                       public FoldingSetNode,
+                       public TrailingObjects<ListInit, const Init *> {
   unsigned NumValues;
 
 public:
-  using const_iterator = Init *const *;
+  using const_iterator = const Init *const *;
 
 private:
   explicit ListInit(unsigned N, const RecTy *EltTy)
@@ -772,13 +780,13 @@ class ListInit final : public TypedInit, public FoldingSetNode,
   static bool classof(const Init *I) {
     return I->getKind() == IK_ListInit;
   }
-  static ListInit *get(ArrayRef<Init *> Range, const RecTy *EltTy);
+  static const ListInit *get(ArrayRef<const Init *> Range, const RecTy *EltTy);
 
   void Profile(FoldingSetNodeID &ID) const;
 
-  Init *getElement(unsigned i) const {
+  const Init *getElement(unsigned i) const {
     assert(i < NumValues && "List element index out of range!");
-    return getTrailingObjects<Init *>()[i];
+    return getTrailingObjects<const Init *>()[i];
   }
   const RecTy *getElementType() const {
     return cast<ListRecTy>(getType())->getElementType();
@@ -786,30 +794,30 @@ class ListInit final : public TypedInit, public FoldingSetNode,
 
   const Record *getElementAsRecord(unsigned i) const;
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
   /// This method is used by classes that refer to other
   /// variables which may not be defined at the time they expression is formed.
   /// If a value is set for the variable later, this method will be called on
   /// users of the value to allow the value to propagate out.
   ///
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
   bool isComplete() const override;
   bool isConcrete() const override;
   std::string getAsString() const override;
 
-  ArrayRef<Init*> getValues() const {
-    return ArrayRef(getTrailingObjects<Init *>(), NumValues);
+  ArrayRef<const Init *> getValues() const {
+    return ArrayRef(getTrailingObjects<const Init *>(), NumValues);
   }
 
-  const_iterator begin() const { return getTrailingObjects<Init *>(); }
+  const_iterator begin() const { return getTrailingObjects<const Init *>(); }
   const_iterator end  () const { return begin() + NumValues; }
 
   size_t         size () const { return NumValues;  }
   bool           empty() const { return NumValues == 0; }
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off list");
   }
 };
@@ -831,12 +839,12 @@ class OpInit : public TypedInit {
   }
 
   // Clone - Clone this operator, replacing arguments with the new list
-  virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0;
+  virtual const OpInit *clone(ArrayRef<const Init *> Operands) const = 0;
 
   virtual unsigned getNumOperands() const = 0;
-  virtual Init *getOperand(unsigned i) const = 0;
+  virtual const Init *getOperand(unsigned i) const = 0;
 
-  Init *getBit(unsigned Bit) const override;
+  const Init *getBit(unsigned Bit) const override;
 };
 
 /// !op (X) - Transform an init.
@@ -859,9 +867,9 @@ class UnOpInit : public OpInit, public FoldingSetNode {
   };
 
 private:
-  Init *LHS;
+  const Init *LHS;
 
-  UnOpInit(UnaryOp opc, Init *lhs, const RecTy *Type)
+  UnOpInit(UnaryOp opc, const Init *lhs, const RecTy *Type)
       : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {}
 
 public:
@@ -872,12 +880,12 @@ class UnOpInit : public OpInit, public FoldingSetNode {
     return I->getKind() == IK_UnOpInit;
   }
 
-  static UnOpInit *get(UnaryOp opc, Init *lhs, const RecTy *Type);
+  static const UnOpInit *get(UnaryOp opc, const Init *lhs, const RecTy *Type);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   // Clone - Clone this operator, replacing arguments with the new list
-  OpInit *clone(ArrayRef<Init *> Operands) const override {
+  const OpInit *clone(ArrayRef<const Init *> Operands) const override {
     assert(Operands.size() == 1 &&
            "Wrong number of operands for unary operation");
     return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
@@ -885,19 +893,19 @@ class UnOpInit : public OpInit, public FoldingSetNode {
 
   unsigned getNumOperands() const override { return 1; }
 
-  Init *getOperand(unsigned i) const override {
+  const Init *getOperand(unsigned i) const override {
     assert(i == 0 && "Invalid operand id for unary operator");
     return getOperand();
   }
 
   UnaryOp getOpcode() const { return (UnaryOp)Opc; }
-  Init *getOperand() const { return LHS; }
+  const Init *getOperand() const { return LHS; }
 
   // Fold - If possible, fold this to a simpler init.  Return this if not
   // possible to fold.
-  Init *Fold(Record *CurRec, bool IsFinal = false) const;
+  const Init *Fold(const Record *CurRec, bool IsFinal = false) const;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
   std::string getAsString() const override;
 };
@@ -937,9 +945,9 @@ class BinOpInit : public OpInit, public FoldingSetNode {
   };
 
 private:
-  Init *LHS, *RHS;
+  const Init *LHS, *RHS;
 
-  BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, const RecTy *Type)
+  BinOpInit(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
       : OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {}
 
 public:
@@ -950,21 +958,22 @@ class BinOpInit : public OpInit, public FoldingSetNode {
     return I->getKind() == IK_BinOpInit;
   }
 
-  static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, const RecTy *Type);
-  static Init *getStrConcat(Init *lhs, Init *rhs);
-  static Init *getListConcat(TypedInit *lhs, Init *rhs);
+  static const BinOpInit *get(BinaryOp opc, const Init *lhs, const Init *rhs,
+                              const RecTy *Type);
+  static const Init *getStrConcat(const Init *lhs, const Init *rhs);
+  static const Init *getListConcat(const TypedInit *lhs, const Init *rhs);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   // Clone - Clone this operator, replacing arguments with the new list
-  OpInit *clone(ArrayRef<Init *> Operands) const override {
+  const OpInit *clone(ArrayRef<const Init *> Operands) const override {
     assert(Operands.size() == 2 &&
            "Wrong number of operands for binary operation");
     return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
   }
 
   unsigned getNumOperands() const override { return 2; }
-  Init *getOperand(unsigned i) const override {
+  const Init *getOperand(unsigned i) const override {
     switch (i) {
     default: llvm_unreachable("Invalid operand id for binary operator");
     case 0: return getLHS();
@@ -973,16 +982,17 @@ class BinOpInit : public OpInit, public FoldingSetNode {
   }
 
   BinaryOp getOpcode() const { return (BinaryOp)Opc; }
-  Init *getLHS() const { return LHS; }
-  Init *getRHS() const { return RHS; }
+  const Init *getLHS() const { return LHS; }
+  const Init *getRHS() const { return RHS; }
 
-  std::optional<bool> CompareInit(unsigned Opc, Init *LHS, Init *RHS) const;
+  std::optional<bool> CompareInit(unsigned Opc, const Init *LHS,
+                                  const Init *RHS) const;
 
   // Fold - If possible, fold this to a simpler init.  Return this if not
   // possi...
[truncated]

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-clang

Author: Rahul Joshi (jurahul)

Changes

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089


Patch is 275.42 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/112705.diff

41 Files Affected:

  • (modified) clang/utils/TableGen/ClangOptionDocEmitter.cpp (+2-2)
  • (modified) llvm/include/llvm/TableGen/Error.h (+2-2)
  • (modified) llvm/include/llvm/TableGen/Record.h (+270-256)
  • (modified) llvm/lib/TableGen/DetailedRecordsBackend.cpp (+1-1)
  • (modified) llvm/lib/TableGen/Error.cpp (+2-2)
  • (modified) llvm/lib/TableGen/Record.cpp (+445-428)
  • (modified) llvm/lib/TableGen/TGParser.cpp (+220-208)
  • (modified) llvm/lib/TableGen/TGParser.h (+42-40)
  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+22-22)
  • (modified) llvm/utils/TableGen/AsmWriterEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/Attributes.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp (+1-1)
  • (modified) llvm/utils/TableGen/CodeEmitterGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/CodeGenMapTable.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+31-30)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.h (+5-5)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.h (+1-1)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.cpp (+11-11)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.h (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenRegisters.cpp (+8-8)
  • (modified) llvm/utils/TableGen/Common/CodeGenSchedule.cpp (+5-4)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+5-5)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+6-5)
  • (modified) llvm/utils/TableGen/CompressInstEmitter.cpp (+4-3)
  • (modified) llvm/utils/TableGen/DAGISelMatcherGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DFAEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DXILEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/DecoderEmitter.cpp (+18-18)
  • (modified) llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/InstrInfoEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/OptionParserEmitter.cpp (+5-5)
  • (modified) llvm/utils/TableGen/RegisterInfoEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/SearchableTableEmitter.cpp (+5-4)
  • (modified) llvm/utils/TableGen/X86FoldTablesEmitter.cpp (+3-3)
  • (modified) llvm/utils/TableGen/X86InstrMappingEmitter.cpp (+1-1)
  • (modified) llvm/utils/TableGen/X86RecognizableInstr.cpp (+3-5)
  • (modified) mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp (+1-2)
diff --git a/clang/utils/TableGen/ClangOptionDocEmitter.cpp b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
index b67c5d1d1146c6..ba8840c1bdca78 100644
--- a/clang/utils/TableGen/ClangOptionDocEmitter.cpp
+++ b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
@@ -367,13 +367,13 @@ void emitOption(const DocumentedOption &Option, const Record *DocInfo,
   for (const Record *VisibilityHelp :
        R->getValueAsListOfDefs("HelpTextsForVariants")) {
     // This is a list of visibilities.
-    ArrayRef<Init *> Visibilities =
+    ArrayRef<const Init *> Visibilities =
         VisibilityHelp->getValueAsListInit("Visibilities")->getValues();
 
     // See if any of the program's visibilities are in the list.
     for (StringRef DocInfoMask :
          DocInfo->getValueAsListOfStrings("VisibilityMask")) {
-      for (Init *Visibility : Visibilities) {
+      for (const Init *Visibility : Visibilities) {
         if (Visibility->getAsUnquotedString() == DocInfoMask) {
           // Use the first one we find.
           Description = escapeRST(VisibilityHelp->getValueAsString("Text"));
diff --git a/llvm/include/llvm/TableGen/Error.h b/llvm/include/llvm/TableGen/Error.h
index 512249b0160c24..b963dcba9869fb 100644
--- a/llvm/include/llvm/TableGen/Error.h
+++ b/llvm/include/llvm/TableGen/Error.h
@@ -49,8 +49,8 @@ void PrintError(const RecordVal *RecVal, const Twine &Msg);
 [[noreturn]] void PrintFatalError(function_ref<void(raw_ostream &OS)> PrintMsg);
 
 // Returns true if the assert failed.
-bool CheckAssert(SMLoc Loc, Init *Condition, Init *Message);
-void dumpMessage(SMLoc Loc, Init *Message);
+bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message);
+void dumpMessage(SMLoc Loc, const Init *Message);
 
 extern SourceMgr SrcMgr;
 extern unsigned ErrorsPrinted;
diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index f856ff4cbd34b5..63267b7633f6cf 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -374,25 +374,26 @@ class Init {
   /// If this value is convertible to type \p Ty, return a value whose
   /// type is \p Ty, generating a !cast operation if required.
   /// Otherwise, return null.
-  virtual Init *getCastTo(const RecTy *Ty) const = 0;
+  virtual const Init *getCastTo(const RecTy *Ty) const = 0;
 
   /// Convert to a value whose type is \p Ty, or return null if this
   /// is not possible. This can happen if the value's type is convertible
   /// to \p Ty, but there are unresolved references.
-  virtual Init *convertInitializerTo(const RecTy *Ty) const = 0;
+  virtual const Init *convertInitializerTo(const RecTy *Ty) const = 0;
 
   /// This function is used to implement the bit range
   /// selection operator. Given a value, it selects the specified bits,
   /// returning them as a new \p Init of type \p bits. If it is not legal
   /// to use the bit selection operator on this value, null is returned.
-  virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
+  virtual const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
     return nullptr;
   }
 
   /// This function is used to implement the FieldInit class.
   /// Implementors of this method should return the type of the named
   /// field if they are of type record.
-  virtual const RecTy *getFieldType(StringInit *FieldName) const {
+  virtual const RecTy *getFieldType(const StringInit *FieldName) const {
     return nullptr;
   }
 
@@ -400,12 +401,12 @@ class Init {
   /// variables which may not be defined at the time the expression is formed.
   /// If a value is set for the variable later, this method will be called on
   /// users of the value to allow the value to propagate out.
-  virtual Init *resolveReferences(Resolver &R) const {
+  virtual const Init *resolveReferences(Resolver &R) const {
     return const_cast<Init *>(this);
   }
 
   /// Get the \p Init value of the specified bit.
-  virtual Init *getBit(unsigned Bit) const = 0;
+  virtual const Init *getBit(unsigned Bit) const = 0;
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
@@ -436,15 +437,16 @@ class TypedInit : public Init {
   /// Get the record keeper that initialized this Init.
   RecordKeeper &getRecordKeeper() const { return ValueTy->getRecordKeeper(); }
 
-  Init *getCastTo(const RecTy *Ty) const override;
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *getCastTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
 
   /// This method is used to implement the FieldInit class.
   /// Implementors of this method should return the type of the named field if
   /// they are of type record.
-  const RecTy *getFieldType(StringInit *FieldName) const override;
+  const RecTy *getFieldType(const StringInit *FieldName) const override;
 };
 
 /// '?' - Represents an uninitialized value.
@@ -470,10 +472,10 @@ class UnsetInit : public Init {
   /// Get the record keeper that initialized this Init.
   RecordKeeper &getRecordKeeper() const { return RK; }
 
-  Init *getCastTo(const RecTy *Ty) const override;
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *getCastTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     return const_cast<UnsetInit*>(this);
   }
 
@@ -487,7 +489,7 @@ class UnsetInit : public Init {
 };
 
 // Represent an argument.
-using ArgAuxType = std::variant<unsigned, Init *>;
+using ArgAuxType = std::variant<unsigned, const Init *>;
 class ArgumentInit : public Init, public FoldingSetNode {
 public:
   enum Kind {
@@ -496,11 +498,11 @@ class ArgumentInit : public Init, public FoldingSetNode {
   };
 
 private:
-  Init *Value;
+  const Init *Value;
   ArgAuxType Aux;
 
 protected:
-  explicit ArgumentInit(Init *Value, ArgAuxType Aux)
+  explicit ArgumentInit(const Init *Value, ArgAuxType Aux)
       : Init(IK_ArgumentInit), Value(Value), Aux(Aux) {}
 
 public:
@@ -511,25 +513,27 @@ class ArgumentInit : public Init, public FoldingSetNode {
 
   RecordKeeper &getRecordKeeper() const { return Value->getRecordKeeper(); }
 
-  static ArgumentInit *get(Init *Value, ArgAuxType Aux);
+  static const ArgumentInit *get(const Init *Value, ArgAuxType Aux);
 
   bool isPositional() const { return Aux.index() == Positional; }
   bool isNamed() const { return Aux.index() == Named; }
 
-  Init *getValue() const { return Value; }
+  const Init *getValue() const { return Value; }
   unsigned getIndex() const {
     assert(isPositional() && "Should be positional!");
     return std::get<Positional>(Aux);
   }
-  Init *getName() const {
+  const Init *getName() const {
     assert(isNamed() && "Should be named!");
     return std::get<Named>(Aux);
   }
-  ArgumentInit *cloneWithValue(Init *Value) const { return get(Value, Aux); }
+  const ArgumentInit *cloneWithValue(const Init *Value) const {
+    return get(Value, Aux);
+  }
 
   void Profile(FoldingSetNodeID &ID) const;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
   std::string getAsString() const override {
     if (isPositional())
       return utostr(getIndex()) + ": " + Value->getAsString();
@@ -541,11 +545,11 @@ class ArgumentInit : public Init, public FoldingSetNode {
 
   bool isComplete() const override { return false; }
   bool isConcrete() const override { return false; }
-  Init *getBit(unsigned Bit) const override { return Value->getBit(Bit); }
-  Init *getCastTo(const RecTy *Ty) const override {
+  const Init *getBit(unsigned Bit) const override { return Value->getBit(Bit); }
+  const Init *getCastTo(const RecTy *Ty) const override {
     return Value->getCastTo(Ty);
   }
-  Init *convertInitializerTo(const RecTy *Ty) const override {
+  const Init *convertInitializerTo(const RecTy *Ty) const override {
     return Value->convertInitializerTo(Ty);
   }
 };
@@ -571,9 +575,9 @@ class BitInit final : public TypedInit {
 
   bool getValue() const { return Value; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     assert(Bit < 1 && "Bit index out of range!");
     return const_cast<BitInit*>(this);
   }
@@ -584,8 +588,9 @@ class BitInit final : public TypedInit {
 
 /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value.
 /// It contains a vector of bits, whose size is determined by the type.
-class BitsInit final : public TypedInit, public FoldingSetNode,
-                       public TrailingObjects<BitsInit, Init *> {
+class BitsInit final : public TypedInit,
+                       public FoldingSetNode,
+                       public TrailingObjects<BitsInit, const Init *> {
   unsigned NumBits;
 
   BitsInit(RecordKeeper &RK, unsigned N)
@@ -602,14 +607,15 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
     return I->getKind() == IK_BitsInit;
   }
 
-  static BitsInit *get(RecordKeeper &RK, ArrayRef<Init *> Range);
+  static BitsInit *get(RecordKeeper &RK, ArrayRef<const Init *> Range);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   unsigned getNumBits() const { return NumBits; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
   std::optional<int64_t> convertInitializerToInt() const;
 
   bool isComplete() const override {
@@ -627,11 +633,11 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
   bool isConcrete() const override;
   std::string getAsString() const override;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     assert(Bit < NumBits && "Bit index out of range!");
-    return getTrailingObjects<Init *>()[Bit];
+    return getTrailingObjects<const Init *>()[Bit];
   }
 };
 
@@ -654,13 +660,14 @@ class IntInit : public TypedInit {
 
   int64_t getValue() const { return Value; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
-  Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *
+  convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
 
   bool isConcrete() const override { return true; }
   std::string getAsString() const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     return BitInit::get(getRecordKeeper(), (Value & (1ULL << Bit)) != 0);
   }
 };
@@ -684,13 +691,13 @@ class AnonymousNameInit : public TypedInit {
 
   unsigned getValue() const { return Value; }
 
-  StringInit *getNameInit() const;
+  const StringInit *getNameInit() const;
 
   std::string getAsString() const override;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off string");
   }
 };
@@ -718,8 +725,8 @@ class StringInit : public TypedInit {
     return I->getKind() == IK_StringInit;
   }
 
-  static StringInit *get(RecordKeeper &RK, StringRef,
-                         StringFormat Fmt = SF_String);
+  static const StringInit *get(RecordKeeper &RK, StringRef,
+                               StringFormat Fmt = SF_String);
 
   static StringFormat determineFormat(StringFormat Fmt1, StringFormat Fmt2) {
     return (Fmt1 == SF_Code || Fmt2 == SF_Code) ? SF_Code : SF_String;
@@ -729,7 +736,7 @@ class StringInit : public TypedInit {
   StringFormat getFormat() const { return Format; }
   bool hasCodeFormat() const { return Format == SF_Code; }
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
   bool isConcrete() const override { return true; }
 
@@ -744,19 +751,20 @@ class StringInit : public TypedInit {
     return std::string(Value);
   }
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off string");
   }
 };
 
 /// [AL, AH, CL] - Represent a list of defs
 ///
-class ListInit final : public TypedInit, public FoldingSetNode,
-                       public TrailingObjects<ListInit, Init *> {
+class ListInit final : public TypedInit,
+                       public FoldingSetNode,
+                       public TrailingObjects<ListInit, const Init *> {
   unsigned NumValues;
 
 public:
-  using const_iterator = Init *const *;
+  using const_iterator = const Init *const *;
 
 private:
   explicit ListInit(unsigned N, const RecTy *EltTy)
@@ -772,13 +780,13 @@ class ListInit final : public TypedInit, public FoldingSetNode,
   static bool classof(const Init *I) {
     return I->getKind() == IK_ListInit;
   }
-  static ListInit *get(ArrayRef<Init *> Range, const RecTy *EltTy);
+  static const ListInit *get(ArrayRef<const Init *> Range, const RecTy *EltTy);
 
   void Profile(FoldingSetNodeID &ID) const;
 
-  Init *getElement(unsigned i) const {
+  const Init *getElement(unsigned i) const {
     assert(i < NumValues && "List element index out of range!");
-    return getTrailingObjects<Init *>()[i];
+    return getTrailingObjects<const Init *>()[i];
   }
   const RecTy *getElementType() const {
     return cast<ListRecTy>(getType())->getElementType();
@@ -786,30 +794,30 @@ class ListInit final : public TypedInit, public FoldingSetNode,
 
   const Record *getElementAsRecord(unsigned i) const;
 
-  Init *convertInitializerTo(const RecTy *Ty) const override;
+  const Init *convertInitializerTo(const RecTy *Ty) const override;
 
   /// This method is used by classes that refer to other
   /// variables which may not be defined at the time they expression is formed.
   /// If a value is set for the variable later, this method will be called on
   /// users of the value to allow the value to propagate out.
   ///
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
   bool isComplete() const override;
   bool isConcrete() const override;
   std::string getAsString() const override;
 
-  ArrayRef<Init*> getValues() const {
-    return ArrayRef(getTrailingObjects<Init *>(), NumValues);
+  ArrayRef<const Init *> getValues() const {
+    return ArrayRef(getTrailingObjects<const Init *>(), NumValues);
   }
 
-  const_iterator begin() const { return getTrailingObjects<Init *>(); }
+  const_iterator begin() const { return getTrailingObjects<const Init *>(); }
   const_iterator end  () const { return begin() + NumValues; }
 
   size_t         size () const { return NumValues;  }
   bool           empty() const { return NumValues == 0; }
 
-  Init *getBit(unsigned Bit) const override {
+  const Init *getBit(unsigned Bit) const override {
     llvm_unreachable("Illegal bit reference off list");
   }
 };
@@ -831,12 +839,12 @@ class OpInit : public TypedInit {
   }
 
   // Clone - Clone this operator, replacing arguments with the new list
-  virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0;
+  virtual const OpInit *clone(ArrayRef<const Init *> Operands) const = 0;
 
   virtual unsigned getNumOperands() const = 0;
-  virtual Init *getOperand(unsigned i) const = 0;
+  virtual const Init *getOperand(unsigned i) const = 0;
 
-  Init *getBit(unsigned Bit) const override;
+  const Init *getBit(unsigned Bit) const override;
 };
 
 /// !op (X) - Transform an init.
@@ -859,9 +867,9 @@ class UnOpInit : public OpInit, public FoldingSetNode {
   };
 
 private:
-  Init *LHS;
+  const Init *LHS;
 
-  UnOpInit(UnaryOp opc, Init *lhs, const RecTy *Type)
+  UnOpInit(UnaryOp opc, const Init *lhs, const RecTy *Type)
       : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {}
 
 public:
@@ -872,12 +880,12 @@ class UnOpInit : public OpInit, public FoldingSetNode {
     return I->getKind() == IK_UnOpInit;
   }
 
-  static UnOpInit *get(UnaryOp opc, Init *lhs, const RecTy *Type);
+  static const UnOpInit *get(UnaryOp opc, const Init *lhs, const RecTy *Type);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   // Clone - Clone this operator, replacing arguments with the new list
-  OpInit *clone(ArrayRef<Init *> Operands) const override {
+  const OpInit *clone(ArrayRef<const Init *> Operands) const override {
     assert(Operands.size() == 1 &&
            "Wrong number of operands for unary operation");
     return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
@@ -885,19 +893,19 @@ class UnOpInit : public OpInit, public FoldingSetNode {
 
   unsigned getNumOperands() const override { return 1; }
 
-  Init *getOperand(unsigned i) const override {
+  const Init *getOperand(unsigned i) const override {
     assert(i == 0 && "Invalid operand id for unary operator");
     return getOperand();
   }
 
   UnaryOp getOpcode() const { return (UnaryOp)Opc; }
-  Init *getOperand() const { return LHS; }
+  const Init *getOperand() const { return LHS; }
 
   // Fold - If possible, fold this to a simpler init.  Return this if not
   // possible to fold.
-  Init *Fold(Record *CurRec, bool IsFinal = false) const;
+  const Init *Fold(const Record *CurRec, bool IsFinal = false) const;
 
-  Init *resolveReferences(Resolver &R) const override;
+  const Init *resolveReferences(Resolver &R) const override;
 
   std::string getAsString() const override;
 };
@@ -937,9 +945,9 @@ class BinOpInit : public OpInit, public FoldingSetNode {
   };
 
 private:
-  Init *LHS, *RHS;
+  const Init *LHS, *RHS;
 
-  BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, const RecTy *Type)
+  BinOpInit(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
       : OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {}
 
 public:
@@ -950,21 +958,22 @@ class BinOpInit : public OpInit, public FoldingSetNode {
     return I->getKind() == IK_BinOpInit;
   }
 
-  static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, const RecTy *Type);
-  static Init *getStrConcat(Init *lhs, Init *rhs);
-  static Init *getListConcat(TypedInit *lhs, Init *rhs);
+  static const BinOpInit *get(BinaryOp opc, const Init *lhs, const Init *rhs,
+                              const RecTy *Type);
+  static const Init *getStrConcat(const Init *lhs, const Init *rhs);
+  static const Init *getListConcat(const TypedInit *lhs, const Init *rhs);
 
   void Profile(FoldingSetNodeID &ID) const;
 
   // Clone - Clone this operator, replacing arguments with the new list
-  OpInit *clone(ArrayRef<Init *> Operands) const override {
+  const OpInit *clone(ArrayRef<const Init *> Operands) const override {
     assert(Operands.size() == 2 &&
            "Wrong number of operands for binary operation");
     return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
   }
 
   unsigned getNumOperands() const override { return 2; }
-  Init *getOperand(unsigned i) const override {
+  const Init *getOperand(unsigned i) const override {
     switch (i) {
     default: llvm_unreachable("Invalid operand id for binary operator");
     case 0: return getLHS();
@@ -973,16 +982,17 @@ class BinOpInit : public OpInit, public FoldingSetNode {
   }
 
   BinaryOp getOpcode() const { return (BinaryOp)Opc; }
-  Init *getLHS() const { return LHS; }
-  Init *getRHS() const { return RHS; }
+  const Init *getLHS() const { return LHS; }
+  const Init *getRHS() const { return RHS; }
 
-  std::optional<bool> CompareInit(unsigned Opc, Init *LHS, Init *RHS) const;
+  std::optional<bool> CompareInit(unsigned Opc, const Init *LHS,
+                                  const Init *RHS) const;
 
   // Fold - If possible, fold this to a simpler init.  Return this if not
   // possi...
[truncated]

@topperc
Copy link
Collaborator

topperc commented Oct 17, 2024

Do Init objects have any methods that can modify them? const was explicitly removed from some uses a long time ago af8ee2c.

Here's a very old thread about removing const from IR Type pointers because they are immutable. https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20110711/124192.html I assume a similar thinking was being applied to Init objects.

@jurahul
Copy link
Contributor Author

jurahul commented Oct 17, 2024

Do Init objects have any methods that can modify them? const was explicitly removed from some uses a long time ago af8ee2c.

Here's a very old thread about removing const from IR Type pointers because they are immutable. https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20110711/124192.html I assume a similar thinking was being applied to Init objects.

That's a good point. Though I do see "const Type*" pointers in the code. I'd think there is a difference between an implementation detail of "this class is uniqued in the impl and therefore immutable" vs "irrespective of being uniqued or not, in this code I do not intend to modify it". The const-ness of pointers expresses the second one, regardless of the implementation detail of whether the object itself is immutable or not. But I'll let the reviewers decide. Note that some changes of this nature have been committed earlier for Clang and MLIR tablegen backends (as a prep for this one). Also, this enables us to get rid of a bunch of const_casts<> in the various Init::Fold() methods (which are const, but currently return non-const Init ptr, and when the Fold is unresolved have to const_cast<> this to non-const pointer to return it). So making them const just seems to play nicer with C++.

@jurahul jurahul requested a review from topperc October 17, 2024 16:59
@jurahul
Copy link
Contributor Author

jurahul commented Oct 17, 2024

Also, this enabled removing one const_cast<> in #112261 (which I think is going to be the final change for the time being to make more pointers const in TableGen backends).

@topperc
Copy link
Collaborator

topperc commented Oct 17, 2024

Also, this enabled removing one const_cast<> in #112261 (which I think is going to be the final change for the time being to make more pointers const in TableGen backends).

That one seems related to Record pointers being const. Can that be fixed independent of Init pointers being const?

@arsenm
Copy link
Contributor

arsenm commented Oct 17, 2024

I think const should always be used in all situations


if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
if (const VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use auto *CurBitVar here and on all the other dyn_cast/cast. But you don't have to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX backend:X86 clang Clang issues not falling into any other category llvm:globalisel llvm:SelectionDAG SelectionDAGISel as well mlir:core MLIR Core Infrastructure mlir tablegen
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants