Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,11 @@ endif()
if(LLVM_VERSION_MAJOR GREATER_EQUAL 23)
set(HOST_DEVICE_SPV_EXTENSIONS "${HOST_DEVICE_SPV_EXTENSIONS},+SPV_KHR_float_controls2")
endif()
# SPV_KHR_bfloat16: a bfloat16 type for loads, stores and conversions to and
# from other floating-point types. The translator maps it to LLVM's bfloat.
if(HAVE_LLVM_SPIRV_KHR_BFLOAT16)
set(HOST_DEVICE_SPV_EXTENSIONS "${HOST_DEVICE_SPV_EXTENSIONS},+SPV_KHR_bfloat16")
endif()
# The SPIR-V side of cl_intel_subgroups: shuffles (also of vectors) and buffer block
# reads/writes. cl_intel_spirv_subgroups is not advertised, as it also requires the
# image block reads/writes.
Expand Down
21 changes: 21 additions & 0 deletions cmake/SetupLLVMSPIRV.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ if(HAVE_LLVM_SPIRV_LIB)
set(LLVM_SPIRV_LIB_HAS_ERROR_HANDLING 0 CACHE BOOL "libLLVMSPIRV has TranslatorOpts::setErrorHandlingKind")
endif()
endif()

# The translator reads SPV_KHR_bfloat16 since LLVM 21, and since 18.1.14,
# 19.1.8 and 20.1.3 on the older release branches. Not cached, so that it
# follows changes of the translator.
file(READ "${LLVM_SPIRV_INCLUDEDIR}/LLVMSPIRVExtensions.inc" LLVMSPIRVEXTENSIONS_CONTENT)
if(LLVMSPIRVEXTENSIONS_CONTENT MATCHES "EXT\\(SPV_KHR_bfloat16\\)")
message(STATUS "libLLVMSPIRV supports SPV_KHR_bfloat16")
set(LLVM_SPIRV_LIB_HAS_KHR_BFLOAT16 1)
else()
message(STATUS "libLLVMSPIRV is too old for SPV_KHR_bfloat16")
set(LLVM_SPIRV_LIB_HAS_KHR_BFLOAT16 0)
endif()
else()
message(STATUS "LLVMSPIRV library NOT found: ${LLVM_SPIRV_INCLUDEDIR} | ${LLVM_SPIRV_LIB}")
endif()
Expand All @@ -105,6 +117,15 @@ set_expr(HAVE_SPIRV_LINK TARGET_SPIRV_LINK)
set_expr(HAVE_LLVM_SPIRV TARGET_LLVM_SPIRV)
set_expr(HAVE_LLVM_OPT TARGET_LLVM_OPT)

# Without the library, the llvm-spirv binary is used, whose extensions can't be
# queried; every translator release for LLVM 21 and later reads SPV_KHR_bfloat16.
if(HAVE_LLVM_SPIRV_LIB)
set_expr(HAVE_LLVM_SPIRV_KHR_BFLOAT16 LLVM_SPIRV_LIB_HAS_KHR_BFLOAT16)
else()
set_expr(HAVE_LLVM_SPIRV_KHR_BFLOAT16
HAVE_LLVM_SPIRV AND (LLVM_VERSION_MAJOR GREATER_EQUAL 21))
endif()

if(HAVE_SPIRV_LINK AND (NOT DEFINED SPIRV_LINK_HAS_USE_HIGHEST_VERSION)
AND NOT CMAKE_CROSSCOMPILING)
# TODO crosscompiling
Expand Down
3 changes: 3 additions & 0 deletions lib/CL/devices/spirv.hh
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,9 @@ enum class Capability : unsigned {
ShaderClockKHR = 5055,
ShaderEnqueueAMDX = 5067,
QuadControlKHR = 5087,
BFloat16TypeKHR = 5116,
BFloat16DotProductKHR = 5117,
BFloat16CooperativeMatrixKHR = 5118,
SampleMaskOverrideCoverageNV = 5249,
GeometryShaderPassthroughNV = 5251,
ShaderViewportIndexLayerEXT = 5254,
Expand Down
7 changes: 7 additions & 0 deletions lib/CL/devices/spirv_queries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ void pocl_setup_spirv_queries(cl_device_id dev) {
static_cast<cl_uint>(spv::Capability::FloatControls2));
}

if (dev->supported_spirv_extensions &&
strstr(dev->supported_spirv_extensions, "SPV_KHR_bfloat16")) {
SpirvExtensions.push_back("SPV_KHR_bfloat16");
SpirvCapabilities.push_back(
static_cast<cl_uint>(spv::Capability::BFloat16TypeKHR));
}

if (strstr(dev->extensions, "cl_intel_spirv_subgroups")) {
SpirvExtensions.push_back("SPV_INTEL_subgroups");
SpirvCapabilities.push_back(
Expand Down
51 changes: 49 additions & 2 deletions lib/CL/pocl_llvm_orc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,44 @@ static double poclExtendHFDF2(_Float16 A) {
}
#endif

#if defined(__x86_64__) || defined(_M_X64)
/* On x86-64 CPUs without native bfloat16 conversions, codegen calls
__truncsfbf2 and __truncdfbf2 for float/double -> bfloat. libgcc only has
them since GCC 13, and on Windows they have the problems of the FP16 helpers
above, so hand kernels our own. A bfloat16 is returned in the low 16 bits of
XMM0, like a float whose low half holds its bits, which spares us from
needing __bf16 support in the compiler that builds PoCL. */
static float poclBF16Result(uint32_t H) {
float R;
__builtin_memcpy(&R, &H, sizeof R);
return R;
}
/* Rounds the bits of a float to the bits of a bfloat16, to nearest even. */
static uint32_t poclFloatBitsToBF16(uint32_t U) {
if ((U & 0x7fffffffu) > 0x7f800000u) // NaN: keep it, but quiet
return (U >> 16) | 0x40u;
return (U + 0x7fffu + ((U >> 16) & 1u)) >> 16;
}
static float poclTruncSFBF2(float A) {
uint32_t U;
__builtin_memcpy(&U, &A, sizeof U);
return poclBF16Result(poclFloatBitsToBF16(U));
}
static float poclTruncDFBF2(double A) {
/* Round to float with round-to-odd first, so that rounding that to
bfloat16 gives the correctly rounded result. */
float F = (float)A;
uint32_t U;
__builtin_memcpy(&U, &F, sizeof U);
if (F == F && (double)F != A) {
if (__builtin_fabs((double)F) > __builtin_fabs(A))
U -= 1;
U |= 1;
}
return poclBF16Result(poclFloatBitsToBF16(U));
}
#endif

namespace {

/* Process-global JIT shared by all CPU host devices, created on the first
Expand Down Expand Up @@ -436,8 +474,9 @@ static bool addLazySVMLGenerator(const char *IRCPath, const char *SVMLPath) {
}

/* Inject symbols that kernel objects reference but that are linked into libpocl
rather than exported by a process library: PoCL's own host callbacks and, on
Windows, the libgcc/compiler-rt stack probe. Defining them as absolute
rather than exported by a process library: PoCL's own host callbacks, the
bfloat16 conversions on x86-64 and, on Windows, the libgcc/compiler-rt stack
probe and FP16 conversions. Defining them as absolute
symbols makes resolution independent of libpocl's (deliberately hidden)
dynamic-symbol visibility. They are constant for the process, so this is done
once at init into the process-symbols JITDylib that every kernel JITDylib
Expand Down Expand Up @@ -467,6 +506,14 @@ void defineHostSymbols(JITDylib &JD) {
Syms[TheJIT->mangleAndIntern("__extendhfdf2")] = {
ExecutorAddr::fromPtr(&poclExtendHFDF2),
JITSymbolFlags::Exported | JITSymbolFlags::Callable};
#endif
#if defined(__x86_64__) || defined(_M_X64)
Syms[TheJIT->mangleAndIntern("__truncsfbf2")] = {
ExecutorAddr::fromPtr(&poclTruncSFBF2),
JITSymbolFlags::Exported | JITSymbolFlags::Callable};
Syms[TheJIT->mangleAndIntern("__truncdfbf2")] = {
ExecutorAddr::fromPtr(&poclTruncDFBF2),
JITSymbolFlags::Exported | JITSymbolFlags::Callable};
#endif
if (Syms.empty())
return;
Expand Down
7 changes: 7 additions & 0 deletions tests/regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(C_PROGRAMS_TO_BUILD test_assign_loop_variable_to_privvar_makes_it_local
test_localmem_load_across_barrier
test_replicated_wi_subgroup_alloca
test_spirv_intel_subgroups
test_spirv_bfloat16
test_indirect_ptrs
test_usm_indirect_ptrs
test_usm_free_realloc
Expand All @@ -57,6 +58,10 @@ endforeach()
target_include_directories(test_indirect_ptrs
PRIVATE $<TARGET_PROPERTY:${POCL_LIBRARY_NAME},INCLUDE_DIRECTORIES>)

if(HAVE_LLVM_SPIRV_KHR_BFLOAT16)
target_compile_definitions(test_spirv_bfloat16 PRIVATE EXPECT_CPU_SPV_KHR_BFLOAT16)
endif()

set(PROGRAMS_TO_BUILD test_barrier_between_for_loops test_early_return
test_for_with_var_iteration_count test_id_dependent_computation
test_locals test_multi_level_loops_with_barriers
Expand Down Expand Up @@ -203,6 +208,8 @@ add_test_pocl(NAME "regression/multi_exit_barrier_region" WORKITEM_HANDLER "loop

add_test_pocl(NAME "regression/spirv_intel_subgroups" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_spirv_intel_subgroups" LABELS ${SPIRV_LABELS})

add_test_pocl(NAME "regression/spirv_bfloat16" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_spirv_bfloat16" LABELS ${SPIRV_LABELS})

add_test_pocl(NAME "regression/divergent_implicit_barrier_entry" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_divergent_implicit_barrier_entry")

add_test_pocl(NAME "regression/uniform_implicit_barrier_entry" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_uniform_implicit_barrier_entry")
Expand Down
Loading
Loading