Skip to content

[SYCL] Set specialization constants in sycl::compile #8166

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
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
49 changes: 27 additions & 22 deletions sycl/source/detail/program_manager/program_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,30 @@ std::vector<device_image_plain> ProgramManager::getSYCLDeviceImages(
return DeviceImages;
}

static void
setSpecializationConstants(const std::shared_ptr<device_image_impl> &InputImpl,
RT::PiProgram Prog, const plugin &Plugin) {
// Set ITT annotation specialization constant if needed.
enableITTAnnotationsIfNeeded(Prog, Plugin);

std::lock_guard<std::mutex> Lock{InputImpl->get_spec_const_data_lock()};
const std::map<std::string, std::vector<device_image_impl::SpecConstDescT>>
&SpecConstData = InputImpl->get_spec_const_data_ref();
const SerializedObj &SpecConsts = InputImpl->get_spec_const_blob_ref();

// Set all specialization IDs from descriptors in the input device image.
for (const auto &[SpecConstNames, SpecConstDescs] : SpecConstData) {
std::ignore = SpecConstNames;
for (const device_image_impl::SpecConstDescT &SpecIDDesc : SpecConstDescs) {
if (SpecIDDesc.IsSet) {
Plugin.call<PiApiKind::piextProgramSetSpecializationConstant>(
Prog, SpecIDDesc.ID, SpecIDDesc.Size,
SpecConsts.data() + SpecIDDesc.BlobOffset);
}
}
}
}

device_image_plain
ProgramManager::compile(const device_image_plain &DeviceImage,
const std::vector<device> &Devs,
Expand Down Expand Up @@ -1876,7 +1900,7 @@ ProgramManager::compile(const device_image_plain &DeviceImage,
InputImpl->get_context(), Devs[0]);

if (InputImpl->get_bin_image_ref()->supportsSpecConstants())
enableITTAnnotationsIfNeeded(Prog, Plugin);
setSpecializationConstants(InputImpl, Prog, Plugin);

DeviceImageImplPtr ObjectImpl = std::make_shared<detail::device_image_impl>(
InputImpl->get_bin_image_ref(), InputImpl->get_context(), Devs,
Expand All @@ -1889,8 +1913,6 @@ ProgramManager::compile(const device_image_plain &DeviceImage,
for (const device &Dev : Devs)
PIDevices.push_back(getSyclObjImpl(Dev)->getHandleRef());

// TODO: Set spec constatns here.

// TODO: Handle zero sized Device list.
std::string CompileOptions;
applyCompileOptionsFromEnvironment(CompileOptions);
Expand Down Expand Up @@ -2073,25 +2095,8 @@ device_image_plain ProgramManager::build(const device_image_plain &DeviceImage,
Img, Context, Devs[0], CompileOpts + LinkOpts, SpecConsts);

if (!DeviceCodeWasInCache &&
InputImpl->get_bin_image_ref()->supportsSpecConstants()) {
enableITTAnnotationsIfNeeded(NativePrg, Plugin);

std::lock_guard<std::mutex> Lock{InputImpl->get_spec_const_data_lock()};
const std::map<std::string,
std::vector<device_image_impl::SpecConstDescT>>
&SpecConstData = InputImpl->get_spec_const_data_ref();

for (const auto &DescPair : SpecConstData) {
for (const device_image_impl::SpecConstDescT &SpecIDDesc :
DescPair.second) {
if (SpecIDDesc.IsSet) {
Plugin.call<PiApiKind::piextProgramSetSpecializationConstant>(
NativePrg, SpecIDDesc.ID, SpecIDDesc.Size,
SpecConsts.data() + SpecIDDesc.BlobOffset);
}
}
}
}
InputImpl->get_bin_image_ref()->supportsSpecConstants())
setSpecializationConstants(InputImpl, NativePrg, Plugin);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we fold enableITTAnnotationsIfNeeded(NativePrg, Plugin) into the helper as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot fully merge it as it is used in another place that sets specialization constants differently. However, we may as well call it inside setSpecializationConstants.


ProgramPtr ProgramManaged(
NativePrg, Plugin.getPiPlugin().PiFunctionTable.piProgramRelease);
Expand Down