Skip to content

[SYCL] use raw pointer to device_impl in ProgramManager (where possible) #18178

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

Open
wants to merge 1 commit into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion sycl/source/detail/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ retrieveKernelBinary(const QueueImplPtr &Queue, const char *KernelName,
auto DeviceImpl = Queue->getDeviceImplPtr();
auto Device = detail::createSyclObjFromImpl<device>(DeviceImpl);
DeviceImage = &detail::ProgramManager::getInstance().getDeviceImage(
KernelName, ContextImpl, Device);
KernelName, ContextImpl, getSyclObjImpl(Device).get());
Program = detail::ProgramManager::getInstance().createURProgram(
*DeviceImage, ContextImpl, {std::move(Device)});
}
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,8 @@ getOrBuildProgramForDeviceGlobal(QueueImplPtr Queue,
// If there was no cached program, build one.
auto Context = createSyclObjFromImpl<context>(ContextImpl);
ProgramManager &PM = ProgramManager::getInstance();
RTDeviceBinaryImage &Img =
PM.getDeviceImage(DeviceGlobalEntry->MImages, ContextImpl, Device);
RTDeviceBinaryImage &Img = PM.getDeviceImage(
DeviceGlobalEntry->MImages, ContextImpl, getSyclObjImpl(Device).get());
device_image_plain DeviceImage =
PM.getDeviceImageFromBinaryImage(&Img, Context, Device);
device_image_plain BuiltImage =
Expand Down
48 changes: 23 additions & 25 deletions sycl/source/detail/program_manager/program_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
auto Device = createSyclObjFromImpl<device>(
MustBuildOnSubdevice == true ? DeviceImpl : RootDevImpl);
const RTDeviceBinaryImage &Img =
getDeviceImage(KernelName, ContextImpl, Device);
getDeviceImage(KernelName, ContextImpl, getSyclObjImpl(Device).get());

// Check that device supports all aspects used by the kernel
if (auto exception = checkDevSupportDeviceRequirements(Device, Img, NDRDesc))
Expand Down Expand Up @@ -1471,9 +1471,9 @@ ProgramManager::ProgramManager()
}
}

const char *getArchName(const device &Device) {
const char *getArchName(const device_impl *DeviceImpl) {
namespace syclex = sycl::ext::oneapi::experimental;
auto Arch = getSyclObjImpl(Device)->getDeviceArch();
auto Arch = DeviceImpl->getDeviceArch();
switch (Arch) {
#define __SYCL_ARCHITECTURE(ARCH, VAL) \
case syclex::architecture::ARCH: \
Expand All @@ -1495,7 +1495,7 @@ template <typename StorageKey>
RTDeviceBinaryImage *getBinImageFromMultiMap(
const std::unordered_multimap<StorageKey, RTDeviceBinaryImage *> &ImagesSet,
const StorageKey &Key, const ContextImplPtr &ContextImpl,
const device &Device) {
const device_impl *DeviceImpl) {
auto [ItBegin, ItEnd] = ImagesSet.equal_range(Key);
if (ItBegin == ItEnd)
return nullptr;
Expand All @@ -1508,7 +1508,7 @@ RTDeviceBinaryImage *getBinImageFromMultiMap(
std::vector<RTDeviceBinaryImage *> DeviceFilteredImgs;
DeviceFilteredImgs.reserve(std::distance(ItBegin, ItEnd));
for (auto It = ItBegin; It != ItEnd; ++It) {
if (doesImageTargetMatchDevice(*It->second, Device))
if (doesImageTargetMatchDevice(*It->second, DeviceImpl))
DeviceFilteredImgs.push_back(It->second);
}

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

RTDeviceBinaryImage &
ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
const ContextImplPtr &ContextImpl,
const device &Device) {
const device_impl *DeviceImpl) {
if constexpr (DbgProgMgr > 0) {
std::cerr << ">>> ProgramManager::getDeviceImage(\"" << KernelName << "\", "
<< ContextImpl.get() << ", " << getSyclObjImpl(Device).get()
<< ")\n";
<< ContextImpl.get() << ", " << DeviceImpl << ")\n";

std::cerr << "available device images:\n";
debugPrintBinaryImages();
Expand All @@ -1548,7 +1547,7 @@ ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
assert(m_SpvFileImage);
return getDeviceImage(
std::unordered_set<RTDeviceBinaryImage *>({m_SpvFileImage.get()}),
ContextImpl, Device);
ContextImpl, DeviceImpl);
}

RTDeviceBinaryImage *Img = nullptr;
Expand All @@ -1557,10 +1556,10 @@ ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,
if (auto KernelId = m_KernelName2KernelIDs.find(KernelName);
KernelId != m_KernelName2KernelIDs.end()) {
Img = getBinImageFromMultiMap(m_KernelIDs2BinImage, KernelId->second,
ContextImpl, Device);
ContextImpl, DeviceImpl);
} else {
Img = getBinImageFromMultiMap(m_ServiceKernels, KernelName, ContextImpl,
Device);
DeviceImpl);
}
}

Expand All @@ -1581,13 +1580,12 @@ ProgramManager::getDeviceImage(KernelNameStrRefT KernelName,

RTDeviceBinaryImage &ProgramManager::getDeviceImage(
const std::unordered_set<RTDeviceBinaryImage *> &ImageSet,
const ContextImplPtr &ContextImpl, const device &Device) {
const ContextImplPtr &ContextImpl, const device_impl *DeviceImpl) {
assert(ImageSet.size() > 0);

if constexpr (DbgProgMgr > 0) {
std::cerr << ">>> ProgramManager::getDeviceImage(Custom SPV file "
<< ContextImpl.get() << ", " << getSyclObjImpl(Device).get()
<< ")\n";
<< ContextImpl.get() << ", " << DeviceImpl << ")\n";

std::cerr << "available device images:\n";
debugPrintBinaryImages();
Expand All @@ -1610,8 +1608,8 @@ RTDeviceBinaryImage &ProgramManager::getDeviceImage(
}

ContextImpl->getAdapter()->call<UrApiKind::urDeviceSelectBinary>(
getSyclObjImpl(Device)->getHandleRef(), UrBinaries.data(),
UrBinaries.size(), &ImgInd);
DeviceImpl->getHandleRef(), UrBinaries.data(), UrBinaries.size(),
&ImgInd);

ImageIterator = ImageSet.begin();
std::advance(ImageIterator, ImgInd);
Expand Down Expand Up @@ -3802,29 +3800,29 @@ checkDevSupportDeviceRequirements(const device &Dev,
}

bool doesImageTargetMatchDevice(const RTDeviceBinaryImage &Img,
const device &Dev) {
const device_impl *DevImpl) {
auto PropRange = Img.getDeviceRequirements();
auto PropIt =
std::find_if(PropRange.begin(), PropRange.end(), [&](const auto &Prop) {
return Prop->Name == std::string_view("compile_target");
});
// Device image has no compile_target property, check target.
if (PropIt == PropRange.end()) {
sycl::backend BE = Dev.get_backend();
sycl::backend BE = DevImpl->getBackend();
const char *Target = Img.getRawData().DeviceTargetSpec;
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64) == 0) {
return (BE == sycl::backend::opencl ||
BE == sycl::backend::ext_oneapi_level_zero);
}
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_X86_64) == 0) {
return Dev.is_cpu();
return DevImpl->is_cpu();
}
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_GEN) == 0) {
return Dev.is_gpu() && (BE == sycl::backend::opencl ||
BE == sycl::backend::ext_oneapi_level_zero);
return DevImpl->is_gpu() && (BE == sycl::backend::opencl ||
BE == sycl::backend::ext_oneapi_level_zero);
}
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_SPIRV64_FPGA) == 0) {
return Dev.is_accelerator();
return DevImpl->is_accelerator();
}
if (strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_NVPTX64) == 0 ||
strcmp(Target, __SYCL_DEVICE_BINARY_TARGET_LLVM_NVPTX64) == 0) {
Expand All @@ -3849,7 +3847,7 @@ bool doesImageTargetMatchDevice(const RTDeviceBinaryImage &Img,
std::string_view CompileTarget(
reinterpret_cast<const char *>(&CompileTargetByteArray[0]),
CompileTargetByteArray.size());
std::string_view ArchName = getArchName(Dev);
std::string_view ArchName = getArchName(DevImpl);
// Note: there are no explicit targets for CPUs, so on x86_64,
// intel_cpu_spr, and intel_cpu_gnr, we use a spir64_x86_64
// compile target image.
Expand Down
6 changes: 3 additions & 3 deletions sycl/source/detail/program_manager/program_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ checkDevSupportDeviceRequirements(const device &Dev,
const NDRDescT &NDRDesc = {});

bool doesImageTargetMatchDevice(const RTDeviceBinaryImage &Img,
const device &Dev);
const device_impl *DevImpl);

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

RTDeviceBinaryImage &getDeviceImage(KernelNameStrRefT KernelName,
const ContextImplPtr &ContextImpl,
const device &Device);
const device_impl *DeviceImpl);

RTDeviceBinaryImage &getDeviceImage(
const std::unordered_set<RTDeviceBinaryImage *> &ImagesToVerify,
const ContextImplPtr &ContextImpl, const device &Device);
const ContextImplPtr &ContextImpl, const device_impl *DeviceImpl);

ur_program_handle_t createURProgram(const RTDeviceBinaryImage &Img,
const ContextImplPtr &ContextImpl,
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/kernel_bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ bool is_compatible(const std::vector<kernel_id> &KernelIDs, const device &Dev) {
if (std::none_of(BinImages.begin(), BinImages.end(),
[&](const detail::RTDeviceBinaryImage *Img) {
return doesDevSupportDeviceRequirements(Dev, *Img) &&
doesImageTargetMatchDevice(*Img, Dev);
doesImageTargetMatchDevice(
*Img, getSyclObjImpl(Dev).get());
}))
return false;
}
Expand Down