[clang] [clang][bytecode] Make memory output of Program::dump more accurate (PR #188925)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 27 05:01:27 PDT 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/188925

>From b8d5273f83c15c1b406de5fbc9352607d0b49de1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 27 Mar 2026 09:32:39 +0100
Subject: [PATCH] [clang][bytecode] Make memory output of Program::dump more
 accurate

---
 clang/lib/AST/ByteCode/Disasm.cpp | 35 +++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Disasm.cpp b/clang/lib/AST/ByteCode/Disasm.cpp
index 5fd15f8a2f0d1..6caa33261dad6 100644
--- a/clang/lib/AST/ByteCode/Disasm.cpp
+++ b/clang/lib/AST/ByteCode/Disasm.cpp
@@ -313,6 +313,20 @@ static const char *primTypeToString(PrimType T) {
   llvm_unreachable("Unhandled PrimType");
 }
 
+static std::string formatBytes(size_t B) {
+  std::string Result;
+  llvm::raw_string_ostream SS(Result);
+
+  if (B < (1u << 10u))
+    SS << B << " B";
+  else if (B < (1u << 20u))
+    SS << llvm::format("{0:F2}", B / 1024.) << " KB";
+  else
+    SS << llvm::format("{0:F2}", B / 1024. / 1024.) << " MB";
+
+  return Result;
+}
+
 LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
   {
     ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
@@ -321,8 +335,25 @@ LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
 
   {
     ColorScope SC(OS, true, {llvm::raw_ostream::WHITE, true});
-    OS << "Total memory : " << Allocator.getTotalMemory() << " bytes\n";
-    OS << "Global Variables: " << Globals.size() << "\n";
+    size_t Bytes = 0;
+    Bytes += Allocator.getTotalMemory();
+    // All the maps.
+    Bytes += GlobalIndices.getMemorySize();
+    Bytes += Records.getMemorySize();
+    Bytes += DummyVariables.getMemorySize();
+
+    // All Records.
+    for (const Record *R : Records.values()) {
+      Bytes += sizeof(Record) + R->BaseMap.getMemorySize() +
+               R->VirtualBaseMap.getMemorySize();
+      Bytes += R->Fields.capacity_in_bytes() + R->Bases.capacity_in_bytes() +
+               R->VirtualBases.capacity_in_bytes();
+    }
+
+    // Globals are allocated via the allocator, so already counted.
+
+    OS << "Total memory : " << formatBytes(Bytes) << '\n';
+    OS << "Global Variables: " << Globals.size() << '\n';
   }
   unsigned GI = 0;
   for (const Global *G : Globals) {



More information about the cfe-commits mailing list