Skip to content

Commit 968ac4c

Browse files
[NFC][SYCL] More use of device_images_range (#20692)
1 parent 7e6e803 commit 968ac4c

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

sycl/source/detail/device_image_impl.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,21 @@ class device_images_range : public iterator_range<device_images_iterator> {
13611361

13621362
public:
13631363
using Base::Base;
1364+
template <typename Container>
1365+
decltype(std::declval<Base>().to<Container>()) to() const {
1366+
return this->Base::to<Container>();
1367+
}
1368+
1369+
template <typename Container>
1370+
std::enable_if_t<std::is_same_v<Container, std::vector<ur_program_handle_t>>,
1371+
Container>
1372+
to() const {
1373+
std::vector<ur_program_handle_t> ProgramHandles;
1374+
ProgramHandles.reserve(size());
1375+
std::transform(begin(), end(), std::back_inserter(ProgramHandles),
1376+
[](device_image_impl &Img) { return Img.get_ur_program(); });
1377+
return ProgramHandles;
1378+
}
13641379
};
13651380

13661381
} // namespace detail

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,24 +2908,22 @@ ProgramManager::compile(const DevImgPlainWithDeps &ImgWithDeps,
29082908
// Returns a merged device binary image, new set of kernel IDs and new
29092909
// specialization constant data.
29102910
static const RTDeviceBinaryImage *
2911-
mergeImageData(const std::vector<device_image_plain> &Imgs,
2912-
std::vector<kernel_id> &KernelIDs,
2911+
mergeImageData(device_images_range Imgs, std::vector<kernel_id> &KernelIDs,
29132912
std::vector<unsigned char> &NewSpecConstBlob,
29142913
device_image_impl::SpecConstMapT &NewSpecConstMap,
29152914
std::unique_ptr<DynRTDeviceBinaryImage> &MergedImageStorage) {
2916-
for (const device_image_plain &Img : Imgs) {
2917-
device_image_impl &DeviceImageImpl = *getSyclObjImpl(Img);
2915+
for (device_image_impl &Img : Imgs) {
29182916
// Duplicates are not expected here, otherwise urProgramLink should fail
2919-
KernelIDs.insert(KernelIDs.end(), DeviceImageImpl.get_kernel_ids().begin(),
2920-
DeviceImageImpl.get_kernel_ids().end());
2917+
KernelIDs.insert(KernelIDs.end(), Img.get_kernel_ids().begin(),
2918+
Img.get_kernel_ids().end());
29212919
// To be able to answer queries about specialziation constants, the new
29222920
// device image should have the specialization constants from all the linked
29232921
// images.
29242922
const std::lock_guard<std::mutex> SpecConstLock(
2925-
DeviceImageImpl.get_spec_const_data_lock());
2923+
Img.get_spec_const_data_lock());
29262924
// Copy all map entries to the new map. Since the blob will be copied to
29272925
// the end of the new blob we need to move the blob offset of each entry.
2928-
for (const auto &SpecConstIt : DeviceImageImpl.get_spec_const_data_ref()) {
2926+
for (const auto &SpecConstIt : Img.get_spec_const_data_ref()) {
29292927
std::vector<device_image_impl::SpecConstDescT> &NewDescEntries =
29302928
NewSpecConstMap[SpecConstIt.first];
29312929

@@ -2943,21 +2941,21 @@ mergeImageData(const std::vector<device_image_plain> &Imgs,
29432941
// Copy the blob from the device image into the new blob. This moves the
29442942
// offsets of the following blobs.
29452943
NewSpecConstBlob.insert(NewSpecConstBlob.end(),
2946-
DeviceImageImpl.get_spec_const_blob_ref().begin(),
2947-
DeviceImageImpl.get_spec_const_blob_ref().end());
2944+
Img.get_spec_const_blob_ref().begin(),
2945+
Img.get_spec_const_blob_ref().end());
29482946
}
29492947
// device_image_impl expects kernel ids to be sorted for fast search
29502948
std::sort(KernelIDs.begin(), KernelIDs.end(), LessByHash<kernel_id>{});
29512949

29522950
// If there is only a single image, use it as the result.
29532951
if (Imgs.size() == 1)
2954-
return getSyclObjImpl(Imgs[0])->get_bin_image_ref();
2952+
return Imgs.front().get_bin_image_ref();
29552953

29562954
// Otherwise we create a dynamic image with the merged information.
29572955
std::vector<const RTDeviceBinaryImage *> BinImgs;
29582956
BinImgs.reserve(Imgs.size());
2959-
for (const device_image_plain &Img : Imgs) {
2960-
auto ImgBinRef = getSyclObjImpl(Img)->get_bin_image_ref();
2957+
for (device_image_impl &Img : Imgs) {
2958+
auto ImgBinRef = Img.get_bin_image_ref();
29612959
// For some cases, like SYCL kernel compiler binaries, we don't have
29622960
// binaries. For these we assume no properties associated, so they can be
29632961
// safely ignored.
@@ -2969,25 +2967,21 @@ mergeImageData(const std::vector<device_image_plain> &Imgs,
29692967
}
29702968

29712969
std::vector<device_image_plain>
2972-
ProgramManager::link(const std::vector<device_image_plain> &Imgs,
2973-
devices_range Devs, const property_list &PropList) {
2970+
ProgramManager::link(device_images_range Imgs, devices_range Devs,
2971+
const property_list &PropList) {
29742972
{
29752973
auto NoAllowedPropertiesCheck = [](int) { return false; };
29762974
detail::PropertyValidator::checkPropsAndThrow(
29772975
PropList, NoAllowedPropertiesCheck, NoAllowedPropertiesCheck);
29782976
}
29792977

2980-
std::vector<ur_program_handle_t> URPrograms;
2981-
URPrograms.reserve(Imgs.size());
2982-
for (const device_image_plain &Img : Imgs)
2983-
URPrograms.push_back(getSyclObjImpl(Img)->get_ur_program());
2984-
2978+
auto URPrograms = Imgs.to<std::vector<ur_program_handle_t>>();
29852979
auto URDevices = Devs.to<std::vector<ur_device_handle_t>>();
29862980

29872981
// FIXME: Linker options are picked from the first object, but is that safe?
29882982
std::string LinkOptionsStr;
29892983
applyLinkOptionsFromEnvironment(LinkOptionsStr);
2990-
device_image_impl &FirstImgImpl = *getSyclObjImpl(Imgs[0]);
2984+
device_image_impl &FirstImgImpl = Imgs.front();
29912985
if (LinkOptionsStr.empty() && FirstImgImpl.get_bin_image_ref())
29922986
appendLinkOptionsFromImage(LinkOptionsStr,
29932987
*(FirstImgImpl.get_bin_image_ref()));
@@ -3045,8 +3039,8 @@ ProgramManager::link(const std::vector<device_image_plain> &Imgs,
30453039
// underlying program disposed of). Protecting from incorrect values by
30463040
// removal of map entries with same handle (obviously invalid entries).
30473041
std::ignore = NativePrograms.erase(LinkedProg);
3048-
for (const device_image_plain &Img : Imgs) {
3049-
if (auto BinImageRef = getSyclObjImpl(Img)->get_bin_image_ref())
3042+
for (device_image_impl &Img : Imgs) {
3043+
if (auto BinImageRef = Img.get_bin_image_ref())
30503044
NativePrograms.insert(
30513045
{LinkedProg, {ContextImpl.shared_from_this(), BinImageRef}});
30523046
}
@@ -3062,15 +3056,14 @@ ProgramManager::link(const std::vector<device_image_plain> &Imgs,
30623056
KernelNameSetT MergedKernelNames;
30633057
std::map<std::string, KernelArgMask, std::less<>>
30643058
MergedEliminatedKernelArgMasks;
3065-
for (const device_image_plain &DevImg : Imgs) {
3066-
device_image_impl &DevImgImpl = *getSyclObjImpl(DevImg);
3067-
CombinedOrigins |= DevImgImpl.getOriginMask();
3068-
RTCInfoPtrs.emplace_back(&(DevImgImpl.getRTCInfo()));
3069-
MergedKernelNames.insert(DevImgImpl.getKernelNames().begin(),
3070-
DevImgImpl.getKernelNames().end());
3059+
for (device_image_impl &DevImg : Imgs) {
3060+
CombinedOrigins |= DevImg.getOriginMask();
3061+
RTCInfoPtrs.emplace_back(&(DevImg.getRTCInfo()));
3062+
MergedKernelNames.insert(DevImg.getKernelNames().begin(),
3063+
DevImg.getKernelNames().end());
30713064
MergedEliminatedKernelArgMasks.insert(
3072-
DevImgImpl.getEliminatedKernelArgMasks().begin(),
3073-
DevImgImpl.getEliminatedKernelArgMasks().end());
3065+
DevImg.getEliminatedKernelArgMasks().begin(),
3066+
DevImg.getEliminatedKernelArgMasks().end());
30743067
}
30753068
auto MergedRTCInfo = detail::KernelCompilerBinaryInfo::Merge(RTCInfoPtrs);
30763069

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class device_impl;
7676
class devices_range;
7777
class queue_impl;
7878
class event_impl;
79+
class device_images_range;
7980
// DeviceLibExt is shared between sycl runtime and sycl-post-link tool.
8081
// If any update is made here, need to sync with DeviceLibExt definition
8182
// in llvm/tools/sycl-post-link/sycl-post-link.cpp
@@ -355,9 +356,9 @@ class ProgramManager {
355356

356357
// Produces set of device images by convering input device images to object
357358
// the executable state
358-
std::vector<device_image_plain>
359-
link(const std::vector<device_image_plain> &Imgs, devices_range Devs,
360-
const property_list &PropList);
359+
std::vector<device_image_plain> link(device_images_range Imgs,
360+
devices_range Devs,
361+
const property_list &PropList);
361362

362363
// Produces new device image by converting input device image to the
363364
// executable state

0 commit comments

Comments
 (0)