Skip to content

Avoid emitting a linker options section in the compiler if it is empty. #139821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dyung
Copy link
Collaborator

@dyung dyung commented May 14, 2025

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:modules C++20 modules and Clang Header Modules clang:codegen IR generation bugs: mangling, exceptions, etc. labels May 14, 2025
@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang-codegen

Author: None (dyung)

Changes

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 --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 =

@nga888 nga888 requested a review from pcc May 14, 2025 12:01
@dyung
Copy link
Collaborator Author

dyung commented May 16, 2025

Hi @pcc, I believe you originally wrote the code that is being modified, would you mind taking a quick look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants