[llvm] [ThinLTO] Only replace uses in ExportM when promoting CFI functions (PR #229634)
Mircea Trofin via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 19:00:51 PDT 2026
https://github.com/mtrofin created https://github.com/llvm/llvm-project/pull/229634
None
>From f887d1eb720b216cf5611e138338cf37965619c2 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Tue, 6 Oct 2026 16:07:29 -0700
Subject: [PATCH] [ThinLTO] Only replace uses in ExportM when promoting CFI
functions
---
.../Transforms/IPO/ThinLTOBitcodeWriter.cpp | 4 +--
.../X86/cfi-import-internal-coroutine.ll | 3 +-
.../cfi-relative-vtable.ll | 36 +++++++++++++++++++
3 files changed, 39 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/Transforms/ThinLTOBitcodeWriter/cfi-relative-vtable.ll
diff --git a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
index d9ec0a51f942f8..0010d94a17d696 100644
--- a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -76,10 +76,10 @@ void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
ExportGV.getValueType(), ExportGV.getAddressSpace(),
GlobalValue::ExternalLinkage, NewName, Aliasee, &ExportM);
ExternalAlias->setVisibility(GlobalValue::HiddenVisibility);
- ExportGV.replaceUsesWithIf(
- ExternalAlias, [](Use &U) { return !isa<GlobalAlias>(U.getUser()); });
if (MustPromote) {
+ ExportGV.replaceUsesWithIf(
+ ExternalAlias, [](Use &U) { return !isa<GlobalAlias>(U.getUser()); });
PromoteExtra->remove(&ExportGV);
PromoteExtra->insert(ExternalAlias);
}
diff --git a/llvm/test/ThinLTO/X86/cfi-import-internal-coroutine.ll b/llvm/test/ThinLTO/X86/cfi-import-internal-coroutine.ll
index 622db2b6a8f6fe..3fdf6a2a2423c8 100644
--- a/llvm/test/ThinLTO/X86/cfi-import-internal-coroutine.ll
+++ b/llvm/test/ThinLTO/X86/cfi-import-internal-coroutine.ll
@@ -21,8 +21,7 @@
; RUN: llvm-dis %t.dir/out.2.3.import.bc -o - | FileCheck %s --check-prefix=IMPORT
; IMPORT: define available_externally hidden ptr @coro.llvm.{{[0-9]+}}()
-; IMPORT-NEXT: %id = call token @llvm.coro.id(i32 8, ptr null, ptr nonnull @coro.b66cc3a748330e63b2451fa89522eb06, ptr null)
-; IMPORT: declare ptr @coro.b66cc3a748330e63b2451fa89522eb06()
+; IMPORT-NEXT: %id = call token @llvm.coro.id(i32 8, ptr null, ptr nonnull @coro.llvm.{{[0-9]+}}, ptr null)
;--- a.ll
source_filename = "a.ll"
diff --git a/llvm/test/Transforms/ThinLTOBitcodeWriter/cfi-relative-vtable.ll b/llvm/test/Transforms/ThinLTOBitcodeWriter/cfi-relative-vtable.ll
new file mode 100644
index 00000000000000..c765f5eb697236
--- /dev/null
+++ b/llvm/test/Transforms/ThinLTOBitcodeWriter/cfi-relative-vtable.ll
@@ -0,0 +1,36 @@
+; REQUIRES: x86-registered-target
+
+; Verify that promoting an internal relative vtable in ThinLTOBitcodeWriter
+; does not replace the vtable's self-reference in its own initializer with the
+; promoted alias, which would break AsmPrinter lowering of dso_local_equivalent.
+
+; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t %s
+; RUN: llvm-modextract -b -n 0 -o - %t | llvm-dis | FileCheck --check-prefix=M0 %s
+; RUN: llvm-modextract -b -n 1 -o %t.m1.bc %t
+; RUN: llvm-dis -o - %t.m1.bc | FileCheck --check-prefix=M1 %s
+; RUN: llc -filetype=null %t.m1.bc
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; M0: @rel_vtable.{{[0-9a-f]+}} = external hidden constant [1 x i32]
+; M0: @f.{{[0-9a-f]+}} = hidden alias void (), ptr @f
+; M0: define internal void @f()
+; M0: define ptr @use_rel()
+; M0-NEXT: ret ptr @rel_vtable.{{[0-9a-f]+}}
+
+; M1: @rel_vtable = internal constant [1 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @f.{{[0-9a-f]+}} to i64), i64 ptrtoint (ptr @rel_vtable to i64)) to i32)]
+; M1: @rel_vtable.{{[0-9a-f]+}} = hidden alias [1 x i32], ptr @rel_vtable
+; M1: declare !guid !{{[0-9]+}} hidden void @f.{{[0-9a-f]+}}()
+ at rel_vtable = internal constant [1 x i32] [
+ i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @f to i64), i64 ptrtoint (ptr @rel_vtable to i64)) to i32)
+], !type !0
+
+define internal void @f() {
+ ret void
+}
+
+define ptr @use_rel() {
+ ret ptr @rel_vtable
+}
+
+!0 = !{i32 0, !"typeid"}
More information about the llvm-commits
mailing list