Skip to content

[SYCL][NATIVECPU] Add SPIR-V work-group/item builtins on Windows #11158

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

Merged
merged 9 commits into from
Sep 14, 2023
Merged
75 changes: 39 additions & 36 deletions llvm/lib/SYCLLowerIR/PrepareSYCLNativeCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#include <functional>
Expand Down Expand Up @@ -199,42 +200,41 @@ Function *cloneFunctionAndAddParam(Function *OldF, Type *T) {
return NewF;
}

// Helper macros for constructing builtin MS names
#define GENMS1(builtin_str) "?" builtin_str "@@YA_KXZ"

#define GEN_IT_proc(b_str, len) "_Z" #len b_str "v"
#define GEN_p(b_str, len, ncpu_bstr, num) \
{ \
{([]() { static_assert(sizeof(b_str) == len + 1); }, \
GEN_IT_proc(b_str, len)), \
GENMS1(b_str)}, \
{ \
ncpu_bstr, num \
} \
}
#define GEN_xyz(b_name, len, ncpu_name) \
GEN_p(#b_name "_x", len, #ncpu_name, 0), \
GEN_p(#b_name "_y", len, #ncpu_name, 1), \
GEN_p(#b_name "_z", len, #ncpu_name, 2)

// Todo: add support for more SPIRV builtins here
static const std::map<std::string, std::pair<std::string, unsigned int>>
BuiltinNamesMap{
{"_Z28__spirv_GlobalInvocationId_xv",
{"__dpcpp_nativecpu_global_id", 0}},
{"_Z28__spirv_GlobalInvocationId_yv",
{"__dpcpp_nativecpu_global_id", 1}},
{"_Z28__spirv_GlobalInvocationId_zv",
{"__dpcpp_nativecpu_global_id", 2}},
{"_Z20__spirv_GlobalSize_xv", {"__dpcpp_nativecpu_global_range", 0}},
{"_Z20__spirv_GlobalSize_yv", {"__dpcpp_nativecpu_global_range", 1}},
{"_Z20__spirv_GlobalSize_zv", {"__dpcpp_nativecpu_global_range", 2}},
{"_Z22__spirv_GlobalOffset_xv",
{"__dpcpp_nativecpu_get_global_offset", 0}},
{"_Z22__spirv_GlobalOffset_yv",
{"__dpcpp_nativecpu_get_global_offset", 1}},
{"_Z22__spirv_GlobalOffset_zv",
{"__dpcpp_nativecpu_get_global_offset", 2}},
{"_Z27__spirv_LocalInvocationId_xv",
{"__dpcpp_nativecpu_get_local_id", 0}},
{"_Z27__spirv_LocalInvocationId_yv",
{"__dpcpp_nativecpu_get_local_id", 1}},
{"_Z27__spirv_LocalInvocationId_zv",
{"__dpcpp_nativecpu_get_local_id", 2}},
{"_Z23__spirv_NumWorkgroups_xv",
{"__dpcpp_nativecpu_get_num_groups", 0}},
{"_Z23__spirv_NumWorkgroups_yv",
{"__dpcpp_nativecpu_get_num_groups", 1}},
{"_Z23__spirv_NumWorkgroups_zv",
{"__dpcpp_nativecpu_get_num_groups", 2}},
{"_Z23__spirv_WorkgroupSize_xv", {"__dpcpp_nativecpu_get_wg_size", 0}},
{"_Z23__spirv_WorkgroupSize_yv", {"__dpcpp_nativecpu_get_wg_size", 1}},
{"_Z23__spirv_WorkgroupSize_zv", {"__dpcpp_nativecpu_get_wg_size", 2}},
{"_Z21__spirv_WorkgroupId_xv", {"__dpcpp_nativecpu_get_wg_id", 0}},
{"_Z21__spirv_WorkgroupId_yv", {"__dpcpp_nativecpu_get_wg_id", 1}},
{"_Z21__spirv_WorkgroupId_zv", {"__dpcpp_nativecpu_get_wg_id", 2}}};
static const std::pair<std::pair<StringRef, StringRef>,
std::pair<StringRef, unsigned int>>
BuiltinNamesMap[] = {
GEN_xyz(__spirv_GlobalInvocationId, 28, __dpcpp_nativecpu_global_id),
GEN_xyz(__spirv_GlobalSize, 20, __dpcpp_nativecpu_global_range),
GEN_xyz(__spirv_GlobalOffset, 22, __dpcpp_nativecpu_get_global_offset),
GEN_xyz(__spirv_LocalInvocationId, 27, __dpcpp_nativecpu_get_local_id),
GEN_xyz(__spirv_NumWorkgroups, 23, __dpcpp_nativecpu_get_num_groups),
GEN_xyz(__spirv_WorkgroupSize, 23, __dpcpp_nativecpu_get_wg_size),
GEN_xyz(__spirv_WorkgroupId, 21, __dpcpp_nativecpu_get_wg_id),
};

static inline bool IsForVisualStudio(StringRef triple_str) {
llvm::Triple triple(triple_str);
return triple.isKnownWindowsMSVCEnvironment();
}

Function *getReplaceFunc(const Module &M, StringRef Name) {
Function *F = M.getFunction(Name);
Expand Down Expand Up @@ -283,10 +283,13 @@ PreservedAnalyses PrepareSYCLNativeCPUPass::run(Module &M,
emitSubkernelForKernel(NewK, NativeCPUArgDescType, StatePtrType);
}

const bool VisualStudioMangling = IsForVisualStudio(M.getTargetTriple());

// Then we iterate over all the supported builtins, find their uses and
// replace them with calls to our Native CPU functions.
for (const auto &Entry : BuiltinNamesMap) {
auto *Glob = M.getFunction(Entry.first);
auto *Glob = M.getFunction(VisualStudioMangling ? Entry.first.second
: Entry.first.first);
if (!Glob)
continue;
auto *ReplaceFunc = getReplaceFunc(M, Entry.second.first);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Temporarily marking this as unsupported until mangling issues are fixed on
// windows
// UNSUPPORTED: windows
// RUN: %clangxx -fsycl-device-only -fsycl-targets=native_cpu -Xclang -sycl-std=2020 -mllvm -sycl-opt -mllvm -inline-threshold=500 -S -emit-llvm -o - %s | FileCheck %s

// check that we added the state struct as a function argument, and that we
Expand Down