Skip to content

Commit 03019c6

Browse files
authored
[clang][driver] When -fveclib=ArmPL flag is in use, always link against libamath (#116432)
Using `-fveclib=ArmPL` without `-lamath` likely effects in the link-time errors.
1 parent 854ea0c commit 03019c6

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
@@ -494,6 +494,39 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
494494
else
495495
A.renderAsInput(Args, CmdArgs);
496496
}
497+
if (const Arg *A = Args.getLastArg(options::OPT_fveclib)) {
498+
const llvm::Triple &Triple = TC.getTriple();
499+
StringRef V = A->getValue();
500+
if (V == "ArmPL" && (Triple.isOSLinux() || Triple.isOSDarwin())) {
501+
// To support -fveclib=ArmPL we need to link against libamath. Some of the
502+
// libamath functions depend on libm, at the same time, libamath exports
503+
// its own implementation of some of the libm functions. These are faster
504+
// and potentially less accurate implementations, hence we need to be
505+
// careful what is being linked in. Since here we are interested only in
506+
// the subset of libamath functions that is covered by the veclib
507+
// mappings, we need to prioritize libm functions by putting -lm before
508+
// -lamath (and then -lm again, to fulfill libamath requirements).
509+
//
510+
// Therefore we need to do the following:
511+
//
512+
// 1. On Linux, link only when actually needed.
513+
//
514+
// 2. Prefer libm functions over libamath.
515+
//
516+
// 3. Link against libm to resolve libamath dependencies.
517+
//
518+
if (Triple.isOSLinux()) {
519+
CmdArgs.push_back(Args.MakeArgString("--push-state"));
520+
CmdArgs.push_back(Args.MakeArgString("--as-needed"));
521+
}
522+
CmdArgs.push_back(Args.MakeArgString("-lm"));
523+
CmdArgs.push_back(Args.MakeArgString("-lamath"));
524+
CmdArgs.push_back(Args.MakeArgString("-lm"));
525+
if (Triple.isOSLinux())
526+
CmdArgs.push_back(Args.MakeArgString("--pop-state"));
527+
addArchSpecificRPath(TC, Args, CmdArgs);
528+
}
529+
}
497530
}
498531

499532
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)