Skip to content

Commit bf98063

Browse files
committed
Change of strategy
This commit changes the strategy from having implicitly created kernel bundles include all device images and then filter later, to have the implicitly created kernel bundles have no device images initially and then add the images once required kernels are known. Signed-off-by: Steffen Larsen <[email protected]>
1 parent ad92a7e commit bf98063

File tree

7 files changed

+87
-40
lines changed

7 files changed

+87
-40
lines changed

sycl/include/CL/sycl/handler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ class __SYCL_EXPORT handler {
730730
// If the kernel lambda is callable with a kernel_handler argument, manifest
731731
// the associated kernel handler.
732732
if (IsCallableWithKernelHandler) {
733-
getOrInsertFilteredHandlerKernelBundle(/*Insert=*/true,
734-
{get_kernel_id<KernelName>()});
733+
getOrInsertHandlerKernelBundle(/*Insert=*/true,
734+
get_kernel_id<KernelName>());
735735
}
736736
}
737737

@@ -1274,7 +1274,7 @@ class __SYCL_EXPORT handler {
12741274
getOrInsertHandlerKernelBundle(bool Insert) const;
12751275

12761276
std::shared_ptr<detail::kernel_bundle_impl>
1277-
getOrInsertFilteredHandlerKernelBundle(bool Insert, kernel_id KernelId) const;
1277+
getOrInsertHandlerKernelBundle(bool Insert, const kernel_id &KernelId) const;
12781278

12791279
void setHandlerKernelBundle(
12801280
const std::shared_ptr<detail::kernel_bundle_impl> &NewKernelBundleImpPtr);

sycl/include/CL/sycl/kernel_bundle.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,13 @@ template <typename KernelName> bool is_compatible(const device &Dev) {
588588

589589
namespace detail {
590590

591+
// TODO: This is no longer in use. Remove when ABI break is allowed.
591592
__SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
592593
join_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles);
594+
595+
__SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
596+
join_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles,
597+
bundle_state State);
593598
}
594599

595600
/// \returns a new kernel bundle that represents the union of all the device
@@ -604,7 +609,7 @@ join(const std::vector<sycl::kernel_bundle<State>> &Bundles) {
604609
KernelBundleImpls.push_back(detail::getSyclObjImpl(Bundle));
605610

606611
std::shared_ptr<detail::kernel_bundle_impl> Impl =
607-
detail::join_impl(KernelBundleImpls);
612+
detail::join_impl(KernelBundleImpls, State);
608613
return detail::createSyclObjFromImpl<kernel_bundle<State>>(Impl);
609614
}
610615

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class kernel_bundle_impl {
7979

8080
public:
8181
kernel_bundle_impl(context Ctx, std::vector<device> Devs, bundle_state State)
82-
: MContext(std::move(Ctx)), MDevices(std::move(Devs)) {
82+
: MContext(std::move(Ctx)), MDevices(std::move(Devs)),
83+
MInitialState(State) {
8384

8485
common_ctor_checks(State);
8586

@@ -89,7 +90,7 @@ class kernel_bundle_impl {
8990

9091
// Interop constructor used by make_kernel
9192
kernel_bundle_impl(context Ctx, std::vector<device> Devs)
92-
: MContext(Ctx), MDevices(Devs) {
93+
: MContext(Ctx), MDevices(Devs), MInitialState(bundle_state::executable) {
9394
if (!checkAllDevicesAreInContext(Devs, Ctx))
9495
throw sycl::exception(
9596
make_error_code(errc::invalid),
@@ -111,7 +112,8 @@ class kernel_bundle_impl {
111112
kernel_bundle_impl(const kernel_bundle<bundle_state::input> &InputBundle,
112113
std::vector<device> Devs, const property_list &PropList,
113114
bundle_state TargetState)
114-
: MContext(InputBundle.get_context()), MDevices(std::move(Devs)) {
115+
: MContext(InputBundle.get_context()), MDevices(std::move(Devs)),
116+
MInitialState(TargetState) {
115117

116118
MSpecConstValues = getSyclObjImpl(InputBundle)->get_spec_const_map_ref();
117119

@@ -161,7 +163,7 @@ class kernel_bundle_impl {
161163
kernel_bundle_impl(
162164
const std::vector<kernel_bundle<bundle_state::object>> &ObjectBundles,
163165
std::vector<device> Devs, const property_list &PropList)
164-
: MDevices(std::move(Devs)) {
166+
: MDevices(std::move(Devs)), MInitialState(bundle_state::executable) {
165167

166168
if (MDevices.empty())
167169
throw sycl::exception(make_error_code(errc::invalid),
@@ -241,7 +243,8 @@ class kernel_bundle_impl {
241243
kernel_bundle_impl(context Ctx, std::vector<device> Devs,
242244
const std::vector<kernel_id> &KernelIDs,
243245
bundle_state State)
244-
: MContext(std::move(Ctx)), MDevices(std::move(Devs)) {
246+
: MContext(std::move(Ctx)), MDevices(std::move(Devs)),
247+
MInitialState(State) {
245248

246249
// TODO: Add a check that all kernel ids are compatible with at least one
247250
// device in Devs
@@ -253,7 +256,8 @@ class kernel_bundle_impl {
253256

254257
kernel_bundle_impl(context Ctx, std::vector<device> Devs,
255258
const DevImgSelectorImpl &Selector, bundle_state State)
256-
: MContext(std::move(Ctx)), MDevices(std::move(Devs)) {
259+
: MContext(std::move(Ctx)), MDevices(std::move(Devs)),
260+
MInitialState(State) {
257261

258262
common_ctor_checks(State);
259263

@@ -262,7 +266,8 @@ class kernel_bundle_impl {
262266
}
263267

264268
// C'tor matches sycl::join API
265-
kernel_bundle_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles) {
269+
kernel_bundle_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles,
270+
bundle_state State) : MInitialState(State) {
266271
if (Bundles.empty())
267272
return;
268273

@@ -486,7 +491,7 @@ class kernel_bundle_impl {
486491
return bundle_state::executable;
487492
// All device images are expected to have the same state
488493
return MDeviceImages.empty()
489-
? bundle_state::input
494+
? MInitialState
490495
: detail::getSyclObjImpl(MDeviceImages[0])->get_state();
491496
}
492497

@@ -496,17 +501,38 @@ class kernel_bundle_impl {
496501

497502
bool isInterop() const { return MIsInterop; }
498503

499-
void filterImages(const kernel_id &KernelId) {
500-
auto ImgHasKernelPred = [&KernelId](const device_image_plain &Img) {
501-
return Img.has_kernel(KernelId);
502-
};
503-
const size_t NumRequiredDevImgs = std::count_if(
504-
MDeviceImages.begin(), MDeviceImages.end(), ImgHasKernelPred);
505-
std::vector<device_image_plain> FilteredDeviceImages;
506-
FilteredDeviceImages.reserve(NumRequiredDevImgs);
507-
std::copy_if(MDeviceImages.begin(), MDeviceImages.end(),
508-
std::back_inserter(FilteredDeviceImages), ImgHasKernelPred);
509-
MDeviceImages = FilteredDeviceImages;
504+
void add_kernel(const kernel_id &KernelID, const device &Dev) {
505+
// Skip if kernel is already there
506+
if (has_kernel(KernelID, Dev))
507+
return;
508+
509+
// First try and get images in current bundle state
510+
const bundle_state BundleState = get_bundle_state();
511+
std::vector<device_image_plain> NewDevImgs =
512+
detail::ProgramManager::getInstance().getSYCLDeviceImages(
513+
MContext, {Dev}, {KernelID}, BundleState);
514+
515+
// If no images were found and the bundle is in input state we try and get
516+
// the image in executable state and then bring the existing binaries into
517+
// executable as well
518+
if (NewDevImgs.empty() && BundleState == bundle_state::input) {
519+
NewDevImgs = detail::ProgramManager::getInstance().getSYCLDeviceImages(
520+
MContext, {Dev}, {KernelID}, bundle_state::executable);
521+
detail::ProgramManager::getInstance().bringSYCLDeviceImagesToState(
522+
MDeviceImages, bundle_state::executable);
523+
}
524+
525+
assert(!NewDevImgs.empty() && "Device images for kernel was not found.");
526+
527+
// Propagate already set specialization constants to the new images
528+
for (device_image_plain DevImg : NewDevImgs)
529+
for (auto SpecConst : MSpecConstValues)
530+
getSyclObjImpl(DevImg)->set_specialization_constant_raw_value(
531+
SpecConst.first.c_str(), SpecConst.second.data());
532+
533+
// Add the images to the collection
534+
MDeviceImages.insert(MDeviceImages.end(), NewDevImgs.begin(),
535+
NewDevImgs.end());
510536
}
511537

512538
private:
@@ -517,6 +543,7 @@ class kernel_bundle_impl {
517543
// from any device image.
518544
SpecConstMapT MSpecConstValues;
519545
bool MIsInterop = false;
546+
bundle_state MInitialState;
520547
};
521548

522549
} // namespace detail

sycl/source/handler.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,10 @@ handler::getOrInsertHandlerKernelBundle(bool Insert) const {
113113

114114
// No kernel bundle yet, create one
115115
if (!KernelBundleImpPtr && Insert) {
116-
KernelBundleImpPtr = detail::getSyclObjImpl(
117-
get_kernel_bundle<bundle_state::input>(MQueue->get_context()));
118-
if (KernelBundleImpPtr->empty()) {
119-
KernelBundleImpPtr = detail::getSyclObjImpl(
120-
get_kernel_bundle<bundle_state::executable>(MQueue->get_context()));
121-
}
116+
// Create an empty kernel bundle to add kernels to later
117+
KernelBundleImpPtr =
118+
detail::getSyclObjImpl(get_kernel_bundle<bundle_state::input>(
119+
MQueue->get_context(), {MQueue->get_device()}, {}));
122120

123121
detail::ExtendedMemberT EMember = {
124122
detail::ExtendedMembersType::HANDLER_KERNEL_BUNDLE, KernelBundleImpPtr};
@@ -134,12 +132,11 @@ handler::getOrInsertHandlerKernelBundle(bool Insert) const {
134132
// returns newly created kernel_bundle with KernelId if Insert is true
135133
// returns shared_ptr(nullptr) if Insert is false
136134
//
137-
// If there already existed a kernel_bundle, the underlying device images are
138-
// filtered such that only the ones containing KernelId remain in the
139-
// kernel_bundle.
135+
// If there already existed an implicitly created kernel_bundle, the kernel is
136+
// inserted into that bundle.
140137
std::shared_ptr<detail::kernel_bundle_impl>
141-
handler::getOrInsertFilteredHandlerKernelBundle(bool Insert,
142-
kernel_id KernelId) const {
138+
handler::getOrInsertHandlerKernelBundle(bool Insert,
139+
const kernel_id &KernelId) const {
143140

144141
std::lock_guard<std::mutex> Lock(
145142
detail::GlobalHandler::instance().getHandlerExtendedMembersMutex());
@@ -162,11 +159,11 @@ handler::getOrInsertFilteredHandlerKernelBundle(bool Insert,
162159
if (Insert) {
163160
KernelBundleImpPtr =
164161
detail::getSyclObjImpl(get_kernel_bundle<bundle_state::input>(
165-
MQueue->get_context(), {KernelId}));
162+
MQueue->get_context(), {MQueue->get_device()}, {KernelId}));
166163
if (KernelBundleImpPtr->empty()) {
167164
KernelBundleImpPtr =
168165
detail::getSyclObjImpl(get_kernel_bundle<bundle_state::executable>(
169-
MQueue->get_context(), {KernelId}));
166+
MQueue->get_context(), {MQueue->get_device()}, {KernelId}));
170167
}
171168

172169
detail::ExtendedMemberT EMember = {
@@ -184,7 +181,7 @@ handler::getOrInsertFilteredHandlerKernelBundle(bool Insert,
184181

185182
// Kernel bundles set explicitly by the user must not be filtered
186183
if (!HandlerImpl->isStateExplicitKernelBundle())
187-
KernelBundleImpPtr->filterImages(KernelId);
184+
KernelBundleImpPtr->add_kernel(KernelId, MQueue->get_device());
188185

189186
return KernelBundleImpPtr;
190187
}
@@ -230,6 +227,13 @@ event handler::finalize() {
230227
// If there were uses of set_specialization_constant build the kernel_bundle
231228
KernelBundleImpPtr = getOrInsertHandlerKernelBundle(/*Insert=*/false);
232229
if (KernelBundleImpPtr) {
230+
// Make sure implicit kernel bundles has the kernel
231+
if (!getHandlerImpl()->isStateExplicitKernelBundle()) {
232+
kernel_id KernelID =
233+
detail::ProgramManager::getInstance().getSYCLKernelID(MKernelName);
234+
KernelBundleImpPtr->add_kernel(KernelID, MQueue->get_device());
235+
}
236+
233237
switch (KernelBundleImpPtr->get_bundle_state()) {
234238
case bundle_state::input: {
235239
// Underlying level expects kernel_bundle to be in executable state
@@ -679,6 +683,10 @@ void handler::verifyUsedKernelBundle(const std::string &KernelName) {
679683
if (!UsedKernelBundleImplPtr)
680684
return;
681685

686+
// Implicit kernel bundles are populated late so we ignore them
687+
if (!getHandlerImpl()->isStateExplicitKernelBundle())
688+
return;
689+
682690
kernel_id KernelID = detail::get_kernel_id_impl(KernelName);
683691
device Dev = detail::getDeviceFromHandler(*this);
684692
if (!UsedKernelBundleImplPtr->has_kernel(KernelID, Dev))

sycl/source/kernel_bundle.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ get_empty_interop_kernel_bundle_impl(const context &Ctx,
146146

147147
std::shared_ptr<detail::kernel_bundle_impl>
148148
join_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles) {
149-
return std::make_shared<detail::kernel_bundle_impl>(Bundles);
149+
return std::make_shared<detail::kernel_bundle_impl>(Bundles,
150+
bundle_state::input);
151+
}
152+
153+
std::shared_ptr<detail::kernel_bundle_impl>
154+
join_impl(const std::vector<detail::KernelBundleImplPtr> &Bundles,
155+
bundle_state State) {
156+
return std::make_shared<detail::kernel_bundle_impl>(Bundles, State);
150157
}
151158

152159
bool has_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3903,6 +3903,7 @@ _ZN2cl4sycl6detail6OSUtil16getCurrentDSODirB5cxx11Ev
39033903
_ZN2cl4sycl6detail6OSUtil17getOSModuleHandleEPKv
39043904
_ZN2cl4sycl6detail6OSUtil7makeDirEPKc
39053905
_ZN2cl4sycl6detail9join_implERKSt6vectorISt10shared_ptrINS1_18kernel_bundle_implEESaIS5_EE
3906+
_ZN2cl4sycl6detail9join_implERKSt6vectorISt10shared_ptrINS1_18kernel_bundle_implEESaIS5_EENS0_12bundle_stateE
39063907
_ZN2cl4sycl6detail9link_implERKSt6vectorINS0_13kernel_bundleILNS0_12bundle_stateE1EEESaIS5_EERKS2_INS0_6deviceESaISA_EERKNS0_13property_listE
39073908
_ZN2cl4sycl6device11get_devicesENS0_4info11device_typeE
39083909
_ZN2cl4sycl6deviceC1EP13_cl_device_id
@@ -4320,7 +4321,7 @@ _ZNK2cl4sycl7context9getNativeEv
43204321
_ZNK2cl4sycl7handler14getHandlerImplEv
43214322
_ZNK2cl4sycl7handler27isStateExplicitKernelBundleEv
43224323
_ZNK2cl4sycl7handler30getOrInsertHandlerKernelBundleEb
4323-
_ZNK2cl4sycl7handler38getOrInsertFilteredHandlerKernelBundleEbNS0_9kernel_idE
4324+
_ZNK2cl4sycl7handler30getOrInsertHandlerKernelBundleEbRKNS0_9kernel_idE
43244325
_ZNK2cl4sycl7program10get_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
43254326
_ZNK2cl4sycl7program10get_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb
43264327
_ZNK2cl4sycl7program10has_kernelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,6 @@
21312131
?getOSMemSize@OSUtil@detail@sycl@cl@@SA_KXZ
21322132
?getOSModuleHandle@OSUtil@detail@sycl@cl@@SA_JPEBX@Z
21332133
?getOrCreateSampler@sampler_impl@detail@sycl@cl@@QEAAPEAU_pi_sampler@@AEBVcontext@34@@Z
2134-
?getOrInsertFilteredHandlerKernelBundle@handler@sycl@cl@@AEBA?AV?$shared_ptr@Vkernel_bundle_impl@detail@sycl@cl@@@std@@_NVkernel_id@23@@Z
21352134
?getOrInsertHandlerKernelBundle@handler@sycl@cl@@AEBA?AV?$shared_ptr@Vkernel_bundle_impl@detail@sycl@cl@@@std@@_N@Z
21362135
?getOrWaitEvents@detail@sycl@cl@@YA?AV?$vector@PEAU_pi_event@@V?$allocator@PEAU_pi_event@@@std@@@std@@V?$vector@Vevent@sycl@cl@@V?$allocator@Vevent@sycl@cl@@@std@@@5@V?$shared_ptr@Vcontext_impl@detail@sycl@cl@@@5@@Z
21372136
?getPixelCoordLinearFiltMode@detail@sycl@cl@@YA?AV?$vec@H$07@23@V?$vec@M$03@23@W4addressing_mode@23@V?$range@$02@23@AEAV523@@Z

0 commit comments

Comments
 (0)