Skip to content

Commit 590ab43

Browse files
authored
RuntimeLibcalls: Move VectorLibrary handling into TargetOptions (#167996)
This fixes the -fveclib flag getting lost on its way to the backend. Previously this was its own cl::opt with a random boolean. Move the flag handling into CommandFlags with other backend ABI-ish options, and have clang directly set it, rather than forcing it to go through command line parsing. Prior to de68181, codegen used TargetLibraryInfo to find the vector function. Clang has special handling for TargetLibraryInfo, where it would directly construct one with the vector library in the pass pipeline. RuntimeLibcallsInfo currently is not used as an analysis in codegen, and needs to know the vector library when constructed. RuntimeLibraryAnalysis could follow the same trick that TargetLibraryInfo is using in the future, but a lot more boilerplate changes are needed to thread that analysis through codegen. Ideally this would come from an IR module flag, and nothing would be in TargetOptions. For now, it's better for all of these sorts of controls to be consistent.
1 parent 3a08e42 commit 590ab43

File tree

18 files changed

+121
-55
lines changed

18 files changed

+121
-55
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,36 @@ static bool initTargetOptions(const CompilerInstance &CI,
481481
Options.JMCInstrument = CodeGenOpts.JMCInstrument;
482482
Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
483483

484+
switch (CodeGenOpts.getVecLib()) {
485+
case llvm::driver::VectorLibrary::NoLibrary:
486+
Options.VectorLibrary = llvm::VectorLibrary::NoLibrary;
487+
break;
488+
case llvm::driver::VectorLibrary::Accelerate:
489+
Options.VectorLibrary = llvm::VectorLibrary::Accelerate;
490+
break;
491+
case llvm::driver::VectorLibrary::Darwin_libsystem_m:
492+
Options.VectorLibrary = llvm::VectorLibrary::DarwinLibSystemM;
493+
break;
494+
case llvm::driver::VectorLibrary::LIBMVEC:
495+
Options.VectorLibrary = llvm::VectorLibrary::LIBMVEC;
496+
break;
497+
case llvm::driver::VectorLibrary::MASSV:
498+
Options.VectorLibrary = llvm::VectorLibrary::MASSV;
499+
break;
500+
case llvm::driver::VectorLibrary::SVML:
501+
Options.VectorLibrary = llvm::VectorLibrary::SVML;
502+
break;
503+
case llvm::driver::VectorLibrary::SLEEF:
504+
Options.VectorLibrary = llvm::VectorLibrary::SLEEFGNUABI;
505+
break;
506+
case llvm::driver::VectorLibrary::ArmPL:
507+
Options.VectorLibrary = llvm::VectorLibrary::ArmPL;
508+
break;
509+
case llvm::driver::VectorLibrary::AMDLIBM:
510+
Options.VectorLibrary = llvm::VectorLibrary::AMDLIBM;
511+
break;
512+
}
513+
484514
switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
485515
case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
486516
Options.SwiftAsyncFramePointer =
@@ -584,6 +614,7 @@ static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts,
584614
BackendArgs.push_back("-limit-float-precision");
585615
BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
586616
}
617+
587618
// Check for the default "clang" invocation that won't set any cl::opt values.
588619
// Skip trying to parse the command line invocation to avoid the issues
589620
// described below.

cross-project-tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ add_lit_testsuite(check-cross-dtlto "Running DTLTO cross-project tests"
104104
DEPENDS ${CROSS_PROJECT_TEST_DEPS}
105105
)
106106

107+
# veclib tests.
108+
add_lit_testsuite(check-cross-veclib "Running veclib cross-project tests"
109+
${CMAKE_CURRENT_BINARY_DIR}/veclib
110+
EXCLUDE_FROM_CHECK_ALL
111+
DEPENDS ${CROSS_PROJECT_TEST_DEPS}
112+
)
113+
107114
# Add check-cross-project-* targets.
108115
add_lit_testsuites(CROSS_PROJECT ${CMAKE_CURRENT_SOURCE_DIR}
109116
DEPENDS ${CROSS_PROJECT_TEST_DEPS}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if "clang" not in config.available_features:
2+
config.unsupported = True
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: aarch64-registered-target
2+
// RUN: %clang -S -target aarch64-unknown-linux-gnu -O2 -fno-math-errno \
3+
// RUN: -fveclib=ArmPL -o - %s | FileCheck -check-prefix=ARMPL %s
4+
// RUN: %clang -S -target aarch64-unknown-linux-gnu -O2 -fno-math-errno \
5+
// RUN: -fveclib=SLEEF -o - %s | FileCheck -check-prefix=SLEEF %s
6+
7+
typedef __SIZE_TYPE__ size_t;
8+
9+
void sincos(double, double *, double *);
10+
11+
// ARMPL: armpl_vsincosq_f64
12+
// ARMPL: armpl_vsincosq_f64
13+
14+
// SLEEF: _ZGVnN2vl8l8_sincos
15+
// SLEEF: _ZGVnN2vl8l8_sincos
16+
void vectorize_sincos(double *restrict x, double *restrict s,
17+
double *restrict c, size_t n) {
18+
for (size_t i = 0; i < n; ++i) {
19+
sincos(x[i], &s[i], &c[i]);
20+
}
21+
}

llvm/include/llvm/Analysis/TargetLibraryInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/IR/InstrTypes.h"
1515
#include "llvm/IR/Module.h"
1616
#include "llvm/IR/PassManager.h"
17+
#include "llvm/IR/SystemLibraries.h"
1718
#include "llvm/Pass.h"
1819
#include "llvm/Support/Compiler.h"
1920
#include "llvm/TargetParser/Triple.h"
@@ -23,7 +24,6 @@
2324
namespace llvm {
2425

2526
template <typename T> class ArrayRef;
26-
enum class VectorLibrary;
2727

2828
/// Provides info so a possible vectorization of a function can be
2929
/// computed. Function 'VectorFnName' is equivalent to 'ScalarFnName'
@@ -119,7 +119,8 @@ class TargetLibraryInfoImpl {
119119

120120
public:
121121
TargetLibraryInfoImpl() = delete;
122-
LLVM_ABI explicit TargetLibraryInfoImpl(const Triple &T);
122+
LLVM_ABI explicit TargetLibraryInfoImpl(
123+
const Triple &T, VectorLibrary VecLib = VectorLibrary::NoLibrary);
123124

124125
// Provide value semantics.
125126
LLVM_ABI TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);

llvm/include/llvm/CodeGen/CommandFlags.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ LLVM_ABI llvm::EABI getEABIVersion();
125125

126126
LLVM_ABI llvm::DebuggerKind getDebuggerTuningOpt();
127127

128+
LLVM_ABI llvm::VectorLibrary getVectorLibrary();
129+
128130
LLVM_ABI bool getEnableStackSizeSection();
129131

130132
LLVM_ABI bool getEnableAddrsig();

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/IR/CallingConv.h"
2424
#include "llvm/IR/InstrTypes.h"
2525
#include "llvm/IR/PassManager.h"
26+
#include "llvm/IR/SystemLibraries.h"
2627
#include "llvm/Support/AtomicOrdering.h"
2728
#include "llvm/Support/CodeGen.h"
2829
#include "llvm/Support/Compiler.h"
@@ -83,7 +84,8 @@ struct RuntimeLibcallsInfo {
8384
const Triple &TT,
8485
ExceptionHandling ExceptionModel = ExceptionHandling::None,
8586
FloatABI::ABIType FloatABI = FloatABI::Default,
86-
EABI EABIVersion = EABI::Default, StringRef ABIName = "");
87+
EABI EABIVersion = EABI::Default, StringRef ABIName = "",
88+
VectorLibrary VecLib = VectorLibrary::NoLibrary);
8789

8890
explicit RuntimeLibcallsInfo(const Module &M);
8991

llvm/include/llvm/IR/SystemLibraries.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ enum class VectorLibrary {
2929
AMDLIBM // AMD Math Vector library.
3030
};
3131

32-
/// Command line flag value for the vector math library to use
33-
///
34-
/// FIXME: This should come from a module flag, and not be mutually exclusive
35-
extern VectorLibrary ClVectorLibrary;
36-
3732
} // namespace llvm
3833

3934
#endif // LLVM_IR_SYSTEMLIBRARIES_H

llvm/include/llvm/Target/TargetOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_TARGET_TARGETOPTIONS_H
1616

1717
#include "llvm/ADT/FloatingPointMode.h"
18+
#include "llvm/IR/SystemLibraries.h"
1819
#include "llvm/MC/MCTargetOptions.h"
1920
#include "llvm/Support/CodeGen.h"
2021
#include "llvm/Support/Compiler.h"
@@ -409,6 +410,9 @@ class TargetOptions {
409410
/// Which debugger to tune for.
410411
DebuggerKind DebuggerTuning = DebuggerKind::Default;
411412

413+
/// Vector math library to use.
414+
VectorLibrary VectorLibrary = VectorLibrary::NoLibrary;
415+
412416
private:
413417
/// Flushing mode to assume in default FP environment.
414418
DenormalMode FPDenormalMode;

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ static void initializeBase(TargetLibraryInfoImpl &TLI, const Triple &T) {
160160
/// target triple. This should be carefully written so that a missing target
161161
/// triple gets a sane set of defaults.
162162
static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
163-
ArrayRef<StringLiteral> StandardNames) {
163+
ArrayRef<StringLiteral> StandardNames,
164+
VectorLibrary VecLib) {
164165
// Set IO unlocked variants as unavailable
165166
// Set them as available per system below
166167
TLI.setUnavailable(LibFunc_getc_unlocked);
@@ -924,23 +925,25 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
924925
if (T.isOSAIX())
925926
TLI.setUnavailable(LibFunc_memrchr);
926927

927-
TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary, T);
928+
TLI.addVectorizableFunctionsFromVecLib(VecLib, T);
928929
}
929930

930931
/// Initialize the set of available library functions based on the specified
931932
/// target triple. This should be carefully written so that a missing target
932933
/// triple gets a sane set of defaults.
933934
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
934-
ArrayRef<StringLiteral> StandardNames) {
935+
ArrayRef<StringLiteral> StandardNames,
936+
VectorLibrary VecLib) {
935937
initializeBase(TLI, T);
936-
initializeLibCalls(TLI, T, StandardNames);
938+
initializeLibCalls(TLI, T, StandardNames, VecLib);
937939
}
938940

939-
TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
941+
TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T,
942+
VectorLibrary VecLib) {
940943
// Default to everything being available.
941944
memset(AvailableArray, -1, sizeof(AvailableArray));
942945

943-
initialize(*this, T, StandardNames);
946+
initialize(*this, T, StandardNames, VecLib);
944947
}
945948

946949
TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)

0 commit comments

Comments
 (0)