Skip to content

Commit dc2fc8d

Browse files
authored
[SYCL][NativeCPU] Support native_cpu in llvm::Triple. (#18783)
The initial design of NativeCPU limited it to Clang for ease of maintenance, but upcoming changes will require knowledge of NativeCPU in the LLVM parts, which changes what is easiest to maintain.
1 parent 2b4e950 commit dc2fc8d

File tree

12 files changed

+79
-47
lines changed

12 files changed

+79
-47
lines changed

clang/lib/Basic/Targets.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
119119
return nullptr;
120120

121121
case llvm::Triple::UnknownArch:
122-
// native_cpu is only known to Clang, not to LLVM.
123-
if (Triple.str() == "native_cpu")
124-
return std::make_unique<NativeCPUTargetInfo>(Triple, Opts);
125-
126122
return nullptr;
127123

128124
case llvm::Triple::arc:
@@ -843,6 +839,9 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
843839

844840
case llvm::Triple::xtensa:
845841
return std::make_unique<XtensaTargetInfo>(Triple, Opts);
842+
843+
case llvm::Triple::native_cpu:
844+
return std::make_unique<NativeCPUTargetInfo>(Triple, Opts);
846845
}
847846
}
848847
} // namespace targets

clang/lib/Driver/Driver.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,10 @@ static bool isValidSYCLTriple(llvm::Triple T) {
975975
return true;
976976

977977
// 'native_cpu' is valid for Native CPU.
978-
if (isSYCLNativeCPU(T))
978+
// TODO This checks for the exact spelling of the triple because other
979+
// spellings would fail confusingly, trying to find nonexistent builtins. This
980+
// should probably be done for NVidia and AMD too.
981+
if (T.isNativeCPU() && T.str() == "native_cpu")
979982
return true;
980983

981984
// Check for invalid SYCL device triple values.
@@ -5565,7 +5568,7 @@ class OffloadingActionBuilder final {
55655568
auto IsAMDGCN = TargetTriple.isAMDGCN();
55665569
auto IsSPIR = TargetTriple.isSPIROrSPIRV();
55675570
bool IsSpirvAOT = TargetTriple.isSPIRAOT();
5568-
bool IsSYCLNativeCPU = isSYCLNativeCPU(TargetTriple);
5571+
bool IsNativeCPU = TargetTriple.isNativeCPU();
55695572
for (const auto &Input : ListIndex) {
55705573
// No need for any conversion if we are coming in from the
55715574
// clang-offload-deps or regular compilation path.
@@ -5705,7 +5708,7 @@ class OffloadingActionBuilder final {
57055708
// AOT compilation.
57065709
bool SYCLDeviceLibLinked = false;
57075710
Action *NativeCPULib = nullptr;
5708-
if (IsSPIR || IsNVPTX || IsAMDGCN || IsSYCLNativeCPU) {
5711+
if (IsSPIR || IsNVPTX || IsAMDGCN || IsNativeCPU) {
57095712
bool UseJitLink =
57105713
IsSPIR &&
57115714
Args.hasFlag(options::OPT_fsycl_device_lib_jit_link,
@@ -5714,7 +5717,7 @@ class OffloadingActionBuilder final {
57145717
SYCLDeviceLibLinked = addSYCLDeviceLibs(
57155718
TC, SYCLDeviceLibs, UseAOTLink,
57165719
C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment(),
5717-
IsSYCLNativeCPU, NativeCPULib, BoundArch);
5720+
IsNativeCPU, NativeCPULib, BoundArch);
57185721
}
57195722
JobAction *LinkSYCLLibs =
57205723
C.MakeAction<LinkJobAction>(SYCLDeviceLibs, types::TY_LLVM_BC);
@@ -5781,11 +5784,11 @@ class OffloadingActionBuilder final {
57815784

57825785
// reflects whether current target is ahead-of-time and can't
57835786
// support runtime setting of specialization constants
5784-
bool IsAOT = IsNVPTX || IsAMDGCN || IsSpirvAOT || IsSYCLNativeCPU;
5787+
bool IsAOT = IsNVPTX || IsAMDGCN || IsSpirvAOT || IsNativeCPU;
57855788

57865789
// post link is not optional - even if not splitting, always need to
57875790
// process specialization constants
5788-
types::ID PostLinkOutType = IsSPIR || IsSYCLNativeCPU
5791+
types::ID PostLinkOutType = IsSPIR || IsNativeCPU
57895792
? types::TY_Tempfiletable
57905793
: types::TY_LLVM_BC;
57915794
auto createPostLinkAction = [&]() {
@@ -5796,7 +5799,7 @@ class OffloadingActionBuilder final {
57965799
return TypedPostLinkAction;
57975800
};
57985801
Action *PostLinkAction = createPostLinkAction();
5799-
if (IsSYCLNativeCPU) {
5802+
if (IsNativeCPU) {
58005803
if (NativeCPULib) {
58015804
// The native cpu device lib is linked without --only-needed
58025805
// as it contains builtins not referenced in source code but
@@ -9103,7 +9106,7 @@ InputInfoList Driver::BuildJobsForActionNoCache(
91039106
Action::OffloadKind DependentOffloadKind;
91049107
if (UI.DependentOffloadKind == Action::OFK_SYCL &&
91059108
TargetDeviceOffloadKind == Action::OFK_None &&
9106-
!(isSYCLNativeCPU(C.getDefaultToolChain().getTriple()) &&
9109+
!(C.getDefaultToolChain().getTriple().isNativeCPU() &&
91079110
UA->getDependentActionsInfo().size() > 1))
91089111
DependentOffloadKind = Action::OFK_Host;
91099112
else
@@ -10000,7 +10003,7 @@ const ToolChain &Driver::getOffloadToolChain(
1000010003
*HostTC, Args, Kind);
1000110004
break;
1000210005
default:
10003-
if (Kind == Action::OFK_SYCL && isSYCLNativeCPU(Target))
10006+
if (Kind == Action::OFK_SYCL && Target.isNativeCPU())
1000410007
TC = std::make_unique<toolchains::SYCLToolChain>(*this, Target, *HostTC,
1000510008
Args);
1000610009
break;

clang/lib/Driver/OffloadBundler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ bool OffloadTargetInfo::isOffloadKindCompatible(
178178
}
179179

180180
bool OffloadTargetInfo::isTripleValid() const {
181-
return !Triple.str().empty() && (Triple.getArch() != Triple::UnknownArch ||
182-
Triple.str() == "native_cpu---");
181+
return !Triple.str().empty() && Triple.getArch() != Triple::UnknownArch;
183182
}
184183

185184
bool OffloadTargetInfo::operator==(const OffloadTargetInfo &Target) const {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5560,8 +5560,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
55605560
// Adjust for SYCL NativeCPU compilations. When compiling in device mode, the
55615561
// first compilation uses the NativeCPU target for LLVM IR generation, the
55625562
// second compilation uses the host target for machine code generation.
5563-
const bool IsSYCLNativeCPU = isSYCLNativeCPU(Triple);
5564-
if (IsSYCL && IsSYCLDevice && IsSYCLNativeCPU && AuxTriple &&
5563+
if (IsSYCL && IsSYCLDevice && Triple.isNativeCPU() && AuxTriple &&
55655564
isa<AssembleJobAction>(JA)) {
55665565
Triple = *AuxTriple;
55675566
TripleStr = Triple.getTriple();
@@ -5696,7 +5695,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56965695
CmdArgs.push_back("-mllvm");
56975696
CmdArgs.push_back("-sycl-opt");
56985697
}
5699-
if (IsSYCLNativeCPU) {
5698+
if (RawTriple.isNativeCPU()) {
57005699
CmdArgs.push_back("-fsycl-is-native-cpu");
57015700
CmdArgs.push_back("-D");
57025701
CmdArgs.push_back("__SYCL_NATIVE_CPU__");
@@ -6077,14 +6076,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
60776076
CmdArgs.push_back("-fdirectives-only");
60786077
}
60796078
} else if (isa<AssembleJobAction>(JA)) {
6080-
if (IsSYCLDevice && !IsSYCLNativeCPU) {
6079+
if (IsSYCLDevice && !RawTriple.isNativeCPU()) {
60816080
CmdArgs.push_back("-emit-llvm-bc");
60826081
} else {
60836082
CmdArgs.push_back("-emit-obj");
60846083
CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
60856084
}
6086-
if (IsSYCLDevice && IsSYCLNativeCPU) {
6087-
// NativeCPU generates an initial LLVM module for an unknown target, then
6085+
if (IsSYCLDevice && RawTriple.isNativeCPU()) {
6086+
// NativeCPU generates an initial LLVM module for a dummy target, then
60886087
// compiles that for host. Avoid generating a warning for that.
60896088
CmdArgs.push_back("-Wno-override-module");
60906089
CmdArgs.push_back("-mllvm");
@@ -10886,7 +10885,7 @@ static bool shouldEmitOnlyKernelsAsEntryPoints(const ToolChain &TC,
1088610885
if (TCArgs.hasFlag(options::OPT_fno_sycl_remove_unused_external_funcs,
1088710886
options::OPT_fsycl_remove_unused_external_funcs, false))
1088810887
return false;
10889-
if (isSYCLNativeCPU(Triple))
10888+
if (Triple.isNativeCPU())
1089010889
return true;
1089110890
// When supporting dynamic linking, non-kernels in a device image can be
1089210891
// called.
@@ -10944,7 +10943,7 @@ static void getTripleBasedSYCLPostLinkOpts(const ToolChain &TC,
1094410943
if (!Triple.isAMDGCN())
1094510944
addArgs(PostLinkArgs, TCArgs, {"-emit-param-info"});
1094610945
// Enable program metadata
10947-
if (Triple.isNVPTX() || Triple.isAMDGCN() || isSYCLNativeCPU(Triple))
10946+
if (Triple.isNVPTX() || Triple.isAMDGCN() || Triple.isNativeCPU())
1094810947
addArgs(PostLinkArgs, TCArgs, {"-emit-program-metadata"});
1094910948
if (OutputType != types::TY_LLVM_BC) {
1095010949
assert(OutputType == types::TY_Tempfiletable);

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
838838
addSingleLibrary(SYCLDeviceTsanLibs[sanitizer_lib_idx]);
839839
#endif
840840

841-
if (isSYCLNativeCPU(TargetTriple))
841+
if (TargetTriple.isNativeCPU())
842842
addLibraries(SYCLNativeCpuDeviceLibs);
843843

844844
return LibraryList;
@@ -1016,7 +1016,7 @@ const char *SYCL::Linker::constructLLVMLinkCommand(
10161016
const bool IsNVPTX = this->getToolChain().getTriple().isNVPTX();
10171017
const bool IsAMDGCN = this->getToolChain().getTriple().isAMDGCN();
10181018
const bool IsSYCLNativeCPU =
1019-
isSYCLNativeCPU(this->getToolChain().getTriple());
1019+
this->getToolChain().getTriple().isNativeCPU();
10201020
StringRef LibPostfix = ".bc";
10211021
StringRef NewLibPostfix = ".new.o";
10221022
if (HostTC->getTriple().isWindowsMSVCEnvironment() &&
@@ -1160,7 +1160,7 @@ void SYCL::Linker::ConstructJob(Compilation &C, const JobAction &JA,
11601160
assert((getToolChain().getTriple().isSPIROrSPIRV() ||
11611161
getToolChain().getTriple().isNVPTX() ||
11621162
getToolChain().getTriple().isAMDGCN() ||
1163-
isSYCLNativeCPU(getToolChain().getTriple())) &&
1163+
getToolChain().getTriple().isNativeCPU()) &&
11641164
"Unsupported target");
11651165

11661166
std::string SubArchName =
@@ -1552,7 +1552,7 @@ static ArrayRef<options::ID> getUnsupportedOpts() {
15521552
// Currently supported options by SYCL NativeCPU device compilation
15531553
static inline bool SupportedByNativeCPU(const llvm::Triple &Triple,
15541554
const OptSpecifier &Opt) {
1555-
if (!isSYCLNativeCPU(Triple))
1555+
if (!Triple.isNativeCPU())
15561556
return false;
15571557

15581558
switch (Opt.getID()) {
@@ -2001,7 +2001,7 @@ Tool *SYCLToolChain::buildBackendCompiler() const {
20012001
}
20022002

20032003
Tool *SYCLToolChain::buildLinker() const {
2004-
assert(getTriple().isSPIROrSPIRV() || isSYCLNativeCPU(getTriple()));
2004+
assert(getTriple().isSPIROrSPIRV() || getTriple().isNativeCPU());
20052005
return new tools::SYCL::Linker(*this);
20062006
}
20072007

clang/lib/Driver/ToolChains/SYCL.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,6 @@ class LLVM_LIBRARY_VISIBILITY BackendCompiler : public Tool {
258258
} // end namespace SYCL
259259
} // end namespace tools
260260

261-
inline bool isSYCLNativeCPU(const llvm::Triple &Triple) {
262-
return Triple.getArch() == llvm::Triple::UnknownArch &&
263-
Triple.str() == "native_cpu";
264-
}
265-
266261
namespace toolchains {
267262

268263
class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
@@ -303,12 +298,12 @@ class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
303298

304299
bool useIntegratedAs() const override { return true; }
305300
bool isPICDefault() const override {
306-
if (isSYCLNativeCPU(this->getTriple()))
301+
if (this->getTriple().isNativeCPU())
307302
return this->HostTC.isPICDefault();
308303
return false;
309304
}
310305
llvm::codegenoptions::DebugInfoFormat getDefaultDebugFormat() const override {
311-
if (isSYCLNativeCPU(this->getTriple()) &&
306+
if (this->getTriple().isNativeCPU() &&
312307
this->HostTC.getTriple().isWindowsMSVCEnvironment())
313308
return this->HostTC.getDefaultDebugFormat();
314309
return ToolChain::getDefaultDebugFormat();

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,8 @@ getTripleBasedSYCLPostLinkOpts(const ArgList &Args,
648648
SmallVector<StringRef, 8> &PostLinkArgs,
649649
const llvm::Triple Triple) {
650650
const llvm::Triple HostTriple(Args.getLastArgValue(OPT_host_triple_EQ));
651-
bool SYCLNativeCPU = Triple.str() == "native_cpu";
652651
bool SpecConstsSupported = (!Triple.isNVPTX() && !Triple.isAMDGCN() &&
653-
!Triple.isSPIRAOT() && !SYCLNativeCPU);
652+
!Triple.isSPIRAOT() && !Triple.isNativeCPU());
654653
if (SpecConstsSupported)
655654
PostLinkArgs.push_back("-spec-const=native");
656655
else
@@ -677,15 +676,15 @@ getTripleBasedSYCLPostLinkOpts(const ArgList &Args,
677676
// TODO: Try to extend this feature for non-Intel GPUs.
678677
if ((!Args.hasFlag(OPT_no_sycl_remove_unused_external_funcs,
679678
OPT_sycl_remove_unused_external_funcs, false) &&
680-
!SYCLNativeCPU) &&
679+
!Triple.isNativeCPU()) &&
681680
!Args.hasArg(OPT_sycl_allow_device_image_dependencies) &&
682681
!Triple.isNVPTX() && !Triple.isAMDGPU())
683682
PostLinkArgs.push_back("-emit-only-kernels-as-entry-points");
684683

685684
if (!Triple.isAMDGCN())
686685
PostLinkArgs.push_back("-emit-param-info");
687686
// Enable program metadata
688-
if (Triple.isNVPTX() || Triple.isAMDGCN() || SYCLNativeCPU)
687+
if (Triple.isNVPTX() || Triple.isAMDGCN() || Triple.isNativeCPU())
689688
PostLinkArgs.push_back("-emit-program-metadata");
690689

691690
bool SplitEsimdByDefault = Triple.isSPIROrSPIRV();
@@ -1568,7 +1567,7 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
15681567
return ClangPath.takeError();
15691568

15701569
llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ));
1571-
if (Triple.str() == "native_cpu")
1570+
if (Triple.isNativeCPU())
15721571
Triple = llvm::Triple(Args.getLastArgValue(OPT_host_triple_EQ));
15731572

15741573
StringRef Arch = Args.getLastArgValue(OPT_arch_EQ);
@@ -1722,10 +1721,12 @@ Expected<StringRef> linkDevice(ArrayRef<StringRef> InputFiles,
17221721
}
17231722
case Triple::loongarch64:
17241723
return generic::clang(InputFiles, Args, IsSYCLKind);
1725-
default:
1726-
if (Triple.str() == "native_cpu" && IsSYCLKind)
1724+
case Triple::native_cpu:
1725+
if (IsSYCLKind)
17271726
return generic::clang(InputFiles, Args, IsSYCLKind);
1728-
1727+
return createStringError(Triple.getArchName() +
1728+
" linking is not supported other than for SYCL");
1729+
default:
17291730
return createStringError(Triple.getArchName() +
17301731
" linking is not supported");
17311732
}

clang/tools/clang-offload-deps/ClangOffloadDeps.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ int main(int argc, const char **argv) {
120120
.Case("fpga", true)
121121
.Default(false);
122122

123-
bool TripleIsValid = Triple(Triples[I]).getArch() != Triple::UnknownArch ||
124-
Triples[I] == "native_cpu";
123+
bool TripleIsValid = Triple(Triples[I]).getArch() != Triple::UnknownArch;
125124

126125
if (!KindIsValid || !TripleIsValid) {
127126
SmallVector<char, 128u> Buf;
@@ -212,7 +211,7 @@ int main(int argc, const char **argv) {
212211
// global variable llvm.used to represent a reference to a symbol. But for
213212
// other targets we have to create a real reference since llvm.used may
214213
// not be representable in the object file.
215-
if (Triples[I] == "native_cpu") {
214+
if (Triple(Triples[I]).isNativeCPU()) {
216215
// SYCL Native CPU doesn't need deps from clang-offload-deps.
217216
} else if (Kinds[I] == "sycl" || Triple(Triples[I]).isSPIR()) {
218217
auto *GV = new GlobalVariable(

clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ class BinaryWrapper {
11011101
Bin = addELFNotes(Bin, Img.File);
11021102
}
11031103
std::pair<Constant *, Constant *> Fbin;
1104-
if (Img.Tgt == "native_cpu") {
1104+
if (Triple(Img.Tgt).isNativeCPU()) {
11051105
auto FBinOrErr = addDeclarationsForNativeCPU(Img.EntriesFile);
11061106
if (!FBinOrErr)
11071107
return FBinOrErr.takeError();

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Triple {
105105
renderscript32, // 32-bit RenderScript
106106
renderscript64, // 64-bit RenderScript
107107
fpga, // Intel FPGA
108+
native_cpu, // Native CPU
108109
ve, // NEC SX-Aurora Vector Engine
109110
LastArchType = ve
110111
};
@@ -1087,6 +1088,9 @@ class Triple {
10871088
return getArch() == Triple::csky;
10881089
}
10891090

1091+
/// Tests whether the target is Native CPU.
1092+
bool isNativeCPU() const { return getArch() == Triple::native_cpu; }
1093+
10901094
/// Tests whether the target is the Apple "arm64e" AArch64 subarch.
10911095
bool isArm64e() const {
10921096
return getArch() == Triple::aarch64 &&

0 commit comments

Comments
 (0)