Skip to content

Commit

Permalink
[TableGen] Print memory stats in detailed record emitter (llvm#106990)
Browse files Browse the repository at this point in the history
Print memory allocation and related statistics when dumping detailed
record information.
  • Loading branch information
jurahul authored Sep 4, 2024
1 parent 1b0a802 commit 30f1cfb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
4 changes: 3 additions & 1 deletion llvm/include/llvm/TableGen/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ class Record {
ArrayRef<AssertionInfo> getAssertions() const { return Assertions; }
ArrayRef<DumpInfo> getDumps() const { return Dumps; }

ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
return SuperClasses;
}

Expand Down Expand Up @@ -2073,6 +2073,8 @@ class RecordKeeper {

void dump() const;

void dumpAllocationStats(raw_ostream &OS) const;

private:
RecordKeeper(RecordKeeper &&) = delete;
RecordKeeper(const RecordKeeper &) = delete;
Expand Down
17 changes: 11 additions & 6 deletions llvm/lib/TableGen/DetailedRecordsBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DetailedRecordsEmitter {
void printVariables(raw_ostream &OS);
void printClasses(raw_ostream &OS);
void printRecords(raw_ostream &OS);
void printAllocationStats(raw_ostream &OS);
void printDefms(const Record &Rec, raw_ostream &OS);
void printTemplateArgs(const Record &Rec, raw_ostream &OS);
void printSuperclasses(const Record &Rec, raw_ostream &OS);
Expand All @@ -55,15 +56,15 @@ void DetailedRecordsEmitter::run(raw_ostream &OS) {
printVariables(OS);
printClasses(OS);
printRecords(OS);
printAllocationStats(OS);
}

// Print the report heading, including the source file name.
void DetailedRecordsEmitter::printReportHeading(raw_ostream &OS) {
OS << formatv("DETAILED RECORDS for file {0}\n", Records.getInputFilename());
}

// Print a section heading with the name of the section and
// the item count.
// Print a section heading with the name of the section and the item count.
void DetailedRecordsEmitter::printSectionHeading(StringRef Title, int Count,
raw_ostream &OS) {
OS << formatv("\n{0} {1} ({2}) {0}\n", "--------------------", Title, Count);
Expand All @@ -79,8 +80,7 @@ void DetailedRecordsEmitter::printVariables(raw_ostream &OS) {
OS << Var.first << " = " << Var.second->getAsString() << '\n';
}

// Print the classes, including the template arguments, superclasses,
// and fields.
// Print classes, including the template arguments, superclasses, and fields.
void DetailedRecordsEmitter::printClasses(raw_ostream &OS) {
const auto &ClassList = Records.getClasses();
printSectionHeading("Classes", ClassList.size(), OS);
Expand All @@ -94,8 +94,7 @@ void DetailedRecordsEmitter::printClasses(raw_ostream &OS) {
}
}

// Print the records, including the defm sequences, supercasses,
// and fields.
// Print the records, including the defm sequences, supercasses, and fields.
void DetailedRecordsEmitter::printRecords(raw_ostream &OS) {
const auto &RecordList = Records.getDefs();
printSectionHeading("Records", RecordList.size(), OS);
Expand All @@ -110,6 +109,12 @@ void DetailedRecordsEmitter::printRecords(raw_ostream &OS) {
}
}

// Print memory allocation related stats.
void DetailedRecordsEmitter::printAllocationStats(raw_ostream &OS) {
OS << formatv("\n{0} Memory Allocation Stats {0}\n", "--------------------");
Records.dumpAllocationStats(OS);
}

// Print the record's defm source locations, if any. Note that they
// are stored in the reverse order of their invocation.
void DetailedRecordsEmitter::printDefms(const Record &Rec, raw_ostream &OS) {
Expand Down
33 changes: 33 additions & 0 deletions llvm/lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,39 @@ struct RecordKeeperImpl {

unsigned AnonCounter;
unsigned LastRecordID;

void dumpAllocationStats(raw_ostream &OS) const;
};
} // namespace detail
} // namespace llvm

void detail::RecordKeeperImpl::dumpAllocationStats(raw_ostream &OS) const {
// Dump memory allocation related stats.
OS << "TheArgumentInitPool size = " << TheArgumentInitPool.size() << '\n';
OS << "TheBitsInitPool size = " << TheBitsInitPool.size() << '\n';
OS << "TheIntInitPool size = " << TheIntInitPool.size() << '\n';
OS << "TheBitsInitPool size = " << TheBitsInitPool.size() << '\n';
OS << "TheListInitPool size = " << TheListInitPool.size() << '\n';
OS << "TheUnOpInitPool size = " << TheUnOpInitPool.size() << '\n';
OS << "TheBinOpInitPool size = " << TheBinOpInitPool.size() << '\n';
OS << "TheTernOpInitPool size = " << TheTernOpInitPool.size() << '\n';
OS << "TheFoldOpInitPool size = " << TheFoldOpInitPool.size() << '\n';
OS << "TheIsAOpInitPool size = " << TheIsAOpInitPool.size() << '\n';
OS << "TheExistsOpInitPool size = " << TheExistsOpInitPool.size() << '\n';
OS << "TheCondOpInitPool size = " << TheCondOpInitPool.size() << '\n';
OS << "TheDagInitPool size = " << TheDagInitPool.size() << '\n';
OS << "RecordTypePool size = " << RecordTypePool.size() << '\n';
OS << "TheVarInitPool size = " << TheVarInitPool.size() << '\n';
OS << "TheVarBitInitPool size = " << TheVarBitInitPool.size() << '\n';
OS << "TheVarDefInitPool size = " << TheVarDefInitPool.size() << '\n';
OS << "TheFieldInitPool size = " << TheFieldInitPool.size() << '\n';
OS << "Bytes allocated = " << Allocator.getBytesAllocated() << '\n';
OS << "Total allocator memory = " << Allocator.getTotalMemory() << "\n\n";

OS << "Number of records instantiated = " << LastRecordID << '\n';
OS << "Number of anonymous records = " << AnonCounter << '\n';
}

//===----------------------------------------------------------------------===//
// Type implementations
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -3261,6 +3290,10 @@ RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) const {
: std::vector<Record *>();
}

void RecordKeeper::dumpAllocationStats(raw_ostream &OS) const {
Impl->dumpAllocationStats(OS);
}

Init *MapResolver::resolve(Init *VarName) {
auto It = Map.find(VarName);
if (It == Map.end())
Expand Down

0 comments on commit 30f1cfb

Please sign in to comment.