-
Notifications
You must be signed in to change notification settings - Fork 753
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
[SYCL] Use custom type to pass CGF around instead of std::function
#16668
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -75,10 +75,8 @@ auto get_native(const SyclObjectT &Obj) | |||||
namespace detail { | ||||||
class queue_impl; | ||||||
|
||||||
#if __SYCL_USE_FALLBACK_ASSERT | ||||||
inline event submitAssertCapture(queue &, event &, queue *, | ||||||
const detail::code_location &); | ||||||
#endif | ||||||
|
||||||
// Function to postprocess submitted command | ||||||
// Arguments: | ||||||
|
@@ -375,8 +373,9 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> { | |||||
std::enable_if_t<std::is_invocable_r_v<void, T, handler &>, event> submit( | ||||||
T CGF, | ||||||
const detail::code_location &CodeLoc = detail::code_location::current()) { | ||||||
return submit_with_event( | ||||||
sycl::ext::oneapi::experimental::empty_properties_t{}, CGF, | ||||||
return submit_with_event<__SYCL_USE_FALLBACK_ASSERT>( | ||||||
sycl::ext::oneapi::experimental::empty_properties_t{}, | ||||||
detail::type_erased_cgfo_ty{CGF}, | ||||||
/*SecondaryQueuePtr=*/nullptr, CodeLoc); | ||||||
} | ||||||
|
||||||
|
@@ -395,9 +394,9 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> { | |||||
std::enable_if_t<std::is_invocable_r_v<void, T, handler &>, event> submit( | ||||||
T CGF, queue &SecondaryQueue, | ||||||
const detail::code_location &CodeLoc = detail::code_location::current()) { | ||||||
return submit_with_event( | ||||||
sycl::ext::oneapi::experimental::empty_properties_t{}, CGF, | ||||||
&SecondaryQueue, CodeLoc); | ||||||
return submit_with_event<__SYCL_USE_FALLBACK_ASSERT>( | ||||||
sycl::ext::oneapi::experimental::empty_properties_t{}, | ||||||
detail::type_erased_cgfo_ty{CGF}, &SecondaryQueue, CodeLoc); | ||||||
} | ||||||
|
||||||
/// Prevents any commands submitted afterward to this queue from executing | ||||||
|
@@ -2786,6 +2785,7 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> { | |||||
|
||||||
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES | ||||||
/// TODO: Unused. Remove these when ABI-break window is open. | ||||||
/// Not using `type_erased_cgfo_ty` on purpose. | ||||||
event submit_impl(std::function<void(handler &)> CGH, | ||||||
const detail::code_location &CodeLoc); | ||||||
event submit_impl(std::function<void(handler &)> CGH, | ||||||
|
@@ -2815,16 +2815,28 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> { | |||||
std::function<void(handler &)> CGH, queue secondQueue, | ||||||
const detail::code_location &CodeLoc, | ||||||
const detail::SubmitPostProcessF &PostProcess, bool IsTopCodeLoc); | ||||||
|
||||||
// Old version when `std::function` was used in place of | ||||||
// `std::function<void(handler &)>`. | ||||||
event submit_with_event_impl(std::function<void(handler &)> CGH, | ||||||
const detail::SubmissionInfo &SubmitInfo, | ||||||
const detail::code_location &CodeLoc, | ||||||
bool IsTopCodeLoc); | ||||||
|
||||||
void submit_without_event_impl(std::function<void(handler &)> CGH, | ||||||
const detail::SubmissionInfo &SubmitInfo, | ||||||
const detail::code_location &CodeLoc, | ||||||
bool IsTopCodeLoc); | ||||||
#endif // __INTEL_PREVIEW_BREAKING_CHANGES | ||||||
|
||||||
/// A template-free versions of submit. | ||||||
event submit_with_event_impl(std::function<void(handler &)> CGH, | ||||||
event submit_with_event_impl(const detail::type_erased_cgfo_ty &CGH, | ||||||
const detail::SubmissionInfo &SubmitInfo, | ||||||
const detail::code_location &CodeLoc, | ||||||
bool IsTopCodeLoc); | ||||||
|
||||||
/// A template-free version of submit_without_event. | ||||||
void submit_without_event_impl(std::function<void(handler &)> CGH, | ||||||
void submit_without_event_impl(const detail::type_erased_cgfo_ty &CGH, | ||||||
const detail::SubmissionInfo &SubmitInfo, | ||||||
const detail::code_location &CodeLoc, | ||||||
bool IsTopCodeLoc); | ||||||
|
@@ -2836,32 +2848,35 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> { | |||||
/// \param CGF is a function object containing command group. | ||||||
/// \param CodeLoc is the code location of the submit call (default argument) | ||||||
/// \return a SYCL event object for the submitted command group. | ||||||
template <typename T, typename PropertiesT> | ||||||
std::enable_if_t<std::is_invocable_r_v<void, T, handler &>, event> | ||||||
submit_with_event( | ||||||
PropertiesT Props, T CGF, queue *SecondaryQueuePtr, | ||||||
// | ||||||
// UseFallBackAssert as template param vs `#if` in function body is necessary | ||||||
// to prevent ODR-violation between TUs built with different fallback assert | ||||||
// modes. | ||||||
Comment on lines
+2852
to
+2854
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @steffenlarsen , any idea if llvm/sycl/include/sycl/queue.hpp Lines 792 to 793 in a87ac06
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "fallback kernel" in this case is not referring to fallback asserts. It's because the implementation of |
||||||
template <bool UseFallbackAssert, typename PropertiesT> | ||||||
event submit_with_event( | ||||||
PropertiesT Props, const detail::type_erased_cgfo_ty &CGF, | ||||||
queue *SecondaryQueuePtr, | ||||||
const detail::code_location &CodeLoc = detail::code_location::current()) { | ||||||
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc); | ||||||
detail::SubmissionInfo SI{}; | ||||||
ProcessSubmitProperties(Props, SI); | ||||||
if (SecondaryQueuePtr) | ||||||
SI.SecondaryQueue() = detail::getSyclObjImpl(*SecondaryQueuePtr); | ||||||
#if __SYCL_USE_FALLBACK_ASSERT | ||||||
SI.PostProcessorFunc() = | ||||||
[this, &SecondaryQueuePtr, | ||||||
&TlsCodeLocCapture](bool IsKernel, bool KernelUsesAssert, event &E) { | ||||||
if (IsKernel && !device_has(aspect::ext_oneapi_native_assert) && | ||||||
KernelUsesAssert && !device_has(aspect::accelerator)) { | ||||||
// __devicelib_assert_fail isn't supported by Device-side Runtime | ||||||
// Linking against fallback impl of __devicelib_assert_fail is | ||||||
// performed by program manager class | ||||||
// Fallback assert isn't supported for FPGA | ||||||
submitAssertCapture(*this, E, SecondaryQueuePtr, | ||||||
TlsCodeLocCapture.query()); | ||||||
} | ||||||
}; | ||||||
#endif // __SYCL_USE_FALLBACK_ASSERT | ||||||
return submit_with_event_impl(std::move(CGF), SI, TlsCodeLocCapture.query(), | ||||||
if constexpr (UseFallbackAssert) | ||||||
SI.PostProcessorFunc() = | ||||||
[this, &SecondaryQueuePtr, | ||||||
&TlsCodeLocCapture](bool IsKernel, bool KernelUsesAssert, event &E) { | ||||||
if (IsKernel && !device_has(aspect::ext_oneapi_native_assert) && | ||||||
KernelUsesAssert && !device_has(aspect::accelerator)) { | ||||||
// __devicelib_assert_fail isn't supported by Device-side Runtime | ||||||
// Linking against fallback impl of __devicelib_assert_fail is | ||||||
// performed by program manager class | ||||||
// Fallback assert isn't supported for FPGA | ||||||
submitAssertCapture(*this, E, SecondaryQueuePtr, | ||||||
TlsCodeLocCapture.query()); | ||||||
} | ||||||
}; | ||||||
return submit_with_event_impl(CGF, SI, TlsCodeLocCapture.query(), | ||||||
TlsCodeLocCapture.isToplevel()); | ||||||
} | ||||||
|
||||||
|
@@ -2871,21 +2886,25 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> { | |||||
/// \param Props is a property list with submission properties. | ||||||
/// \param CGF is a function object containing command group. | ||||||
/// \param CodeLoc is the code location of the submit call (default argument) | ||||||
template <typename T, typename PropertiesT> | ||||||
std::enable_if_t<std::is_invocable_r_v<void, T, handler &>, void> | ||||||
submit_without_event(PropertiesT Props, T CGF, | ||||||
const detail::code_location &CodeLoc) { | ||||||
#if __SYCL_USE_FALLBACK_ASSERT | ||||||
// If post-processing is needed, fall back to the regular submit. | ||||||
// TODO: Revisit whether we can avoid this. | ||||||
submit_with_event(Props, CGF, nullptr, CodeLoc); | ||||||
#else | ||||||
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc); | ||||||
detail::SubmissionInfo SI{}; | ||||||
ProcessSubmitProperties(Props, SI); | ||||||
submit_without_event_impl(CGF, SI, TlsCodeLocCapture.query(), | ||||||
TlsCodeLocCapture.isToplevel()); | ||||||
#endif // __SYCL_USE_FALLBACK_ASSERT | ||||||
// | ||||||
// UseFallBackAssert as template param vs `#if` in function body is necessary | ||||||
// to prevent ODR-violation between TUs built with different fallback assert | ||||||
// modes. | ||||||
template <bool UseFallbackAssert, typename PropertiesT> | ||||||
void submit_without_event(PropertiesT Props, | ||||||
const detail::type_erased_cgfo_ty &CGF, | ||||||
const detail::code_location &CodeLoc) { | ||||||
if constexpr (UseFallbackAssert) { | ||||||
// If post-processing is needed, fall back to the regular submit. | ||||||
// TODO: Revisit whether we can avoid this. | ||||||
submit_with_event<UseFallbackAssert>(Props, CGF, nullptr, CodeLoc); | ||||||
} else { | ||||||
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc); | ||||||
detail::SubmissionInfo SI{}; | ||||||
ProcessSubmitProperties(Props, SI); | ||||||
submit_without_event_impl(CGF, SI, TlsCodeLocCapture.query(), | ||||||
TlsCodeLocCapture.isToplevel()); | ||||||
} | ||||||
} | ||||||
|
||||||
/// parallel_for_impl with a kernel represented as a lambda + range that | ||||||
|
@@ -3114,10 +3133,10 @@ event submitAssertCapture(queue &Self, event &Event, queue *SecondaryQueue, | |||||
}); | ||||||
}; | ||||||
|
||||||
CopierEv = Self.submit_with_event( | ||||||
CopierEv = Self.submit_with_event<true>( | ||||||
sycl::ext::oneapi::experimental::empty_properties_t{}, CopierCGF, | ||||||
SecondaryQueue, CodeLoc); | ||||||
CheckerEv = Self.submit_with_event( | ||||||
CheckerEv = Self.submit_with_event<true>( | ||||||
sycl::ext::oneapi::experimental::empty_properties_t{}, CheckerCGF, | ||||||
SecondaryQueue, CodeLoc); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// RUN: %{build} -o %t.out | ||
// RUN: %{run} %t.out | ||
|
||
#include <sycl/detail/core.hpp> | ||
#include <sycl/usm.hpp> | ||
|
||
int *p = nullptr; | ||
|
||
void foo(sycl::handler &cgh) { | ||
auto *copy = p; | ||
cgh.single_task([=]() { *copy = 42; }); | ||
} | ||
|
||
int main() { | ||
sycl::queue q; | ||
p = sycl::malloc_shared<int>(1, q); | ||
*p = 0; | ||
q.submit(foo).wait(); | ||
assert(*p == 42); | ||
sycl::free(p, q); | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I first saw this I was wondering if we could use
std::invokable
instead, but I think that'd change the ABI, plus it might have the same too-much-templating problem asstd::function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how it's applicable here.
std::invokable
is a concept, not a data type; so more likestd::is_invokable_r
trait that we are already using.