@@ -1310,9 +1310,6 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
1310
1310
}
1311
1311
}
1312
1312
}
1313
- // Define macros associated with `any_device_has/all_devices_have` according
1314
- // to the aspects defined in the DeviceConfigFile for the SYCL targets.
1315
- populateSYCLDeviceTraitsMacrosArgs (C.getInputArgs (), UniqueSYCLTriplesVec);
1316
1313
// We'll need to use the SYCL and host triples as the key into
1317
1314
// getOffloadingDeviceToolChain, because the device toolchains we're
1318
1315
// going to create will depend on both.
@@ -6261,6 +6258,101 @@ class OffloadingActionBuilder final {
6261
6258
return FinalDeviceSections;
6262
6259
}
6263
6260
6261
+ // / Reads device config file to find information about the SYCL targets in
6262
+ // / `Targets`, and defines device traits macros accordingly.
6263
+ void populateSYCLDeviceTraitsMacrosArgs (
6264
+ Compilation &C, DerivedArgList &Args,
6265
+ SmallVector<DeviceTargetInfo, 4 > &Targets) const {
6266
+ if (Targets.empty ())
6267
+ return ;
6268
+
6269
+ const auto &TargetTable = DeviceConfigFile::TargetTable;
6270
+ std::map<StringRef, unsigned int > AllDevicesHave;
6271
+ std::map<StringRef, bool > AnyDeviceHas;
6272
+ bool AnyDeviceHasAnyAspect = false ;
6273
+ unsigned int ValidTargets = 0 ;
6274
+ for (const auto &[TC, BoundArch] : Targets) {
6275
+ assert (TC && " Invalid SYCL Offload Toolchain" );
6276
+ // Try and find the device arch, if it's empty, try to search for either
6277
+ // the whole Triple or just the 'ArchName' string.
6278
+ auto TargetIt = TargetTable.end ();
6279
+ const llvm::Triple &TargetTriple = TC->getTriple ();
6280
+ const StringRef TargetArch{BoundArch};
6281
+ if (!TargetArch.empty ()) {
6282
+ TargetIt = llvm::find_if (TargetTable, [&](const auto &Value) {
6283
+ using namespace tools ::SYCL;
6284
+ StringRef Device{Value.first };
6285
+ if (Device.consume_front (gen::AmdGPU))
6286
+ return TargetArch.equals (Device) && TargetTriple.isAMDGCN ();
6287
+ if (Device.consume_front (gen::NvidiaGPU))
6288
+ return TargetArch.equals (Device) && TargetTriple.isNVPTX ();
6289
+ if (Device.consume_front (gen::IntelGPU))
6290
+ return TargetArch.equals (Device) && TargetTriple.isSPIRAOT ();
6291
+ return TargetArch.equals (Device) && isValidSYCLTriple (TargetTriple);
6292
+ });
6293
+ } else {
6294
+ TargetIt = TargetTable.find (TargetTriple.str ());
6295
+ if (TargetIt == TargetTable.end ())
6296
+ TargetIt = TargetTable.find (TargetTriple.getArchName ().str ());
6297
+ }
6298
+
6299
+ if (TargetIt != TargetTable.end ()) {
6300
+ const DeviceConfigFile::TargetInfo &Info = (*TargetIt).second ;
6301
+ ++ValidTargets;
6302
+ const auto &AspectList = Info.aspects ;
6303
+ const auto &MaySupportOtherAspects = Info.maySupportOtherAspects ;
6304
+ if (!AnyDeviceHasAnyAspect)
6305
+ AnyDeviceHasAnyAspect = MaySupportOtherAspects;
6306
+ for (const auto &aspect : AspectList) {
6307
+ // If target has an entry in the config file, the set of aspects
6308
+ // supported by all devices supporting the target is 'AspectList'.
6309
+ // If there's no entry, such set is empty.
6310
+ const auto &AspectIt = AllDevicesHave.find (aspect);
6311
+ if (AspectIt != AllDevicesHave.end ())
6312
+ ++AllDevicesHave[aspect];
6313
+ else
6314
+ AllDevicesHave[aspect] = 1 ;
6315
+ // If target has an entry in the config file AND
6316
+ // 'MaySupportOtherAspects' is false, the set of aspects supported
6317
+ // by any device supporting the target is 'AspectList'. If there's
6318
+ // no entry OR 'MaySupportOtherAspects' is true, such set contains
6319
+ // all the aspects.
6320
+ AnyDeviceHas[aspect] = true ;
6321
+ }
6322
+ }
6323
+ }
6324
+
6325
+ // If there's no entry for the target in the device config file, the set
6326
+ // of aspects supported by any device supporting the target contains all
6327
+ // the aspects.
6328
+ if (ValidTargets == 0 )
6329
+ AnyDeviceHasAnyAspect = true ;
6330
+
6331
+ const Driver &D = C.getDriver ();
6332
+ if (AnyDeviceHasAnyAspect) {
6333
+ // There exists some target that supports any given aspect.
6334
+ constexpr static StringRef MacroAnyDeviceAnyAspect{
6335
+ " -D__SYCL_ANY_DEVICE_HAS_ANY_ASPECT__=1" };
6336
+ D.addSYCLDeviceTraitsMacroArg (Args, MacroAnyDeviceAnyAspect);
6337
+ } else {
6338
+ // Some of the aspects are not supported at all by any of the targets.
6339
+ // Thus, we need to define individual macros for each supported aspect.
6340
+ for (const auto &[TargetKey, SupportedTarget] : AnyDeviceHas) {
6341
+ assert (SupportedTarget);
6342
+ const SmallString<64 > MacroAnyDevice{
6343
+ {" -D__SYCL_ANY_DEVICE_HAS_" , TargetKey, " __=1" }};
6344
+ D.addSYCLDeviceTraitsMacroArg (Args, MacroAnyDevice);
6345
+ }
6346
+ }
6347
+ for (const auto &[TargetKey, SupportedTargets] : AllDevicesHave) {
6348
+ if (SupportedTargets != ValidTargets)
6349
+ continue ;
6350
+ const SmallString<64 > MacroAllDevices{
6351
+ {" -D__SYCL_ALL_DEVICES_HAVE_" , TargetKey, " __=1" }};
6352
+ D.addSYCLDeviceTraitsMacroArg (Args, MacroAllDevices);
6353
+ }
6354
+ }
6355
+
6264
6356
bool initialize () override {
6265
6357
using namespace tools ::SYCL;
6266
6358
// Get the SYCL toolchains. If we don't get any, the action builder will
@@ -6520,6 +6612,11 @@ class OffloadingActionBuilder final {
6520
6612
checkForOffloadMismatch (C, Args, SYCLTargetInfoList);
6521
6613
checkForMisusedAddDefaultSpecConstsImageFlag (C, Args, SYCLTargetInfoList);
6522
6614
6615
+ // Define macros associated with `any_device_has/all_devices_have`
6616
+ // according to the aspects defined in the DeviceConfigFile for the SYCL
6617
+ // targets.
6618
+ populateSYCLDeviceTraitsMacrosArgs (C, Args, SYCLTargetInfoList);
6619
+
6523
6620
DeviceLinkerInputs.resize (SYCLTargetInfoList.size ());
6524
6621
return false ;
6525
6622
}
@@ -10410,92 +10507,6 @@ llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args,
10410
10507
return llvm::Error::success ();
10411
10508
}
10412
10509
10413
- void Driver::populateSYCLDeviceTraitsMacrosArgs (
10414
- const llvm::opt::ArgList &Args,
10415
- const llvm::SmallVector<llvm::Triple, 4 > &UniqueSYCLTriplesVec) {
10416
- const auto &TargetTable = DeviceConfigFile::TargetTable;
10417
- std::map<StringRef, unsigned int > AllDevicesHave;
10418
- std::map<StringRef, bool > AnyDeviceHas;
10419
- bool AnyDeviceHasAnyAspect = false ;
10420
- unsigned int ValidTargets = 0 ;
10421
- for (const auto &TargetTriple : UniqueSYCLTriplesVec) {
10422
- // Try and find the whole triple, if there's no match, remove parts of the
10423
- // triple from the end to find partial matches.
10424
- auto TargetTripleStr = TargetTriple.getTriple ();
10425
- bool Found = false ;
10426
- bool EmptyTriple = false ;
10427
- auto TripleIt = TargetTable.end ();
10428
- while (!Found && !EmptyTriple) {
10429
- TripleIt = TargetTable.find (TargetTripleStr);
10430
- Found = (TripleIt != TargetTable.end ());
10431
- if (!Found) {
10432
- auto Pos = TargetTripleStr.find_last_of (' -' );
10433
- EmptyTriple = (Pos == std::string::npos);
10434
- TargetTripleStr =
10435
- EmptyTriple ? TargetTripleStr : TargetTripleStr.substr (0 , Pos);
10436
- }
10437
- }
10438
- if (Found) {
10439
- assert (TripleIt != TargetTable.end ());
10440
- const auto &TargetInfo = (*TripleIt).second ;
10441
- ++ValidTargets;
10442
- const auto &AspectList = TargetInfo.aspects ;
10443
- const auto &MaySupportOtherAspects = TargetInfo.maySupportOtherAspects ;
10444
- if (!AnyDeviceHasAnyAspect)
10445
- AnyDeviceHasAnyAspect = MaySupportOtherAspects;
10446
- for (const auto &aspect : AspectList) {
10447
- // If target has an entry in the config file, the set of aspects
10448
- // supported by all devices supporting the target is 'AspectList'. If
10449
- // there's no entry, such set is empty.
10450
- const auto &AspectIt = AllDevicesHave.find (aspect);
10451
- if (AspectIt != AllDevicesHave.end ())
10452
- ++AllDevicesHave[aspect];
10453
- else
10454
- AllDevicesHave[aspect] = 1 ;
10455
- // If target has an entry in the config file AND
10456
- // 'MaySupportOtherAspects' is false, the set of aspects supported by
10457
- // any device supporting the target is 'AspectList'. If there's no
10458
- // entry OR 'MaySupportOtherAspects' is true, such set contains all
10459
- // the aspects.
10460
- AnyDeviceHas[aspect] = true ;
10461
- }
10462
- }
10463
- }
10464
-
10465
- if (ValidTargets == 0 ) {
10466
- // If there's no entry for the target in the device config file, the set
10467
- // of aspects supported by any device supporting the target contains all
10468
- // the aspects.
10469
- AnyDeviceHasAnyAspect = true ;
10470
- }
10471
-
10472
- if (AnyDeviceHasAnyAspect) {
10473
- // There exists some target that supports any given aspect.
10474
- SmallString<64 > MacroAnyDeviceAnyAspect (
10475
- " -D__SYCL_ANY_DEVICE_HAS_ANY_ASPECT__=1" );
10476
- SYCLDeviceTraitsMacrosArgs.push_back (
10477
- Args.MakeArgString (MacroAnyDeviceAnyAspect));
10478
- } else {
10479
- // Some of the aspects are not supported at all by any of the targets.
10480
- // Thus, we need to define individual macros for each supported aspect.
10481
- for (const auto &[TargetKey, SupportedTarget] : AnyDeviceHas) {
10482
- assert (SupportedTarget);
10483
- SmallString<64 > MacroAnyDevice (" -D__SYCL_ANY_DEVICE_HAS_" );
10484
- MacroAnyDevice += TargetKey;
10485
- MacroAnyDevice += " __=1" ;
10486
- SYCLDeviceTraitsMacrosArgs.push_back (Args.MakeArgString (MacroAnyDevice));
10487
- }
10488
- }
10489
- for (const auto &[TargetKey, SupportedTargets] : AllDevicesHave) {
10490
- if (SupportedTargets != ValidTargets)
10491
- continue ;
10492
- SmallString<64 > MacroAllDevices (" -D__SYCL_ALL_DEVICES_HAVE_" );
10493
- MacroAllDevices += TargetKey;
10494
- MacroAllDevices += " __=1" ;
10495
- SYCLDeviceTraitsMacrosArgs.push_back (Args.MakeArgString (MacroAllDevices));
10496
- }
10497
- }
10498
-
10499
10510
static const char *GetStableCStr (llvm::StringSet<> &SavedStrings, StringRef S) {
10500
10511
return SavedStrings.insert (S).first ->getKeyData ();
10501
10512
}
0 commit comments