diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -6396,6 +6396,19 @@ !1 = !{i64 2, i64 -1, i64 -1, i1 true} !0 = !{!1} +'``exclude``' Metadata +^^^^^^^^^^^^^^^^^^^^^^^ + +``exclude`` metadata may be attached to a global variable to signify that its +section should not be included in the final executable or shared library. This +option is only valid for global variables with an explicit section targeting ELF +or COFF.This is done using the ``SHF_EXCLUDE`` flag on ELF targets and the +``IMAGE_SCN_LNK_REMOVE`` and ``IMAGE_SCN_MEM_DISCARDABLE`` flags for COFF +targets. + +.. code-block:: text + + @object = private constant [1 x i8] c"\00", section ".foo" !exclude !0 '``unpredictable``' Metadata ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/llvm/include/llvm/IR/FixedMetadataKinds.def b/llvm/include/llvm/IR/FixedMetadataKinds.def --- a/llvm/include/llvm/IR/FixedMetadataKinds.def +++ b/llvm/include/llvm/IR/FixedMetadataKinds.def @@ -44,3 +44,4 @@ LLVM_FIXED_MD_KIND(MD_annotation, "annotation", 30) LLVM_FIXED_MD_KIND(MD_nosanitize, "nosanitize", 31) LLVM_FIXED_MD_KIND(MD_func_sanitize, "func_sanitize", 32) +LLVM_FIXED_MD_KIND(MD_exclude, "exclude", 33) diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -449,9 +449,6 @@ Name == ".llvmbc" || Name == ".llvmcmd") return SectionKind::getMetadata(); - if (Name == ".llvm.offloading") - return SectionKind::getExclude(); - if (Name.empty() || Name[0] != '.') return K; // Default implementation based on some magic section names. diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp --- a/llvm/lib/Target/TargetLoweringObjectFile.cpp +++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp @@ -240,6 +240,10 @@ return SectionKind::getBSS(); } + // Global variables with '!exclude' should get the exclude section kind. + if (GVar->hasSection() && GVar->hasMetadata(LLVMContext::MD_exclude)) + return SectionKind::getExclude(); + // If the global is marked constant, we can put it into a mergable section, // a mergable string section, or general .data if it contains relocations. if (GVar->isConstant()) { diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp --- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp +++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp @@ -281,6 +281,7 @@ MDString::get(Ctx, SectionName)}; MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); + GV->setMetadata(LLVMContext::MD_exclude, llvm::MDNode::get(Ctx, MDVals)); appendToCompilerUsed(M, GV); } diff --git a/llvm/test/CodeGen/X86/coff-exclude.ll b/llvm/test/CodeGen/X86/coff-exclude.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/coff-exclude.ll @@ -0,0 +1,18 @@ +; RUN: llc -mtriple x86_64-win32-gnu < %s | FileCheck %s + +@a = global i32 1 +@b = global i32 1, !exclude !0 +@c = global i32 1, section "aaa" +; CHECK-DAG: c +; CHECK-DAG: .section aaa,"dw" +@d = global i32 1, section "bbb", !exclude !0 +; CHECK-DAG: d +; CHECK-DAG: .section bbb,"ynD" +@e = global i32 1, section "bbb", !exclude !0 +; CHECK-DAG: e +@f = global i32 1, section "ccc", !exclude !0 +@g = global i32 1, section "ccc" +; CHECK-DAG: f +; CHECK-DAG: .section ccc,"ynD" + +!0 = !{ptr null} diff --git a/llvm/test/CodeGen/X86/elf-exclude.ll b/llvm/test/CodeGen/X86/elf-exclude.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/elf-exclude.ll @@ -0,0 +1,18 @@ +; RUN: llc -mtriple x86_64-pc-linux-gnu < %s | FileCheck %s + +@a = global i32 1 +@b = global i32 1, !exclude !0 +@c = global i32 1, section "aaa" +; CHECK-DAG: .type c,@object +; CHECK-DAG: .section aaa,"aw",@progbits +@d = global i32 1, section "bbb", !exclude !0 +; CHECK-DAG: .type d,@object +; CHECK-DAG: .section bbb,"e",@progbits +@e = global i32 1, section "bbb", !exclude !0 +; CHECK-DAG: .type e,@object +@f = global i32 1, section "ccc", !exclude !0 +@g = global i32 1, section "ccc" +; CHECK-DAG: .type f,@object +; CHECK-DAG: .section ccc,"e",@progbits + +!0 = !{ptr null} diff --git a/llvm/test/CodeGen/X86/offload_sections.ll b/llvm/test/CodeGen/X86/offload_sections.ll --- a/llvm/test/CodeGen/X86/offload_sections.ll +++ b/llvm/test/CodeGen/X86/offload_sections.ll @@ -1,8 +1,10 @@ ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s --check-prefix=CHECK-ELF ; RUN: llc < %s -mtriple=x86_64-win32-gnu | FileCheck %s --check-prefix=CHECK-COFF -@llvm.embedded.object = private constant [1 x i8] c"\00", section ".llvm.offloading" +@llvm.embedded.object = private constant [1 x i8] c"\00", section ".llvm.offloading", align 8, !exclude !0 @llvm.compiler.used = appending global [1 x ptr] [ptr @llvm.embedded.object], section "llvm.metadata" +!0 = !{ptr @llvm.embedded.object, !".llvm.offloading"} + ; CHECK-ELF: .section .llvm.offloading,"e",@llvm_offloading -; CHECK-COFF: .section .llvm.offloading,"dr" +; CHECK-COFF: .section .llvm.offloading,"ynD"