Skip to content

Commit 1baa6f7

Browse files
authored
[llvm][ELF] Add statistics on various section sizes (#102363)
Useful with other infrastructure that consume LLVM statistics to get an idea of distribution of section sizes. The breakdown of various section types is subject to change, this is just an initial go at gather some sort of stats. Example stats compiling X86ISelLowering.cpp (-g1): ``` "elf-object-writer.AllocROBytes": 308268, "elf-object-writer.AllocRWBytes": 6240, "elf-object-writer.AllocTextBytes": 1659203, "elf-object-writer.DebugBytes": 3180386, "elf-object-writer.OtherBytes": 5862, "elf-object-writer.RelocationBytes": 2623440, "elf-object-writer.StrtabBytes": 228599, "elf-object-writer.SymtabBytes": 120336, "elf-object-writer.UnwindBytes": 85216, ```
1 parent ae059a1 commit 1baa6f7

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ADT/DenseMap.h"
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/SmallVector.h"
17+
#include "llvm/ADT/Statistic.h"
1718
#include "llvm/ADT/StringExtras.h"
1819
#include "llvm/ADT/StringRef.h"
1920
#include "llvm/ADT/Twine.h"
@@ -62,10 +63,23 @@
6263

6364
using namespace llvm;
6465

65-
#undef DEBUG_TYPE
66-
#define DEBUG_TYPE "reloc-info"
66+
#define DEBUG_TYPE "elf-object-writer"
6767

6868
namespace {
69+
namespace stats {
70+
71+
STATISTIC(AllocTextBytes, "Total size of SHF_ALLOC text sections");
72+
STATISTIC(AllocROBytes, "Total size of SHF_ALLOC readonly sections");
73+
STATISTIC(AllocRWBytes, "Total size of SHF_ALLOC read-write sections");
74+
STATISTIC(StrtabBytes, "Total size of SHT_STRTAB sections");
75+
STATISTIC(SymtabBytes, "Total size of SHT_SYMTAB sections");
76+
STATISTIC(RelocationBytes, "Total size of relocation sections");
77+
STATISTIC(DynsymBytes, "Total size of SHT_DYNSYM sections");
78+
STATISTIC(DebugBytes, "Total size of debug info sections");
79+
STATISTIC(UnwindBytes, "Total size of unwind sections");
80+
STATISTIC(OtherBytes, "Total size of uncategorized sections");
81+
82+
} // namespace stats
6983

7084
struct ELFWriter;
7185

@@ -951,6 +965,44 @@ void ELFWriter::writeSectionHeader(const MCAssembler &Asm) {
951965
else
952966
Size = Offsets.second - Offsets.first;
953967

968+
auto SectionHasFlag = [&](uint64_t Flag) -> bool {
969+
return Section->getFlags() & Flag;
970+
};
971+
972+
if (Section->getName().starts_with(".debug")) {
973+
stats::DebugBytes += Size;
974+
} else if (Section->getName().starts_with(".eh_frame")) {
975+
stats::UnwindBytes += Size;
976+
} else if (SectionHasFlag(ELF::SHF_ALLOC)) {
977+
if (SectionHasFlag(ELF::SHF_EXECINSTR)) {
978+
stats::AllocTextBytes += Size;
979+
} else if (SectionHasFlag(ELF::SHF_WRITE)) {
980+
stats::AllocRWBytes += Size;
981+
} else {
982+
stats::AllocROBytes += Size;
983+
}
984+
} else {
985+
switch (Section->getType()) {
986+
case ELF::SHT_STRTAB:
987+
stats::StrtabBytes += Size;
988+
break;
989+
case ELF::SHT_SYMTAB:
990+
stats::SymtabBytes += Size;
991+
break;
992+
case ELF::SHT_DYNSYM:
993+
stats::DynsymBytes += Size;
994+
break;
995+
case ELF::SHT_REL:
996+
case ELF::SHT_RELA:
997+
case ELF::SHT_CREL:
998+
stats::RelocationBytes += Size;
999+
break;
1000+
default:
1001+
stats::OtherBytes += Size;
1002+
break;
1003+
}
1004+
}
1005+
9541006
writeSection(GroupSymbolIndex, Offsets.first, Size, *Section);
9551007
}
9561008
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; REQUIRES: asserts
2+
; RUN: llc -o /dev/null -filetype=obj -stats %s 2>&1 | FileCheck %s
3+
4+
; CHECK-DAG: 1 elf-object-writer - Total size of SHF_ALLOC text sections
5+
; CHECK-DAG: 1 elf-object-writer - Total size of SHF_ALLOC read-write sections
6+
7+
target triple = "x86_64-unknown-linux-gnu"
8+
9+
@g = global i8 1
10+
11+
define void @f() {
12+
ret void
13+
}

0 commit comments

Comments
 (0)