[llvm] [ThinLTO] Don't convert functions to declarations if `force-import-all` is enabled (PR #134541)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 6 21:32:47 PDT 2025
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/134541
>From fd9f457f070e5572449481cdde77b4754576c346 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Mon, 7 Apr 2025 00:32:27 -0400
Subject: [PATCH] [ThinLTO] Don't convert functions to declarations if
`force-import-all` is enabled
On one hand, we intend to `force-import-all` functions when the option is
enabled. On the other hand, we currently drop definitions of some functions and
convert them to declarations, which contradicts this intent.
With this PR, functions will no longer be converted to declarations when
`force-import-all` is enabled.
---
llvm/lib/Transforms/IPO/FunctionImport.cpp | 5 ++-
llvm/test/ThinLTO/AMDGPU/Inputs/in-0.ll | 13 ++++++++
llvm/test/ThinLTO/AMDGPU/Inputs/in-1.ll | 13 ++++++++
llvm/test/ThinLTO/AMDGPU/force-import-all.ll | 35 ++++++++++++++++++++
llvm/test/ThinLTO/AMDGPU/lit.local.cfg | 2 ++
5 files changed, 67 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/ThinLTO/AMDGPU/Inputs/in-0.ll
create mode 100644 llvm/test/ThinLTO/AMDGPU/Inputs/in-1.ll
create mode 100644 llvm/test/ThinLTO/AMDGPU/force-import-all.ll
create mode 100644 llvm/test/ThinLTO/AMDGPU/lit.local.cfg
diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp
index f1dce5d7904f9..19a6bc55b0147 100644
--- a/llvm/lib/Transforms/IPO/FunctionImport.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp
@@ -1701,13 +1701,16 @@ void llvm::thinLTOFinalizeInModule(Module &TheModule,
if (NewLinkage == GV.getLinkage())
return;
+ bool ForceImportFunction = isa<Function>(GV) && ForceImportAll;
+
// Check for a non-prevailing def that has interposable linkage
// (e.g. non-odr weak or linkonce). In that case we can't simply
// convert to available_externally, since it would lose the
// interposable property and possibly get inlined. Simply drop
// the definition in that case.
if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
- GlobalValue::isInterposableLinkage(GV.getLinkage())) {
+ GlobalValue::isInterposableLinkage(GV.getLinkage()) &&
+ !ForceImportFunction) {
if (!convertToDeclaration(GV))
// FIXME: Change this to collect replaced GVs and later erase
// them from the parent module once thinLTOResolvePrevailingGUID is
diff --git a/llvm/test/ThinLTO/AMDGPU/Inputs/in-0.ll b/llvm/test/ThinLTO/AMDGPU/Inputs/in-0.ll
new file mode 100644
index 0000000000000..84fbd66bdae27
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/Inputs/in-0.ll
@@ -0,0 +1,13 @@
+define void @f1(ptr %v) #0 {
+entry:
+ call void @weak_common(ptr %v)
+ ret void
+}
+
+define weak hidden void @weak_common(ptr %v) #0 {
+entry:
+ store i32 12345, ptr %v
+ ret void
+}
+
+attributes #0 = { noinline }
diff --git a/llvm/test/ThinLTO/AMDGPU/Inputs/in-1.ll b/llvm/test/ThinLTO/AMDGPU/Inputs/in-1.ll
new file mode 100644
index 0000000000000..ee367d466a03f
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/Inputs/in-1.ll
@@ -0,0 +1,13 @@
+define void @f2(ptr %v) #0 {
+entry:
+ call void @weak_common(ptr %v)
+ ret void
+}
+
+define weak hidden void @weak_common(ptr %v) #0 {
+entry:
+ store i32 12345, ptr %v
+ ret void
+}
+
+attributes #0 = { noinline }
diff --git a/llvm/test/ThinLTO/AMDGPU/force-import-all.ll b/llvm/test/ThinLTO/AMDGPU/force-import-all.ll
new file mode 100644
index 0000000000000..e71735638e3a5
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/force-import-all.ll
@@ -0,0 +1,35 @@
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %s -o %t.main.bc
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %p/Inputs/in-0.ll -o %t.in.0.bc
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %p/Inputs/in-1.ll -o %t.in.1.bc
+; RUN: llvm-lto -thinlto-action=run -force-import-all %t.main.bc %t.in.0.bc %t.in.1.bc --thinlto-save-temps=%t.2.
+; RUN: llvm-dis %t.2.0.3.imported.bc -o - | FileCheck --check-prefix=MOD1 %s
+; RUN: llvm-dis %t.2.1.3.imported.bc -o - | FileCheck --check-prefix=MOD2 %s
+; RUN: llvm-dis %t.2.2.3.imported.bc -o - | FileCheck --check-prefix=MOD3 %s
+
+define void @f0(ptr %p) #0 {
+entry:
+ call void @f1(ptr %p)
+ call void @f2(ptr %p)
+ ret void
+}
+
+define weak hidden void @weak_common(ptr %v) #0 {
+entry:
+ store i32 12345, ptr %v
+ ret void
+}
+
+declare void @f1(ptr)
+
+declare void @f2(ptr)
+
+attributes #0 = { noinline }
+
+; MOD1: define available_externally void @f1
+; MOD1: define available_externally void @f2
+
+; MOD2: define void @f1
+; MOD2: define available_externally hidden void @weak_common
+
+; MOD3: define void @f2
+; MOD3: define available_externally hidden void @weak_common
diff --git a/llvm/test/ThinLTO/AMDGPU/lit.local.cfg b/llvm/test/ThinLTO/AMDGPU/lit.local.cfg
new file mode 100644
index 0000000000000..7c492428aec76
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "AMDGPU" in config.root.targets:
+ config.unsupported = True
More information about the llvm-commits
mailing list