[Mlir-commits] [llvm] [mlir] [LLVM][TableGen] Change `RecordKeeper::getClass` to return const pointer (PR #112261)
Rahul Joshi
llvmlistbot at llvm.org
Mon Oct 14 14:04:20 PDT 2024
https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/112261
Change `RecordKeeper::getClass` to return const record pointer.
>From 17a19dcaa78b5ca54d4bfe6c0b6f42b5d762fe79 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 14 Oct 2024 13:49:12 -0700
Subject: [PATCH] [LLVM][TableGen] Change `RecordKeeper::getClass` to return
const pointer
Change `RecordKeeper::getClass` to return const record pointer.
---
llvm/include/llvm/TableGen/Record.h | 8 ++---
llvm/lib/TableGen/Record.cpp | 6 ++--
llvm/lib/TableGen/TGParser.cpp | 33 ++++++++++---------
llvm/lib/TableGen/TGParser.h | 10 +++---
.../TableGen/Common/CodeGenRegisters.cpp | 2 +-
mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp | 2 +-
6 files changed, 32 insertions(+), 29 deletions(-)
diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index f856ff4cbd34b5..5cca9d0fd31531 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -1349,11 +1349,11 @@ class VarDefInit final : public TypedInit,
public FoldingSetNode,
public TrailingObjects<VarDefInit, ArgumentInit *> {
SMLoc Loc;
- Record *Class;
+ const Record *Class;
DefInit *Def = nullptr; // after instantiation
unsigned NumArgs;
- explicit VarDefInit(SMLoc Loc, Record *Class, unsigned N);
+ explicit VarDefInit(SMLoc Loc, const Record *Class, unsigned N);
DefInit *instantiate();
@@ -1367,7 +1367,7 @@ class VarDefInit final : public TypedInit,
static bool classof(const Init *I) {
return I->getKind() == IK_VarDefInit;
}
- static VarDefInit *get(SMLoc Loc, Record *Class,
+ static VarDefInit *get(SMLoc Loc, const Record *Class,
ArrayRef<ArgumentInit *> Args);
void Profile(FoldingSetNodeID &ID) const;
@@ -1990,7 +1990,7 @@ class RecordKeeper {
const GlobalMap &getGlobals() const { return ExtraGlobals; }
/// Get the class with the specified name.
- Record *getClass(StringRef Name) const {
+ const Record *getClass(StringRef Name) const {
auto I = Classes.find(Name);
return I == Classes.end() ? nullptr : I->second.get();
}
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 447ecb7d74d24f..f12aef5c0c9bb4 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2284,7 +2284,7 @@ const RecTy *DefInit::getFieldType(StringInit *FieldName) const {
std::string DefInit::getAsString() const { return std::string(Def->getName()); }
-static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class,
+static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class,
ArrayRef<ArgumentInit *> Args) {
ID.AddInteger(Args.size());
ID.AddPointer(Class);
@@ -2293,11 +2293,11 @@ static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class,
ID.AddPointer(I);
}
-VarDefInit::VarDefInit(SMLoc Loc, Record *Class, unsigned N)
+VarDefInit::VarDefInit(SMLoc Loc, const Record *Class, unsigned N)
: TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class),
NumArgs(N) {}
-VarDefInit *VarDefInit::get(SMLoc Loc, Record *Class,
+VarDefInit *VarDefInit::get(SMLoc Loc, const Record *Class,
ArrayRef<ArgumentInit *> Args) {
FoldingSetNodeID ID;
ProfileVarDefInit(ID, Class, Args);
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index aed4f3fe0e96b5..350e423394ab56 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -34,7 +34,7 @@ namespace llvm {
struct SubClassReference {
SMRange RefRange;
- Record *Rec = nullptr;
+ const Record *Rec = nullptr;
SmallVector<ArgumentInit *, 4> TemplateArgs;
SubClassReference() = default;
@@ -110,7 +110,7 @@ static void checkConcrete(Record &R) {
/// Return an Init with a qualifier prefix referring
/// to CurRec's name.
-static Init *QualifyName(Record &CurRec, Init *Name) {
+static Init *QualifyName(const Record &CurRec, Init *Name) {
RecordKeeper &RK = CurRec.getRecords();
Init *NewName = BinOpInit::getStrConcat(
CurRec.getNameInit(),
@@ -118,7 +118,7 @@ static Init *QualifyName(Record &CurRec, Init *Name) {
NewName = BinOpInit::getStrConcat(NewName, Name);
if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
- NewName = BinOp->Fold(&CurRec);
+ NewName = BinOp->Fold(const_cast<Record *>(&CurRec));
return NewName;
}
@@ -127,7 +127,7 @@ static Init *QualifyName(MultiClass *MC, Init *Name) {
}
/// Return the qualified version of the implicit 'NAME' template argument.
-static Init *QualifiedNameOfImplicitName(Record &Rec) {
+static Init *QualifiedNameOfImplicitName(const Record &Rec) {
return QualifyName(Rec, StringInit::get(Rec.getRecords(), "NAME"));
}
@@ -296,7 +296,7 @@ bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
/// args as SubClass's template arguments.
bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
- Record *SC = SubClass.Rec;
+ const Record *SC = SubClass.Rec;
MapResolver R(CurRec);
// Loop over all the subclass record's fields. Add regular fields to the new
@@ -586,8 +586,9 @@ bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
return false;
}
-bool TGParser::resolveArguments(Record *Rec, ArrayRef<ArgumentInit *> ArgValues,
- SMLoc Loc, ArgValueHandler ArgValueHandler) {
+bool TGParser::resolveArguments(const Record *Rec,
+ ArrayRef<ArgumentInit *> ArgValues, SMLoc Loc,
+ ArgValueHandler ArgValueHandler) {
ArrayRef<Init *> ArgNames = Rec->getTemplateArgs();
assert(ArgValues.size() <= ArgNames.size() &&
"Too many template arguments allowed");
@@ -629,7 +630,7 @@ bool TGParser::resolveArguments(Record *Rec, ArrayRef<ArgumentInit *> ArgValues,
/// Resolve the arguments of class and set them to MapResolver.
/// Returns true if failed.
-bool TGParser::resolveArgumentsOfClass(MapResolver &R, Record *Rec,
+bool TGParser::resolveArgumentsOfClass(MapResolver &R, const Record *Rec,
ArrayRef<ArgumentInit *> ArgValues,
SMLoc Loc) {
return resolveArguments(Rec, ArgValues, Loc,
@@ -704,13 +705,13 @@ Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
///
/// ClassID ::= ID
///
-Record *TGParser::ParseClassID() {
+const Record *TGParser::ParseClassID() {
if (Lex.getCode() != tgtok::Id) {
TokError("expected name for ClassID");
return nullptr;
}
- Record *Result = Records.getClass(Lex.getCurStrVal());
+ const Record *Result = Records.getClass(Lex.getCurStrVal());
if (!Result) {
std::string Msg("Couldn't find class '" + Lex.getCurStrVal() + "'");
if (MultiClasses[Lex.getCurStrVal()].get())
@@ -2699,7 +2700,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, const RecTy *ItemType,
// Value ::= CLASSID '<' ArgValueList '>' (CLASSID has been consumed)
// This is supposed to synthesize a new anonymous definition, deriving
// from the class with the template arguments, but no body.
- Record *Class = Records.getClass(Name->getValue());
+ const Record *Class = Records.getClass(Name->getValue());
if (!Class) {
Error(NameLoc.Start,
"Expected a class name, got '" + Name->getValue() + "'");
@@ -3185,7 +3186,8 @@ 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) {
+ SmallVectorImpl<ArgumentInit *> &Result, Record *CurRec,
+ const Record *ArgsRec) {
assert(Result.empty() && "Result vector is not empty");
ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
@@ -3978,7 +3980,7 @@ bool TGParser::ParseClass() {
return TokError("expected class name after 'class' keyword");
const std::string &Name = Lex.getCurStrVal();
- Record *CurRec = Records.getClass(Name);
+ Record *CurRec = const_cast<Record *>(Records.getClass(Name));
if (CurRec) {
// If the body was previously defined, this is an error.
if (!CurRec->getValues().empty() ||
@@ -4399,7 +4401,8 @@ bool TGParser::ParseFile() {
// If necessary, replace an argument with a cast to the required type.
// The argument count has already been checked.
bool TGParser::CheckTemplateArgValues(
- SmallVectorImpl<llvm::ArgumentInit *> &Values, SMLoc Loc, Record *ArgsRec) {
+ SmallVectorImpl<llvm::ArgumentInit *> &Values, SMLoc Loc,
+ const Record *ArgsRec) {
ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
for (llvm::ArgumentInit *&Value : Values) {
@@ -4409,7 +4412,7 @@ bool TGParser::CheckTemplateArgValues(
if (Value->isNamed())
ArgName = Value->getName();
- RecordVal *Arg = ArgsRec->getValue(ArgName);
+ const RecordVal *Arg = ArgsRec->getValue(ArgName);
const RecTy *ArgType = Arg->getType();
if (TypedInit *ArgValue = dyn_cast<TypedInit>(Value->getValue())) {
diff --git a/llvm/lib/TableGen/TGParser.h b/llvm/lib/TableGen/TGParser.h
index f33ae1ce2c8107..04b9d27a65f9c7 100644
--- a/llvm/lib/TableGen/TGParser.h
+++ b/llvm/lib/TableGen/TGParser.h
@@ -248,9 +248,9 @@ class TGParser {
using ArgValueHandler = std::function<void(Init *, Init *)>;
bool resolveArguments(
- Record *Rec, ArrayRef<ArgumentInit *> ArgValues, SMLoc Loc,
+ const Record *Rec, ArrayRef<ArgumentInit *> ArgValues, SMLoc Loc,
ArgValueHandler ArgValueHandler = [](Init *, Init *) {});
- bool resolveArgumentsOfClass(MapResolver &R, Record *Rec,
+ bool resolveArgumentsOfClass(MapResolver &R, const Record *Rec,
ArrayRef<ArgumentInit *> ArgValues, SMLoc Loc);
bool resolveArgumentsOfMultiClass(SubstStack &Substs, MultiClass *MC,
ArrayRef<ArgumentInit *> ArgValues,
@@ -295,7 +295,7 @@ class TGParser {
void ParseValueList(SmallVectorImpl<llvm::Init *> &Result, Record *CurRec,
const RecTy *ItemType = nullptr);
bool ParseTemplateArgValueList(SmallVectorImpl<llvm::ArgumentInit *> &Result,
- Record *CurRec, Record *ArgsRec);
+ Record *CurRec, const Record *ArgsRec);
void ParseDagArgList(
SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
Record *CurRec);
@@ -314,12 +314,12 @@ class TGParser {
Init *ParseOperationCond(Record *CurRec, const RecTy *ItemType);
const RecTy *ParseOperatorType();
Init *ParseObjectName(MultiClass *CurMultiClass);
- Record *ParseClassID();
+ const Record *ParseClassID();
MultiClass *ParseMultiClassID();
bool ApplyLetStack(Record *CurRec);
bool ApplyLetStack(RecordsEntry &Entry);
bool CheckTemplateArgValues(SmallVectorImpl<llvm::ArgumentInit *> &Values,
- SMLoc Loc, Record *ArgsRec);
+ SMLoc Loc, const Record *ArgsRec);
};
} // end namespace llvm
diff --git a/llvm/utils/TableGen/Common/CodeGenRegisters.cpp b/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
index b53492dafb25b6..babb534724e788 100644
--- a/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
@@ -649,7 +649,7 @@ struct TupleExpander : SetTheory::Expander {
return;
// Precompute some types.
- Record *RegisterCl = Def->getRecords().getClass("Register");
+ const Record *RegisterCl = Def->getRecords().getClass("Register");
const RecTy *RegisterRecTy = RecordRecTy::get(RegisterCl);
std::vector<StringRef> RegNames =
Def->getValueAsListOfStrings("RegAsmNames");
diff --git a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
index 3480c81ff7d4bb..75286231f5902f 100644
--- a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
+++ b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
@@ -1184,7 +1184,7 @@ static bool emitSerializationFns(const RecordKeeper &records, raw_ostream &os) {
utilsString;
raw_string_ostream dSerFn(dSerFnString), dDesFn(dDesFnString),
serFn(serFnString), deserFn(deserFnString);
- Record *attrClass = records.getClass("Attr");
+ const Record *attrClass = records.getClass("Attr");
// Emit the serialization and deserialization functions simultaneously.
StringRef opVar("op");
More information about the Mlir-commits
mailing list