Skip to content

Commit b8b6ddc

Browse files
vinser52againull
andauthored
[SYCL] use raw pointer to device_impl in ProgramManager (where possible) (#18178)
Just a simple code refactoring. --------- Co-authored-by: Artur Gainullin <[email protected]>
1 parent 33e5510 commit b8b6ddc

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

sycl/source/detail/helpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ retrieveKernelBinary(const QueueImplPtr &Queue, const char *KernelName,
8787
auto DeviceImpl = Queue->getDeviceImplPtr();
8888
auto Device = detail::createSyclObjFromImpl<device>(DeviceImpl);
8989
DeviceImage = &detail::ProgramManager::getInstance().getDeviceImage(
90-
KernelName, ContextImpl, Device);
90+
KernelName, ContextImpl, DeviceImpl.get());
9191
Program = detail::ProgramManager::getInstance().createURProgram(
9292
*DeviceImage, ContextImpl, {std::move(Device)});
9393
}

sycl/source/detail/memory_manager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,8 @@ getOrBuildProgramForDeviceGlobal(QueueImplPtr Queue,
12231223
// If there was no cached program, build one.
12241224
auto Context = createSyclObjFromImpl<context>(ContextImpl);
12251225
ProgramManager &PM = ProgramManager::getInstance();
1226-
RTDeviceBinaryImage &Img =
1227-
PM.getDeviceImage(DeviceGlobalEntry->MImages, ContextImpl, Device);
1226+
RTDeviceBinaryImage &Img = PM.getDeviceImage(
1227+
DeviceGlobalEntry->MImages, ContextImpl, getSyclObjImpl(Device).get());
12281228
device_image_plain DeviceImage =
12291229
PM.getDeviceImageFromBinaryImage(&Img, Context, Device);
12301230
device_image_plain BuiltImage =

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
873873
auto Device = createSyclObjFromImpl<device>(
874874
MustBuildOnSubdevice == true ? DeviceImpl : RootDevImpl);
875875
const RTDeviceBinaryImage &Img =
876-
getDeviceImage(KernelName, ContextImpl, Device);
876+
getDeviceImage(KernelName, ContextImpl, getSyclObjImpl(Device).get());
877877

878878
// Check that device supports all aspects used by the kernel
879879
if (auto exception = checkDevSupportDeviceRequirements(Device, Img, NDRDesc))
@@ -1470,9 +1470,9 @@ ProgramManager::ProgramManager()
14701470
}
14711471
}
14721472

1473-
const char *getArchName(const device &Device) {
1473+
const char *getArchName(const device_impl *DeviceImpl) {
14741474
namespace syclex = sycl::ext::oneapi::experimental;
1475-
auto Arch = getSyclObjImpl(Device)->getDeviceArch();
1475+
auto Arch = DeviceImpl->getDeviceArch();
14761476
switch (Arch) {
14771477
#define __SYCL_ARCHITECTURE(ARCH, VAL) \
14781478
case syclex::architecture::ARCH: \
@@ -1494,7 +1494,7 @@ template <typename StorageKey>
14941494
RTDeviceBinaryImage *getBinImageFromMultiMap(
14951495
const std::unordered_multimap<StorageKey, RTDeviceBinaryImage *> &ImagesSet,
14961496
const StorageKey &Key, const ContextImplPtr &ContextImpl,
1497-
const device &Device) {
1497+
const device_impl *DeviceImpl) {
14981498
auto [ItBegin, ItEnd] = ImagesSet.equal_range(Key);
14991499
if (ItBegin == ItEnd)
15001500
return nullptr;
@@ -1507,7 +1507,7 @@ RTDeviceBinaryImage *getBinImageFromMultiMap(
15071507
std::vector<RTDeviceBinaryImage *> DeviceFilteredImgs;
15081508
DeviceFilteredImgs.reserve(std::distance(ItBegin, ItEnd));
15091509
for (auto It = ItBegin; It != ItEnd; ++It) {
1510-
if (doesImageTargetMatchDevice(*It->second, Device))
1510+
if (doesImageTargetMatchDevice(*It->second, DeviceImpl))
15111511
DeviceFilteredImgs.push_back(It->second);
15121512
}
15131513

@@ -1525,19 +1525,18 @@ RTDeviceBinaryImage *getBinImageFromMultiMap(
15251525
// Ask the native runtime under the given context to choose the device image
15261526
// it prefers.
15271527
ContextImpl->getAdapter()->call<UrApiKind::urDeviceSelectBinary>(
1528-
getSyclObjImpl(Device)->getHandleRef(), UrBinaries.data(),
1529-
UrBinaries.size(), &ImgInd);
1528+
DeviceImpl->getHandleRef(), UrBinaries.data(), UrBinaries.size(),
1529+
&ImgInd);
15301530
return DeviceFilteredImgs[ImgInd];
15311531
}
15321532

15331533
RTDeviceBinaryImage &
15341534
ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
15351535
const ContextImplPtr &ContextImpl,
1536-
const device &Device) {
1536+
const device_impl *DeviceImpl) {
15371537
if constexpr (DbgProgMgr > 0) {
15381538
std::cerr << ">>> ProgramManager::getDeviceImage(\"" << KernelName << "\", "
1539-
<< ContextImpl.get() << ", " << getSyclObjImpl(Device).get()
1540-
<< ")\n";
1539+
<< ContextImpl.get() << ", " << DeviceImpl << ")\n";
15411540

15421541
std::cerr << "available device images:\n";
15431542
debugPrintBinaryImages();
@@ -1547,7 +1546,7 @@ ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
15471546
assert(m_SpvFileImage);
15481547
return getDeviceImage(
15491548
std::unordered_set<RTDeviceBinaryImage *>({m_SpvFileImage.get()}),
1550-
ContextImpl, Device);
1549+
ContextImpl, DeviceImpl);
15511550
}
15521551

15531552
RTDeviceBinaryImage *Img = nullptr;
@@ -1556,10 +1555,10 @@ ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
15561555
if (auto KernelId = m_KernelName2KernelIDs.find(KernelName);
15571556
KernelId != m_KernelName2KernelIDs.end()) {
15581557
Img = getBinImageFromMultiMap(m_KernelIDs2BinImage, KernelId->second,
1559-
ContextImpl, Device);
1558+
ContextImpl, DeviceImpl);
15601559
} else {
15611560
Img = getBinImageFromMultiMap(m_ServiceKernels, KernelName, ContextImpl,
1562-
Device);
1561+
DeviceImpl);
15631562
}
15641563
}
15651564

@@ -1580,13 +1579,12 @@ ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
15801579

15811580
RTDeviceBinaryImage &ProgramManager::getDeviceImage(
15821581
const std::unordered_set<RTDeviceBinaryImage *> &ImageSet,
1583-
const ContextImplPtr &ContextImpl, const device &Device) {
1582+
const ContextImplPtr &ContextImpl, const device_impl *DeviceImpl) {
15841583
assert(ImageSet.size() > 0);
15851584

15861585
if constexpr (DbgProgMgr > 0) {
15871586
std::cerr << ">>> ProgramManager::getDeviceImage(Custom SPV file "
1588-
<< ContextImpl.get() << ", " << getSyclObjImpl(Device).get()
1589-
<< ")\n";
1587+
<< ContextImpl.get() << ", " << DeviceImpl << ")\n";
15901588

15911589
std::cerr << "available device images:\n";
15921590
debugPrintBinaryImages();
@@ -1609,8 +1607,8 @@ RTDeviceBinaryImage &ProgramManager::getDeviceImage(
16091607
}
16101608

16111609
ContextImpl->getAdapter()->call<UrApiKind::urDeviceSelectBinary>(
1612-
getSyclObjImpl(Device)->getHandleRef(), UrBinaries.data(),
1613-
UrBinaries.size(), &ImgInd);
1610+
DeviceImpl->getHandleRef(), UrBinaries.data(), UrBinaries.size(),
1611+
&ImgInd);
16141612

16151613
ImageIterator = ImageSet.begin();
16161614
std::advance(ImageIterator, ImgInd);
@@ -3801,29 +3799,29 @@ checkDevSupportDeviceRequirements(const device &Dev,
38013799
}
38023800

38033801
bool doesImageTargetMatchDevice(const RTDeviceBinaryImage &Img,
3804-
const device &Dev) {
3802+
const device_impl *DevImpl) {
38053803
auto PropRange = Img.getDeviceRequirements();
38063804
auto PropIt =
38073805
std::find_if(PropRange.begin(), PropRange.end(), [&](const auto &Prop) {
38083806
return Prop->Name == std::string_view("compile_target");
38093807
});
38103808
// Device image has no compile_target property, check target.
38113809
if (PropIt == PropRange.end()) {
3812-
sycl::backend BE = Dev.get_backend();
3810+
sycl::backend BE = DevImpl->getBackend();
38133811
const char *Target = Img.getRawData().DeviceTargetSpec;
38143812
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64) == 0) {
38153813
return (BE == sycl::backend::opencl ||
38163814
BE == sycl::backend::ext_oneapi_level_zero);
38173815
}
38183816
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_X86_64) == 0) {
3819-
return Dev.is_cpu();
3817+
return DevImpl->is_cpu();
38203818
}
38213819
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_GEN) == 0) {
3822-
return Dev.is_gpu() && (BE == sycl::backend::opencl ||
3823-
BE == sycl::backend::ext_oneapi_level_zero);
3820+
return DevImpl->is_gpu() && (BE == sycl::backend::opencl ||
3821+
BE == sycl::backend::ext_oneapi_level_zero);
38243822
}
38253823
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_FPGA) == 0) {
3826-
return Dev.is_accelerator();
3824+
return DevImpl->is_accelerator();
38273825
}
38283826
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_NVPTX64) == 0 ||
38293827
strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_LLVM_NVPTX64) == 0) {
@@ -3848,7 +3846,7 @@ bool doesImageTargetMatchDevice(const RTDeviceBinaryImage &Img,
38483846
std::string_view CompileTarget(
38493847
reinterpret_cast<const char *>(&CompileTargetByteArray[0]),
38503848
CompileTargetByteArray.size());
3851-
std::string_view ArchName = getArchName(Dev);
3849+
std::string_view ArchName = getArchName(DevImpl);
38523850
// Note: there are no explicit targets for CPUs, so on x86_64,
38533851
// intel_cpu_spr, and intel_cpu_gnr, we use a spir64_x86_64
38543852
// compile target image.

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ checkDevSupportDeviceRequirements(const device &Dev,
6363
const NDRDescT &NDRDesc = {});
6464

6565
bool doesImageTargetMatchDevice(const RTDeviceBinaryImage &Img,
66-
const device &Dev);
66+
const device_impl *DevImpl);
6767

6868
// This value must be the same as in libdevice/device_itt.h.
6969
// See sycl/doc/design/ITTAnnotations.md for more info.
@@ -136,11 +136,11 @@ class ProgramManager {
136136

137137
RTDeviceBinaryImage &getDeviceImage(KernelNameStrRefT KernelName,
138138
const ContextImplPtr &ContextImpl,
139-
const device &Device);
139+
const device_impl *DeviceImpl);
140140

141141
RTDeviceBinaryImage &getDeviceImage(
142142
const std::unordered_set<RTDeviceBinaryImage *> &ImagesToVerify,
143-
const ContextImplPtr &ContextImpl, const device &Device);
143+
const ContextImplPtr &ContextImpl, const device_impl *DeviceImpl);
144144

145145
ur_program_handle_t createURProgram(const RTDeviceBinaryImage &Img,
146146
const ContextImplPtr &ContextImpl,

sycl/source/kernel_bundle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ bool is_compatible(const std::vector<kernel_id> &KernelIDs, const device &Dev) {
365365
if (std::none_of(BinImages.begin(), BinImages.end(),
366366
[&](const detail::RTDeviceBinaryImage *Img) {
367367
return doesDevSupportDeviceRequirements(Dev, *Img) &&
368-
doesImageTargetMatchDevice(*Img, Dev);
368+
doesImageTargetMatchDevice(
369+
*Img, getSyclObjImpl(Dev).get());
369370
}))
370371
return false;
371372
}

0 commit comments

Comments
 (0)