[llvm] [TableGen] Print memory stats in detailed record emitter (PR #106990)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 05:25:10 PDT 2024


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/106990

>From 4ffc61ee91a31e1a54f6c3150fe6cb9f00729495 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 2 Sep 2024 06:35:21 -0700
Subject: [PATCH] [TableGen] Print memory stats in detailed record emitter

Print memory allocation and related statistics when dumping detailed
record information.
---
 llvm/include/llvm/TableGen/Record.h          |  4 ++-
 llvm/lib/TableGen/DetailedRecordsBackend.cpp | 17 ++++++----
 llvm/lib/TableGen/Record.cpp                 | 33 ++++++++++++++++++++
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h
index a339946e67cf2d..ff596df94e4f5a 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -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;
   }
 
@@ -2073,6 +2073,8 @@ class RecordKeeper {
 
   void dump() const;
 
+  void dumpAllocationStats(raw_ostream &OS) const;
+
 private:
   RecordKeeper(RecordKeeper &&) = delete;
   RecordKeeper(const RecordKeeper &) = delete;
diff --git a/llvm/lib/TableGen/DetailedRecordsBackend.cpp b/llvm/lib/TableGen/DetailedRecordsBackend.cpp
index 45e621483c8173..7d17c4d68c3f12 100644
--- a/llvm/lib/TableGen/DetailedRecordsBackend.cpp
+++ b/llvm/lib/TableGen/DetailedRecordsBackend.cpp
@@ -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);
@@ -55,6 +56,7 @@ void DetailedRecordsEmitter::run(raw_ostream &OS) {
   printVariables(OS);
   printClasses(OS);
   printRecords(OS);
+  printAllocationStats(OS);
 }
 
 // Print the report heading, including the source file name.
@@ -62,8 +64,7 @@ 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);
@@ -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);
@@ -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);
@@ -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) {
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index bcecee8e550c88..cead8f865a6079 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -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
 //===----------------------------------------------------------------------===//
@@ -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())



More information about the llvm-commits mailing list