Skip to content

[SYCL] Fix linking of compressed device images when dependencies are not compressed #18906

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

Merged
merged 7 commits into from
Jun 11, 2025
Merged
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
39 changes: 28 additions & 11 deletions sycl/source/detail/program_manager/program_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,15 @@ ProgramManager::collectDeviceImageDeps(const RTDeviceBinaryImage &Img,
return DeviceImagesToLink;
}

static inline void
CheckAndDecompressImage([[maybe_unused]] RTDeviceBinaryImage *Img) {
#ifndef SYCL_RT_ZSTD_NOT_AVAIABLE
if (auto CompImg = dynamic_cast<CompressedRTDeviceBinaryImage *>(Img))
if (CompImg->IsCompressed())
CompImg->Decompress();
#endif
}

std::set<RTDeviceBinaryImage *>
ProgramManager::collectDeviceImageDepsForImportedSymbols(
const RTDeviceBinaryImage &MainImg, const device &Dev,
Expand All @@ -713,13 +722,30 @@ ProgramManager::collectDeviceImageDepsForImportedSymbols(
bool Found = false;
for (auto It = Range.first; It != Range.second; ++It) {
RTDeviceBinaryImage *Img = It->second;
if (Img->getFormat() != Format ||
!doesDevSupportDeviceRequirements(Dev, *Img) ||

if (!doesDevSupportDeviceRequirements(Dev, *Img) ||
!compatibleWithDevice(Img, *getSyclObjImpl(Dev).get()))
continue;

// If the image is a special device image, we need to check if it
// should be used for this device.
if (isSpecialDeviceImage(Img) &&
!isSpecialDeviceImageShouldBeUsed(Img, *getSyclObjImpl(Dev).get()))
continue;

// If any of the images is compressed, we need to decompress it
// and then check if the format matches.
if (Format == SYCL_DEVICE_BINARY_TYPE_COMPRESSED_NONE ||
Img->getFormat() == SYCL_DEVICE_BINARY_TYPE_COMPRESSED_NONE) {
auto MainImgPtr = const_cast<RTDeviceBinaryImage *>(&MainImg);
CheckAndDecompressImage(MainImgPtr);
CheckAndDecompressImage(Img);
Format = MainImg.getFormat();
}
// Skip this image if its format differs from the main image.
if (Img->getFormat() != Format)
continue;

DeviceImagesToLink.insert(Img);
Found = true;
for (const sycl_device_binary_property &ISProp :
Expand Down Expand Up @@ -836,15 +862,6 @@ setSpecializationConstants(const std::shared_ptr<device_image_impl> &InputImpl,
}
}

static inline void
CheckAndDecompressImage([[maybe_unused]] RTDeviceBinaryImage *Img) {
#ifndef SYCL_RT_ZSTD_NOT_AVAIABLE
if (auto CompImg = dynamic_cast<CompressedRTDeviceBinaryImage *>(Img))
if (CompImg->IsCompressed())
CompImg->Decompress();
#endif
}

// When caching is enabled, the returned UrProgram will already have
// its ref count incremented.
ur_program_handle_t ProgramManager::getBuiltURProgram(
Expand Down
25 changes: 25 additions & 0 deletions sycl/test-e2e/DeviceImageDependencies/dynamic_compress.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Test device image linking when using dynamic libraries when one of
// the device image is compressed and the other is not.

// UNSUPPORTED: target-nvidia || target-amd
// UNSUPPORTED-INTENDED: Linking using dynamic libraries is not supported on AMD
// and Nvidia.
// REQUIRES: zstd

// DEFINE: %{dynamic_lib_options} = -fsycl %fPIC %shared_lib -fsycl-allow-device-image-dependencies -I %S/Inputs %if windows %{-DMAKE_DLL %}
// DEFINE: %{dynamic_lib_suffix} = %if windows %{dll%} %else %{so%}

// RUN: %clangxx %{dynamic_lib_options} %S/Inputs/d.cpp -o %T/libdevice_d.%{dynamic_lib_suffix}
// RUN: %clangxx %{dynamic_lib_options} %S/Inputs/c.cpp %if windows %{%T/libdevice_d.lib%} -o %T/libdevice_c.%{dynamic_lib_suffix}
// RUN: %clangxx %{dynamic_lib_options} %S/Inputs/b.cpp %if windows %{%T/libdevice_c.lib%} -o %T/libdevice_b.%{dynamic_lib_suffix}
// RUN: %clangxx %{dynamic_lib_options} %S/Inputs/a.cpp %if windows %{%T/libdevice_b.lib%} -o %T/libdevice_a.%{dynamic_lib_suffix}

// Compressed main executable, while dependencies are not compressed.

// RUN: %clangxx -fsycl --offload-compress %{sycl_target_opts} -fsycl-allow-device-image-dependencies -fsycl-device-code-split=per_kernel %S/Inputs/basic.cpp -o %t.out \
// RUN: %if windows \
// RUN: %{%T/libdevice_a.lib%} \
// RUN: %else \
// RUN: %{-L%T -ldevice_a -ldevice_b -ldevice_c -ldevice_d -Wl,-rpath=%T%}

// RUN: %{run} %t.out
Loading