Skip to content

Commit 2e95f08

Browse files
authored
Revert "[SYCL] Refactor program build and cache (#8104)"
This reverts commit 2032ce4.
1 parent a07348c commit 2e95f08

File tree

4 files changed

+104
-114
lines changed

4 files changed

+104
-114
lines changed

sycl/source/detail/kernel_program_cache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace sycl {
1414
__SYCL_INLINE_VER_NAMESPACE(_V1) {
1515
namespace detail {
1616
KernelProgramCache::~KernelProgramCache() {
17-
for (auto &ProgIt : MCachedPrograms.Cache) {
17+
for (auto &ProgIt : MCachedPrograms) {
1818
ProgramWithBuildStateT &ProgWithState = ProgIt.second;
1919
PiProgramT *ToBeDeleted = ProgWithState.Ptr.load();
2020

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ class KernelProgramCache {
3939
bool isFilledIn() const { return !Msg.empty(); }
4040
};
4141

42-
/// Denotes the state of a build.
43-
enum BuildState { BS_InProgress, BS_Done, BS_Failed };
44-
4542
/// Denotes pointer to some entity with its general state and build error.
4643
/// The pointer is not null if and only if the entity is usable.
4744
/// State of the entity is provided by the user of cache instance.
4845
/// Currently there is only a single user - ProgramManager class.
4946
template <typename T> struct BuildResult {
5047
std::atomic<T *> Ptr;
51-
std::atomic<BuildState> State;
48+
std::atomic<int> State;
5249
BuildError Error;
5350

5451
/// Condition variable to signal that build result is ready.
@@ -65,23 +62,15 @@ class KernelProgramCache {
6562
/// A mutex to be employed along with MBuildCV.
6663
std::mutex MBuildResultMutex;
6764

68-
BuildResult(T *P, BuildState S) : Ptr{P}, State{S}, Error{"", 0} {}
65+
BuildResult(T *P, int S) : Ptr{P}, State{S}, Error{"", 0} {}
6966
};
7067

7168
using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
7269
using PiProgramPtrT = std::atomic<PiProgramT *>;
7370
using ProgramWithBuildStateT = BuildResult<PiProgramT>;
7471
using ProgramCacheKeyT = std::pair<std::pair<SerializedObj, std::uintptr_t>,
7572
std::pair<RT::PiDevice, std::string>>;
76-
using CommonProgramKeyT = std::pair<std::uintptr_t, RT::PiDevice>;
77-
78-
struct ProgramCache {
79-
std::map<ProgramCacheKeyT, ProgramWithBuildStateT> Cache;
80-
std::multimap<CommonProgramKeyT, ProgramCacheKeyT> KeyMap;
81-
82-
size_t size() const noexcept { return Cache.size(); }
83-
};
84-
73+
using ProgramCacheT = std::map<ProgramCacheKeyT, ProgramWithBuildStateT>;
8574
using ContextPtr = context_impl *;
8675

8776
using PiKernelT = std::remove_pointer<RT::PiKernel>::type;
@@ -102,66 +91,21 @@ class KernelProgramCache {
10291

10392
void setContextPtr(const ContextPtr &AContext) { MParentContext = AContext; }
10493

105-
Locked<ProgramCache> acquireCachedPrograms() {
94+
Locked<ProgramCacheT> acquireCachedPrograms() {
10695
return {MCachedPrograms, MProgramCacheMutex};
10796
}
10897

10998
Locked<KernelCacheT> acquireKernelsPerProgramCache() {
11099
return {MKernelsPerProgramCache, MKernelsPerProgramCacheMutex};
111100
}
112101

113-
std::pair<ProgramWithBuildStateT *, bool>
114-
getOrInsertProgram(const ProgramCacheKeyT &CacheKey) {
115-
auto LockedCache = acquireCachedPrograms();
116-
auto &ProgCache = LockedCache.get();
117-
auto Inserted = ProgCache.Cache.emplace(
118-
std::piecewise_construct, std::forward_as_tuple(CacheKey),
119-
std::forward_as_tuple(nullptr, BS_InProgress));
120-
if (Inserted.second) {
121-
// Save reference between the common key and the full key.
122-
CommonProgramKeyT CommonKey =
123-
std::make_pair(CacheKey.first.second, CacheKey.second.first);
124-
ProgCache.KeyMap.emplace(std::piecewise_construct,
125-
std::forward_as_tuple(CommonKey),
126-
std::forward_as_tuple(CacheKey));
127-
}
128-
return std::make_pair(&Inserted.first->second, Inserted.second);
129-
}
130-
131-
std::pair<KernelWithBuildStateT *, bool>
132-
getOrInsertKernel(RT::PiProgram Program, const std::string &KernelName) {
133-
auto LockedCache = acquireKernelsPerProgramCache();
134-
auto &Cache = LockedCache.get()[Program];
135-
auto Inserted = Cache.emplace(
136-
std::piecewise_construct, std::forward_as_tuple(KernelName),
137-
std::forward_as_tuple(nullptr, BS_InProgress));
138-
return std::make_pair(&Inserted.first->second, Inserted.second);
139-
}
140-
141102
template <typename T, class Predicate>
142103
void waitUntilBuilt(BuildResult<T> &BR, Predicate Pred) const {
143104
std::unique_lock<std::mutex> Lock(BR.MBuildResultMutex);
144105

145106
BR.MBuildCV.wait(Lock, Pred);
146107
}
147108

148-
template <typename ExceptionT, typename RetT>
149-
RetT *waitUntilBuilt(BuildResult<RetT> *BuildResult) {
150-
// Any thread which will find nullptr in cache will wait until the pointer
151-
// is not null anymore.
152-
waitUntilBuilt(*BuildResult, [BuildResult]() {
153-
int State = BuildResult->State.load();
154-
return State == BuildState::BS_Done || State == BuildState::BS_Failed;
155-
});
156-
157-
if (BuildResult->Error.isFilledIn()) {
158-
const BuildError &Error = BuildResult->Error;
159-
throw ExceptionT(Error.Msg, Error.Code);
160-
}
161-
162-
return BuildResult->Ptr.load();
163-
}
164-
165109
template <typename T> void notifyAllBuild(BuildResult<T> &BR) const {
166110
BR.MBuildCV.notify_all();
167111
}
@@ -188,7 +132,7 @@ class KernelProgramCache {
188132
///
189133
/// This member function should only be used in unit tests.
190134
void reset() {
191-
MCachedPrograms = ProgramCache{};
135+
MCachedPrograms = ProgramCacheT{};
192136
MKernelsPerProgramCache = KernelCacheT{};
193137
MKernelFastCache = KernelFastCacheT{};
194138
}
@@ -197,7 +141,7 @@ class KernelProgramCache {
197141
std::mutex MProgramCacheMutex;
198142
std::mutex MKernelsPerProgramCacheMutex;
199143

200-
ProgramCache MCachedPrograms;
144+
ProgramCacheT MCachedPrograms;
201145
KernelCacheT MKernelsPerProgramCache;
202146
ContextPtr MParentContext;
203147

0 commit comments

Comments
 (0)