[llvm] [llvm][ELF] Add statistics on various section sizes (PR #102363)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 7 13:34:25 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-mc
Author: Arthur Eubanks (aeubanks)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/102363.diff
2 Files Affected:
- (modified) llvm/lib/MC/ELFObjectWriter.cpp (+48-2)
- (added) llvm/test/CodeGen/X86/section-stats.ll (+7)
``````````diff
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index c40a074137ab2..4d871e4c4bf5e 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -14,6 +14,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
@@ -62,10 +63,23 @@
using namespace llvm;
-#undef DEBUG_TYPE
-#define DEBUG_TYPE "reloc-info"
+#define DEBUG_TYPE "elf-object-writer"
namespace {
+namespace stats {
+
+STATISTIC(AllocTextBytes, "Total size of SHF_ALLOC text sections");
+STATISTIC(AllocROBytes, "Total size of SHF_ALLOC readonly sections");
+STATISTIC(AllocRWBytes, "Total size of SHF_ALLOC read-write sections");
+STATISTIC(StrtabBytes, "Total size of SHT_STRTAB sections");
+STATISTIC(SymtabBytes, "Total size of SHT_SYMTAB sections");
+STATISTIC(RelocationBytes, "Total size of relocation sections");
+STATISTIC(DynsymBytes, "Total size of SHT_DYNSYM sections");
+STATISTIC(DebugBytes, "Total size of debug info sections");
+STATISTIC(UnwindBytes, "Total size of unwind sections");
+STATISTIC(OtherBytes, "Total size of uncategorized sections");
+
+} // namespace stats
struct ELFWriter;
@@ -951,6 +965,38 @@ void ELFWriter::writeSectionHeader(const MCAssembler &Asm) {
else
Size = Offsets.second - Offsets.first;
+ auto SectionHasFlag = [&](uint64_t Flag) -> bool {
+ return Section->getFlags() & Flag;
+ };
+ auto SectionIsType = [&](uint64_t Type) -> bool {
+ return Section->getType() == Type;
+ };
+
+ if (SectionIsType(ELF::SHT_STRTAB)) {
+ stats::StrtabBytes += Size;
+ } else if (SectionIsType(ELF::SHT_SYMTAB)) {
+ stats::SymtabBytes += Size;
+ } else if (SectionIsType(ELF::SHT_DYNSYM)) {
+ stats::DynsymBytes += Size;
+ } else if (SectionIsType(ELF::SHT_REL) || SectionIsType(ELF::SHT_RELA) ||
+ SectionIsType(ELF::SHT_RELR) || SectionIsType(ELF::SHT_CREL)) {
+ stats::RelocationBytes += Size;
+ } else if (SectionIsType(ELF::SHT_X86_64_UNWIND)) {
+ stats::UnwindBytes += Size;
+ } else if (Section->getName().starts_with(".debug")) {
+ stats::DebugBytes += Size;
+ } else if (SectionHasFlag(ELF::SHF_ALLOC)) {
+ if (SectionHasFlag(ELF::SHF_EXECINSTR)) {
+ stats::AllocTextBytes += Size;
+ } else if (SectionHasFlag(ELF::SHF_WRITE)) {
+ stats::AllocRWBytes += Size;
+ } else {
+ stats::AllocROBytes += Size;
+ }
+ } else {
+ stats::OtherBytes += Size;
+ }
+
writeSection(GroupSymbolIndex, Offsets.first, Size, *Section);
}
}
diff --git a/llvm/test/CodeGen/X86/section-stats.ll b/llvm/test/CodeGen/X86/section-stats.ll
new file mode 100644
index 0000000000000..26d9cf2d60676
--- /dev/null
+++ b/llvm/test/CodeGen/X86/section-stats.ll
@@ -0,0 +1,7 @@
+; RUN: llc -o /dev/null -filetype=obj -stats %s 2>&1 | FileCheck %s
+
+; CHECK: {{[0-9+]}} elf-object-writer - Total size of SHF_ALLOC text sections
+
+define void @f() {
+ ret void
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/102363
More information about the llvm-commits
mailing list