-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[TableGen] Only store direct superclasses in Record #123072
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
Changes from 14 commits
f12511a
2a96ff4
683fe14
e235152
e169673
2eb1354
6d74050
21492b5
9a645ee
d39a1fc
e52de29
9c70b67
834a337
2af1d81
0889f4c
e57064b
0160783
8bbe3c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -610,29 +610,28 @@ functions returns null. | |
| Getting Record Superclasses | ||
| =========================== | ||
|
|
||
| The ``Record`` class provides a function to obtain the superclasses of a | ||
| record. It is named ``getSuperClasses`` and returns an ``ArrayRef`` of an | ||
| array of ``std::pair`` pairs. The superclasses are in post-order: the order | ||
| in which the superclasses were visited while copying their fields into the | ||
| record. Each pair consists of a pointer to the ``Record`` instance for a | ||
| superclass record and an instance of the ``SMRange`` class. The range | ||
| indicates the source file locations of the beginning and end of the class | ||
| definition. | ||
|
|
||
| This example obtains the superclasses of the ``Prototype`` record and then | ||
| iterates over the pairs in the returned array. | ||
| The ``Record`` class provides a function to obtain the direct superclasses | ||
| of a record. It is named ``getDirectSuperClasses`` and returns an | ||
| ``ArrayRef`` of an array of ``std::pair`` pairs. Each pair consists of a | ||
| pointer to the ``Record`` instance for a superclass record and an instance | ||
| of the ``SMRange`` class. The range indicates the source file locations of | ||
| the beginning and end of the class definition. | ||
|
Comment on lines
+617
to
+618
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR, but since you are changing the comment:
Is this true?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I noticed this too, and I think you're probably right and the documentation is wrong, but I didn't want to go down the rabbit hole of fact-checking the documentation just now. |
||
|
|
||
| This example obtains the direct superclasses of the ``Prototype`` record and | ||
| then iterates over the pairs in the returned array. | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| ArrayRef<std::pair<const Record *, SMRange>> | ||
| Superclasses = Prototype->getSuperClasses(); | ||
| for (const auto &SuperPair : Superclasses) { | ||
| Superclasses = Prototype->getDirectSuperClasses(); | ||
| for (const auto &[Super, Range] : Superclasses) { | ||
| ... | ||
| } | ||
|
|
||
| The ``Record`` class also provides a function, ``getDirectSuperClasses``, to | ||
| append the *direct* superclasses of a record to a given vector of type | ||
| ``SmallVectorImpl<Record *>``. | ||
| The ``Record`` class also provides a function, ``getSuperClasses``, to | ||
| return a vector of *all* superclasses of a record. The superclasses are in | ||
| post-order: the order in which the superclasses were visited while copying | ||
| their fields into the record. | ||
|
|
||
| Emitting Text to the Output Stream | ||
| ================================== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1630,9 +1630,9 @@ class Record { | |
| SmallVector<AssertionInfo, 0> Assertions; | ||
| SmallVector<DumpInfo, 0> Dumps; | ||
|
|
||
| // All superclasses in the inheritance forest in post-order (yes, it | ||
| // Direct superclasses, which are roots of the inheritance forest (yes, it | ||
| // must be a forest; diamond-shaped inheritance is not allowed). | ||
| SmallVector<std::pair<const Record *, SMRange>, 0> SuperClasses; | ||
| SmallVector<std::pair<const Record *, SMRange>, 0> DirectSuperClasses; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the fact that we're only storing a small number of direct super classes now, would it make more sense to use SmallVector<..., N> with N not equal to zero? N = 0 will disable any stack allocation and allocate on heap only, IIRC
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried N=1 but it seemed to increase memory usage overall. I think the problem is that |
||
|
|
||
| // Tracks Record instances. Not owned by Record. | ||
| RecordKeeper &TrackedRecords; | ||
|
|
@@ -1666,8 +1666,9 @@ class Record { | |
| Record(const Record &O) | ||
| : Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs), | ||
| Values(O.Values), Assertions(O.Assertions), | ||
| SuperClasses(O.SuperClasses), TrackedRecords(O.TrackedRecords), | ||
| ID(getNewUID(O.getRecords())), Kind(O.Kind) {} | ||
| DirectSuperClasses(O.DirectSuperClasses), | ||
| TrackedRecords(O.TrackedRecords), ID(getNewUID(O.getRecords())), | ||
| Kind(O.Kind) {} | ||
|
|
||
| static unsigned getNewUID(RecordKeeper &RK); | ||
|
|
||
|
|
@@ -1718,15 +1719,30 @@ class Record { | |
| ArrayRef<AssertionInfo> getAssertions() const { return Assertions; } | ||
| ArrayRef<DumpInfo> getDumps() const { return Dumps; } | ||
|
|
||
| ArrayRef<std::pair<const Record *, SMRange>> getSuperClasses() const { | ||
| return SuperClasses; | ||
| /// Append all superclasses in post-order to \p Classes. | ||
| void getSuperClasses(std::vector<const Record *> &Classes) const { | ||
| for (const Record *SC : make_first_range(DirectSuperClasses)) { | ||
| SC->getSuperClasses(Classes); | ||
| Classes.push_back(SC); | ||
| } | ||
| } | ||
|
|
||
| /// Return all superclasses in post-order. | ||
| std::vector<const Record *> getSuperClasses() const { | ||
| std::vector<const Record *> Classes; | ||
| getSuperClasses(Classes); | ||
| return Classes; | ||
| } | ||
|
|
||
| /// Determine whether this record has the specified direct superclass. | ||
| bool hasDirectSuperClass(const Record *SuperClass) const; | ||
| bool hasDirectSuperClass(const Record *SuperClass) const { | ||
| return is_contained(make_first_range(DirectSuperClasses), SuperClass); | ||
| } | ||
|
|
||
| /// Append the direct superclasses of this record to Classes. | ||
| void getDirectSuperClasses(SmallVectorImpl<const Record *> &Classes) const; | ||
| /// Return the direct superclasses of this record. | ||
| ArrayRef<std::pair<const Record *, SMRange>> getDirectSuperClasses() const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (idea for a future patch)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh I see, it is copied around but ultimately never used for anything. I can do a follow up to remove it completely. Or maybe doing that change first, before landing this one, would be less disruptive (in case there are any out-of-tree users of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure location should be removed completely as it might be used for diagnostics.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Though unfortunately we're not doing it at this moment, I think we should make good uses of this location (where the superclass was reference) to show a nice backtrace. Personally I feel like if it doesn't take a lot of efforts we should probably keep the location. |
||
| return DirectSuperClasses; | ||
| } | ||
|
|
||
| bool isTemplateArg(const Init *Name) const { | ||
| return llvm::is_contained(TemplateArgs, Name); | ||
|
|
@@ -1794,29 +1810,32 @@ class Record { | |
| void checkUnusedTemplateArgs(); | ||
|
|
||
| bool isSubClassOf(const Record *R) const { | ||
| for (const auto &[SC, _] : SuperClasses) | ||
| if (SC == R) | ||
| for (const Record *SC : make_first_range(DirectSuperClasses)) { | ||
| if (SC == R || SC->isSubClassOf(R)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool isSubClassOf(StringRef Name) const { | ||
| for (const auto &[SC, _] : SuperClasses) { | ||
| for (const Record *SC : make_first_range(DirectSuperClasses)) { | ||
| if (const auto *SI = dyn_cast<StringInit>(SC->getNameInit())) { | ||
| if (SI->getValue() == Name) | ||
| return true; | ||
| } else if (SC->getNameInitAsString() == Name) { | ||
| return true; | ||
| } | ||
| if (SC->isSubClassOf(Name)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void addSuperClass(const Record *R, SMRange Range) { | ||
| void addDirectSuperClass(const Record *R, SMRange Range) { | ||
| assert(!CorrespondingDefInit && | ||
| "changing type of record after it has been referenced"); | ||
| assert(!isSubClassOf(R) && "Already subclassing record!"); | ||
| SuperClasses.emplace_back(R, Range); | ||
| DirectSuperClasses.emplace_back(R, Range); | ||
| } | ||
|
|
||
| /// If there are any field references that refer to fields that have been | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.