Skip to content

Commit 12654b6

Browse files
committed
[clang][driver] When -fveclib=ArmPL flag is in use, always link against libamath
Using `-fveclib=ArmPL` without libamath likely effects in the link-time errors.
1 parent 5f99eb9 commit 12654b6

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,35 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
490490
else
491491
A.renderAsInput(Args, CmdArgs);
492492
}
493+
if (const Arg *A = Args.getLastArg(options::OPT_fveclib)) {
494+
const llvm::Triple &Triple = TC.getTriple();
495+
StringRef V = A->getValue();
496+
if (V == "ArmPL" && (Triple.isOSLinux() || Triple.isOSDarwin())) {
497+
// To support -fveclib=ArmPL we need to link against libamath.
498+
// Some of the libamath functions depend on libm, at the same time,
499+
// libamath exports its own implementation of some of the libm
500+
// functions. Since here we are interested only in the subset of
501+
// libamath functions that is covered by the veclib mappings,
502+
// we need to do the following:
503+
//
504+
// 1. On Linux, link only when actually needed.
505+
//
506+
// 2. Prefer libm functions over libamath.
507+
//
508+
// 3. Link against libm to resolve libamath dependencies.
509+
//
510+
if (Triple.isOSLinux()) {
511+
CmdArgs.push_back(Args.MakeArgString("--push-state"));
512+
CmdArgs.push_back(Args.MakeArgString("--as-needed"));
513+
}
514+
CmdArgs.push_back(Args.MakeArgString("-lm"));
515+
CmdArgs.push_back(Args.MakeArgString("-lamath"));
516+
CmdArgs.push_back(Args.MakeArgString("-lm"));
517+
if (Triple.isOSLinux())
518+
CmdArgs.push_back(Args.MakeArgString("--pop-state"));
519+
addArchSpecificRPath(TC, Args, CmdArgs);
520+
}
521+
}
493522
}
494523

495524
void tools::addLinkerCompressDebugSectionsOption(

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
8484
else if (TC.getTriple().isWindowsArm64EC())
8585
CmdArgs.push_back("-machine:arm64ec");
8686

87+
if (const Arg *A = Args.getLastArg(options::OPT_fveclib)) {
88+
StringRef V = A->getValue();
89+
if (V == "ArmPL")
90+
CmdArgs.push_back(Args.MakeArgString("--dependent-lib=amath"));
91+
}
92+
8793
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
8894
!C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
8995
CmdArgs.push_back("-defaultlib:libcmt");

clang/test/Driver/fveclib.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,16 @@
112112
/* Verify no warning when math-errno is re-enabled for a different veclib (that does not imply -fno-math-errno). */
113113
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fmath-errno -fveclib=LIBMVEC %s 2>&1 | FileCheck --check-prefix=CHECK-REPEAT-VECLIB %s
114114
// CHECK-REPEAT-VECLIB-NOT: math errno enabled
115+
116+
/// Verify that vectorized routines library is being linked in.
117+
// RUN: %clang -### --target=aarch64-pc-windows-msvc -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-MSVC %s
118+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-LINUX %s
119+
// RUN: %clang -### --target=arm64-apple-darwin -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-DARWIN %s
120+
// CHECK-LINKING-ARMPL-LINUX: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
121+
// CHECK-LINKING-ARMPL-DARWIN: "-lm" "-lamath" "-lm"
122+
// CHECK-LINKING-ARMPL-MSVC: "--dependent-lib=amath"
123+
124+
/// Verify that the RPATH is being set when needed.
125+
// RUN: %clang -### --target=aarch64-linux-gnu -resource-dir=%S/../../../clang/test/Driver/Inputs/resource_dir_with_arch_subdir -frtlib-add-rpath -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-RPATH-ARMPL %s
126+
// CHECK-RPATH-ARMPL: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
127+
// CHECK-RPATH-ARMPL-SAME: "-rpath"

flang/test/Driver/fveclib.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@
3030

3131
! TODO: if we add support for -nostdlib or -nodefaultlibs we need to test that
3232
! these prevent "-framework Accelerate" being added on Darwin
33+
34+
! RUN: %flang -### --target=aarch64-pc-windows-msvc -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-MSVC %s
35+
! RUN: %flang -### --target=aarch64-linux-gnu -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-LINUX %s
36+
! RUN: %flang -### --target=arm64-apple-darwin -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-DARWIN %s
37+
! CHECK-LINKING-ARMPL-LINUX: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
38+
! CHECK-LINKING-ARMPL-DARWIN: "-lm" "-lamath" "-lm"
39+
! CHECK-LINKING-ARMPL-MSVC: "--dependent-lib=amath"
40+
41+
! RUN: %flang -### --target=aarch64-linux-gnu -resource-dir=%S/../../../clang/test/Driver/Inputs/resource_dir_with_arch_subdir -frtlib-add-rpath -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-RPATH-ARMPL %s
42+
! CHECK-RPATH-ARMPL: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
43+
! We need to see "-rpath" at least twice, one for veclib, one for the Fortran runtime
44+
! CHECK-RPATH-ARMPL-SAME: "-rpath"
45+
! CHECK-RPATH-ARMPL-SAME: "-rpath"

0 commit comments

Comments
 (0)