@@ -5528,18 +5528,17 @@ class OffloadingActionBuilder final {
5528
5528
// device libraries are only needed when current toolchain is using
5529
5529
// AOT compilation.
5530
5530
bool SYCLDeviceLibLinked = false ;
5531
- if (IsSPIR || IsNVPTX) {
5531
+ Action *NativeCPULib = nullptr ;
5532
+ if (IsSPIR || IsNVPTX || IsSYCLNativeCPU) {
5532
5533
bool UseJitLink =
5533
5534
IsSPIR &&
5534
5535
Args.hasFlag (options::OPT_fsycl_device_lib_jit_link,
5535
5536
options::OPT_fno_sycl_device_lib_jit_link, false );
5536
5537
bool UseAOTLink = IsSPIR && (IsSpirvAOT || !UseJitLink);
5537
5538
SYCLDeviceLibLinked = addSYCLDeviceLibs (
5538
5539
TC, SYCLDeviceLibs, UseAOTLink,
5539
- C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment());
5540
- }
5541
- if (IsSYCLNativeCPU) {
5542
- SYCLDeviceLibLinked |= addSYCLNativeCPULibs(TC, SYCLDeviceLibs);
5540
+ C.getDefaultToolChain ().getTriple ().isWindowsMSVCEnvironment (),
5541
+ IsSYCLNativeCPU, NativeCPULib);
5543
5542
}
5544
5543
JobAction *LinkSYCLLibs =
5545
5544
C.MakeAction <LinkJobAction>(SYCLDeviceLibs, types::TY_LLVM_BC);
@@ -5622,6 +5621,15 @@ class OffloadingActionBuilder final {
5622
5621
};
5623
5622
Action *PostLinkAction = createPostLinkAction ();
5624
5623
if (IsSYCLNativeCPU) {
5624
+ if (NativeCPULib) {
5625
+ // The native cpu device lib is linked without --only-needed
5626
+ // as it contains builtins not referenced in source code but
5627
+ // needed by the native cpu backend.
5628
+ clang::driver::ActionList AllLibs = {FullDeviceLinkAction,
5629
+ NativeCPULib};
5630
+ FullDeviceLinkAction =
5631
+ C.MakeAction <LinkJobAction>(AllLibs, types::TY_LLVM_BC);
5632
+ }
5625
5633
// for SYCL Native CPU, we just take the linked device
5626
5634
// modules, lower them to an object file , and link it to the host
5627
5635
// object file.
@@ -5806,60 +5814,9 @@ class OffloadingActionBuilder final {
5806
5814
}
5807
5815
}
5808
5816
5809
- bool addSYCLNativeCPULibs(const ToolChain *TC,
5810
- ActionList &DeviceLinkObjects) {
5811
- std::string LibSpirvFile;
5812
- if (Args.hasArg(options::OPT_fsycl_libspirv_path_EQ)) {
5813
- auto ProvidedPath =
5814
- Args.getLastArgValue(options::OPT_fsycl_libspirv_path_EQ).str();
5815
- if (llvm::sys::fs::exists(ProvidedPath))
5816
- LibSpirvFile = ProvidedPath;
5817
- } else {
5818
- SmallVector<StringRef, 8> LibraryPaths;
5819
-
5820
- // Expected path w/out install.
5821
- SmallString<256> WithoutInstallPath(C.getDriver().ResourceDir);
5822
- llvm::sys::path::append(WithoutInstallPath, Twine("../../clc"));
5823
- LibraryPaths.emplace_back(WithoutInstallPath.c_str());
5824
-
5825
- // Expected path w/ install.
5826
- SmallString<256> WithInstallPath(C.getDriver().ResourceDir);
5827
- llvm::sys::path::append(WithInstallPath, Twine("../../../share/clc"));
5828
- LibraryPaths.emplace_back(WithInstallPath.c_str());
5829
-
5830
- // Select libclc variant based on target triple.
5831
- // On Windows long is 32 bits, so we have to select the right remangled
5832
- // libclc version.
5833
- std::string LibSpirvTargetName =
5834
- (TC->getAuxTriple()->isOSWindows())
5835
- ? "remangled-l32-signed_char.libspirv-"
5836
- : "remangled-l64-signed_char.libspirv-";
5837
- LibSpirvTargetName.append(TC->getTripleString() + ".bc");
5838
-
5839
- for (StringRef LibraryPath : LibraryPaths) {
5840
- SmallString<128> LibSpirvTargetFile(LibraryPath);
5841
- llvm::sys::path::append(LibSpirvTargetFile, LibSpirvTargetName);
5842
- if (llvm::sys::fs::exists(LibSpirvTargetFile) ||
5843
- Args.hasArg(options::OPT__HASH_HASH_HASH)) {
5844
- LibSpirvFile = std::string(LibSpirvTargetFile.str());
5845
- break;
5846
- }
5847
- }
5848
- }
5849
-
5850
- if (!LibSpirvFile.empty()) {
5851
- Arg *LibClcInputArg = MakeInputArg(Args, C.getDriver().getOpts(),
5852
- Args.MakeArgString(LibSpirvFile));
5853
- auto *SYCLLibClcInputAction =
5854
- C.MakeAction<InputAction>(*LibClcInputArg, types::TY_LLVM_BC);
5855
- DeviceLinkObjects.push_back(SYCLLibClcInputAction);
5856
- return true;
5857
- }
5858
- return false;
5859
- }
5860
-
5861
5817
bool addSYCLDeviceLibs (const ToolChain *TC, ActionList &DeviceLinkObjects,
5862
- bool isSpirvAOT, bool isMSVCEnv) {
5818
+ bool isSpirvAOT, bool isMSVCEnv, bool isNativeCPU,
5819
+ Action *&NativeCPULib) {
5863
5820
int NumOfDeviceLibLinked = 0 ;
5864
5821
SmallVector<SmallString<128 >, 4 > LibLocCandidates;
5865
5822
SYCLInstallation.getSYCLDeviceLibPath (LibLocCandidates);
@@ -5876,6 +5833,14 @@ class OffloadingActionBuilder final {
5876
5833
SmallString<128 > LibName (LLCandidate);
5877
5834
llvm::sys::path::append (LibName, DeviceLib);
5878
5835
if (llvm::sys::fs::exists (LibName)) {
5836
+
5837
+ // NativeCPU currently only needs libsycl-nativecpu_utils and
5838
+ // libclc, so temporarily skip other device libs in invocation.
5839
+ // Todo: remove once NativeCPU tests the other libraries.
5840
+ if (isNativeCPU &&
5841
+ !LibName.str ().contains (" libsycl-nativecpu_utils" ))
5842
+ continue ;
5843
+
5879
5844
++NumOfDeviceLibLinked;
5880
5845
Arg *InputArg = MakeInputArg (Args, C.getDriver ().getOpts (),
5881
5846
Args.MakeArgString (LibName));
@@ -5909,14 +5874,24 @@ class OffloadingActionBuilder final {
5909
5874
}
5910
5875
if (!LibLocSelected)
5911
5876
LibLocSelected = !LibLocSelected;
5877
+
5878
+ // The device link stage may remove symbols not referenced in the
5879
+ // source code. Since libsycl-nativecpu_utils contains such symbols
5880
+ // which are later needed by the NativeCPU backend passes we link
5881
+ // that library separately afterwards without --only-needed.
5882
+ if (isNativeCPU) {
5883
+ assert (!NativeCPULib);
5884
+ NativeCPULib = DeviceLinkObjects.back ();
5885
+ DeviceLinkObjects.pop_back ();
5886
+ }
5912
5887
}
5913
5888
}
5914
5889
}
5915
5890
5916
5891
// For NVPTX backend we need to also link libclc and CUDA libdevice
5917
5892
// at the same stage that we link all of the unbundled SYCL libdevice
5918
5893
// objects together.
5919
- if (TC->getTriple().isNVPTX() && NumOfDeviceLibLinked) {
5894
+ if (( TC->getTriple ().isNVPTX () || isNativeCPU ) && NumOfDeviceLibLinked) {
5920
5895
std::string LibSpirvFile;
5921
5896
if (Args.hasArg (options::OPT_fsycl_libspirv_path_EQ)) {
5922
5897
auto ProvidedPath =
@@ -5936,13 +5911,18 @@ class OffloadingActionBuilder final {
5936
5911
llvm::sys::path::append (WithInstallPath, Twine (" ../../../share/clc" ));
5937
5912
LibraryPaths.emplace_back (WithInstallPath.c_str ());
5938
5913
5914
+ // TODO: check if the isNVPTX() path can also use
5915
+ // TC->getTripleString() so that the conditional could be removed
5916
+ const std::string TrStr =
5917
+ isNativeCPU ? TC->getTripleString () : " nvptx64-nvidia-cuda" ;
5918
+
5939
5919
// Select remangled libclc variant
5940
- std::string LibSpirvTargetName =
5941
- ( TC->getAuxTriple()->isOSWindows() )
5942
- ? "remangled-l32-signed_char.libspirv-nvptx64-nvidia-cuda. "
5943
- "bc"
5944
- : "remangled-l64-signed_char.libspirv-nvptx64-nvidia-cuda."
5945
- " bc";
5920
+ StringRef LibSpirvTargetNamePref =
5921
+ TC->getAuxTriple ()->isOSWindows ()
5922
+ ? " remangled-l32-signed_char.libspirv-"
5923
+ : " remangled-l64-signed_char.libspirv- " ;
5924
+ llvm::Twine LibSpirvTargetNameTemp = LibSpirvTargetNamePref + TrStr;
5925
+ llvm::Twine LibSpirvTargetName = LibSpirvTargetNameTemp + " . bc" ;
5946
5926
5947
5927
for (StringRef LibraryPath : LibraryPaths) {
5948
5928
SmallString<128 > LibSpirvTargetFile (LibraryPath);
@@ -5954,7 +5934,6 @@ class OffloadingActionBuilder final {
5954
5934
}
5955
5935
}
5956
5936
}
5957
-
5958
5937
if (!LibSpirvFile.empty ()) {
5959
5938
Arg *LibClcInputArg = MakeInputArg (Args, C.getDriver ().getOpts (),
5960
5939
Args.MakeArgString (LibSpirvFile));
@@ -5963,6 +5942,11 @@ class OffloadingActionBuilder final {
5963
5942
DeviceLinkObjects.push_back (SYCLLibClcInputAction);
5964
5943
}
5965
5944
5945
+ if (isNativeCPU) {
5946
+ // return here to not generate cuda actions
5947
+ return NumOfDeviceLibLinked != 0 ;
5948
+ }
5949
+
5966
5950
const toolchains::CudaToolChain *CudaTC =
5967
5951
static_cast <const toolchains::CudaToolChain *>(TC);
5968
5952
for (const auto &LinkInputEnum : enumerate(DeviceLinkerInputs)) {
@@ -9238,7 +9222,10 @@ InputInfoList Driver::BuildJobsForActionNoCache(
9238
9222
Action::OffloadKind DependentOffloadKind;
9239
9223
if (UI.DependentOffloadKind == Action::OFK_SYCL &&
9240
9224
TargetDeviceOffloadKind == Action::OFK_None &&
9241
- !(isSYCLNativeCPU(Args) && isSYCLNativeCPU(C.getDefaultToolChain().getTriple(), TC->getTriple())))
9225
+ !(isSYCLNativeCPU (Args) &&
9226
+ isSYCLNativeCPU (C.getDefaultToolChain ().getTriple (),
9227
+ TC->getTriple ()) &&
9228
+ UA->getDependentActionsInfo ().size () > 1 ))
9242
9229
DependentOffloadKind = Action::OFK_Host;
9243
9230
else
9244
9231
DependentOffloadKind = UI.DependentOffloadKind ;
0 commit comments