Skip to content

Commit

Permalink
[SYCL] Move bfloat support from experimental to supported. (#6524)
Browse files Browse the repository at this point in the history
This change makes bfloat16 a supported feature.

Signed-off-by: Rajiv Deodhar <rajiv.deodhar@intel.com>
Signed-off-by: JackAKirk <jack.kirk@codeplay.com>
  • Loading branch information
rdeodhar committed Nov 28, 2022
1 parent 67f6bba commit 7b47ebb
Show file tree
Hide file tree
Showing 45 changed files with 1,554 additions and 815 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/NVPTX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void NVPTXTargetInfo::getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const {
Builder.defineMacro("__PTX__");
Builder.defineMacro("__NVPTX__");
if (Opts.CUDAIsDevice || Opts.OpenMPIsDevice) {
if (Opts.CUDAIsDevice || Opts.OpenMPIsDevice || Opts.SYCLIsDevice) {
// Set __CUDA_ARCH__ for the GPU specified.
std::string CUDAArchCode = [this] {
switch (GPU) {
Expand Down
87 changes: 87 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
#include <cstdlib> // ::getenv
#include <map>
#include <memory>
#include <regex>
#include <sstream>
#include <utility>
#if LLVM_ON_UNIX
#include <unistd.h> // getpid
Expand Down Expand Up @@ -5064,6 +5066,76 @@ class OffloadingActionBuilder final {
}
}

// Return whether to use native bfloat16 library.
bool selectBfloatLibs(const ToolChain *TC, bool &useNative) {
const OptTable &Opts = C.getDriver().getOpts();
const char *TargetOpt = nullptr;
const char *DeviceOpt = nullptr;
bool needLibs = false;
for (auto *A : Args) {
llvm::Triple *TargetBE = nullptr;

auto GetTripleIt = [&, this](llvm::StringRef Triple) {
llvm::Triple TargetTriple{Triple};
auto TripleIt = llvm::find_if(SYCLTripleList, [&](auto &SYCLTriple) {
return SYCLTriple == TargetTriple;
});
return TripleIt != SYCLTripleList.end() ? &*TripleIt : nullptr;
};

if (A->getOption().matches(options::OPT_fsycl_targets_EQ)) {
// spir64 target is actually JIT compilation, so we defer selection of
// bfloat16 libraries to runtime. For AOT we need libraries.
needLibs = TC->getTriple().getSubArch() != llvm::Triple::NoSubArch;
TargetBE = GetTripleIt(A->getValue(0));
if (TargetBE)
TargetOpt = A->getValue(0);
else
continue;
} else if (A->getOption().matches(options::OPT_Xsycl_backend_EQ)) {
// Passing device args: -Xsycl-target-backend=<triple> <opt>
TargetBE = GetTripleIt(A->getValue(0));
if (TargetBE)
DeviceOpt = A->getValue(1);
else
continue;
} else if (A->getOption().matches(options::OPT_Xsycl_backend)) {
// Passing device args: -Xsycl-target-backend <opt>
TargetBE = &SYCLTripleList.front();
DeviceOpt = A->getValue(0);
} else if (A->getOption().matches(options::OPT_Xs_separate)) {
// Passing device args: -Xs <opt>
DeviceOpt = A->getValue(0);
} else {
continue;
};
}
useNative = false;
if (needLibs)
if (TC->getTriple().getSubArch() == llvm::Triple::SPIRSubArch_gen &&
TargetOpt && DeviceOpt) {

auto checkBF = [=](std::string &Dev) {
static const std::regex BFFs("pvc.*|ats.*");
return std::regex_match(Dev, BFFs);
};

needLibs = true;
std::string Params{DeviceOpt};
size_t DevicesPos = Params.find("-device ");
useNative = false;
if (DevicesPos != std::string::npos) {
useNative = true;
std::istringstream Devices(Params.substr(DevicesPos + 8));
for (std::string S; std::getline(Devices, S, ',');) {
useNative &= checkBF(S);
}
}
}

return needLibs;
}

bool addSYCLDeviceLibs(const ToolChain *TC, ActionList &DeviceLinkObjects,
bool isSpirvAOT, bool isMSVCEnv) {
struct DeviceLibOptInfo {
Expand Down Expand Up @@ -5139,6 +5211,10 @@ class OffloadingActionBuilder final {
{"libsycl-fallback-imf", "libimf-fp32"},
{"libsycl-fallback-imf-fp64", "libimf-fp64"},
{"libsycl-fallback-imf-bf16", "libimf-bf16"}};
const SYCLDeviceLibsList sycl_device_bfloat16_fallback_lib = {
{"libsycl-fallback-bfloat16", "libm-bfloat16"}};
const SYCLDeviceLibsList sycl_device_bfloat16_native_lib = {
{"libsycl-native-bfloat16", "libm-bfloat16"}};
// ITT annotation libraries are linked in separately whenever the device
// code instrumentation is enabled.
const SYCLDeviceLibsList sycl_device_annotation_libs = {
Expand Down Expand Up @@ -5188,6 +5264,17 @@ class OffloadingActionBuilder final {
addInputs(sycl_device_wrapper_libs);
if (isSpirvAOT || TC->getTriple().isNVPTX())
addInputs(sycl_device_fallback_libs);

bool nativeBfloatLibs;
bool needBfloatLibs = selectBfloatLibs(TC, nativeBfloatLibs);
if (needBfloatLibs) {
// Add native or fallback bfloat16 library.
if (nativeBfloatLibs)
addInputs(sycl_device_bfloat16_native_lib);
else
addInputs(sycl_device_bfloat16_fallback_lib);
}

if (Args.hasFlag(options::OPT_fsycl_instrument_device_code,
options::OPT_fno_sycl_instrument_device_code, true))
addInputs(sycl_device_annotation_libs);
Expand Down
Loading

0 comments on commit 7b47ebb

Please sign in to comment.