1313#include " clang/Driver/DriverDiagnostic.h"
1414#include " clang/Driver/InputInfo.h"
1515#include " clang/Driver/Options.h"
16+ #include " llvm/ADT/SmallSet.h"
1617#include " llvm/Option/Option.h"
18+ #include " llvm/SYCLLowerIR/DeviceConfigFile.hpp"
1719#include " llvm/Support/CommandLine.h"
1820#include " llvm/Support/FileSystem.h"
1921#include " llvm/Support/Path.h"
20- #include " llvm/SYCLLowerIR/DeviceConfigFile.hpp"
2122#include < algorithm>
2223#include < sstream>
2324
@@ -299,6 +300,10 @@ bool SYCL::shouldDoPerObjectFileLinking(const Compilation &C) {
299300// Return whether to use native bfloat16 library.
300301static bool selectBfloatLibs (const llvm::Triple &Triple, const Compilation &C,
301302 bool &UseNative) {
303+
304+ static llvm::SmallSet<StringRef, 8 > GPUArchsWithNBF16{
305+ " intel_gpu_pvc" , " intel_gpu_acm_g10" , " intel_gpu_acm_g11" ,
306+ " intel_gpu_acm_g12" , " intel_gpu_bmg_g21" };
302307 const llvm::opt::ArgList &Args = C.getArgs ();
303308 bool NeedLibs = false ;
304309
@@ -324,32 +329,47 @@ static bool selectBfloatLibs(const llvm::Triple &Triple, const Compilation &C,
324329 }
325330 }
326331
327- UseNative = false ;
328-
329- // Check for intel_gpu_pvc as the target
330- if (Arg *SYCLTarget = Args.getLastArg (options::OPT_fsycl_targets_EQ)) {
331- if (SYCLTarget->getValues ().size () == 1 ) {
332- StringRef SYCLTargetStr = SYCLTarget->getValue ();
333- if (SYCLTargetStr == " intel_gpu_pvc" )
334- UseNative = true ;
335- }
336- }
337-
338- auto checkBF = [](StringRef Device) {
339- return Device.starts_with (" pvc" ) || Device.starts_with (" ats" );
340- };
332+ // We need to select fallback/native bfloat16 devicelib in AOT compilation
333+ // targetting for Intel GPU devices. Users have 2 ways to apply AOT,
334+ // 1). clang++ -fsycl -fsycl-targets=spir64_gen -Xs "-device pvc,...,"
335+ // 2). clang++ -fsycl -fsycl-targets=intel_gpu_pvc,...
336+ // We assume that users will only apply either 1) or 2) and won't mix the
337+ // 2 ways in their compiling command.
341338
342339 std::string Params;
343340 for (const auto &Arg : TargArgs) {
344341 Params += " " ;
345342 Params += Arg;
346343 }
344+
345+ auto checkBF = [](StringRef Device) {
346+ return Device.starts_with (" pvc" ) || Device.starts_with (" ats" ) ||
347+ Device.starts_with (" dg2" ) || Device.starts_with (" bmg" );
348+ };
349+
347350 size_t DevicesPos = Params.find (" -device " );
348- if (!UseNative && DevicesPos != std::string::npos) {
351+ // "-device xxx" is used to specify AOT target device.
352+ if (DevicesPos != std::string::npos) {
349353 UseNative = true ;
350354 std::istringstream Devices (Params.substr (DevicesPos + 8 ));
351355 for (std::string S; std::getline (Devices, S, ' ,' );)
352356 UseNative &= checkBF (S);
357+ return NeedLibs;
358+ } else {
359+ // -fsycl-targets=intel_gpu_xxx is used to specify AOT target device.
360+ // Multiple Intel GPU devices can be specified, native bfloat16 devicelib
361+ // can be involved only when all GPU deivces specified support native
362+ // bfloat16 native conversion.
363+ UseNative = true ;
364+ if (Arg *SYCLTarget = Args.getLastArg (options::OPT_fsycl_targets_EQ)) {
365+ for (auto TargetsV : SYCLTarget->getValues ()) {
366+ if (!GPUArchsWithNBF16.contains (StringRef (TargetsV))) {
367+ UseNative = false ;
368+ break ;
369+ }
370+ }
371+ }
372+ return NeedLibs;
353373 }
354374 }
355375 return NeedLibs;
@@ -505,7 +525,8 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
505525 }
506526
507527 if (TargetTriple.isNVPTX () && IgnoreSingleLibs)
508- LibraryList.push_back (Args.MakeArgString (" devicelib-nvptx64-nvidia-cuda.bc" ));
528+ LibraryList.push_back (
529+ Args.MakeArgString (" devicelib-nvptx64-nvidia-cuda.bc" ));
509530
510531 if (TargetTriple.isAMDGCN () && IgnoreSingleLibs)
511532 LibraryList.push_back (Args.MakeArgString (" devicelib-amdgcn-amd-amdhsa.bc" ));
0 commit comments