Skip to content

Commit eb7b242

Browse files
Merge branch 'sycl' into refactorcache
2 parents 223f28d + 91a91c3 commit eb7b242

File tree

71 files changed

+620
-137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+620
-137
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
494494
def TargetSPIRV : TargetArch<["spirv", "spirv32", "spirv64"]>;
495495
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
496496
def TargetNVPTX : TargetArch<["nvptx", "nvptx64"]>;
497+
def TargetNativeCPU : TargetArch<["native_cpu"]>;
497498
def TargetWindows : TargetSpec {
498499
let OSes = ["Win32"];
499500
}
@@ -4530,6 +4531,11 @@ def RISCVVLSCC: DeclOrTypeAttr, TargetSpecificAttr<TargetRISCV> {
45304531
let Documentation = [RISCVVLSCCDocs];
45314532
}
45324533

4534+
def NativeCPULibclcCall : DeclOrTypeAttr, TargetSpecificAttr<TargetNativeCPU> {
4535+
let Spellings = [Clang<"libclc_call", 0>];
4536+
let Documentation = [Undocumented];
4537+
}
4538+
45334539
def Target : InheritableAttr {
45344540
let Spellings = [GCC<"target">];
45354541
let Args = [StringArgument<"featuresStr">];

clang/include/clang/Basic/TargetInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,11 @@ class TargetInfo : public TransferrableTargetInfo,
17111711
return CC_C;
17121712
}
17131713

1714+
/// Gets the calling convention for libclc built-ins for the given target.
1715+
virtual CallingConv getLibclcCallingConv() const {
1716+
return getDefaultCallingConv();
1717+
}
1718+
17141719
/// Get the default atomic options.
17151720
AtomicOptions getAtomicOpts() const { return AtomicOpts; }
17161721

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5959,7 +5959,7 @@ def no_offloadlib
59595959
: Flag<["--"], "no-offloadlib">,
59605960
MarshallingInfoFlag<LangOpts<"NoGPULib">>,
59615961
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
5962-
HelpText<"Do not link device library for CUDA/HIP device compilation">;
5962+
HelpText<"Do not link device library for CUDA/HIP/SYCL device compilation">;
59635963
def offloadlib : Flag<["--"], "offloadlib">,
59645964
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
59655965
HelpText<"Link device libraries for GPU device compilation">;

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,6 +4341,7 @@ bool AttributedType::isCallingConv() const {
43414341
case attr::PreserveNone:
43424342
case attr::RISCVVectorCC:
43434343
case attr::RISCVVLSCC:
4344+
case attr::NativeCPULibclcCall:
43444345
return true;
43454346
}
43464347
llvm_unreachable("invalid attr kind");

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
21252125
case attr::RISCVVLSCC:
21262126
OS << "riscv_vls_cc";
21272127
break;
2128+
case attr::NativeCPULibclcCall:
2129+
OS << "libclc_call";
2130+
break;
21282131
case attr::NoDeref:
21292132
OS << "noderef";
21302133
break;

clang/lib/Basic/Targets/NativeCPU.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ class LLVM_LIBRARY_VISIBILITY NativeCPUTargetInfo final : public TargetInfo {
4949

5050
void setSupportedOpenCLOpts() override { supportAllOpenCLOpts(); }
5151

52+
CallingConv getLibclcCallingConv() const override { return CC_SpirFunction; }
53+
5254
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
55+
if (CC == CC_SpirFunction)
56+
return CCCR_OK;
57+
5358
if (HostTarget)
5459
return HostTarget->checkCallingConvention(CC);
5560

clang/lib/CodeGen/CGCall.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
334334
}
335335
}
336336

337+
if (D->hasAttr<NativeCPULibclcCallAttr>())
338+
return CC_SpirFunction;
339+
337340
return CC_C;
338341
}
339342

clang/lib/CodeGen/Targets/NativeCPU.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class NativeCPUABIInfo : public DefaultABIInfo {
2020
public:
2121
NativeCPUABIInfo(CodeGen::CodeGenTypes &CGT, const ABIInfo *HostABIInfo)
2222
: DefaultABIInfo(CGT), HostABIInfo(HostABIInfo) {}
23+
24+
void computeInfo(CGFunctionInfo &FI) const override;
2325
};
2426

2527
class NativeCPUTargetCodeGenInfo : public TargetCodeGenInfo {
@@ -37,6 +39,17 @@ class NativeCPUTargetCodeGenInfo : public TargetCodeGenInfo {
3739
};
3840
} // namespace
3941

42+
void NativeCPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
43+
if (HostABIInfo &&
44+
FI.getCallingConvention() != llvm::CallingConv::SPIR_FUNC) {
45+
HostABIInfo->computeInfo(FI);
46+
return;
47+
}
48+
49+
DefaultABIInfo::computeInfo(FI);
50+
FI.setEffectiveCallingConvention(llvm::CallingConv::C);
51+
}
52+
4053
std::unique_ptr<TargetCodeGenInfo> CodeGen::createNativeCPUTargetCodeGenInfo(
4154
CodeGenModule &CGM,
4255
std::unique_ptr<TargetCodeGenInfo> HostTargetCodeGenInfo) {

clang/lib/Driver/Driver.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5647,10 +5647,9 @@ class OffloadingActionBuilder final {
56475647
} else
56485648
FullLinkObjects = LinkObjects;
56495649

5650-
// FIXME: Link all wrapper and fallback device libraries as default,
5651-
// When spv online link is supported by all backends, the fallback
5652-
// device libraries are only needed when current toolchain is using
5653-
// AOT compilation.
5650+
// TODO: spv online link is deprecated and will be removed in the
5651+
// future, need to remove the logic handling jit link when the option
5652+
// is removed in compiler.
56545653
bool SYCLDeviceLibLinked = false;
56555654
Action *NativeCPULib = nullptr;
56565655
if (IsSPIR || IsNVPTX || IsAMDGCN || IsNativeCPU) {

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,7 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT,
434434
const llvm::opt::ArgList &Args = C.getArgs();
435435
enum { JIT = 0, AOT_CPU, AOT_DG2, AOT_PVC };
436436
auto addSingleLibrary = [&](StringRef DeviceLibName) {
437-
SmallString<128> LibName(DeviceLibName);
438-
llvm::sys::path::replace_extension(LibName, LibSuffix);
439-
LibraryList.push_back(Args.MakeArgString(LibName));
437+
LibraryList.push_back(Args.MakeArgString(Twine(DeviceLibName) + LibSuffix));
440438
};
441439

442440
// This function is used to check whether there is only one GPU device
@@ -561,8 +559,13 @@ addSYCLDeviceSanitizerLibs(const Compilation &C, bool IsSpirvAOT,
561559
}
562560
#endif
563561

564-
SmallVector<std::string, 8>
565-
SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
562+
// Get the list of SYCL device libraries to link with user's device image if
563+
// some deprecated options are used including: -f[no-]sycl-device-lib=xxx,
564+
// -f[no-]sycl-device-lib-jit-link.
565+
// TODO: remove getDeviceLibrariesLegacy when we remove deprecated options
566+
// related to sycl device library link.
567+
static SmallVector<std::string, 8>
568+
getDeviceLibrariesLegacy(const Compilation &C, const llvm::Triple &TargetTriple,
566569
bool IsSpirvAOT) {
567570
SmallVector<std::string, 8> LibraryList;
568571
const llvm::opt::ArgList &Args = C.getArgs();
@@ -740,6 +743,111 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
740743
return LibraryList;
741744
}
742745

746+
// Get the list of SYCL device libraries to link with user's device image.
747+
SmallVector<std::string, 8>
748+
SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
749+
bool IsSpirvAOT) {
750+
SmallVector<std::string, 8> LibraryList;
751+
const llvm::opt::ArgList &Args = C.getArgs();
752+
if (Args.getLastArg(options::OPT_fsycl_device_lib_EQ,
753+
options::OPT_fno_sycl_device_lib_EQ) ||
754+
Args.getLastArg(options::OPT_fsycl_device_lib_jit_link,
755+
options::OPT_fno_sycl_device_lib_jit_link))
756+
return getDeviceLibrariesLegacy(C, TargetTriple, IsSpirvAOT);
757+
758+
bool NoOffloadLib =
759+
!Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true);
760+
if (TargetTriple.isNVPTX()) {
761+
if (!NoOffloadLib)
762+
LibraryList.push_back(
763+
Args.MakeArgString("devicelib-nvptx64-nvidia-cuda.bc"));
764+
return LibraryList;
765+
}
766+
767+
if (TargetTriple.isAMDGCN()) {
768+
if (!NoOffloadLib)
769+
LibraryList.push_back(
770+
Args.MakeArgString("devicelib-amdgcn-amd-amdhsa.bc"));
771+
return LibraryList;
772+
}
773+
774+
// Ignore no-offloadlib for NativeCPU device library, it provides some
775+
// critical builtins which must be linked with user's device image.
776+
if (TargetTriple.isNativeCPU()) {
777+
LibraryList.push_back(Args.MakeArgString("libsycl-nativecpu_utils.bc"));
778+
return LibraryList;
779+
}
780+
781+
using SYCLDeviceLibsList = SmallVector<StringRef>;
782+
const SYCLDeviceLibsList SYCLDeviceLibs = {"libsycl-crt",
783+
"libsycl-complex",
784+
"libsycl-complex-fp64",
785+
"libsycl-cmath",
786+
"libsycl-cmath-fp64",
787+
#if defined(_WIN32)
788+
"libsycl-msvc-math",
789+
#endif
790+
"libsycl-imf",
791+
"libsycl-imf-fp64",
792+
"libsycl-imf-bf16",
793+
"libsycl-fallback-cassert",
794+
"libsycl-fallback-cstring",
795+
"libsycl-fallback-complex",
796+
"libsycl-fallback-complex-fp64",
797+
"libsycl-fallback-cmath",
798+
"libsycl-fallback-cmath-fp64",
799+
"libsycl-fallback-imf",
800+
"libsycl-fallback-imf-fp64",
801+
"libsycl-fallback-imf-bf16"};
802+
bool IsWindowsMSVCEnv =
803+
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment();
804+
bool IsNewOffload = C.getDriver().getUseNewOffloadingDriver();
805+
StringRef LibSuffix = ".bc";
806+
if (IsNewOffload)
807+
// For new offload model, we use packaged .bc files.
808+
LibSuffix = IsWindowsMSVCEnv ? ".new.obj" : ".new.o";
809+
auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) {
810+
for (const StringRef &Lib : LibsList) {
811+
LibraryList.push_back(Args.MakeArgString(Twine(Lib) + LibSuffix));
812+
}
813+
};
814+
815+
if (!NoOffloadLib)
816+
addLibraries(SYCLDeviceLibs);
817+
818+
// ITT annotation libraries are linked in separately whenever the device
819+
// code instrumentation is enabled.
820+
const SYCLDeviceLibsList SYCLDeviceAnnotationLibs = {
821+
"libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers",
822+
"libsycl-itt-stubs"};
823+
if (Args.hasFlag(options::OPT_fsycl_instrument_device_code,
824+
options::OPT_fno_sycl_instrument_device_code, true))
825+
addLibraries(SYCLDeviceAnnotationLibs);
826+
827+
const SYCLDeviceLibsList SYCLDeviceBfloat16FallbackLib = {
828+
"libsycl-fallback-bfloat16"};
829+
const SYCLDeviceLibsList SYCLDeviceBfloat16NativeLib = {
830+
"libsycl-native-bfloat16"};
831+
bool NativeBfloatLibs;
832+
bool NeedBfloatLibs = selectBfloatLibs(TargetTriple, C, NativeBfloatLibs);
833+
if (NeedBfloatLibs && !NoOffloadLib) {
834+
// Add native or fallback bfloat16 library.
835+
if (NativeBfloatLibs)
836+
addLibraries(SYCLDeviceBfloat16NativeLib);
837+
else
838+
addLibraries(SYCLDeviceBfloat16FallbackLib);
839+
}
840+
841+
// Currently, device sanitizer support is required by some developers on
842+
// Linux platform only, so compiler only provides device sanitizer libraries
843+
// on Linux platform.
844+
#if !defined(_WIN32)
845+
addSYCLDeviceSanitizerLibs(C, IsSpirvAOT, LibSuffix, LibraryList);
846+
#endif
847+
848+
return LibraryList;
849+
}
850+
743851
/// Reads device config file to find information about the SYCL targets in
744852
/// `Targets`, and defines device traits macros accordingly.
745853
void SYCL::populateSYCLDeviceTraitsMacrosArgs(

0 commit comments

Comments
 (0)