https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/188925
None >From 80d3245a77389a4bc1c9205a4ba92bd9dfdc07e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> 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 | 38 +++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/ByteCode/Disasm.cpp b/clang/lib/AST/ByteCode/Disasm.cpp index 5fd15f8a2f0d1..6bb78bb45719c 100644 --- a/clang/lib/AST/ByteCode/Disasm.cpp +++ b/clang/lib/AST/ByteCode/Disasm.cpp @@ -313,6 +313,23 @@ 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::formatv("{0:F2}", B / 1024.) << " KB"; + else if (B < (1u << 30u)) + SS << llvm::formatv("{0:F2}", B / 1024. / 1024.) << " MB"; + else if (B < (static_cast<size_t>(1) << 40u)) + SS << llvm::formatv("{0:F2}", B / 1024. / 1024. / 1024.) << " GB"; + else + SS << B << " B"; + 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 +338,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
