From 3f4e02493dd3877d95a3a0e0372694991b9de6f4 Mon Sep 17 00:00:00 2001 From: Shilei Tian Date: Sat, 12 Apr 2025 14:21:25 -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/LTO/LTO.cpp | 7 ++++- llvm/lib/Transforms/IPO/FunctionImport.cpp | 8 +++--- llvm/test/ThinLTO/AMDGPU/Inputs/in-f1.ll | 13 ++++++++++ llvm/test/ThinLTO/AMDGPU/force-import-all.ll | 27 ++++++++++++++++++++ llvm/test/ThinLTO/AMDGPU/lit.local.cfg | 2 ++ 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 llvm/test/ThinLTO/AMDGPU/Inputs/in-f1.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/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 97d23feff230f..4b6a87edeff87 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -75,6 +75,8 @@ static cl::opt extern cl::opt CodeGenDataThinLTOTwoRounds; +extern cl::opt ForceImportAll; + namespace llvm { /// Enable global value internalization in LTO. cl::opt EnableLTOInternalization( @@ -406,8 +408,11 @@ static void thinLTOResolvePrevailingGUID( Visibility = S->getVisibility(); } // Alias and aliasee can't be turned into available_externally. + // When force-import-all is used, it indicates that object linking is not + // supported by the target. In this case, we can't change the linkage as + // well in case the global is converted to declaration. else if (!isa(S.get()) && - !GlobalInvolvedWithAlias.count(S.get())) + !GlobalInvolvedWithAlias.count(S.get()) && !ForceImportAll) S->setLinkage(GlobalValue::AvailableExternallyLinkage); // For ELF, set visibility to the computed visibility from summaries. We diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp index f1dce5d7904f9..12da7f57a8f61 100644 --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -71,6 +71,10 @@ STATISTIC(NumImportedModules, "Number of modules imported from"); STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index"); STATISTIC(NumLiveSymbols, "Number of live symbols in index"); +cl::opt + ForceImportAll("force-import-all", cl::init(false), cl::Hidden, + cl::desc("Import functions with noinline attribute")); + /// Limit on instruction count of imported functions. static cl::opt ImportInstrLimit( "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), @@ -80,10 +84,6 @@ static cl::opt ImportCutoff( "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"), cl::desc("Only import first N functions if N>=0 (default -1)")); -static cl::opt - ForceImportAll("force-import-all", cl::init(false), cl::Hidden, - cl::desc("Import functions with noinline attribute")); - static cl::opt ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), cl::Hidden, cl::value_desc("x"), diff --git a/llvm/test/ThinLTO/AMDGPU/Inputs/in-f1.ll b/llvm/test/ThinLTO/AMDGPU/Inputs/in-f1.ll new file mode 100644 index 0000000000000..84fbd66bdae27 --- /dev/null +++ b/llvm/test/ThinLTO/AMDGPU/Inputs/in-f1.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/force-import-all.ll b/llvm/test/ThinLTO/AMDGPU/force-import-all.ll new file mode 100644 index 0000000000000..829fc515ee306 --- /dev/null +++ b/llvm/test/ThinLTO/AMDGPU/force-import-all.ll @@ -0,0 +1,27 @@ +; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %s -o %t.main.bc +; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %p/Inputs/in-f1.ll -o %t.in.bc +; RUN: llvm-lto -thinlto-action=run -force-import-all %t.main.bc %t.in.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 + +define void @f0(ptr %p) #0 { +entry: + call void @f1(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) + +attributes #0 = { noinline } + +; MOD1: define weak hidden void @weak_common +; MOD1: define available_externally void @f1 + +; MOD2: define void @f1 +; MOD2: define weak 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