[PATCH] D129151: [Metadata] Add 'exclude' metadata to add the exclude flags on globals

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 5 10:16:16 PDT 2022


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, tianshilei1992, MaskRay, yaxunl, tra, saiislam, alexander-shaposhnikov, jhenderson, JonChesterfield.
Herald added subscribers: jsji, StephenFan, pengfei, hiraditya.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patchs adds a new metadata kind `exclude` which implies that the
global variable should be given the necessary flags during code
generation to not be included in the final executable. This is done
using the ``SHF_EXCLUDE`` flag on ELF for example. This should make it
easier to specify this flag on a variable without needing to explicitly
check the section name in the target backend.

Depends on D129053 <https://reviews.llvm.org/D129053> D129052 <https://reviews.llvm.org/D129052>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129151

Files:
  llvm/docs/LangRef.rst
  llvm/include/llvm/IR/FixedMetadataKinds.def
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/lib/Target/TargetLoweringObjectFile.cpp
  llvm/lib/Transforms/Utils/ModuleUtils.cpp
  llvm/test/CodeGen/X86/offload_sections.ll


Index: llvm/test/CodeGen/X86/offload_sections.ll
===================================================================
--- llvm/test/CodeGen/X86/offload_sections.ll
+++ 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
 
- at llvm.embedded.object = private constant [1 x i8] c"\00", section ".llvm.offloading"
+ at 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", at llvm_offloading
-; CHECK-COFF: .section	.llvm.offloading,"dr"
+; CHECK-COFF: .section	.llvm.offloading,"ynD"
Index: llvm/lib/Transforms/Utils/ModuleUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ 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);
 }
Index: llvm/lib/Target/TargetLoweringObjectFile.cpp
===================================================================
--- llvm/lib/Target/TargetLoweringObjectFile.cpp
+++ llvm/lib/Target/TargetLoweringObjectFile.cpp
@@ -213,6 +213,9 @@
   // Global variables require more detailed analysis.
   const auto *GVar = cast<GlobalVariable>(GO);
 
+  if (GVar->hasSection() && GVar->hasMetadata(LLVMContext::MD_exclude))
+    return SectionKind::getExclude();
+
   // Handle thread-local data first.
   if (GVar->isThreadLocal()) {
     if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ 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.
Index: llvm/include/llvm/IR/FixedMetadataKinds.def
===================================================================
--- llvm/include/llvm/IR/FixedMetadataKinds.def
+++ 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)
Index: llvm/docs/LangRef.rst
===================================================================
--- llvm/docs/LangRef.rst
+++ llvm/docs/LangRef.rst
@@ -6396,6 +6396,18 @@
     !1 = !{i64 2, i64 -1, i64 -1, i1 true}
     !0 = !{!1}
 
+'``exclude``' Metadata
+^^^^^^^^^^^^^^^^^^^^^^^
+
+``exclude`` metadata may be attached to a global variables to signify that it
+should not be included in the final executable or shared library. 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. This option is only
+valid for global variables with an explicit section targeting ELF or COFF.
+
+.. code-block:: text
+
+  @object = private constant [1 x i8] c"\00", section ".foo" !exclude !0
 
 '``unpredictable``' Metadata
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129151.442344.patch
Type: text/x-patch
Size: 3779 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220705/62c52ab5/attachment.bin>


More information about the llvm-commits mailing list