From 2c5cf70088a621b727759e061e22407bc96b75eb Mon Sep 17 00:00:00 2001 From: Haohai Wen Date: Mon, 4 Aug 2025 15:04:07 +0800 Subject: [PATCH 1/2] [X86] Set .llvmbc and .llvmcmd to exclude sections The linked .llvmbc and .llvmcmd sections are meaningless in the final binary. They can occupy significant space and should be simply discarded. This patch sets them to exclude section kind for ELF and COFF. Wasm does not support exclude kind, so those sections are retained as metadata and they will be dropped by linker explicitly. --- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 13 +++++++++---- llvm/test/CodeGen/X86/embed-bitcode.ll | 6 ++++-- llvm/test/LTO/X86/embed-bitcode.ll | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index d19ef923ef740..1220bed503fc1 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -466,10 +466,12 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { Name == getInstrProfSectionName(IPSK_covdata, Triple::ELF, /*AddSegmentInfo=*/false) || Name == getInstrProfSectionName(IPSK_covname, Triple::ELF, - /*AddSegmentInfo=*/false) || - Name == ".llvmbc" || Name == ".llvmcmd") + /*AddSegmentInfo=*/false)) return SectionKind::getMetadata(); + if (Name == ".llvmbc" || Name == ".llvmcmd") + return SectionKind::getExclude(); + if (!Name.starts_with(".")) return K; // Default implementation based on some magic section names. @@ -1735,9 +1737,12 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( Name == getInstrProfSectionName(IPSK_covdata, Triple::COFF, /*AddSegmentInfo=*/false) || Name == getInstrProfSectionName(IPSK_covname, Triple::COFF, - /*AddSegmentInfo=*/false) || - Name == ".llvmbc" || Name == ".llvmcmd") + /*AddSegmentInfo=*/false)) Kind = SectionKind::getMetadata(); + + if (Name == ".llvmbc" || Name == ".llvmcmd") + Kind = SectionKind::getExclude(); + int Selection = 0; unsigned Characteristics = getCOFFSectionFlags(Kind, TM); StringRef COMDATSymName = ""; diff --git a/llvm/test/CodeGen/X86/embed-bitcode.ll b/llvm/test/CodeGen/X86/embed-bitcode.ll index d4af9544bc1be..7b08c69926d8a 100644 --- a/llvm/test/CodeGen/X86/embed-bitcode.ll +++ b/llvm/test/CodeGen/X86/embed-bitcode.ll @@ -4,17 +4,19 @@ ; RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=COFF ; CHECK: .text PROGBITS 0000000000000000 [[#%x,OFF:]] 000000 00 AX 0 -; CHECK-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] 000004 00 0 -; CHECK-NEXT: .llvmcmd PROGBITS 0000000000000000 [[#%x,OFF:]] 000005 00 0 +; CHECK-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] 000004 00 E 0 +; CHECK-NEXT: .llvmcmd PROGBITS 0000000000000000 [[#%x,OFF:]] 000005 00 E 0 ; COFF: Name: .llvmbc (2E 6C 6C 76 6D 62 63 00) ; COFF: Characteristics [ ; COFF-NEXT: IMAGE_SCN_ALIGN_1BYTES +; COFF-NEXT: IMAGE_SCN_LNK_REMOVE ; COFF-NEXT: IMAGE_SCN_MEM_DISCARDABLE ; COFF-NEXT: ] ; COFF: Name: .llvmcmd (2E 6C 6C 76 6D 63 6D 64) ; COFF: Characteristics [ ; COFF-NEXT: IMAGE_SCN_ALIGN_1BYTES +; COFF-NEXT: IMAGE_SCN_LNK_REMOVE ; COFF-NEXT: IMAGE_SCN_MEM_DISCARDABLE ; COFF-NEXT: ] diff --git a/llvm/test/LTO/X86/embed-bitcode.ll b/llvm/test/LTO/X86/embed-bitcode.ll index bdddd079d2265..d8ebbdf85bc32 100644 --- a/llvm/test/LTO/X86/embed-bitcode.ll +++ b/llvm/test/LTO/X86/embed-bitcode.ll @@ -19,7 +19,7 @@ ; RUN: llvm-dis %t-embedded.bc -o - | FileCheck %s --check-prefixes=CHECK-LL,CHECK-NOOPT ; CHECK-ELF: .text PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 AX 0 -; CHECK-ELF-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 0 +; CHECK-ELF-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 E 0 ; CHECK-LL: @_start ; CHECK-LL: @foo From a395b7467dead2821ecd56467d19e5e4e26eaaed Mon Sep 17 00:00:00 2001 From: Haohai Wen Date: Mon, 4 Aug 2025 16:24:45 +0800 Subject: [PATCH 2/2] Fix failed clang test --- clang/test/CodeGen/thinlto_embed_bitcode.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CodeGen/thinlto_embed_bitcode.ll b/clang/test/CodeGen/thinlto_embed_bitcode.ll index 590cadbfd418b..df25bf8a6c33d 100644 --- a/clang/test/CodeGen/thinlto_embed_bitcode.ll +++ b/clang/test/CodeGen/thinlto_embed_bitcode.ll @@ -34,7 +34,7 @@ ; RUN: diff %t-redo.o %t.o ; CHECK-ELF: .text PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 AX 0 -; CHECK-ELF-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 0 +; CHECK-ELF-NEXT: .llvmbc PROGBITS 0000000000000000 [[#%x,OFF:]] [[#%x,SIZE:]] 00 E 0 ; CHECK-ELF-CMD: .llvmcmd ; CHECK-ELF-NO-CMD-NOT: .llvmcmd