Skip to content

[clang][win] Fix __global_delete breaking __attribute__((used)) - #217753

Merged
dpaoliello merged 1 commit into
llvm:mainfrom
dpaoliello:fixusedglobaldel
Aug 21, 2026
Merged

dpaoliello merged 1 commit into
llvm:mainfrom
dpaoliello:fixusedglobaldel

Conversation

@dpaoliello

Copy link
Copy Markdown
Contributor

The __empty_global_delete fallback introduced in #209585 was marked used via llvm::appendToUsed(), which creates a global named "llvm.used" during codegen. CodeGenModule::emitLLVMUsed() later unconditionally creates its own global of that same name at end-of-TU, so the name collision renamed the latter to "llvm.used.1" -- a name LLVM ignores. The result was that every __attribute__((used)) global in the TU silently lost its used semantics whenever a __global_delete wrapper was emitted, letting those symbols be dropped.

Mark the fallback used via CodeGenModule::addUsedGlobal() instead, so it joins the single llvm.used that emitLLVMUsed() emits. emitLLVMUsed() runs well after emitGlobalDeleteForwardingBodies() in Release(), so the fallback is still recorded in time.

Fixes a regression reported on #209585.

The `__empty_global_delete` fallback introduced in llvm#209585 was marked used via
`llvm::appendToUsed()`, which creates a global named "llvm.used" during codegen.
`CodeGenModule::emitLLVMUsed()` later unconditionally creates its own global of
that same name at end-of-TU, so the name collision renamed the latter to
"llvm.used.1" -- a name LLVM ignores. The result was that every
`__attribute__((used))` global in the TU silently lost its used semantics
whenever a `__global_delete` wrapper was emitted, letting those symbols be
dropped.

Mark the fallback used via `CodeGenModule::addUsedGlobal()` instead, so it joins
the single llvm.used that `emitLLVMUsed()` emits. `emitLLVMUsed()` runs well after
`emitGlobalDeleteForwardingBodies()` in `Release()`, so the fallback is still
recorded in time.

Fixes a regression reported on llvm#209585.
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Aug 20, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Daniel Paoliello (dpaoliello)

Changes

The __empty_global_delete fallback introduced in #209585 was marked used via llvm::appendToUsed(), which creates a global named "llvm.used" during codegen. CodeGenModule::emitLLVMUsed() later unconditionally creates its own global of that same name at end-of-TU, so the name collision renamed the latter to "llvm.used.1" -- a name LLVM ignores. The result was that every __attribute__((used)) global in the TU silently lost its used semantics whenever a __global_delete wrapper was emitted, letting those symbols be dropped.

Mark the fallback used via CodeGenModule::addUsedGlobal() instead, so it joins the single llvm.used that emitLLVMUsed() emits. emitLLVMUsed() runs well after emitGlobalDeleteForwardingBodies() in Release(), so the fallback is still recorded in time.

Fixes a regression reported on #209585.


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+1-1)
  • (added) clang/test/CodeGenCXX/msvc-global-delete-llvm-used.cpp (+17)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index d348e80780ee9..8e7bfd336e631 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -9100,7 +9100,7 @@ CodeGenModule::getOrCreateMSVCGlobalDeleteWrapper(const FunctionDecl *GlobOD) {
     // uses ::delete that alias is replaced by a real forwarding body, leaving
     // the empty otherwise unreferenced, so explicitly mark it used to ensure
     // it is always emitted (matching MSVC).
-    appendToUsed(M, {EmptyFn});
+    addUsedGlobal(EmptyFn);
   }
 
   // The wrapper defaults to a weak alias to the trapping __empty_global_delete
diff --git a/clang/test/CodeGenCXX/msvc-global-delete-llvm-used.cpp b/clang/test/CodeGenCXX/msvc-global-delete-llvm-used.cpp
new file mode 100644
index 0000000000000..07811b0f8eecc
--- /dev/null
+++ b/clang/test/CodeGenCXX/msvc-global-delete-llvm-used.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -emit-llvm -fms-extensions %s -triple=x86_64-pc-windows-msvc -o - \
+// RUN:   | FileCheck %s --implicit-check-not="@llvm.used.1"
+
+// The __empty_global_delete fallback is marked used so it is always emitted.
+// It must join the single llvm.used that CodeGenModule emits at end-of-TU: if
+// it creates its own llvm.used first, the one holding __attribute__((used))
+// globals gets renamed to llvm.used.1, which LLVM ignores.
+
+struct S { virtual ~S(); };
+S::~S() {}
+void del(S *s) { ::delete s; }
+
+__attribute__((used)) static void keep_me() {}
+
+// CHECK: @llvm.used = appending global
+// CHECK-SAME: @"?__empty_global_delete@@YAXPEAX_K@Z"
+// CHECK-SAME: @"?keep_me@@YAXXZ"

@dpaoliello
dpaoliello requested a review from nico August 20, 2026 20:42

@efriedma-quic efriedma-quic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dpaoliello
dpaoliello merged commit 061865f into llvm:main Aug 21, 2026
15 checks passed
@dpaoliello
dpaoliello deleted the fixusedglobaldel branch August 21, 2026 19:33
@dpaoliello

Copy link
Copy Markdown
Contributor Author

/cherry-pick 061865f

@llvmbot

llvmbot commented Aug 21, 2026

Copy link
Copy Markdown
Member

/cherry-pick 061865f

Error: Command failed due to missing milestone.

@dpaoliello

Copy link
Copy Markdown
Contributor Author

/cherry-pick 061865f

@llvmbot

llvmbot commented Aug 21, 2026

Copy link
Copy Markdown
Member

/pull-request #218040

dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 23, 2026
…#217753)

The `__empty_global_delete` fallback introduced in llvm#209585 was marked
used via `llvm::appendToUsed()`, which creates a global named
"llvm.used" during codegen. `CodeGenModule::emitLLVMUsed()` later
unconditionally creates its own global of that same name at end-of-TU,
so the name collision renamed the latter to "llvm.used.1" -- a name LLVM
ignores. The result was that every `__attribute__((used))` global in the
TU silently lost its used semantics whenever a `__global_delete` wrapper
was emitted, letting those symbols be dropped.

Mark the fallback used via `CodeGenModule::addUsedGlobal()` instead, so
it joins the single llvm.used that `emitLLVMUsed()` emits.
`emitLLVMUsed()` runs well after `emitGlobalDeleteForwardingBodies()` in
`Release()`, so the fallback is still recorded in time.

Fixes a regression reported on llvm#209585.

(cherry picked from commit 061865f)
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Aug 28, 2026
This contains a fix for a prior CL that provides an alternate
solution to the linked bug (returning to our prior behavior).
The behavior change will happen in a followup CL, this just
updates the package.

We also remove an old cherry-pick from last year that was a
no-op.

Bug: 542981236
Change-Id: I3c5f32fe49479df8f6b5b3fc99bce2572509b536
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8280057
Commit-Queue: Devon Loehr <[email protected]>
Reviewed-by: Nico Weber <[email protected]>
Commit-Queue: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1688204}
benji-cannot-code pushed a commit to benji-cannot-code/fast-vc-chromium-benchmark-run-4 that referenced this pull request Sep 25, 2026
This contains a fix for a prior CL that provides an alternate
solution to the linked bug (returning to our prior behavior).
The behavior change will happen in a followup CL, this just
updates the package.

We also remove an old cherry-pick from last year that was a
no-op.

Bug: 542981236
Change-Id: I3c5f32fe49479df8f6b5b3fc99bce2572509b536
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8280057
Commit-Queue: Devon Loehr <[email protected]>
Reviewed-by: Nico Weber <[email protected]>
Commit-Queue: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1688204}
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 Clang issues not falling into any other category

Projects

Development

Successfully merging this pull request may close these issues.

3 participants