[llvm] [llvm][ELF] Add statistics on various section sizes (PR #102363)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 14:13:31 PDT 2024


https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/102363

>From a7c81b13ec998b51b9b1e420f10fecb95e43f835 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 7 Aug 2024 20:22:35 +0000
Subject: [PATCH 1/4] [llvm][ELF] Add statistics on various section sizes

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.
---
 llvm/lib/MC/ELFObjectWriter.cpp        | 50 ++++++++++++++++++++++++--
 llvm/test/CodeGen/X86/section-stats.ll |  7 ++++
 2 files changed, 55 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/section-stats.ll

diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index c40a074137ab2d..4d871e4c4bf5e0 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 00000000000000..26d9cf2d60676e
--- /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
+}

>From 7eda54ec956ee16b549ffc7d42d110e0f472ff6a Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Thu, 8 Aug 2024 15:47:16 +0000
Subject: [PATCH 2/4] fix test

---
 llvm/test/CodeGen/X86/section-stats.ll | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/test/CodeGen/X86/section-stats.ll b/llvm/test/CodeGen/X86/section-stats.ll
index 26d9cf2d60676e..0ce57fd459ee44 100644
--- a/llvm/test/CodeGen/X86/section-stats.ll
+++ b/llvm/test/CodeGen/X86/section-stats.ll
@@ -1,7 +1,10 @@
+; REQUIRES: asserts
 ; 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
 
+target triple = "x86_64-unknown-linux-gnu"
+
 define void @f() {
     ret void
 }

>From 2a17f8d87ce8035f347de47aafecd649a31a2062 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Thu, 8 Aug 2024 20:06:16 +0000
Subject: [PATCH 3/4] address comments

---
 llvm/lib/MC/ELFObjectWriter.cpp | 38 +++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 4d871e4c4bf5e0..7a8a6c8daf9593 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -968,23 +968,11 @@ void ELFWriter::writeSectionHeader(const MCAssembler &Asm) {
     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")) {
+    if (Section->getName().starts_with(".debug")) {
       stats::DebugBytes += Size;
+    } else if (Section->getName().starts_with(".eh_frame")) {
+      stats::UnwindBytes += Size;
     } else if (SectionHasFlag(ELF::SHF_ALLOC)) {
       if (SectionHasFlag(ELF::SHF_EXECINSTR)) {
         stats::AllocTextBytes += Size;
@@ -994,7 +982,25 @@ void ELFWriter::writeSectionHeader(const MCAssembler &Asm) {
         stats::AllocROBytes += Size;
       }
     } else {
-      stats::OtherBytes += Size;
+      switch (Section->getType()) {
+      case ELF::SHT_STRTAB:
+        stats::StrtabBytes += Size;
+        break;
+      case ELF::SHT_SYMTAB:
+        stats::SymtabBytes += Size;
+        break;
+      case ELF::SHT_DYNSYM:
+        stats::DynsymBytes += Size;
+        break;
+      case ELF::SHT_REL:
+      case ELF::SHT_RELA:
+      case ELF::SHT_CREL:
+        stats::RelocationBytes += Size;
+        break;
+      default:
+        stats::OtherBytes += Size;
+        break;
+      }
     }
 
     writeSection(GroupSymbolIndex, Offsets.first, Size, *Section);

>From 8bdc8786e1f0996378ebc63024aa732b8f836689 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Thu, 8 Aug 2024 21:13:10 +0000
Subject: [PATCH 4/4] address test comment

---
 llvm/test/CodeGen/X86/section-stats.ll | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/X86/section-stats.ll b/llvm/test/CodeGen/X86/section-stats.ll
index 0ce57fd459ee44..94d0a965ac59ee 100644
--- a/llvm/test/CodeGen/X86/section-stats.ll
+++ b/llvm/test/CodeGen/X86/section-stats.ll
@@ -1,10 +1,13 @@
 ; REQUIRES: asserts
 ; 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
+; CHECK-DAG: 1 elf-object-writer - Total size of SHF_ALLOC text sections
+; CHECK-DAG: 1 elf-object-writer - Total size of SHF_ALLOC read-write sections
 
 target triple = "x86_64-unknown-linux-gnu"
 
+ at g = global i8 1
+
 define void @f() {
     ret void
 }



More information about the llvm-commits mailing list