Skip to content

release/23.x: [clang][win] Fix __global_delete breaking __attribute__((used)) (#217753) - #218040

Merged
dyung merged 1 commit into
llvm:release/23.xfrom
llvmbot:issue217753
Aug 23, 2026
Merged

dyung merged 1 commit into
llvm:release/23.xfrom
llvmbot:issue217753

Conversation

@llvmbot

@llvmbot llvmbot commented Aug 21, 2026

Copy link
Copy Markdown
Member

Backport 061865f

Requested by: @dpaoliello

@llvmbot

llvmbot commented Aug 21, 2026

Copy link
Copy Markdown
Member Author

@efriedma-quic What do you think about merging this PR to the release branch?

@llvmbot
llvmbot requested a review from efriedma-quic August 21, 2026 21:59
@llvmorg-github-actions llvmorg-github-actions Bot added the clang Clang issues not falling into any other category label Aug 21, 2026
@llvmorg-github-actions llvmorg-github-actions Bot added the clang:codegen IR generation bugs: mangling, exceptions, etc. label Aug 21, 2026
@llvmorg-github-actions

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

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: llvmbot

Changes

Backport 061865f

Requested by: @dpaoliello


Full diff: https://github.com/llvm/llvm-project/pull/218040.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 de060e31b8800..313a364e80aff 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -9026,7 +9026,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 21, 2026 21:59

@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

@github-project-automation github-project-automation Bot moved this from Needs Triage to Needs Merge in LLVM Release Status Aug 21, 2026
@github-actions

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 99403 tests passed
  • 2999 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

libc

libc.src/sys/socket/linux/libc.test.src.sys.socket.linux.sockatmark_test.__build__
[32m[==========] [0mRunning 3 tests from 1 test suite.
[32m[ RUN      ] [0mLlvmLibcSockatmarkTest.SocketpairReturnsFalse
/home/gha/actions-runner/_work/llvm-project/llvm-project/libc/test/src/sys/socket/linux/sockatmark_test.cpp:37: FAILURE
Failed to match LIBC_NAMESPACE::sockatmark(sockpair[0]) against Succeeds(0).
Expected return value to be equal to 0 but got -1.
Expected errno to be equal to 0(0) but got ENOTSUP(95).
[31m[  FAILED  ] [0mLlvmLibcSockatmarkTest.SocketpairReturnsFalse
[32m[ RUN      ] [0mLlvmLibcSockatmarkTest.InvalidFdFails
[32m[       OK ] [0mLlvmLibcSockatmarkTest.InvalidFdFails (1 us)
[32m[ RUN      ] [0mLlvmLibcSockatmarkTest.NonSocketFdFails
[32m[       OK ] [0mLlvmLibcSockatmarkTest.NonSocketFdFails (12 us)
Ran 3 tests.  PASS: 2  FAIL: 1

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

…#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)
@dyung
dyung merged commit 190c84f into llvm:release/23.x Aug 23, 2026
2 of 4 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Merge to Done in LLVM Release Status Aug 23, 2026
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.

4 participants