-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SYCL] Move bfloat support from experimental to supported. #6524
Changes from 45 commits
6014cef
bdd88e5
73ed541
0fe1884
feb9d5f
129f53f
2115f09
3c2eb80
74aa175
bd05711
f8e894c
2ad68f6
4b78c03
0fce16d
4bcb383
35308f8
fa045e2
3322d6a
b12fd94
87b0f09
f217eb4
a908b11
aab4c78
a2568ba
4d7a22b
38e5ad4
b9accad
ca7880a
dc3b2b5
c955d36
1aa6ad3
ff04ce1
802f502
8d7f46a
190f2a3
84c50f3
df058ba
fed4d1d
28259d0
c11115b
6b05a2a
a82d73a
3fc8885
1ec6838
105094b
432e775
c135643
4eca414
8876ac8
f0f2727
17673bf
1094b8c
8d40228
c5a85cf
cf8f6e0
5e50646
45d3e70
a7be718
cac1c18
208c09a
46f406d
6830857
46e5278
10fc9a3
6195545
437e34a
09dc4c5
386353e
0f93586
48f3cac
d33cb10
28992c2
ec28c8b
b958fc7
ec70b20
1b86012
3e1e681
8c633d3
1a59e03
b2fd6cc
fab2e54
35b8910
a05c872
ac5f603
6d45ed1
077d0fe
2ff6a9d
d7c80ee
20d13df
cd1d0a2
4bf60b9
45c32f7
5de1bf7
6ec2bb9
49e9cd1
2065060
e24e57b
41098ab
37b05f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5081,6 +5081,62 @@ class OffloadingActionBuilder final { | |
} | ||
} | ||
|
||
// Return whether to use native bfloat16 library. | ||
bool useNativeBfloat(const ToolChain *TC, bool &isAOT) { | ||
isAOT = false; | ||
if (!TC->getTriple().isSPIR()) | ||
return false; | ||
|
||
const OptTable &Opts = C.getDriver().getOpts(); | ||
const char *TargetOpt = nullptr; | ||
const char *DeviceOpt = nullptr; | ||
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)) { | ||
// Passing arg: -fsycl-targets=<targets>. | ||
isAOT = true; | ||
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=val. | ||
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=val. | ||
TargetBE = &SYCLTripleList.front(); | ||
DeviceOpt = A->getValue(0); | ||
} else { | ||
mdtoguchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue; | ||
}; | ||
} | ||
if (TC->getTriple().getSubArch() != llvm::Triple::SPIRSubArch_gen) | ||
return false; | ||
|
||
if (TargetOpt && DeviceOpt) { | ||
// Currently we support only single AOT target for bfloat16. | ||
if (!(strstr(TargetOpt, "*") || strstr(TargetOpt, ","))) | ||
return strstr(DeviceOpt, "pvc") || strstr(DeviceOpt, "ats"); | ||
else | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
bool addSYCLDeviceLibs(const ToolChain *TC, ActionList &DeviceLinkObjects, | ||
bool isSpirvAOT, bool isMSVCEnv) { | ||
struct DeviceLibOptInfo { | ||
|
@@ -5094,7 +5150,8 @@ class OffloadingActionBuilder final { | |
// of "internal" libraries cannot be affected via -fno-sycl-device-lib. | ||
llvm::StringMap<bool> devicelib_link_info = { | ||
{"libc", true}, {"libm-fp32", true}, {"libm-fp64", true}, | ||
{"libimf-fp32", true}, {"libimf-fp64", true}, {"internal", true}}; | ||
{"libimf-fp32", true}, {"libimf-fp64", true}, {"libm-bfloat16", true}, | ||
{"internal", true}}; | ||
if (Arg *A = Args.getLastArg(options::OPT_fsycl_device_lib_EQ, | ||
options::OPT_fno_sycl_device_lib_EQ)) { | ||
if (A->getValues().size() == 0) | ||
|
@@ -5153,6 +5210,10 @@ class OffloadingActionBuilder final { | |
{"libsycl-fallback-cmath-fp64", "libm-fp64"}, | ||
{"libsycl-fallback-imf", "libimf-fp32"}, | ||
{"libsycl-fallback-imf-fp64", "libimf-fp64"}}; | ||
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 = { | ||
|
@@ -5202,6 +5263,18 @@ class OffloadingActionBuilder final { | |
addInputs(sycl_device_wrapper_libs); | ||
if (isSpirvAOT || TC->getTriple().isNVPTX()) | ||
addInputs(sycl_device_fallback_libs); | ||
|
||
bool isAOT; | ||
bool useNativeBfloatLib = useNativeBfloat(TC, isAOT); | ||
if (isAOT && | ||
TC->getTriple().getSubArch() != llvm::Triple::SPIRSubArch_fpga) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fpga check is unnecessary given your explicit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that was an oversight. |
||
// Add native or fallback bfloat16 library. | ||
if (useNativeBfloatLib) | ||
addInputs(sycl_device_bfloat16_native_lib); | ||
else | ||
addInputs(sycl_device_bfloat16_fallback_lib); | ||
} | ||
mdtoguchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (Args.hasFlag(options::OPT_fsycl_instrument_device_code, | ||
options::OPT_fno_sycl_instrument_device_code, true)) | ||
addInputs(sycl_device_annotation_libs); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//==--- bfloat16_wrapper.cpp - wrappers for bfloat16 library functions ----==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===---------------------------------------------------------------------===// | ||
|
||
#include "device.h" | ||
|
||
#ifdef __SPIR__ | ||
|
||
#include <CL/__spirv/spirv_ops.hpp> | ||
#include <cstdint> | ||
|
||
DEVICE_EXTERN_C_INLINE | ||
uint16_t __devicelib_ConvertFToBF16INTEL(const float &x) { | ||
return __spirv_ConvertFToBF16INTEL(x); | ||
} | ||
|
||
DEVICE_EXTERN_C_INLINE | ||
float __devicelib_ConvertBF16ToFINTEL(const uint16_t &x) { | ||
return __spirv_ConvertBF16ToFINTEL(x); | ||
} | ||
|
||
#endif // __SPIR__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//==------- fallback-bfloat16.cpp - bfloat16 conversions in software -------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===---------------------------------------------------------------------===// | ||
|
||
#include "device.h" | ||
|
||
#ifdef __SPIR__ | ||
|
||
#include <cstdint> | ||
|
||
// To support fallback device libraries on-demand loading, please update the | ||
// DeviceLibFuncMap in llvm/tools/sycl-post-link/sycl-post-link.cpp if you add | ||
// or remove any item in this file. | ||
// TODO: generate the DeviceLibFuncMap in sycl-post-link.cpp automatically | ||
// during the build based on libdevice to avoid manually sync. | ||
|
||
DEVICE_EXTERN_C_INLINE uint16_t | ||
__devicelib_ConvertFToBF16INTEL(const float &a) { | ||
// In case float value is nan - propagate bfloat16's qnan | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this code commented off? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding necessary changes to llvm-test-suite. They are in the corresponding PR intel/llvm-test-suite#1129. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't that mean that the emulation is broken, then? Is there another way to test for NaN? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use here and later a classic |
||
if (__spirv_IsNan(a)) | ||
return 0xffc1; | ||
union { | ||
uint32_t intStorage; | ||
float floatValue; | ||
}; | ||
floatValue = a; | ||
// Do RNE and truncate | ||
uint32_t roundingBias = ((intStorage >> 16) & 0x1) + 0x00007FFF; | ||
return static_cast<uint16_t>((intStorage + roundingBias) >> 16); | ||
} | ||
|
||
DEVICE_EXTERN_C_INLINE float | ||
__devicelib_ConvertBF16ToFINTEL(const uint16_t &a) { | ||
union { | ||
uint32_t intStorage; | ||
float floatValue; | ||
}; | ||
intStorage = a << 16; | ||
return floatValue; | ||
} | ||
|
||
#endif // __SPIR__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case
-fsycl-targets=spir64
will also setisAOT
totrue
. Is this the intention? Thespir64
target isn't considered AOT.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed the bool variable to be more meaningful. It tells whether addition of bfloat libs is needed.