Skip to content

Commit 743f839

Browse files
authored
[NFC][LLVM][TableGen] Change RecordKeeper::getClass to return const pointer (#112261)
Change `RecordKeeper::getClass` to return const record pointer. 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
1 parent 60105ac commit 743f839

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,11 +1355,11 @@ class VarDefInit final
13551355
public FoldingSetNode,
13561356
public TrailingObjects<VarDefInit, const ArgumentInit *> {
13571357
SMLoc Loc;
1358-
Record *Class;
1358+
const Record *Class;
13591359
const DefInit *Def = nullptr; // after instantiation
13601360
unsigned NumArgs;
13611361

1362-
explicit VarDefInit(SMLoc Loc, Record *Class, unsigned N);
1362+
explicit VarDefInit(SMLoc Loc, const Record *Class, unsigned N);
13631363

13641364
const DefInit *instantiate();
13651365

@@ -1373,7 +1373,7 @@ class VarDefInit final
13731373
static bool classof(const Init *I) {
13741374
return I->getKind() == IK_VarDefInit;
13751375
}
1376-
static const VarDefInit *get(SMLoc Loc, Record *Class,
1376+
static const VarDefInit *get(SMLoc Loc, const Record *Class,
13771377
ArrayRef<const ArgumentInit *> Args);
13781378

13791379
void Profile(FoldingSetNodeID &ID) const;
@@ -2000,7 +2000,7 @@ class RecordKeeper {
20002000
const GlobalMap &getGlobals() const { return ExtraGlobals; }
20012001

20022002
/// Get the class with the specified name.
2003-
Record *getClass(StringRef Name) const {
2003+
const Record *getClass(StringRef Name) const {
20042004
auto I = Classes.find(Name);
20052005
return I == Classes.end() ? nullptr : I->second.get();
20062006
}

llvm/lib/TableGen/Record.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ const RecTy *DefInit::getFieldType(const StringInit *FieldName) const {
22942294

22952295
std::string DefInit::getAsString() const { return std::string(Def->getName()); }
22962296

2297-
static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class,
2297+
static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class,
22982298
ArrayRef<const ArgumentInit *> Args) {
22992299
ID.AddInteger(Args.size());
23002300
ID.AddPointer(Class);
@@ -2303,11 +2303,11 @@ static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class,
23032303
ID.AddPointer(I);
23042304
}
23052305

2306-
VarDefInit::VarDefInit(SMLoc Loc, Record *Class, unsigned N)
2306+
VarDefInit::VarDefInit(SMLoc Loc, const Record *Class, unsigned N)
23072307
: TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class),
23082308
NumArgs(N) {}
23092309

2310-
const VarDefInit *VarDefInit::get(SMLoc Loc, Record *Class,
2310+
const VarDefInit *VarDefInit::get(SMLoc Loc, const Record *Class,
23112311
ArrayRef<const ArgumentInit *> Args) {
23122312
FoldingSetNodeID ID;
23132313
ProfileVarDefInit(ID, Class, Args);

llvm/lib/TableGen/TGParser.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace llvm {
3434

3535
struct SubClassReference {
3636
SMRange RefRange;
37-
Record *Rec = nullptr;
37+
const Record *Rec = nullptr;
3838
SmallVector<const ArgumentInit *, 4> TemplateArgs;
3939

4040
SubClassReference() = default;
@@ -110,7 +110,7 @@ static void checkConcrete(Record &R) {
110110

111111
/// Return an Init with a qualifier prefix referring
112112
/// to CurRec's name.
113-
static const Init *QualifyName(Record &CurRec, const Init *Name) {
113+
static const Init *QualifyName(const Record &CurRec, const Init *Name) {
114114
RecordKeeper &RK = CurRec.getRecords();
115115
const Init *NewName = BinOpInit::getStrConcat(
116116
CurRec.getNameInit(),
@@ -127,7 +127,7 @@ static const Init *QualifyName(MultiClass *MC, const Init *Name) {
127127
}
128128

129129
/// Return the qualified version of the implicit 'NAME' template argument.
130-
static const Init *QualifiedNameOfImplicitName(Record &Rec) {
130+
static const Init *QualifiedNameOfImplicitName(const Record &Rec) {
131131
return QualifyName(Rec, StringInit::get(Rec.getRecords(), "NAME"));
132132
}
133133

@@ -298,7 +298,7 @@ bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const Init *ValName,
298298
/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
299299
/// args as SubClass's template arguments.
300300
bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
301-
Record *SC = SubClass.Rec;
301+
const Record *SC = SubClass.Rec;
302302
MapResolver R(CurRec);
303303

304304
// Loop over all the subclass record's fields. Add regular fields to the new
@@ -588,7 +588,7 @@ bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
588588
return false;
589589
}
590590

591-
bool TGParser::resolveArguments(Record *Rec,
591+
bool TGParser::resolveArguments(const Record *Rec,
592592
ArrayRef<const ArgumentInit *> ArgValues,
593593
SMLoc Loc, ArgValueHandler ArgValueHandler) {
594594
ArrayRef<const Init *> ArgNames = Rec->getTemplateArgs();
@@ -632,7 +632,7 @@ bool TGParser::resolveArguments(Record *Rec,
632632

633633
/// Resolve the arguments of class and set them to MapResolver.
634634
/// Returns true if failed.
635-
bool TGParser::resolveArgumentsOfClass(MapResolver &R, Record *Rec,
635+
bool TGParser::resolveArgumentsOfClass(MapResolver &R, const Record *Rec,
636636
ArrayRef<const ArgumentInit *> ArgValues,
637637
SMLoc Loc) {
638638
return resolveArguments(
@@ -710,13 +710,13 @@ const Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
710710
///
711711
/// ClassID ::= ID
712712
///
713-
Record *TGParser::ParseClassID() {
713+
const Record *TGParser::ParseClassID() {
714714
if (Lex.getCode() != tgtok::Id) {
715715
TokError("expected name for ClassID");
716716
return nullptr;
717717
}
718718

719-
Record *Result = Records.getClass(Lex.getCurStrVal());
719+
const Record *Result = Records.getClass(Lex.getCurStrVal());
720720
if (!Result) {
721721
std::string Msg("Couldn't find class '" + Lex.getCurStrVal() + "'");
722722
if (MultiClasses[Lex.getCurStrVal()].get())
@@ -2708,7 +2708,7 @@ const Init *TGParser::ParseSimpleValue(Record *CurRec, const RecTy *ItemType,
27082708
// Value ::= CLASSID '<' ArgValueList '>' (CLASSID has been consumed)
27092709
// This is supposed to synthesize a new anonymous definition, deriving
27102710
// from the class with the template arguments, but no body.
2711-
Record *Class = Records.getClass(Name->getValue());
2711+
const Record *Class = Records.getClass(Name->getValue());
27122712
if (!Class) {
27132713
Error(NameLoc.Start,
27142714
"Expected a class name, got '" + Name->getValue() + "'");
@@ -3196,7 +3196,7 @@ void TGParser::ParseValueList(SmallVectorImpl<const Init *> &Result,
31963196
// NamedArgValueList ::= [NameValue '=' Value {',' NameValue '=' Value}*]
31973197
bool TGParser::ParseTemplateArgValueList(
31983198
SmallVectorImpl<const ArgumentInit *> &Result, Record *CurRec,
3199-
Record *ArgsRec) {
3199+
const Record *ArgsRec) {
32003200
assert(Result.empty() && "Result vector is not empty");
32013201
ArrayRef<const Init *> TArgs = ArgsRec->getTemplateArgs();
32023202

@@ -3990,7 +3990,7 @@ bool TGParser::ParseClass() {
39903990
return TokError("expected class name after 'class' keyword");
39913991

39923992
const std::string &Name = Lex.getCurStrVal();
3993-
Record *CurRec = Records.getClass(Name);
3993+
Record *CurRec = const_cast<Record *>(Records.getClass(Name));
39943994
if (CurRec) {
39953995
// If the body was previously defined, this is an error.
39963996
if (!CurRec->getValues().empty() ||
@@ -4411,7 +4411,8 @@ bool TGParser::ParseFile() {
44114411
// If necessary, replace an argument with a cast to the required type.
44124412
// The argument count has already been checked.
44134413
bool TGParser::CheckTemplateArgValues(
4414-
SmallVectorImpl<const ArgumentInit *> &Values, SMLoc Loc, Record *ArgsRec) {
4414+
SmallVectorImpl<const ArgumentInit *> &Values, SMLoc Loc,
4415+
const Record *ArgsRec) {
44154416
ArrayRef<const Init *> TArgs = ArgsRec->getTemplateArgs();
44164417

44174418
for (const ArgumentInit *&Value : Values) {
@@ -4421,7 +4422,7 @@ bool TGParser::CheckTemplateArgValues(
44214422
if (Value->isNamed())
44224423
ArgName = Value->getName();
44234424

4424-
RecordVal *Arg = ArgsRec->getValue(ArgName);
4425+
const RecordVal *Arg = ArgsRec->getValue(ArgName);
44254426
const RecTy *ArgType = Arg->getType();
44264427

44274428
if (const auto *ArgValue = dyn_cast<TypedInit>(Value->getValue())) {

llvm/lib/TableGen/TGParser.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ class TGParser {
248248

249249
using ArgValueHandler = std::function<void(const Init *, const Init *)>;
250250
bool resolveArguments(
251-
Record *Rec, ArrayRef<const ArgumentInit *> ArgValues, SMLoc Loc,
251+
const Record *Rec, ArrayRef<const ArgumentInit *> ArgValues, SMLoc Loc,
252252
ArgValueHandler ArgValueHandler = [](const Init *, const Init *) {});
253-
bool resolveArgumentsOfClass(MapResolver &R, Record *Rec,
253+
bool resolveArgumentsOfClass(MapResolver &R, const Record *Rec,
254254
ArrayRef<const ArgumentInit *> ArgValues,
255255
SMLoc Loc);
256256
bool resolveArgumentsOfMultiClass(SubstStack &Substs, MultiClass *MC,
@@ -296,7 +296,7 @@ class TGParser {
296296
void ParseValueList(SmallVectorImpl<const Init *> &Result, Record *CurRec,
297297
const RecTy *ItemType = nullptr);
298298
bool ParseTemplateArgValueList(SmallVectorImpl<const ArgumentInit *> &Result,
299-
Record *CurRec, Record *ArgsRec);
299+
Record *CurRec, const Record *ArgsRec);
300300
void ParseDagArgList(
301301
SmallVectorImpl<std::pair<const Init *, const StringInit *>> &Result,
302302
Record *CurRec);
@@ -316,12 +316,12 @@ class TGParser {
316316
const Init *ParseOperationCond(Record *CurRec, const RecTy *ItemType);
317317
const RecTy *ParseOperatorType();
318318
const Init *ParseObjectName(MultiClass *CurMultiClass);
319-
Record *ParseClassID();
319+
const Record *ParseClassID();
320320
MultiClass *ParseMultiClassID();
321321
bool ApplyLetStack(Record *CurRec);
322322
bool ApplyLetStack(RecordsEntry &Entry);
323323
bool CheckTemplateArgValues(SmallVectorImpl<const ArgumentInit *> &Values,
324-
SMLoc Loc, Record *ArgsRec);
324+
SMLoc Loc, const Record *ArgsRec);
325325
};
326326

327327
} // end namespace llvm

llvm/utils/TableGen/Common/CodeGenRegisters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ struct TupleExpander : SetTheory::Expander {
649649
return;
650650

651651
// Precompute some types.
652-
Record *RegisterCl = Def->getRecords().getClass("Register");
652+
const Record *RegisterCl = Def->getRecords().getClass("Register");
653653
const RecTy *RegisterRecTy = RecordRecTy::get(RegisterCl);
654654
std::vector<StringRef> RegNames =
655655
Def->getValueAsListOfStrings("RegAsmNames");

mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ static bool emitSerializationFns(const RecordKeeper &records, raw_ostream &os) {
11841184
utilsString;
11851185
raw_string_ostream dSerFn(dSerFnString), dDesFn(dDesFnString),
11861186
serFn(serFnString), deserFn(deserFnString);
1187-
Record *attrClass = records.getClass("Attr");
1187+
const Record *attrClass = records.getClass("Attr");
11881188

11891189
// Emit the serialization and deserialization functions simultaneously.
11901190
StringRef opVar("op");

0 commit comments

Comments
 (0)