Skip to content

Commit d19c8ea

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 d19c8ea

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,39 @@ 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. Some of the
498+
// libamath functions depend on libm, at the same time, libamath exports
499+
// its own implementation of some of the libm functions. These are faster
500+
// and potentially less accurate implementations, hence we need to be
501+
// careful what is being linked in. Since here we are interested only in
502+
// the subset of libamath functions that is covered by the veclib
503+
// mappings, we need to prioritize libm functions by putting -lm before
504+
// -lamath (and then -lm again, to fulfill libamath requirements).
505+
//
506+
// Therefore we need to do the following:
507+
//
508+
// 1. On Linux, link only when actually needed.
509+
//
510+
// 2. Prefer libm functions over libamath.
511+
//
512+
// 3. Link against libm to resolve libamath dependencies.
513+
//
514+
if (Triple.isOSLinux()) {
515+
CmdArgs.push_back(Args.MakeArgString("--push-state"));
516+
CmdArgs.push_back(Args.MakeArgString("--as-needed"));
517+
}
518+
CmdArgs.push_back(Args.MakeArgString("-lm"));
519+
CmdArgs.push_back(Args.MakeArgString("-lamath"));
520+
CmdArgs.push_back(Args.MakeArgString("-lm"));
521+
if (Triple.isOSLinux())
522+
CmdArgs.push_back(Args.MakeArgString("--pop-state"));
523+
addArchSpecificRPath(TC, Args, CmdArgs);
524+
}
525+
}
493526
}
494527

495528
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,20 @@
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=aarch64-linux-gnu -fveclib=ArmPL %s -lamath 2>&1 | FileCheck --check-prefix=CHECK-LINKING-AMATH-BEFORE-ARMPL-LINUX %s
120+
// RUN: %clang -### --target=arm64-apple-darwin -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-DARWIN %s
121+
// RUN: %clang -### --target=arm64-apple-darwin -fveclib=ArmPL %s -lamath 2>&1 | FileCheck --check-prefix=CHECK-LINKING-AMATH-BEFORE-ARMPL-DARWIN %s
122+
// CHECK-LINKING-ARMPL-LINUX: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
123+
// CHECK-LINKING-ARMPL-DARWIN: "-lm" "-lamath" "-lm"
124+
// CHECK-LINKING-ARMPL-MSVC: "--dependent-lib=amath"
125+
// CHECK-LINKING-AMATH-BEFORE-ARMPL-LINUX: "-lamath" {{.*}}"--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
126+
// CHECK-LINKING-AMATH-BEFORE-ARMPL-DARWIN: "-lamath" {{.*}}"-lm" "-lamath" "-lm"
127+
128+
/// Verify that the RPATH is being set when needed.
129+
// 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
130+
// CHECK-RPATH-ARMPL: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
131+
// CHECK-RPATH-ARMPL-SAME: "-rpath"

flang/test/Driver/fveclib.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,20 @@
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=aarch64-linux-gnu -fveclib=ArmPL %s -lamath 2>&1 | FileCheck --check-prefix=CHECK-LINKING-AMATH-BEFORE-ARMPL-LINUX %s
37+
! RUN: %flang -### --target=arm64-apple-darwin -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-LINKING-ARMPL-DARWIN %s
38+
! RUN: %flang -### --target=arm64-apple-darwin -fveclib=ArmPL %s -lamath 2>&1 | FileCheck --check-prefix=CHECK-LINKING-AMATH-BEFORE-ARMPL-DARWIN %s
39+
! CHECK-LINKING-ARMPL-LINUX: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
40+
! CHECK-LINKING-ARMPL-DARWIN: "-lm" "-lamath" "-lm"
41+
! CHECK-LINKING-ARMPL-MSVC: "--dependent-lib=amath"
42+
! CHECK-LINKING-AMATH-BEFORE-ARMPL-LINUX: "-lamath" {{.*}}"--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
43+
! CHECK-LINKING-AMATH-BEFORE-ARMPL-DARWIN: "-lamath" {{.*}}"-lm" "-lamath" "-lm"
44+
45+
! 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
46+
! CHECK-RPATH-ARMPL: "--push-state" "--as-needed" "-lm" "-lamath" "-lm" "--pop-state"
47+
! We need to see "-rpath" at least twice, one for veclib, one for the Fortran runtime
48+
! CHECK-RPATH-ARMPL-SAME: "-rpath"
49+
! CHECK-RPATH-ARMPL-SAME: "-rpath"

0 commit comments

Comments
 (0)