[clang] Avoid emitting a linker options section in the compiler if it is empty. (PR #139821)

via cfe-commits cfe-commits at lists.llvm.org
Tue May 13 18:10:13 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang-codegen

Author: None (dyung)

<details>
<summary>Changes</summary>

Recently in some of our internal testing, we noticed that the compiler was sometimes generating an empty linker.options section which seems unnecessary. This proposed change causes the compiler to simply omit emitting the linker.options section if it is empty.

---
Full diff: https://github.com/llvm/llvm-project/pull/139821.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+5-3) 
- (modified) clang/test/Modules/module-impl-with-link.c (+1-1) 


``````````diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 50041f883cfe5..3bf52428130f7 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3274,9 +3274,11 @@ void CodeGenModule::EmitModuleLinkOptions() {
   LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
 
   // Add the linker options metadata flag.
-  auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
-  for (auto *MD : LinkerOptionsMetadata)
-    NMD->addOperand(MD);
+  if (!LinkerOptionsMetadata.empty()) {
+    auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
+    for (auto *MD : LinkerOptionsMetadata)
+      NMD->addOperand(MD);
+  }
 }
 
 void CodeGenModule::EmitDeferred() {
diff --git a/clang/test/Modules/module-impl-with-link.c b/clang/test/Modules/module-impl-with-link.c
index ffd388c56b90a..be2f1359b5e94 100644
--- a/clang/test/Modules/module-impl-with-link.c
+++ b/clang/test/Modules/module-impl-with-link.c
@@ -3,4 +3,4 @@
 #include "foo.h"
 // Make sure we don't generate linker option for module Clib since this TU is
 // an implementation of Clib.
-// CHECK: !llvm.linker.options = !{}
+// CHECK-NOT: !llvm.linker.options =

``````````

</details>


https://github.com/llvm/llvm-project/pull/139821


More information about the cfe-commits mailing list