[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 07:37:33 PDT 2025
https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/134541
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.
>From 70db06f706a2443ed66ca6dda88268b9e302c271 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sun, 6 Apr 2025 10:36:41 -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/noinline.ll | 15 +++++++++++
llvm/test/ThinLTO/AMDGPU/force-import-all.ll | 26 ++++++++++++++++++++
llvm/test/ThinLTO/AMDGPU/lit.local.cfg | 2 ++
4 files changed, 47 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/ThinLTO/AMDGPU/Inputs/noinline.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/noinline.ll b/llvm/test/ThinLTO/AMDGPU/Inputs/noinline.ll
new file mode 100644
index 0000000000000..6f93f880c3157
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/Inputs/noinline.ll
@@ -0,0 +1,15 @@
+define void @f2(ptr %v) #0 {
+entry:
+ %v.addr = alloca ptr, align 8, addrspace(5)
+ store ptr %v, ptr addrspace(5) %v.addr, align 8
+ call void @f3(ptr %v)
+ ret void
+}
+
+define weak hidden void @f3(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..f8beb0dae390f
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/force-import-all.ll
@@ -0,0 +1,26 @@
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %s -o %t1.bc
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %p/Inputs/noinline.ll -o %t2.bc
+; RUN: llvm-lto -thinlto-action=run -force-import-all %t1.bc %t2.bc -thinlto-save-temps=%t3.
+; RUN: llvm-dis %t3.0.3.imported.bc -o - | FileCheck %s --check-prefix=MOD1
+; RUN: llvm-dis %t3.1.3.imported.bc -o - | FileCheck %s --check-prefix=MOD2
+
+define i32 @f1(ptr %p) #0 {
+entry:
+ call void @f2(ptr %p)
+ ret i32 0
+}
+
+define weak hidden void @f3(ptr %v) #0 {
+entry:
+ store i32 12345, ptr %v
+ ret void
+}
+
+declare void @f2(ptr)
+
+attributes #0 = { noinline }
+
+; MOD1: define weak hidden void @f3
+; MOD1: define available_externally void @f2
+; MOD2: define void @f2
+; MOD2: define available_externally hidden void @f3
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