Skip to content

Commit 60633fe

Browse files
authored
[SYCL][NATIVECPU] Add SPIR-V work-group/item builtins on Windows (#11158)
1. Enabling translation of calls to SpirV workgroup and workitem functions into calls to NativeCPU builtins on Windows. 2. Enabling NativeCPU test previously unsupported on Windows. 3. BuiltinMap now uses StringRef making the initialization static.
1 parent 97451b1 commit 60633fe

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

llvm/lib/SYCLLowerIR/PrepareSYCLNativeCPU.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/Support/ErrorHandling.h"
3838
#include "llvm/Support/FileSystem.h"
3939
#include "llvm/Support/raw_ostream.h"
40+
#include "llvm/TargetParser/Triple.h"
4041
#include "llvm/Transforms/Utils/Cloning.h"
4142
#include "llvm/Transforms/Utils/ValueMapper.h"
4243
#include <functional>
@@ -199,42 +200,41 @@ Function *cloneFunctionAndAddParam(Function *OldF, Type *T) {
199200
return NewF;
200201
}
201202

203+
// Helper macros for constructing builtin MS names
204+
#define GENMS1(builtin_str) "?" builtin_str "@@YA_KXZ"
205+
206+
#define GEN_IT_proc(b_str, len) "_Z" #len b_str "v"
207+
#define GEN_p(b_str, len, ncpu_bstr, num) \
208+
{ \
209+
{([]() { static_assert(sizeof(b_str) == len + 1); }, \
210+
GEN_IT_proc(b_str, len)), \
211+
GENMS1(b_str)}, \
212+
{ \
213+
ncpu_bstr, num \
214+
} \
215+
}
216+
#define GEN_xyz(b_name, len, ncpu_name) \
217+
GEN_p(#b_name "_x", len, #ncpu_name, 0), \
218+
GEN_p(#b_name "_y", len, #ncpu_name, 1), \
219+
GEN_p(#b_name "_z", len, #ncpu_name, 2)
220+
202221
// Todo: add support for more SPIRV builtins here
203-
static const std::map<std::string, std::pair<std::string, unsigned int>>
204-
BuiltinNamesMap{
205-
{"_Z28__spirv_GlobalInvocationId_xv",
206-
{"__dpcpp_nativecpu_global_id", 0}},
207-
{"_Z28__spirv_GlobalInvocationId_yv",
208-
{"__dpcpp_nativecpu_global_id", 1}},
209-
{"_Z28__spirv_GlobalInvocationId_zv",
210-
{"__dpcpp_nativecpu_global_id", 2}},
211-
{"_Z20__spirv_GlobalSize_xv", {"__dpcpp_nativecpu_global_range", 0}},
212-
{"_Z20__spirv_GlobalSize_yv", {"__dpcpp_nativecpu_global_range", 1}},
213-
{"_Z20__spirv_GlobalSize_zv", {"__dpcpp_nativecpu_global_range", 2}},
214-
{"_Z22__spirv_GlobalOffset_xv",
215-
{"__dpcpp_nativecpu_get_global_offset", 0}},
216-
{"_Z22__spirv_GlobalOffset_yv",
217-
{"__dpcpp_nativecpu_get_global_offset", 1}},
218-
{"_Z22__spirv_GlobalOffset_zv",
219-
{"__dpcpp_nativecpu_get_global_offset", 2}},
220-
{"_Z27__spirv_LocalInvocationId_xv",
221-
{"__dpcpp_nativecpu_get_local_id", 0}},
222-
{"_Z27__spirv_LocalInvocationId_yv",
223-
{"__dpcpp_nativecpu_get_local_id", 1}},
224-
{"_Z27__spirv_LocalInvocationId_zv",
225-
{"__dpcpp_nativecpu_get_local_id", 2}},
226-
{"_Z23__spirv_NumWorkgroups_xv",
227-
{"__dpcpp_nativecpu_get_num_groups", 0}},
228-
{"_Z23__spirv_NumWorkgroups_yv",
229-
{"__dpcpp_nativecpu_get_num_groups", 1}},
230-
{"_Z23__spirv_NumWorkgroups_zv",
231-
{"__dpcpp_nativecpu_get_num_groups", 2}},
232-
{"_Z23__spirv_WorkgroupSize_xv", {"__dpcpp_nativecpu_get_wg_size", 0}},
233-
{"_Z23__spirv_WorkgroupSize_yv", {"__dpcpp_nativecpu_get_wg_size", 1}},
234-
{"_Z23__spirv_WorkgroupSize_zv", {"__dpcpp_nativecpu_get_wg_size", 2}},
235-
{"_Z21__spirv_WorkgroupId_xv", {"__dpcpp_nativecpu_get_wg_id", 0}},
236-
{"_Z21__spirv_WorkgroupId_yv", {"__dpcpp_nativecpu_get_wg_id", 1}},
237-
{"_Z21__spirv_WorkgroupId_zv", {"__dpcpp_nativecpu_get_wg_id", 2}}};
222+
static const std::pair<std::pair<StringRef, StringRef>,
223+
std::pair<StringRef, unsigned int>>
224+
BuiltinNamesMap[] = {
225+
GEN_xyz(__spirv_GlobalInvocationId, 28, __dpcpp_nativecpu_global_id),
226+
GEN_xyz(__spirv_GlobalSize, 20, __dpcpp_nativecpu_global_range),
227+
GEN_xyz(__spirv_GlobalOffset, 22, __dpcpp_nativecpu_get_global_offset),
228+
GEN_xyz(__spirv_LocalInvocationId, 27, __dpcpp_nativecpu_get_local_id),
229+
GEN_xyz(__spirv_NumWorkgroups, 23, __dpcpp_nativecpu_get_num_groups),
230+
GEN_xyz(__spirv_WorkgroupSize, 23, __dpcpp_nativecpu_get_wg_size),
231+
GEN_xyz(__spirv_WorkgroupId, 21, __dpcpp_nativecpu_get_wg_id),
232+
};
233+
234+
static inline bool IsForVisualStudio(StringRef triple_str) {
235+
llvm::Triple triple(triple_str);
236+
return triple.isKnownWindowsMSVCEnvironment();
237+
}
238238

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

286+
const bool VisualStudioMangling = IsForVisualStudio(M.getTargetTriple());
287+
286288
// Then we iterate over all the supported builtins, find their uses and
287289
// replace them with calls to our Native CPU functions.
288290
for (const auto &Entry : BuiltinNamesMap) {
289-
auto *Glob = M.getFunction(Entry.first);
291+
auto *Glob = M.getFunction(VisualStudioMangling ? Entry.first.second
292+
: Entry.first.first);
290293
if (!Glob)
291294
continue;
292295
auto *ReplaceFunc = getReplaceFunc(M, Entry.second.first);

sycl/test/check_device_code/native_cpu/native_cpu_builtins.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Temporarily marking this as unsupported until mangling issues are fixed on
2-
// windows
3-
// UNSUPPORTED: windows
41
// 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
52

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

0 commit comments

Comments
 (0)