Skip to content

[UR] Replace calls to UR in native handle functions to proper OpenCL functions #17016

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 1 commit into from
Mar 21, 2025

Conversation

RossBrunton
Copy link
Contributor

At various points, OpenCL native handles need to be retained to ensure
SYCL semantics. Previously, this relied on the fact that UR handles were
typecast CL handles and shared the same reference count.

However, the SYCL RT shouldn't assume this, so instead we call the
appropriate (dynamically looked-up) CL functions on the native handles
instead.

This is in preperation for oneapi-src/unified-runtime#1176 .

This change should also have no observable effect for SYCL code; there
is no change in lifetime semantics.

@RossBrunton RossBrunton force-pushed the ross/urtocl branch 2 times, most recently from 85c448c to 93ce35e Compare February 18, 2025 16:03
@RossBrunton RossBrunton force-pushed the ross/urtocl branch 2 times, most recently from 688137b to 2238fb5 Compare February 20, 2025 16:10
@RossBrunton
Copy link
Contributor Author

@cperkinsintel Do you still have concerns about this approach?

@aelovikov-intel I'm not sure what testing could be done here. If my understanding is correct, only one "OpenCL" implementation may be loaded at a time, and if that were unloaded in some way it would break a lot of things internal to the OpenCL UR backend.

@nrspruit After an embarrassing amount of time trying to get Windows to work, CI seems to be working now.

@aelovikov-intel
Copy link
Contributor

@aelovikov-intel I'm not sure what testing could be done here. If my understanding is correct, only one "OpenCL" implementation may be loaded at a time, and if that were unloaded in some way it would break a lot of things internal to the OpenCL UR backend.

I think the answer to my question is that ICD loader "unifies" all available OpenCL implementations in the system and properly dispatches to the correct one when called. As such, that function pointer is the same for both OpenCL CPU and GPU devices (and is provided by the ICD loader itself) even though the actual implementations are different.

@RossBrunton
Copy link
Contributor Author

@aelovikov-intel I'm not sure what testing could be done here. If my understanding is correct, only one "OpenCL" implementation may be loaded at a time, and if that were unloaded in some way it would break a lot of things internal to the OpenCL UR backend.

I think the answer to my question is that ICD loader "unifies" all available OpenCL implementations in the system and properly dispatches to the correct one when called. As such, that function pointer is the same for both OpenCL CPU and GPU devices (and is provided by the ICD loader itself) even though the actual implementations are different.

That's my understanding as well; each handle type in OpenCL has a field for the "implementation" which points to a struct containing function pointers for each cl method. The ICD implements cl functions such that they dispatch to the appropriate function pointer for the specific handle.

On Linux, running gdb on clinfo shows that only one libOpenCL.so is loaded even though I have multiple opencl implementations installed.

@RossBrunton
Copy link
Contributor Author

@intel/llvm-reviewers-runtime Can I get this reviewed? It's blocking a few UR improvements I want to merge.

Comment on lines 123 to 125
#define __SYCL_OCL_GET_FUNCTION(FN) \
(sycl::_V1::detail::dynLookupFunction<decltype(FN)>("OpenCL", \
"libOpenCL.so", #FN))
Copy link
Contributor

@aelovikov-intel aelovikov-intel Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this generalization is unnecessary at this point. Can we just have a non-macro

template <typename FnTy> FnTy *get_ocl_func(const char *FuncName) { ... }
void *get_ocl_func_impl(const char *FuncName);

? All the win/lin dispatch for the shared library name can be done inside libsycl.so/.dll

Maybe even

void *get_ocl_func_impl(const char *FuncName);
template <typename... Tys>
auto call_ocl(const char *func, Tys&& ...args);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your get_ocl_func requires specifying the function name twice; once as a string and once as a value for decltype(x). Without that, callers would have to do something like:

void *fn = reinterpret_cast<decltype(clDoThing)>("clDoThing");

which I think is less ergonomic than a macro.

call_ocl could work, but it looks like it relies on the programmer getting the signature exactly correct, and gives no feedback if they get it wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about __SYCL_OCL_CALL macro then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, __SYCL_OCL_CALL looks much nicer. Updated.

At various points, OpenCL native handles need to be retained to ensure
SYCL semantics. Previously, this relied on the fact that UR handles were
typecast CL handles and shared the same reference count.

However, the SYCL RT shouldn't assume this, so instead we call the
appropriate (dynamically looked-up) CL functions on the native handles
instead.

This is in preperation for oneapi-src/unified-runtime#1176 .

This change should also have no observable effect for SYCL code; there
is no change in lifetime semantics.
@RossBrunton
Copy link
Contributor Author

@intel/llvm-gatekeepers Please merge.

@kbenzie kbenzie merged commit e1cf106 into intel:sycl Mar 21, 2025
32 of 34 checks passed
@sarnex
Copy link
Contributor

sarnex commented Mar 21, 2025

@RossBrunton @kbenzie Build is failing in Windows postcommit

FAILED: tools/sycl/source/CMakeFiles/sycl_object.dir/detail/os_util.cpp.obj 
ccache C:\PROGRA~2\Intel\oneAPI\compiler\latest\bin\icx.exe  /nologo /TP -DCL_TARGET_OPENCL_VERSION=300 -DSYCL2020_DISABLE_DEPRECATION_WARNINGS -DSYCL_EXT_JIT_ENABLE -DSYCL_RT_ZSTD_NOT_AVAIABLE -DUNICODE -DXPTI_ENABLE_INSTRUMENTATION -DXPTI_STATIC_LIBRARY -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__SYCL_BUILD_SYCL_DLL -D__SYCL_INTERNAL_API -ID:\github\_work\llvm\llvm\build\tools\sycl\source -ID:\github\_work\llvm\llvm\src\sycl\source -ID:\github\_work\llvm\llvm\build\include -ID:\github\_work\llvm\llvm\src\llvm\include -ID:\github\_work\llvm\llvm\src\xpti\include -ID:\github\_work\llvm\llvm\src\sycl\ur_win_proxy_loader -ID:\github\_work\llvm\llvm\src\sycl\include -ID:\github\_work\llvm\llvm\build\_deps\boost_unordered-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_assert-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_config-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_container_hash-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_core-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_describe-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_mp11-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_predef-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_static_assert-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_throw_exception-src\include -ID:\github\_work\llvm\llvm\src\sycl-jit\common\include -ID:\github\_work\llvm\llvm\src\sycl-jit\jit-compiler\include -ID:\github\_work\llvm\llvm\src\unified-runtime\include -ID:\github\_work\llvm\llvm\build\_deps\opencl-headers-src /WX /W4 /fp:precise /clang:-Wno-nonportable-include-path /clang:-Wno-cast-function-type-mismatch /Zc:inline /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /EHsc /Zi -Wno-covered-switch-default /O2 /Ob2  -Qstd:c++17 -MD   -wd4996 -UNDEBUG /MD -Winstantiation-after-specialization -QMD -QMT tools\sycl\source\CMakeFiles\sycl_object.dir\detail\os_util.cpp.obj -QMF tools\sycl\source\CMakeFiles\sycl_object.dir\detail\os_util.cpp.obj.d /Fotools\sycl\source\CMakeFiles\sycl_object.dir\detail\os_util.cpp.obj /Fdtools\sycl\source\CMakeFiles\sycl_object.dir\ -c D:\github\_work\llvm\llvm\src\sycl\source\detail\os_util.cpp
D:\github\_work\llvm\llvm\src\sycl\source\detail\os_util.cpp(324,10): error: implicit conversion between pointer-to-function and pointer-to-object is a Microsoft extension [-Werror,-Wmicrosoft-cast]
  324 |   return retVal;
      |          ^~~~~~

Can you please fix asap or revert? Thx

@kbenzie
Copy link
Contributor

kbenzie commented Mar 21, 2025

@RossBrunton @kbenzie Build is failing in Windows postcommit

FAILED: tools/sycl/source/CMakeFiles/sycl_object.dir/detail/os_util.cpp.obj 
ccache C:\PROGRA~2\Intel\oneAPI\compiler\latest\bin\icx.exe  /nologo /TP -DCL_TARGET_OPENCL_VERSION=300 -DSYCL2020_DISABLE_DEPRECATION_WARNINGS -DSYCL_EXT_JIT_ENABLE -DSYCL_RT_ZSTD_NOT_AVAIABLE -DUNICODE -DXPTI_ENABLE_INSTRUMENTATION -DXPTI_STATIC_LIBRARY -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__SYCL_BUILD_SYCL_DLL -D__SYCL_INTERNAL_API -ID:\github\_work\llvm\llvm\build\tools\sycl\source -ID:\github\_work\llvm\llvm\src\sycl\source -ID:\github\_work\llvm\llvm\build\include -ID:\github\_work\llvm\llvm\src\llvm\include -ID:\github\_work\llvm\llvm\src\xpti\include -ID:\github\_work\llvm\llvm\src\sycl\ur_win_proxy_loader -ID:\github\_work\llvm\llvm\src\sycl\include -ID:\github\_work\llvm\llvm\build\_deps\boost_unordered-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_assert-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_config-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_container_hash-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_core-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_describe-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_mp11-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_predef-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_static_assert-src\include -ID:\github\_work\llvm\llvm\build\_deps\boost_throw_exception-src\include -ID:\github\_work\llvm\llvm\src\sycl-jit\common\include -ID:\github\_work\llvm\llvm\src\sycl-jit\jit-compiler\include -ID:\github\_work\llvm\llvm\src\unified-runtime\include -ID:\github\_work\llvm\llvm\build\_deps\opencl-headers-src /WX /W4 /fp:precise /clang:-Wno-nonportable-include-path /clang:-Wno-cast-function-type-mismatch /Zc:inline /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /EHsc /Zi -Wno-covered-switch-default /O2 /Ob2  -Qstd:c++17 -MD   -wd4996 -UNDEBUG /MD -Winstantiation-after-specialization -QMD -QMT tools\sycl\source\CMakeFiles\sycl_object.dir\detail\os_util.cpp.obj -QMF tools\sycl\source\CMakeFiles\sycl_object.dir\detail\os_util.cpp.obj.d /Fotools\sycl\source\CMakeFiles\sycl_object.dir\detail\os_util.cpp.obj /Fdtools\sycl\source\CMakeFiles\sycl_object.dir\ -c D:\github\_work\llvm\llvm\src\sycl\source\detail\os_util.cpp
D:\github\_work\llvm\llvm\src\sycl\source\detail\os_util.cpp(324,10): error: implicit conversion between pointer-to-function and pointer-to-object is a Microsoft extension [-Werror,-Wmicrosoft-cast]
  324 |   return retVal;
      |          ^~~~~~

Can you please fix asap or revert? Thx

I've created #17574 which I hope will resolve this

RossBrunton added a commit to RossBrunton/intel-llvm that referenced this pull request Mar 27, 2025
… OpenCL functions (intel#17016)"

This reverts commit e1cf106.

In testing, it turns out a number of people link against
`libOpenCL.so.1` rather than `libOpenCL.so`, which is considered
a seperate library by the linker. Reverting this change for now
while we consider the best option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants