Skip to content

Commit edf80dc

Browse files
authored
[LTO][Veclib] Fix vector library handling with LTO (#170638)
Commit #167996 moved VecLib into TargetOptions and ensured clang properly sets it. However, some LTO backend code paths were still creating _TargetLibraryInfoImpl_ without passing the VecLib parameter from `TargetMachine::Options`. This PR completes the fix by ensuring that: _LTOBackend.cpp, ThinLTOCodeGenerator.cpp, UpdateCompilerUsed.cpp_ all pass `TM->Options.VecLib` when constructing _TargetLibraryInfoImpl_. Without this fix, vector library information (e.g., -fveclib=ArmPL) would not be properly recognized during LTO optimization and code generation, potentially causing incorrect optimizations or linker errors when vector library functions are referenced.
1 parent 358c4d8 commit edf80dc

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
279279
RegisterPassPlugins(Conf.PassPlugins, PB);
280280

281281
std::unique_ptr<TargetLibraryInfoImpl> TLII(
282-
new TargetLibraryInfoImpl(TM->getTargetTriple()));
282+
new TargetLibraryInfoImpl(TM->getTargetTriple(), TM->Options.VecLib));
283283
if (Conf.Freestanding)
284284
TLII->disableAllFunctions();
285285
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
@@ -445,7 +445,7 @@ static void codegen(const Config &Conf, TargetMachine *TM,
445445
// keep the pointer and may use it until their destruction. See #138194.
446446
{
447447
legacy::PassManager CodeGenPasses;
448-
TargetLibraryInfoImpl TLII(Mod.getTargetTriple());
448+
TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
449449
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
450450
CodeGenPasses.add(new RuntimeLibraryInfoWrapper(
451451
Mod.getTargetTriple(), TM->Options.ExceptionModel,

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
249249
PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
250250

251251
std::unique_ptr<TargetLibraryInfoImpl> TLII(
252-
new TargetLibraryInfoImpl(TM.getTargetTriple()));
252+
new TargetLibraryInfoImpl(TM.getTargetTriple(), TM.Options.VecLib));
253253
if (Freestanding)
254254
TLII->disableAllFunctions();
255255
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });

llvm/lib/LTO/UpdateCompilerUsed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PreserveLibCallsAndAsmUsed {
5757
// same names are added to llvm.compiler.used to prevent them from being
5858
// deleted by optimizations.
5959
void initializeLibCalls(const Module &TheModule) {
60-
TargetLibraryInfoImpl TLII(TM.getTargetTriple());
60+
TargetLibraryInfoImpl TLII(TM.getTargetTriple(), TM.Options.VecLib);
6161
TargetLibraryInfo TLI(TLII);
6262

6363
// TargetLibraryInfo has info on C runtime library calls on the current
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt -module-summary %s -o %t.bc
2+
; RUN: llvm-lto2 run %t.bc -o %t.o -save-temps \
3+
; RUN: -r %t.bc,compute,px \
4+
; RUN: -mcpu=neoverse-v1 -O3 \
5+
; RUN: -vector-library=ArmPL
6+
; RUN: llvm-nm %t.o.1 | FileCheck %s
7+
8+
; This test verifies that the VecLib propagation in LTO prevents a crash
9+
; when compiling scalable vector frem operations.
10+
11+
; CHECK: compute
12+
13+
target triple = "aarch64-unknown-linux-gnu"
14+
15+
define <vscale x 2 x double> @compute(<vscale x 2 x double> %a, <vscale x 2 x double> %b) {
16+
entry:
17+
%rem = frem <vscale x 2 x double> %a, %b
18+
ret <vscale x 2 x double> %rem
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-lto -exported-symbol=compute -exported-symbol=armpl_vsinq_f64 -o %t.o %t.bc
3+
; RUN: llvm-nm %t.o | FileCheck %s
4+
5+
; This test ensures that ArmPL vector library functions are preserved through
6+
; the old LTO API (llvm-lto). TargetLibraryInfoImpl in LTO backend passes
7+
; receive the VecLib parameter from TargetMachine Options.
8+
9+
; CHECK: armpl_vsinq_f64
10+
; CHECK: compute
11+
12+
target triple = "aarch64-unknown-linux-gnu"
13+
14+
@llvm.compiler.used = appending global [1 x ptr] [ptr @armpl_vsinq_f64], section "llvm.metadata"
15+
16+
declare aarch64_vector_pcs <2 x double> @armpl_vsinq_f64(<2 x double>)
17+
18+
define void @compute(ptr %out, ptr %in) {
19+
entry:
20+
%v = load <2 x double>, ptr %in, align 16
21+
%result = call aarch64_vector_pcs <2 x double> @armpl_vsinq_f64(<2 x double> %v)
22+
store <2 x double> %result, ptr %out, align 16
23+
ret void
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: opt -module-summary %s -o %t.bc
2+
; RUN: llvm-lto2 run -save-temps -o %t.o %t.bc \
3+
; RUN: -r=%t.bc,compute,px \
4+
; RUN: -r=%t.bc,armpl_vsinq_f64
5+
; RUN: llvm-dis %t.o.1.5.precodegen.bc -o - | FileCheck %s
6+
7+
; This test ensures that ArmPL vector library functions are preserved through
8+
; the new LTO API (llvm-lto2). TargetLibraryInfoImpl in LTO backend passes
9+
; receive the VecLib parameter from TargetMachine Options.
10+
11+
; CHECK: @llvm.compiler.used = appending global [1 x ptr] [ptr @armpl_vsinq_f64]
12+
; CHECK: declare aarch64_vector_pcs <2 x double> @armpl_vsinq_f64(<2 x double>)
13+
14+
target triple = "aarch64-unknown-linux-gnu"
15+
16+
@llvm.compiler.used = appending global [1 x ptr] [ptr @armpl_vsinq_f64], section "llvm.metadata"
17+
18+
declare aarch64_vector_pcs <2 x double> @armpl_vsinq_f64(<2 x double>)
19+
20+
define void @compute(ptr %out, ptr %in) {
21+
entry:
22+
%v = load <2 x double>, ptr %in, align 16
23+
%result = call aarch64_vector_pcs <2 x double> @armpl_vsinq_f64(<2 x double> %v)
24+
store <2 x double> %result, ptr %out, align 16
25+
ret void
26+
}

0 commit comments

Comments
 (0)