Skip to content

Commit ba0e6f8

Browse files
committed
[SYCL][RTC] Ensure template kernel instantiation
Signed-off-by: Julian Oppermann <[email protected]>
1 parent a4a28bd commit ba0e6f8

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

sycl/source/detail/jit_compiler.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,17 +1173,20 @@ std::vector<uint8_t> jit_compiler::compileSYCL(
11731173
const std::vector<std::string> &UserArgs, std::string *LogPtr,
11741174
const std::vector<std::string> &RegisteredKernelNames) {
11751175

1176-
// TODO: Handle template instantiation.
1177-
if (!RegisteredKernelNames.empty()) {
1178-
throw sycl::exception(
1179-
sycl::errc::build,
1180-
"Property `sycl::ext::oneapi::experimental::registered_kernel_names` "
1181-
"is not yet supported for the `sycl_jit` source language");
1176+
// RegisteredKernelNames may contain template specializations, so we just put
1177+
// them in main() which ensures they are instantiated.
1178+
std::ostringstream ss;
1179+
ss << "int main() {\n";
1180+
for (const std::string &KernelName : RegisteredKernelNames) {
1181+
ss << " (void)" << KernelName << ";\n";
11821182
}
1183+
ss << " return 0;\n}\n" << std::endl;
1184+
1185+
std::string FinalSource = SYCLSource + ss.str();
11831186

11841187
std::string SYCLFileName = Id + ".cpp";
11851188
::jit_compiler::InMemoryFile SourceFile{SYCLFileName.c_str(),
1186-
SYCLSource.c_str()};
1189+
FinalSource.c_str()};
11871190

11881191
std::vector<::jit_compiler::InMemoryFile> IncludeFilesView;
11891192
IncludeFilesView.reserve(IncludePairs.size());

sycl/test-e2e/KernelCompiler/kernel_compiler_sycl_jit.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ void ff_cp(int *ptr) {
6464
sycl::id<1> GId = Item.get_global_id();
6565
ptr[GId.get(0)] = AddEm(GId.get(0), 37);
6666
}
67+
68+
// this name will be mangled
69+
template <typename T>
70+
SYCL_EXTERNAL SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((sycl::ext::oneapi::experimental::nd_range_kernel<1>))
71+
void ff_templated(T *ptr) {
72+
73+
sycl::nd_item<1> Item = sycl::ext::oneapi::this_work_item::get_nd_item<1>();
74+
75+
sycl::id<1> GId = Item.get_global_id();
76+
ptr[GId.get(0)] = PlusEm(GId.get(0), 38);
77+
}
6778
)===";
6879

6980
void test_1(sycl::queue &Queue, sycl::kernel &Kernel, int seed) {
@@ -125,19 +136,32 @@ int test_build_and_run() {
125136
// Compilation of empty prop list, no devices.
126137
exe_kb kbExe1 = syclex::build(kbSrc);
127138

128-
// // Compilation with props and devices
139+
// Compilation with props and devices
129140
std::string log;
130141
std::vector<std::string> flags{"-g", "-fno-fast-math",
131142
"-fsycl-instrument-device-code"};
132143
std::vector<sycl::device> devs = kbSrc.get_devices();
133144
exe_kb kbExe2 = syclex::build(
134-
kbSrc, devs, syclex::properties{syclex::build_options{flags}});
145+
kbSrc, devs,
146+
syclex::properties{syclex::build_options{flags}, syclex::save_log{&log},
147+
syclex::registered_kernel_names{"ff_templated<int>"}});
135148

136-
// extern "C" was used, so the name "ff_cp" is not mangled.
149+
// extern "C" was used, so the name "ff_cp" is not mangled and can be used
150+
// directly.
137151
sycl::kernel k = kbExe2.ext_oneapi_get_kernel("ff_cp");
138152

153+
// The templated function name will have been mangled. Mapping from original
154+
// name to mangled is not yet supported. So we cannot yet do this:
155+
// sycl::kernel k2 = kbExe2.ext_oneapi_get_kernel("ff_templated<int>");
156+
157+
// Instead, we can TEMPORARILY use the mangled name. Once demangling is
158+
// supported this might no longer work.
159+
sycl::kernel k2 =
160+
kbExe2.ext_oneapi_get_kernel("_Z26__sycl_kernel_ff_templatedIiEvPT_");
161+
139162
// Test the kernels.
140-
test_1(q, k, 37 + 5); // ff_cp seeds 37. AddEm will add 5 more.
163+
test_1(q, k, 37 + 5); // ff_cp seeds 37. AddEm will add 5 more.
164+
test_1(q, k2, 38 + 6); // ff_templated seeds 38. PlusEm adds 6 more.
141165

142166
return 0;
143167
}

0 commit comments

Comments
 (0)