Skip to content

Commit a36946d

Browse files
authored
[SYCL] Use refs to device_impl in ProgramManager (#18320)
Refactored the `ProgramManager` to use `device_impl &` instead of `const device &`. See #18270 and #18251 that started the refactoring. Signed-off-by: Sergei Vinogradov <[email protected]>
1 parent 5750198 commit a36946d

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,7 @@ static const char *getUrDeviceTarget(const char *URDeviceTarget) {
583583
}
584584

585585
static bool compatibleWithDevice(RTDeviceBinaryImage *BinImage,
586-
const device &Dev) {
587-
detail::device_impl &DeviceImpl = *detail::getSyclObjImpl(Dev);
586+
const device_impl &DeviceImpl) {
588587
auto &Adapter = DeviceImpl.getAdapter();
589588

590589
const ur_device_handle_t &URDeviceHandle = DeviceImpl.getHandleRef();
@@ -621,7 +620,7 @@ bool ProgramManager::isSpecialDeviceImage(RTDeviceBinaryImage *BinImage) {
621620
}
622621

623622
bool ProgramManager::isSpecialDeviceImageShouldBeUsed(
624-
RTDeviceBinaryImage *BinImage, const device &Dev) {
623+
RTDeviceBinaryImage *BinImage, const device_impl &DeviceImpl) {
625624
// Decide whether a devicelib image should be used.
626625
int Bfloat16DeviceLibVersion = -1;
627626
if (m_Bfloat16DeviceLibImages[0].get() == BinImage)
@@ -640,7 +639,6 @@ bool ProgramManager::isSpecialDeviceImageShouldBeUsed(
640639
// more devicelib images in this way.
641640
enum { DEVICELIB_FALLBACK = 0, DEVICELIB_NATIVE };
642641
ur_bool_t NativeBF16Supported = false;
643-
detail::device_impl &DeviceImpl = *detail::getSyclObjImpl(Dev);
644642
ur_result_t CallSuccessful =
645643
DeviceImpl.getAdapter()->call_nocheck<UrApiKind::urDeviceGetInfo>(
646644
DeviceImpl.getHandleRef(),
@@ -658,15 +656,15 @@ bool ProgramManager::isSpecialDeviceImageShouldBeUsed(
658656
return false;
659657
}
660658

661-
static bool checkLinkingSupport(const device &Dev,
659+
static bool checkLinkingSupport(const device_impl &DeviceImpl,
662660
const RTDeviceBinaryImage &Img) {
663661
const char *Target = Img.getRawData().DeviceTargetSpec;
664662
// TODO replace with extension checks once implemented in UR.
665663
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64) == 0) {
666664
return true;
667665
}
668666
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_GEN) == 0) {
669-
return Dev.is_gpu() && Dev.get_backend() == backend::opencl;
667+
return DeviceImpl.is_gpu() && DeviceImpl.getBackend() == backend::opencl;
670668
}
671669
return false;
672670
}
@@ -701,7 +699,8 @@ ProgramManager::collectDeviceImageDepsForImportedSymbols(
701699
HandledSymbols.insert(ISProp->Name);
702700
}
703701
ur::DeviceBinaryType Format = MainImg.getFormat();
704-
if (!WorkList.empty() && !checkLinkingSupport(Dev, MainImg))
702+
if (!WorkList.empty() &&
703+
!checkLinkingSupport(*getSyclObjImpl(Dev).get(), MainImg))
705704
throw exception(make_error_code(errc::feature_not_supported),
706705
"Cannot resolve external symbols, linking is unsupported "
707706
"for the backend");
@@ -715,10 +714,10 @@ ProgramManager::collectDeviceImageDepsForImportedSymbols(
715714
RTDeviceBinaryImage *Img = It->second;
716715
if (Img->getFormat() != Format ||
717716
!doesDevSupportDeviceRequirements(Dev, *Img) ||
718-
!compatibleWithDevice(Img, Dev))
717+
!compatibleWithDevice(Img, *getSyclObjImpl(Dev).get()))
719718
continue;
720719
if (isSpecialDeviceImage(Img) &&
721-
!isSpecialDeviceImageShouldBeUsed(Img, Dev))
720+
!isSpecialDeviceImageShouldBeUsed(Img, *getSyclObjImpl(Dev).get()))
722721
continue;
723722
DeviceImagesToLink.insert(Img);
724723
Found = true;
@@ -2415,14 +2414,14 @@ kernel_id ProgramManager::getSYCLKernelID(KernelNameStrRefT KernelName) {
24152414
"No kernel found with the specified name");
24162415
}
24172416

2418-
bool ProgramManager::hasCompatibleImage(const device &Dev) {
2417+
bool ProgramManager::hasCompatibleImage(const device_impl &DeviceImpl) {
24192418
std::lock_guard<std::mutex> Guard(m_KernelIDsMutex);
24202419

24212420
return std::any_of(
24222421
m_BinImg2KernelIDs.cbegin(), m_BinImg2KernelIDs.cend(),
24232422
[&](std::pair<RTDeviceBinaryImage *,
24242423
std::shared_ptr<std::vector<kernel_id>>>
2425-
Elem) { return compatibleWithDevice(Elem.first, Dev); });
2424+
Elem) { return compatibleWithDevice(Elem.first, DeviceImpl); });
24262425
}
24272426

24282427
std::vector<kernel_id> ProgramManager::getAllSYCLKernelIDs() {
@@ -2555,7 +2554,7 @@ device_image_plain ProgramManager::getDeviceImageFromBinaryImage(
25552554
RTDeviceBinaryImage *BinImage, const context &Ctx, const device &Dev) {
25562555
const bundle_state ImgState = getBinImageState(BinImage);
25572556

2558-
assert(compatibleWithDevice(BinImage, Dev));
2557+
assert(compatibleWithDevice(BinImage, *getSyclObjImpl(Dev).get()));
25592558

25602559
std::shared_ptr<std::vector<sycl::kernel_id>> KernelIDs;
25612560
// Collect kernel names for the image.
@@ -2640,7 +2639,7 @@ ProgramManager::getSYCLDeviceImagesWithCompatibleState(
26402639
KernelImageMap.insert({KernelID, {}});
26412640

26422641
for (RTDeviceBinaryImage *BinImage : BinImages) {
2643-
if (!compatibleWithDevice(BinImage, Dev) ||
2642+
if (!compatibleWithDevice(BinImage, *getSyclObjImpl(Dev).get()) ||
26442643
!doesDevSupportDeviceRequirements(Dev, *BinImage))
26452644
continue;
26462645

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class ProgramManager {
254254
const char *UniqueId);
255255

256256
// Returns true if any available image is compatible with the device Dev.
257-
bool hasCompatibleImage(const device &Dev);
257+
bool hasCompatibleImage(const device_impl &DeviceImpl);
258258

259259
// The function gets a device_global entry identified by the pointer to the
260260
// device_global object from the device_global map.
@@ -406,7 +406,7 @@ class ProgramManager {
406406

407407
bool isSpecialDeviceImage(RTDeviceBinaryImage *BinImage);
408408
bool isSpecialDeviceImageShouldBeUsed(RTDeviceBinaryImage *BinImage,
409-
const device &Dev);
409+
const device_impl &DeviceImpl);
410410

411411
protected:
412412
/// The three maps below are used during kernel resolution. Any kernel is

sycl/source/device_selector.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ namespace detail {
3333
// itself, so only matching devices will be scored.
3434
static int getDevicePreference(const device &Device) {
3535
int Score = 0;
36-
36+
const device_impl &DeviceImpl = *getSyclObjImpl(Device).get();
3737
// Strongly prefer devices with available images.
3838
auto &program_manager = sycl::detail::ProgramManager::getInstance();
39-
if (program_manager.hasCompatibleImage(Device))
39+
if (program_manager.hasCompatibleImage(DeviceImpl))
4040
Score += 1000;
4141

4242
// Prefer level_zero backend devices.
43-
if (detail::getSyclObjImpl(Device)->getBackend() ==
44-
backend::ext_oneapi_level_zero)
43+
if (DeviceImpl.getBackend() == backend::ext_oneapi_level_zero)
4544
Score += 50;
4645

4746
return Score;

0 commit comments

Comments
 (0)