Skip to content

[SYCL] Provide _invalid_parameter symbol in device code on Windows #18400

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 26 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
437cb90
add stl wrapper to make _invalid_parameter SYCL_EXTERNAL
ianayl May 10, 2025
efbae82
Add cstdint for uint_ptr
ianayl May 10, 2025
ebd043f
I didn't know that DLLs did not reach into device code
ianayl May 12, 2025
9e697ed
applied clang-format and removed redundant __has_include_next check
ianayl May 12, 2025
2d79ed0
Create e2e test to ensure behavior is fixed
ianayl May 12, 2025
06fed51
Revert removing #include_next
ianayl May 12, 2025
2cca817
Apply clang-format
ianayl May 12, 2025
b9d385c
Fix use of <sycl/sycl.hpp>
ianayl May 12, 2025
0adc2a6
clean up std_array.cpp
ianayl May 12, 2025
92504bf
Remove corecrt.h wrapper and opt for libdevice crt_wrapper.cpp
ianayl May 12, 2025
d0fec57
Apply suggestions from code review
aelovikov-intel May 12, 2025
acf4c7a
Update libdevice/crt_wrapper.cpp
aelovikov-intel May 12, 2025
464bf57
apply clang format
ianayl May 12, 2025
709a8a0
Implement _invalid_parameter using __devicelib_assert_fail
ianayl May 13, 2025
794ae4c
apply clang-format
ianayl May 13, 2025
47afa8a
guard _invalid_parameters with _WIN32
ianayl May 13, 2025
c560e6e
Add __cdecl
ianayl May 13, 2025
120557e
Revert "Add __cdecl"
ianayl May 13, 2025
f090d40
Revert "guard _invalid_parameters with _WIN32"
ianayl May 13, 2025
f108550
Revert "apply clang-format"
ianayl May 13, 2025
b872aec
Revert "Implement _invalid_parameter using __devicelib_assert_fail"
ianayl May 13, 2025
2690b30
Revert "apply clang format"
ianayl May 13, 2025
015c506
Revert "Update libdevice/crt_wrapper.cpp"
ianayl May 13, 2025
5a167e8
Revert "Apply suggestions from code review"
ianayl May 13, 2025
0bfb971
Revert "Remove corecrt.h wrapper and opt for libdevice crt_wrapper.cpp"
ianayl May 13, 2025
50de999
Revert "Revert "Apply suggestions from code review""
ianayl May 13, 2025
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
51 changes: 51 additions & 0 deletions sycl/include/sycl/stl_wrappers/corecrt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//==---------------- Wrapper around corecrt.h ------------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// When std::array is used in device code, MSVC's STL uses a _invalid_parameter
// function. This causes issue when _invalid_parameter is invoked from device
// code:

// 1. `_invalid_parameter` is provided via ucrtbased.dll at runtime: DLLs are
// not loaded for device code, thus causing undefined symbol errors.

// 2. MSVC's STL never defined the function as SYCL_EXTERNAL, errors are thrown
// when device code tries to invoke `_invalid_parameter`.

// As a workaround, this wrapper wraps around corecrt.h and defines a custom
// _invalid_parameter for device code compilation.

// This new SYCL_EXTERNAL definition of _invalid_parameter has to be declared
// before corecrt.h is included: Thus, we have this STL wrapper instead of
// declaring _invalid_parameter function in SYCL headers.

#pragma once

#if defined(__SYCL_DEVICE_ONLY__) && defined(_DEBUG)

#include <cstdint> // For uintptr_t
#include <sycl/detail/defines_elementary.hpp> // For __DPCPP_SYCL_EXTERNAL

extern "C" __DPCPP_SYCL_EXTERNAL void __cdecl _invalid_parameter(
wchar_t const *, wchar_t const *, wchar_t const *, unsigned int,
uintptr_t) {
// Do nothing when called in device code
}

#endif

#if defined(__has_include_next)
// GCC/clang support go through this path.
#include_next <corecrt.h>
#else
// MSVC doesn't support "#include_next", so we have to be creative.
// Our header is located in "stl_wrappers/corecrt.h" so it won't be picked by
// the aforementioned include. MSVC's installation, on the other hand, has the
// layout where the following would result in the <corecrt.h> we want. This is
// obviously hacky, but the best we can do...
#include <../ucrt/corecrt.h>
#endif
9 changes: 4 additions & 5 deletions sycl/test-e2e/Basic/std_array.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// REQUIRES: windows
// Check that std::array is supported on device in debug mode on Windows.

// RUN: not clang-cl -fsycl -o %t.exe %s /Od /MDd /Zi /EHsc 2>&1 | FileCheck %s
// REQUIRES: windows

// FIXME: This code should have compiled cleanly.
// CHECK: error: SYCL kernel cannot call an undefined function without SYCL_EXTERNAL attribute
// CHECK: note: '_invalid_parameter' declared here
// RUN: %clangxx --driver-mode=cl -fsycl -o %t.exe %s /Od /MDd /Zi /EHsc
// RUN: %{run} %t.exe

#include <sycl/queue.hpp>

Expand Down