Skip to content

[SYCL] enable_shared_from_this for kernel_bundle_impl #18899

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
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
8 changes: 8 additions & 0 deletions sycl/include/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,11 @@ class __SYCL_EXPORT handler {
void setStateSpecConstSet();
bool isStateExplicitKernelBundle() const;

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
detail::kernel_bundle_impl *
#else
std::shared_ptr<detail::kernel_bundle_impl>
#endif
getOrInsertHandlerKernelBundle(bool Insert) const;

void setHandlerKernelBundle(kernel Kernel);
Expand Down Expand Up @@ -1739,6 +1743,10 @@ class __SYCL_EXPORT handler {
/// called.
void setUserFacingNodeType(ext::oneapi::experimental::node_type Type);

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
Copy link
Contributor

Choose a reason for hiding this comment

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

Why can't this be done now? Same in the next file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

kernel_bundle<bundle_state::input> getKernelBundle() const;
#endif

public:
handler(const handler &) = delete;
handler(handler &&) = delete;
Expand Down
8 changes: 8 additions & 0 deletions sycl/include/sycl/kernel_bundle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,12 +1330,16 @@ void handler::set_specialization_constant(

setStateSpecConstSet();

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
getKernelBundle().set_specialization_constant<SpecName>(Value);
#else
std::shared_ptr<detail::kernel_bundle_impl> KernelBundleImplPtr =
getOrInsertHandlerKernelBundle(/*Insert=*/true);

detail::createSyclObjFromImpl<kernel_bundle<bundle_state::input>>(
std::move(KernelBundleImplPtr))
.set_specialization_constant<SpecName>(Value);
#endif
}

template <auto &SpecName>
Expand All @@ -1347,12 +1351,16 @@ handler::get_specialization_constant() const {
"Specialization constants cannot be read after "
"explicitly setting the used kernel bundle");

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
return getKernelBundle().get_specialization_constant<SpecName>();
#else
std::shared_ptr<detail::kernel_bundle_impl> KernelBundleImplPtr =
getOrInsertHandlerKernelBundle(/*Insert=*/true);

return detail::createSyclObjFromImpl<kernel_bundle<bundle_state::input>>(
std::move(KernelBundleImplPtr))
.get_specialization_constant<SpecName>();
#endif
}

} // namespace _V1
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ make_kernel_bundle(ur_native_handle_t NativeHandle,
ImageOriginInterop);
device_image_plain DevImg{DevImgImpl};

return std::make_shared<kernel_bundle_impl>(TargetContext, Devices, DevImg);
return kernel_bundle_impl::create(TargetContext, Devices, DevImg);
}

// TODO: Unused. Remove when allowed.
Expand Down
12 changes: 9 additions & 3 deletions sycl/source/detail/graph_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,14 @@ exec_graph_impl::enqueueNodeDirect(const sycl::context &Ctx,
std::tie(CmdTraceEvent, InstanceID) = emitKernelInstrumentationData(
StreamID, CGExec->MSyclKernel, CodeLoc, CGExec->MIsTopCodeLoc,
CGExec->MKernelName.data(), CGExec->MKernelNameBasedCachePtr, nullptr,
CGExec->MNDRDesc, CGExec->MKernelBundle, CGExec->MArgs);
CGExec->MNDRDesc,
CGExec
->MKernelBundle
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
.get()
#endif
,
CGExec->MArgs);
if (CmdTraceEvent)
sycl::detail::emitInstrumentationGeneral(
StreamID, InstanceID, CmdTraceEvent, xpti::trace_task_begin, nullptr);
Expand Down Expand Up @@ -1538,8 +1545,7 @@ void exec_graph_impl::populateURKernelUpdateStructs(
EliminatedArgMask = Kernel->getKernelArgMask();
} else if (auto SyclKernelImpl =
KernelBundleImplPtr
? KernelBundleImplPtr->tryGetKernel(ExecCG.MKernelName,
KernelBundleImplPtr)
? KernelBundleImplPtr->tryGetKernel(ExecCG.MKernelName)
: std::shared_ptr<kernel_impl>{nullptr}) {
UrKernel = SyclKernelImpl->getHandleRef();
EliminatedArgMask = SyclKernelImpl->getKernelArgMask();
Expand Down
3 changes: 1 addition & 2 deletions sycl/source/detail/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ retrieveKernelBinary(queue_impl &Queue, KernelNameStrRefT KernelName,
DeviceImage = KernelCG->MSyclKernel->getDeviceImage()->get_bin_image_ref();
Program = KernelCG->MSyclKernel->getDeviceImage()->get_ur_program_ref();
} else if (auto SyclKernelImpl =
KernelBundleImpl ? KernelBundleImpl->tryGetKernel(
KernelName, KernelBundleImpl)
KernelBundleImpl ? KernelBundleImpl->tryGetKernel(KernelName)
: std::shared_ptr<kernel_impl>{nullptr}) {
// Retrieve the device image from the kernel bundle.
DeviceImage = SyclKernelImpl->getDeviceImage()->get_bin_image_ref();
Expand Down
79 changes: 45 additions & 34 deletions sycl/source/detail/kernel_bundle_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ class kernel_impl;
/// The class is an impl counterpart of the sycl::kernel_bundle.
// It provides an access and utilities to manage set of sycl::device_images
// objects.
class kernel_bundle_impl {
class kernel_bundle_impl
: public std::enable_shared_from_this<kernel_bundle_impl> {

using SpecConstMapT = std::map<std::string, std::vector<unsigned char>>;

struct private_tag {
explicit private_tag() = default;
};

void common_ctor_checks() const {
const bool AllDevicesInTheContext =
checkAllDevicesAreInContext(MDevices, MContext);
Expand All @@ -92,7 +97,8 @@ class kernel_bundle_impl {
}

public:
kernel_bundle_impl(context Ctx, std::vector<device> Devs, bundle_state State)
kernel_bundle_impl(context Ctx, std::vector<device> Devs, bundle_state State,
private_tag)
: MContext(std::move(Ctx)), MDevices(std::move(Devs)), MState(State) {

common_ctor_checks();
Expand All @@ -103,7 +109,7 @@ class kernel_bundle_impl {
}

// Interop constructor used by make_kernel
kernel_bundle_impl(context Ctx, std::vector<device> Devs)
kernel_bundle_impl(context Ctx, std::vector<device> Devs, private_tag)
: MContext(Ctx), MDevices(Devs), MState(bundle_state::executable) {
if (!checkAllDevicesAreInContext(Devs, Ctx))
throw sycl::exception(
Expand All @@ -114,8 +120,8 @@ class kernel_bundle_impl {

// Interop constructor
kernel_bundle_impl(context Ctx, std::vector<device> Devs,
device_image_plain &DevImage)
: kernel_bundle_impl(Ctx, Devs) {
device_image_plain &DevImage, private_tag Tag)
: kernel_bundle_impl(Ctx, Devs, Tag) {
MDeviceImages.emplace_back(DevImage);
MUniqueDeviceImages.emplace_back(DevImage);
}
Expand All @@ -125,7 +131,7 @@ class kernel_bundle_impl {
// signature
kernel_bundle_impl(const kernel_bundle<bundle_state::input> &InputBundle,
std::vector<device> Devs, const property_list &PropList,
bundle_state TargetState)
bundle_state TargetState, private_tag)
: MContext(InputBundle.get_context()), MDevices(std::move(Devs)),
MState(TargetState) {

Expand Down Expand Up @@ -193,7 +199,7 @@ class kernel_bundle_impl {
// Matches sycl::link
kernel_bundle_impl(
const std::vector<kernel_bundle<bundle_state::object>> &ObjectBundles,
std::vector<device> Devs, const property_list &PropList)
std::vector<device> Devs, const property_list &PropList, private_tag)
: MDevices(std::move(Devs)), MState(bundle_state::executable) {
if (MDevices.empty())
throw sycl::exception(make_error_code(errc::invalid),
Expand Down Expand Up @@ -414,7 +420,7 @@ class kernel_bundle_impl {

kernel_bundle_impl(context Ctx, std::vector<device> Devs,
const std::vector<kernel_id> &KernelIDs,
bundle_state State)
bundle_state State, private_tag)
: MContext(std::move(Ctx)), MDevices(std::move(Devs)), MState(State) {

common_ctor_checks();
Expand All @@ -425,7 +431,8 @@ class kernel_bundle_impl {
}

kernel_bundle_impl(context Ctx, std::vector<device> Devs,
const DevImgSelectorImpl &Selector, bundle_state State)
const DevImgSelectorImpl &Selector, bundle_state State,
private_tag)
: MContext(std::move(Ctx)), MDevices(std::move(Devs)), MState(State) {

common_ctor_checks();
Expand All @@ -437,7 +444,7 @@ class kernel_bundle_impl {

// C'tor matches sycl::join API
kernel_bundle_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles,
bundle_state State)
bundle_state State, private_tag)
: MState(State) {
if (Bundles.empty())
return;
Expand Down Expand Up @@ -501,7 +508,8 @@ class kernel_bundle_impl {
// oneapi_ext_kernel_compiler
// construct from source string
kernel_bundle_impl(const context &Context, syclex::source_language Lang,
const std::string &Src, include_pairs_t IncludePairsVec)
const std::string &Src, include_pairs_t IncludePairsVec,
private_tag)
: MContext(Context), MDevices(Context.get_devices()),
MDeviceImages{device_image_plain{std::make_shared<device_image_impl>(
Src, MContext, MDevices, Lang, std::move(IncludePairsVec))}},
Expand All @@ -513,7 +521,7 @@ class kernel_bundle_impl {
// oneapi_ext_kernel_compiler
// construct from source bytes
kernel_bundle_impl(const context &Context, syclex::source_language Lang,
const std::vector<std::byte> &Bytes)
const std::vector<std::byte> &Bytes, private_tag)
: MContext(Context), MDevices(Context.get_devices()),
MDeviceImages{device_image_plain{std::make_shared<device_image_impl>(
Bytes, MContext, MDevices, Lang)}},
Expand All @@ -528,7 +536,7 @@ class kernel_bundle_impl {
const context &Context, const std::vector<device> &Devs,
std::vector<device_image_plain> &&DevImgs,
std::vector<std::shared_ptr<ManagedDeviceBinaries>> &&DevBinaries,
bundle_state State)
bundle_state State, private_tag)
: MContext(Context), MDevices(Devs),
MSharedDeviceBinaries(std::move(DevBinaries)),
MUniqueDeviceImages(std::move(DevImgs)), MState(State) {
Expand All @@ -540,6 +548,12 @@ class kernel_bundle_impl {
MDeviceImages.emplace_back(DevImg);
}

template <typename... Ts>
static std::shared_ptr<kernel_bundle_impl> create(Ts &&...args) {
return std::make_shared<kernel_bundle_impl>(std::forward<Ts>(args)...,
private_tag{});
}

std::shared_ptr<kernel_bundle_impl> build_from_source(
const std::vector<device> Devices,
const std::vector<sycl::detail::string_view> &BuildOptions,
Expand All @@ -559,9 +573,8 @@ class kernel_bundle_impl {
for (std::shared_ptr<device_image_impl> &DevImgImpl : NewDevImgImpls)
NewDevImgs.emplace_back(std::move(DevImgImpl));
}
return std::make_shared<kernel_bundle_impl>(
MContext, Devices, std::move(NewDevImgs), std::move(NewBinReso),
bundle_state::executable);
return create(MContext, Devices, std::move(NewDevImgs),
std::move(NewBinReso), bundle_state::executable);
}

std::shared_ptr<kernel_bundle_impl> compile_from_source(
Expand All @@ -584,9 +597,8 @@ class kernel_bundle_impl {
for (std::shared_ptr<device_image_impl> &DevImgImpl : NewDevImgImpls)
NewDevImgs.emplace_back(std::move(DevImgImpl));
}
return std::make_shared<kernel_bundle_impl>(
MContext, Devices, std::move(NewDevImgs), std::move(NewBinReso),
bundle_state::object);
return create(MContext, Devices, std::move(NewDevImgs),
std::move(NewBinReso), bundle_state::object);
}

public:
Expand All @@ -597,15 +609,16 @@ class kernel_bundle_impl {
});
}

kernel
ext_oneapi_get_kernel(const std::string &Name,
const std::shared_ptr<kernel_bundle_impl> &Self) const {
kernel ext_oneapi_get_kernel(const std::string &Name) const {
if (!hasSourceBasedImages())
throw sycl::exception(make_error_code(errc::invalid),
"'ext_oneapi_get_kernel' is only available in "
"kernel_bundles successfully built from "
"kernel_bundle<bundle_state::ext_oneapi_source>.");

std::shared_ptr<kernel_bundle_impl> Self =
const_cast<kernel_bundle_impl *>(this)->shared_from_this();
Copy link
Contributor

Choose a reason for hiding this comment

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

Temporary until tryGetSourceBasedKernel is updated, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I missed the idea. How is it related to tryGetSourceBasedKernel() and what is temporary?


// TODO: When linking is properly implemented for kernel compiler binaries,
// there can be scenarios where multiple binaries have the same
// kernels. In this case, all these bundles should be found and the
Expand Down Expand Up @@ -717,11 +730,8 @@ class kernel_bundle_impl {
return Result;
}

kernel
get_kernel(const kernel_id &KernelID,
const std::shared_ptr<detail::kernel_bundle_impl> &Self) const {
if (std::shared_ptr<kernel_impl> KernelImpl =
tryGetOfflineKernel(KernelID, Self))
kernel get_kernel(const kernel_id &KernelID) const {
if (std::shared_ptr<kernel_impl> KernelImpl = tryGetOfflineKernel(KernelID))
return detail::createSyclObjFromImpl<kernel>(std::move(KernelImpl));
throw sycl::exception(make_error_code(errc::invalid),
"The kernel bundle does not contain the kernel "
Expand Down Expand Up @@ -876,9 +886,8 @@ class kernel_bundle_impl {
});
}

std::shared_ptr<kernel_impl> tryGetOfflineKernel(
const kernel_id &KernelID,
const std::shared_ptr<detail::kernel_bundle_impl> &Self) const {
std::shared_ptr<kernel_impl>
tryGetOfflineKernel(const kernel_id &KernelID) const {
using ImageImpl = std::shared_ptr<detail::device_image_impl>;
// Selected image.
ImageImpl SelectedImage = nullptr;
Expand Down Expand Up @@ -938,13 +947,15 @@ class kernel_bundle_impl {
SelectedImage->get_ur_program_ref());

return std::make_shared<kernel_impl>(
Kernel, detail::getSyclObjImpl(MContext), SelectedImage, Self, ArgMask,
Kernel, detail::getSyclObjImpl(MContext), SelectedImage,
const_cast<kernel_bundle_impl *>(this)->shared_from_this(), ArgMask,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can do

class kernel_bundle_impl : public std::enable_shared_from_this<kernel_bundle_impl> {
  using Base = std::enable_shared_from_this<kernel_bundle_impl>;
public
  std::shared_ptr<kernel_bundle_impl> shared_from_this() const {
    return const_cast<...>(this)->Base::shared_from_this();
  }
};

If compiler would complain about incomplete types, make it a template:

template <typename Self = kernel_bundle_impl>
... shared_from_this() const {
  Self *self = this;
  // use `self` instead of `this` to make template-type-dependent
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the idea is to have ugly cast in single place, right? Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm actually not sure if this or just dropping a bunch of consts is better. We're essentially lying here when we do a const_cast...

SelectedImage->get_ur_program_ref(), CacheMutex);
}

std::shared_ptr<kernel_impl>
tryGetKernel(detail::KernelNameStrRefT Name,
const std::shared_ptr<kernel_bundle_impl> &Self) const {
tryGetKernel(detail::KernelNameStrRefT Name) const {
std::shared_ptr<kernel_bundle_impl> Self =
const_cast<kernel_bundle_impl *>(this)->shared_from_this();
// TODO: For source-based kernels, it may be faster to keep a map between
// {kernel_name, device} and their corresponding image.
// First look through the kernels registered in source-based images.
Expand All @@ -961,7 +972,7 @@ class kernel_bundle_impl {
if (std::optional<kernel_id> MaybeKernelID =
sycl::detail::ProgramManager::getInstance().tryGetSYCLKernelID(
Name))
return tryGetOfflineKernel(*MaybeKernelID, Self);
return tryGetOfflineKernel(*MaybeKernelID);
return nullptr;
}

Expand Down
Loading