Author: Timm Baeder
Date: 2026-03-27T15:33:27+01:00
New Revision: 5f09e24acbe634b724c93c3a8ccd1957b8f585c8

URL: 
https://github.com/llvm/llvm-project/commit/5f09e24acbe634b724c93c3a8ccd1957b8f585c8
DIFF: 
https://github.com/llvm/llvm-project/commit/5f09e24acbe634b724c93c3a8ccd1957b8f585c8.diff

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

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Disasm.cpp

Removed: 
    


################################################################################
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) {


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to