Skip to content

[DONOTMERGE] Revert "[SYCL] Guard access to the cache of device lib programs" #6117

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 6 additions & 9 deletions sycl/source/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ class context_impl {
/// reference.
const std::vector<device> &getDevices() const { return MDevices; }

using CachedLibProgramsT =
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>;

/// In contrast to user programs, which are compiled from user code, library
/// programs come from the SYCL runtime. They are identified by the
/// corresponding extension:
Expand All @@ -154,10 +151,10 @@ class context_impl {
/// See `doc/design/DeviceLibExtensions.rst' for
/// more details.
///
/// \returns an instance of sycl::detail::Locked which wraps a map with device
/// library programs and the corresponding lock for synchronized access.
Locked<CachedLibProgramsT> acquireCachedLibPrograms() {
return {MCachedLibPrograms, MCachedLibProgramsMutex};
/// \returns a map with device library programs.
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram> &
getCachedLibPrograms() {
return MCachedLibPrograms;
}

KernelProgramCache &getKernelProgramCache() const;
Expand All @@ -182,8 +179,8 @@ class context_impl {
PlatformImplPtr MPlatform;
property_list MPropList;
bool MHostContext;
CachedLibProgramsT MCachedLibPrograms;
std::mutex MCachedLibProgramsMutex;
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>
MCachedLibPrograms;
mutable KernelProgramCache MKernelProgramCache;
mutable PropertySupport MSupportBufferLocationByDevices;
};
Expand Down
43 changes: 25 additions & 18 deletions sycl/source/detail/program_manager/program_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ RT::PiProgram ProgramManager::getBuiltPIProgram(

ProgramPtr BuiltProgram =
build(std::move(ProgramManaged), ContextImpl, CompileOpts, LinkOpts,
getRawSyclObjImpl(Device)->getHandleRef(), DeviceLibReqMask);
getRawSyclObjImpl(Device)->getHandleRef(),
ContextImpl->getCachedLibPrograms(), DeviceLibReqMask);

emitBuiltProgramInfo(BuiltProgram.get(), ContextImpl);

Expand Down Expand Up @@ -778,14 +779,13 @@ static const char *getDeviceLibExtensionStr(DeviceLibExt Extension) {
PI_INVALID_OPERATION);
}

static RT::PiProgram loadDeviceLibFallback(const ContextImplPtr Context,
DeviceLibExt Extension,
const RT::PiDevice &Device) {
static RT::PiProgram loadDeviceLibFallback(
const ContextImplPtr Context, DeviceLibExt Extension,
const RT::PiDevice &Device,
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>
&CachedLibPrograms) {

const char *LibFileName = getDeviceLibFilename(Extension);

auto LockedCache = Context->acquireCachedLibPrograms();
auto CachedLibPrograms = LockedCache.get();
auto CacheResult = CachedLibPrograms.emplace(
std::make_pair(std::make_pair(Extension, Device), nullptr));
bool Cached = !CacheResult.second;
Expand Down Expand Up @@ -923,9 +923,11 @@ static bool isDeviceLibRequired(DeviceLibExt Ext, uint32_t DeviceLibReqMask) {
return ((DeviceLibReqMask & Mask) == Mask);
}

static std::vector<RT::PiProgram>
getDeviceLibPrograms(const ContextImplPtr Context, const RT::PiDevice &Device,
uint32_t DeviceLibReqMask) {
static std::vector<RT::PiProgram> getDeviceLibPrograms(
const ContextImplPtr Context, const RT::PiDevice &Device,
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>
&CachedLibPrograms,
uint32_t DeviceLibReqMask) {
std::vector<RT::PiProgram> Programs;

std::pair<DeviceLibExt, bool> RequiredDeviceLibExt[] = {
Expand Down Expand Up @@ -973,18 +975,21 @@ getDeviceLibPrograms(const ContextImplPtr Context, const RT::PiDevice &Device,
bool DeviceSupports = DevExtList.npos != DevExtList.find(ExtStr);

if (!DeviceSupports || InhibitNativeImpl) {
Programs.push_back(loadDeviceLibFallback(Context, Ext, Device));
Programs.push_back(
loadDeviceLibFallback(Context, Ext, Device, CachedLibPrograms));
FallbackIsLoaded = true;
}
}
return Programs;
}

ProgramManager::ProgramPtr
ProgramManager::build(ProgramPtr Program, const ContextImplPtr Context,
const std::string &CompileOptions,
const std::string &LinkOptions,
const RT::PiDevice &Device, uint32_t DeviceLibReqMask) {
ProgramManager::ProgramPtr ProgramManager::build(
ProgramPtr Program, const ContextImplPtr Context,
const std::string &CompileOptions, const std::string &LinkOptions,
const RT::PiDevice &Device,
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>
&CachedLibPrograms,
uint32_t DeviceLibReqMask) {

if (DbgProgMgr > 0) {
std::cerr << ">>> ProgramManager::build(" << Program.get() << ", "
Expand All @@ -1003,7 +1008,8 @@ ProgramManager::build(ProgramPtr Program, const ContextImplPtr Context,

std::vector<RT::PiProgram> LinkPrograms;
if (LinkDeviceLibs) {
LinkPrograms = getDeviceLibPrograms(Context, Device, DeviceLibReqMask);
LinkPrograms = getDeviceLibPrograms(Context, Device, CachedLibPrograms,
DeviceLibReqMask);
}

static const char *ForceLinkEnv = std::getenv("SYCL_FORCE_LINK");
Expand Down Expand Up @@ -1893,7 +1899,8 @@ device_image_plain ProgramManager::build(const device_image_plain &DeviceImage,

ProgramPtr BuiltProgram =
build(std::move(ProgramManaged), ContextImpl, CompileOpts, LinkOpts,
getRawSyclObjImpl(Devs[0])->getHandleRef(), DeviceLibReqMask);
getRawSyclObjImpl(Devs[0])->getHandleRef(),
ContextImpl->getCachedLibPrograms(), DeviceLibReqMask);

emitBuiltProgramInfo(BuiltProgram.get(), ContextImpl);

Expand Down
2 changes: 2 additions & 0 deletions sycl/source/detail/program_manager/program_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ class ProgramManager {
ProgramPtr build(ProgramPtr Program, const ContextImplPtr Context,
const std::string &CompileOptions,
const std::string &LinkOptions, const RT::PiDevice &Device,
std::map<std::pair<DeviceLibExt, RT::PiDevice>,
RT::PiProgram> &CachedLibPrograms,
uint32_t DeviceLibReqMask);
/// Provides a new kernel set id for grouping kernel names together
KernelSetId getNextKernelSetId() const;
Expand Down