@@ -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.
745853void SYCL::populateSYCLDeviceTraitsMacrosArgs (
0 commit comments