Skip to content

Commit d256b9e

Browse files
authored
[TableGen] Change DefInit::Def to a const Record pointer (llvm#110747)
This change undoes a const_cast<> introduced in an earlier change to help transition to const pointers. It 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 1905cdb commit d256b9e

File tree

16 files changed

+67
-63
lines changed

16 files changed

+67
-63
lines changed

clang/utils/TableGen/ClangOptionDocEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ const unsigned UnlimitedArgs = unsigned(-1);
181181

182182
// Get the number of arguments expected for an option, or -1 if any number of
183183
// arguments are accepted.
184-
unsigned getNumArgsForKind(Record *OptionKind, const Record *Option) {
184+
unsigned getNumArgsForKind(const Record *OptionKind, const Record *Option) {
185185
return StringSwitch<unsigned>(OptionKind->getName())
186186
.Cases("KIND_JOINED", "KIND_JOINED_OR_SEPARATE", "KIND_SEPARATE", 1)
187187
.Cases("KIND_REMAINING_ARGS", "KIND_REMAINING_ARGS_JOINED",

llvm/include/llvm/TableGen/DirectiveEmitter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ class Directive : public BaseRecord {
155155
return Def->getValueAsListOfDefs("leafConstructs");
156156
}
157157

158-
Record *getAssociation() const { return Def->getValueAsDef("association"); }
158+
const Record *getAssociation() const {
159+
return Def->getValueAsDef("association");
160+
}
159161

160-
Record *getCategory() const { return Def->getValueAsDef("category"); }
162+
const Record *getCategory() const { return Def->getValueAsDef("category"); }
161163
};
162164

163165
// Wrapper class that contains Clause's information defined in DirectiveBase.td

llvm/include/llvm/TableGen/Record.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class ListInit final : public TypedInit, public FoldingSetNode,
783783
return cast<ListRecTy>(getType())->getElementType();
784784
}
785785

786-
Record *getElementAsRecord(unsigned i) const;
786+
const Record *getElementAsRecord(unsigned i) const;
787787

788788
Init *convertInitializerTo(const RecTy *Ty) const override;
789789

@@ -1316,9 +1316,9 @@ class VarBitInit final : public TypedInit {
13161316
class DefInit : public TypedInit {
13171317
friend class Record;
13181318

1319-
Record *Def;
1319+
const Record *Def;
13201320

1321-
explicit DefInit(Record *D);
1321+
explicit DefInit(const Record *D);
13221322

13231323
public:
13241324
DefInit(const DefInit &) = delete;
@@ -1330,7 +1330,7 @@ class DefInit : public TypedInit {
13301330

13311331
Init *convertInitializerTo(const RecTy *Ty) const override;
13321332

1333-
Record *getDef() const { return Def; }
1333+
const Record *getDef() const { return Def; }
13341334

13351335
const RecTy *getFieldType(StringInit *FieldName) const override;
13361336

@@ -1473,7 +1473,7 @@ class DagInit final : public TypedInit, public FoldingSetNode,
14731473
void Profile(FoldingSetNodeID &ID) const;
14741474

14751475
Init *getOperator() const { return Val; }
1476-
Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const;
1476+
const Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const;
14771477

14781478
StringInit *getName() const { return ValName; }
14791479

@@ -1660,7 +1660,7 @@ class Record {
16601660
// this record.
16611661
SmallVector<SMLoc, 4> Locs;
16621662
SmallVector<SMLoc, 0> ForwardDeclarationLocs;
1663-
SmallVector<SMRange, 0> ReferenceLocs;
1663+
mutable SmallVector<SMRange, 0> ReferenceLocs;
16641664
SmallVector<Init *, 0> TemplateArgs;
16651665
SmallVector<RecordVal, 0> Values;
16661666
SmallVector<AssertionInfo, 0> Assertions;
@@ -1729,7 +1729,7 @@ class Record {
17291729
}
17301730

17311731
/// Add a reference to this record value.
1732-
void appendReferenceLoc(SMRange Loc) { ReferenceLocs.push_back(Loc); }
1732+
void appendReferenceLoc(SMRange Loc) const { ReferenceLocs.push_back(Loc); }
17331733

17341734
/// Return the references of this record value.
17351735
ArrayRef<SMRange> getReferenceLocs() const { return ReferenceLocs; }
@@ -1931,13 +1931,13 @@ class Record {
19311931
/// This method looks up the specified field and returns its value as a
19321932
/// Record, throwing an exception if the field does not exist or if the value
19331933
/// is not the right type.
1934-
Record *getValueAsDef(StringRef FieldName) const;
1934+
const Record *getValueAsDef(StringRef FieldName) const;
19351935

19361936
/// This method looks up the specified field and returns its value as a
19371937
/// Record, returning null if the field exists but is "uninitialized" (i.e.
19381938
/// set to `?`), and throwing an exception if the field does not exist or if
19391939
/// its value is not the right type.
1940-
Record *getValueAsOptionalDef(StringRef FieldName) const;
1940+
const Record *getValueAsOptionalDef(StringRef FieldName) const;
19411941

19421942
/// This method looks up the specified field and returns its value as a bit,
19431943
/// throwing an exception if the field does not exist or if the value is not

llvm/lib/TableGen/Record.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ Init *ListInit::convertInitializerTo(const RecTy *Ty) const {
746746
return nullptr;
747747
}
748748

749-
Record *ListInit::getElementAsRecord(unsigned i) const {
749+
const Record *ListInit::getElementAsRecord(unsigned i) const {
750750
assert(i < NumValues && "List element index out of range!");
751751
DefInit *DI = dyn_cast<DefInit>(getElement(i));
752752
if (!DI)
@@ -1713,7 +1713,7 @@ Init *TernOpInit::Fold(Record *CurRec) const {
17131713
StringInit *RHSs = dyn_cast<StringInit>(RHS);
17141714

17151715
if (LHSd && MHSd && RHSd) {
1716-
Record *Val = RHSd->getDef();
1716+
const Record *Val = RHSd->getDef();
17171717
if (LHSd->getAsString() == RHSd->getAsString())
17181718
Val = MHSd->getDef();
17191719
return Val->getDefInit();
@@ -2265,7 +2265,7 @@ Init *VarBitInit::resolveReferences(Resolver &R) const {
22652265
return const_cast<VarBitInit*>(this);
22662266
}
22672267

2268-
DefInit::DefInit(Record *D)
2268+
DefInit::DefInit(const Record *D)
22692269
: TypedInit(IK_DefInit, D->getType()), Def(D) {}
22702270

22712271
Init *DefInit::convertInitializerTo(const RecTy *Ty) const {
@@ -2445,7 +2445,7 @@ Init *FieldInit::resolveReferences(Resolver &R) const {
24452445

24462446
Init *FieldInit::Fold(Record *CurRec) const {
24472447
if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
2448-
Record *Def = DI->getDef();
2448+
const Record *Def = DI->getDef();
24492449
if (Def == CurRec)
24502450
PrintFatalError(CurRec->getLoc(),
24512451
Twine("Attempting to access field '") +
@@ -2656,7 +2656,7 @@ void DagInit::Profile(FoldingSetNodeID &ID) const {
26562656
ArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
26572657
}
26582658

2659-
Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
2659+
const Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
26602660
if (DefInit *DefI = dyn_cast<DefInit>(Val))
26612661
return DefI->getDef();
26622662
PrintFatalError(Loc, "Expected record as operator");
@@ -2837,8 +2837,8 @@ const RecordRecTy *Record::getType() const {
28372837

28382838
DefInit *Record::getDefInit() const {
28392839
if (!CorrespondingDefInit) {
2840-
CorrespondingDefInit = new (TrackedRecords.getImpl().Allocator)
2841-
DefInit(const_cast<Record *>(this));
2840+
CorrespondingDefInit =
2841+
new (TrackedRecords.getImpl().Allocator) DefInit(this);
28422842
}
28432843
return CorrespondingDefInit;
28442844
}
@@ -3108,7 +3108,7 @@ Record::getValueAsListOfStrings(StringRef FieldName) const {
31083108
return Strings;
31093109
}
31103110

3111-
Record *Record::getValueAsDef(StringRef FieldName) const {
3111+
const Record *Record::getValueAsDef(StringRef FieldName) const {
31123112
const RecordVal *R = getValue(FieldName);
31133113
if (!R || !R->getValue())
31143114
PrintFatalError(getLoc(), "Record `" + getName() +
@@ -3120,7 +3120,7 @@ Record *Record::getValueAsDef(StringRef FieldName) const {
31203120
FieldName + "' does not have a def initializer!");
31213121
}
31223122

3123-
Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
3123+
const Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
31243124
const RecordVal *R = getValue(FieldName);
31253125
if (!R || !R->getValue())
31263126
PrintFatalError(getLoc(), "Record `" + getName() +
@@ -3134,7 +3134,6 @@ Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
31343134
FieldName + "' does not have either a def initializer or '?'!");
31353135
}
31363136

3137-
31383137
bool Record::getValueAsBit(StringRef FieldName) const {
31393138
const RecordVal *R = getValue(FieldName);
31403139
if (!R || !R->getValue())

llvm/lib/TableGen/TGParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3001,7 +3001,8 @@ Init *TGParser::ParseValue(Record *CurRec, const RecTy *ItemType,
30013001
// Add a reference to this field if we know the record class.
30023002
if (TrackReferenceLocs) {
30033003
if (auto *DI = dyn_cast<DefInit>(Result)) {
3004-
DI->getDef()->getValue(FieldName)->addReferenceLoc(FieldNameLoc);
3004+
const RecordVal *V = DI->getDef()->getValue(FieldName);
3005+
const_cast<RecordVal *>(V)->addReferenceLoc(FieldNameLoc);
30053006
} else if (auto *TI = dyn_cast<TypedInit>(Result)) {
30063007
if (auto *RecTy = dyn_cast<RecordRecTy>(TI->getType())) {
30073008
for (const Record *R : RecTy->getClasses())

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3698,7 +3698,7 @@ static bool hasNullFragReference(DagInit *DI) {
36983698
DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator());
36993699
if (!OpDef)
37003700
return false;
3701-
Record *Operator = OpDef->getDef();
3701+
const Record *Operator = OpDef->getDef();
37023702

37033703
// If this is the null fragment, return true.
37043704
if (Operator->getName() == "null_frag")

llvm/utils/TableGen/Common/CodeGenSchedule.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ void CodeGenSchedModels::addProcModel(const Record *ProcDef) {
552552

553553
std::string Name = std::string(ModelKey->getName());
554554
if (ModelKey->isSubClassOf("SchedMachineModel")) {
555-
Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
555+
const Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
556556
ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
557557
} else {
558558
// An itinerary is defined without a machine model. Infer a new model.
@@ -674,9 +674,9 @@ void CodeGenSchedModels::collectSchedRW() {
674674
}
675675
// Initialize Aliases vectors.
676676
for (const Record *ADef : AliasDefs) {
677-
Record *AliasDef = ADef->getValueAsDef("AliasRW");
677+
const Record *AliasDef = ADef->getValueAsDef("AliasRW");
678678
getSchedRW(AliasDef).IsAlias = true;
679-
Record *MatchDef = ADef->getValueAsDef("MatchRW");
679+
const Record *MatchDef = ADef->getValueAsDef("MatchRW");
680680
CodeGenSchedRW &RW = getSchedRW(MatchDef);
681681
if (RW.IsAlias)
682682
PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
@@ -781,7 +781,7 @@ void CodeGenSchedModels::expandRWSeqForProc(
781781
for (const Record *Rec : SchedWrite.Aliases) {
782782
const CodeGenSchedRW &AliasRW = getSchedRW(Rec->getValueAsDef("AliasRW"));
783783
if (Rec->getValueInit("SchedModel")->isComplete()) {
784-
Record *ModelDef = Rec->getValueAsDef("SchedModel");
784+
const Record *ModelDef = Rec->getValueAsDef("SchedModel");
785785
if (&getProcModel(ModelDef) != &ProcModel)
786786
continue;
787787
}
@@ -854,7 +854,7 @@ void CodeGenSchedModels::collectSchedClasses() {
854854
// Create a SchedClass for each unique combination of itinerary class and
855855
// SchedRW list.
856856
for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
857-
Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
857+
const Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
858858
IdxVec Writes, Reads;
859859
if (!Inst->TheDef->isValueUnset("SchedRW"))
860860
findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
@@ -1050,7 +1050,7 @@ void CodeGenSchedModels::createInstRWClass(const Record *InstRWDef) {
10501050
if (OrigNumInstrs == InstDefs.size()) {
10511051
assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
10521052
"expected a generic SchedClass");
1053-
Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
1053+
const Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
10541054
// Make sure we didn't already have a InstRW containing this
10551055
// instruction on this model.
10561056
for (const Record *RWD : RWDefs) {
@@ -1279,7 +1279,7 @@ struct PredCheck {
12791279
unsigned RWIdx;
12801280
const Record *Predicate;
12811281

1282-
PredCheck(bool r, unsigned w, Record *p)
1282+
PredCheck(bool r, unsigned w, const Record *p)
12831283
: IsRead(r), RWIdx(w), Predicate(p) {}
12841284
};
12851285

@@ -1318,7 +1318,7 @@ class PredTransitions {
13181318
#endif
13191319

13201320
private:
1321-
bool mutuallyExclusive(Record *PredDef, ArrayRef<Record *> Preds,
1321+
bool mutuallyExclusive(const Record *PredDef, ArrayRef<const Record *> Preds,
13221322
ArrayRef<PredCheck> Term);
13231323
void getIntersectingVariants(const CodeGenSchedRW &SchedRW, unsigned TransIdx,
13241324
std::vector<TransVariant> &IntersectingVariants);
@@ -1336,8 +1336,8 @@ class PredTransitions {
13361336
// predicates are not exclusive because the predicates for a given SchedWrite
13371337
// are always checked in the order they are defined in the .td file. Later
13381338
// conditions implicitly negate any prior condition.
1339-
bool PredTransitions::mutuallyExclusive(Record *PredDef,
1340-
ArrayRef<Record *> Preds,
1339+
bool PredTransitions::mutuallyExclusive(const Record *PredDef,
1340+
ArrayRef<const Record *> Preds,
13411341
ArrayRef<PredCheck> Term) {
13421342
for (const PredCheck &PC : Term) {
13431343
if (PC.Predicate == PredDef)
@@ -1382,9 +1382,9 @@ bool PredTransitions::mutuallyExclusive(Record *PredDef,
13821382
return false;
13831383
}
13841384

1385-
static std::vector<Record *> getAllPredicates(ArrayRef<TransVariant> Variants,
1386-
unsigned ProcId) {
1387-
std::vector<Record *> Preds;
1385+
static std::vector<const Record *>
1386+
getAllPredicates(ArrayRef<TransVariant> Variants, unsigned ProcId) {
1387+
std::vector<const Record *> Preds;
13881388
for (auto &Variant : Variants) {
13891389
if (!Variant.VarOrSeqDef->isSubClassOf("SchedVar"))
13901390
continue;
@@ -1406,7 +1406,7 @@ void PredTransitions::getIntersectingVariants(
14061406
if (SchedRW.HasVariants) {
14071407
unsigned VarProcIdx = 0;
14081408
if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
1409-
Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
1409+
const Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
14101410
VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
14111411
}
14121412
if (VarProcIdx == 0 || VarProcIdx == TransVec[TransIdx].ProcIndex) {
@@ -1425,7 +1425,7 @@ void PredTransitions::getIntersectingVariants(
14251425
// that processor.
14261426
unsigned AliasProcIdx = 0;
14271427
if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1428-
Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
1428+
const Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
14291429
AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
14301430
}
14311431
if (AliasProcIdx && AliasProcIdx != TransVec[TransIdx].ProcIndex)
@@ -1451,13 +1451,13 @@ void PredTransitions::getIntersectingVariants(
14511451
if (AliasProcIdx == 0)
14521452
GenericRW = true;
14531453
}
1454-
std::vector<Record *> AllPreds =
1454+
std::vector<const Record *> AllPreds =
14551455
getAllPredicates(Variants, TransVec[TransIdx].ProcIndex);
14561456
for (TransVariant &Variant : Variants) {
14571457
// Don't expand variants if the processor models don't intersect.
14581458
// A zero processor index means any processor.
14591459
if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
1460-
Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
1460+
const Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
14611461
if (mutuallyExclusive(PredDef, AllPreds, TransVec[TransIdx].PredTerm))
14621462
continue;
14631463
}
@@ -1489,7 +1489,7 @@ void PredTransitions::pushVariant(const TransVariant &VInfo, bool IsRead) {
14891489
// then the whole transition is specific to this processor.
14901490
IdxVec SelectedRWs;
14911491
if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
1492-
Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
1492+
const Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
14931493
Trans.PredTerm.emplace_back(IsRead, VInfo.RWIdx, PredDef);
14941494
ConstRecVec SelectedDefs =
14951495
VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
@@ -1861,7 +1861,7 @@ void CodeGenSchedModels::collectProcResources() {
18611861
// This class may have a default ReadWrite list which can be overriden by
18621862
// InstRW definitions.
18631863
for (const Record *RW : SC.InstRWs) {
1864-
Record *RWModelDef = RW->getValueAsDef("SchedModel");
1864+
const Record *RWModelDef = RW->getValueAsDef("SchedModel");
18651865
unsigned PIdx = getProcModel(RWModelDef).Index;
18661866
IdxVec Writes, Reads;
18671867
findRWs(RW->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);

llvm/utils/TableGen/Common/GlobalISel/CombinerUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline bool isSpecificDef(const Init &N, StringRef Def) {
3232
/// subclass of the given class and coerce it to a def if it is. This is
3333
/// primarily useful for testing for subclasses of GIDefKind and similar in
3434
/// DagInit's since DagInit's support any type inside them.
35-
inline Record *getDefOfSubClass(const Init &N, StringRef Cls) {
35+
inline const Record *getDefOfSubClass(const Init &N, StringRef Cls) {
3636
if (const DefInit *OpI = dyn_cast<DefInit>(&N))
3737
if (OpI->getDef()->isSubClassOf(Cls))
3838
return OpI->getDef();

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ class CopyOrAddZeroRegRenderer : public OperandRenderer {
19981998

19991999
public:
20002000
CopyOrAddZeroRegRenderer(unsigned NewInsnID, StringRef SymbolicName,
2001-
Record *ZeroRegisterDef)
2001+
const Record *ZeroRegisterDef)
20022002
: OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID),
20032003
SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) {
20042004
assert(!SymbolicName.empty() && "Cannot copy from an unspecified source");

mlir/include/mlir/TableGen/Attribute.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class EnumAttr : public Attribute {
204204
std::vector<EnumAttrCase> getAllCases() const;
205205

206206
bool genSpecializedAttr() const;
207-
llvm::Record *getBaseAttrClass() const;
207+
const llvm::Record *getBaseAttrClass() const;
208208
StringRef getSpecializedAttrClassName() const;
209209
bool printBitEnumPrimaryGroups() const;
210210
};

mlir/lib/TableGen/Attribute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ bool EnumAttr::genSpecializedAttr() const {
229229
return def->getValueAsBit("genSpecializedAttr");
230230
}
231231

232-
llvm::Record *EnumAttr::getBaseAttrClass() const {
232+
const llvm::Record *EnumAttr::getBaseAttrClass() const {
233233
return def->getValueAsDef("baseAttrClass");
234234
}
235235

0 commit comments

Comments
 (0)