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 SeachableTableEmitter to use const RecordKeeper #110032

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/utils/TableGen/ClangAttrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ static LateAttrParseKind getLateAttrParseKind(const Record *Attr) {
auto *LAPK = Attr->getValueAsDef(LateParsedStr);

// Typecheck the `LateParsed` field.
SmallVector<Record *, 1> SuperClasses;
SmallVector<const Record *, 1> SuperClasses;
LAPK->getDirectSuperClasses(SuperClasses);
if (SuperClasses.size() != 1)
PrintFatalError(Attr, "Field `" + Twine(LateParsedStr) +
Expand Down
19 changes: 10 additions & 9 deletions llvm/include/llvm/TableGen/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ class DagRecTy : public RecTy {
///
/// The list of superclasses is non-redundant, i.e. only contains classes that
/// are not the superclass of some other listed class.
class RecordRecTy final : public RecTy, public FoldingSetNode,
public TrailingObjects<RecordRecTy, Record *> {
class RecordRecTy final : public RecTy,
public FoldingSetNode,
public TrailingObjects<RecordRecTy, const Record *> {
friend class Record;
friend detail::RecordKeeperImpl;

Expand All @@ -248,23 +249,23 @@ class RecordRecTy final : public RecTy, public FoldingSetNode,
}

/// Get the record type with the given non-redundant list of superclasses.
static RecordRecTy *get(RecordKeeper &RK, ArrayRef<Record *> Classes);
static RecordRecTy *get(Record *Class);
static RecordRecTy *get(RecordKeeper &RK, ArrayRef<const Record *> Classes);
static RecordRecTy *get(const Record *Class);

void Profile(FoldingSetNodeID &ID) const;

ArrayRef<Record *> getClasses() const {
return ArrayRef(getTrailingObjects<Record *>(), NumClasses);
ArrayRef<const Record *> getClasses() const {
return ArrayRef(getTrailingObjects<const Record *>(), NumClasses);
}

using const_record_iterator = Record * const *;
using const_record_iterator = const Record *const *;

const_record_iterator classes_begin() const { return getClasses().begin(); }
const_record_iterator classes_end() const { return getClasses().end(); }

std::string getAsString() const override;

bool isSubClassOf(Record *Class) const;
bool isSubClassOf(const Record *Class) const;
bool typeIsConvertibleTo(const RecTy *RHS) const override;

bool typeIsA(const RecTy *RHS) const override;
Expand Down Expand Up @@ -1763,7 +1764,7 @@ class Record {
bool hasDirectSuperClass(const Record *SuperClass) const;

/// Append the direct superclasses of this record to Classes.
void getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const;
void getDirectSuperClasses(SmallVectorImpl<const Record *> &Classes) const;

bool isTemplateArg(Init *Name) const {
return llvm::is_contained(TemplateArgs, Name);
Expand Down
50 changes: 25 additions & 25 deletions llvm/lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,22 @@ std::string DagRecTy::getAsString() const {
}

static void ProfileRecordRecTy(FoldingSetNodeID &ID,
ArrayRef<Record *> Classes) {
ArrayRef<const Record *> Classes) {
ID.AddInteger(Classes.size());
for (Record *R : Classes)
for (const Record *R : Classes)
ID.AddPointer(R);
}

RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
ArrayRef<Record *> UnsortedClasses) {
ArrayRef<const Record *> UnsortedClasses) {
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
if (UnsortedClasses.empty())
return &RKImpl.AnyRecord;

FoldingSet<RecordRecTy> &ThePool = RKImpl.RecordTypePool;

SmallVector<Record *, 4> Classes(UnsortedClasses);
llvm::sort(Classes, [](Record *LHS, Record *RHS) {
SmallVector<const Record *, 4> Classes(UnsortedClasses);
llvm::sort(Classes, [](const Record *LHS, const Record *RHS) {
return LHS->getNameInitAsString() < RHS->getNameInitAsString();
});

Expand All @@ -263,16 +263,16 @@ RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
#endif

void *Mem = RKImpl.Allocator.Allocate(
totalSizeToAlloc<Record *>(Classes.size()), alignof(RecordRecTy));
totalSizeToAlloc<const Record *>(Classes.size()), alignof(RecordRecTy));
RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes.size());
std::uninitialized_copy(Classes.begin(), Classes.end(),
Ty->getTrailingObjects<Record *>());
Ty->getTrailingObjects<const Record *>());
ThePool.InsertNode(Ty, IP);
return Ty;
}
RecordRecTy *RecordRecTy::get(Record *Class) {
RecordRecTy *RecordRecTy::get(const Record *Class) {
assert(Class && "unexpected null class");
return get(Class->getRecords(), Class);
return get(Class->getRecords(), {Class});
}

void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
Expand All @@ -285,7 +285,7 @@ std::string RecordRecTy::getAsString() const {

std::string Str = "{";
bool First = true;
for (Record *R : getClasses()) {
for (const Record *R : getClasses()) {
if (!First)
Str += ", ";
First = false;
Expand All @@ -295,11 +295,10 @@ std::string RecordRecTy::getAsString() const {
return Str;
}

bool RecordRecTy::isSubClassOf(Record *Class) const {
return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
return MySuperClass == Class ||
MySuperClass->isSubClassOf(Class);
});
bool RecordRecTy::isSubClassOf(const Record *Class) const {
return llvm::any_of(getClasses(), [Class](const Record *MySuperClass) {
return MySuperClass == Class || MySuperClass->isSubClassOf(Class);
});
}

bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
Expand All @@ -310,21 +309,21 @@ bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
if (!RTy)
return false;

return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
return isSubClassOf(TargetClass);
});
return llvm::all_of(RTy->getClasses(), [this](const Record *TargetClass) {
return isSubClassOf(TargetClass);
});
}

bool RecordRecTy::typeIsA(const RecTy *RHS) const {
return typeIsConvertibleTo(RHS);
}

static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
SmallVector<Record *, 4> CommonSuperClasses;
SmallVector<Record *, 4> Stack(T1->getClasses());
SmallVector<const Record *, 4> CommonSuperClasses;
SmallVector<const Record *, 4> Stack(T1->getClasses());

while (!Stack.empty()) {
Record *R = Stack.pop_back_val();
const Record *R = Stack.pop_back_val();

if (T2->isSubClassOf(R)) {
CommonSuperClasses.push_back(R);
Expand Down Expand Up @@ -2162,8 +2161,8 @@ std::string ExistsOpInit::getAsString() const {

RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
for (Record *Rec : RecordType->getClasses()) {
if (RecordVal *Field = Rec->getValue(FieldName))
for (const Record *Rec : RecordType->getClasses()) {
if (const RecordVal *Field = Rec->getValue(FieldName))
return Field->getType();
}
}
Expand Down Expand Up @@ -2831,7 +2830,7 @@ void Record::checkName() {
}

RecordRecTy *Record::getType() const {
SmallVector<Record *, 4> DirectSCs;
SmallVector<const Record *, 4> DirectSCs;
getDirectSuperClasses(DirectSCs);
return RecordRecTy::get(TrackedRecords, DirectSCs);
}
Expand Down Expand Up @@ -2882,7 +2881,8 @@ bool Record::hasDirectSuperClass(const Record *Superclass) const {
return false;
}

void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
void Record::getDirectSuperClasses(
SmallVectorImpl<const Record *> &Classes) const {
ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();

while (!SCs.empty()) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/TableGen/TGParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3004,9 +3004,9 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
DI->getDef()->getValue(FieldName)->addReferenceLoc(FieldNameLoc);
} else if (auto *TI = dyn_cast<TypedInit>(Result)) {
if (auto *RecTy = dyn_cast<RecordRecTy>(TI->getType())) {
for (Record *R : RecTy->getClasses())
for (const Record *R : RecTy->getClasses())
if (auto *RV = R->getValue(FieldName))
RV->addReferenceLoc(FieldNameLoc);
const_cast<RecordVal *>(RV)->addReferenceLoc(FieldNameLoc);
}
}
}
Expand Down
Loading
Loading