Skip to content

Commit 5495889

Browse files
author
Sergey Kanaev
committed
[SYCL] Fix review comment
Signed-off-by: Sergey Kanaev <[email protected]>
1 parent 96e92a8 commit 5495889

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

sycl/include/CL/sycl/detail/kernel_program_cache.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,36 @@ namespace detail {
2525
class context_impl;
2626
class KernelProgramCache {
2727
public:
28-
struct BuildResultT {
28+
struct BuildError {
2929
std::string Msg;
3030
cl_int Code;
31+
bool FilledIn;
3132
};
3233

33-
/// Denotes pointer to some entity with its state.
34+
/// Denotes pointer to some entity with its general state and build error.
3435
/// The pointer is not null if and only if the entity is usable.
3536
/// State of the entity is provided by the user of cache instance.
3637
/// Currently there is only a single user - ProgramManager class.
3738
template<typename T>
38-
struct EntityWithBuildResult {
39+
struct BuildResult {
3940
std::atomic<T *> Ptr;
4041
std::atomic<int> State;
41-
std::unique_ptr<BuildResultT> BuildResult;
42+
BuildError Error;
4243

43-
EntityWithBuildResult(T* P, int S)
44-
: Ptr{P}, State{S}
44+
BuildResult(T* P, int S)
45+
: Ptr{P}, State{S}, Error{"", 0, false}
4546
{}
4647
};
4748

4849
using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
4950
using PiProgramPtrT = std::atomic<PiProgramT *>;
50-
using ProgramWithBuildStateT = EntityWithBuildResult<PiProgramT>;
51+
using ProgramWithBuildStateT = BuildResult<PiProgramT>;
5152
using ProgramCacheT = std::map<OSModuleHandle, ProgramWithBuildStateT>;
5253
using ContextPtr = context_impl *;
5354

5455
using PiKernelT = std::remove_pointer<RT::PiKernel>::type;
5556
using PiKernelPtrT = std::atomic<PiKernelT *>;
56-
using KernelWithBuildStateT = EntityWithBuildResult<PiKernelT>;
57+
using KernelWithBuildStateT = BuildResult<PiKernelT>;
5758
using KernelByNameT = std::map<string_class, KernelWithBuildStateT>;
5859
using KernelCacheT = std::map<RT::PiProgram, KernelByNameT>;
5960

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,22 @@ DeviceImage &ProgramManager::getDeviceImage(OSModuleHandle M,
110110
}
111111

112112
template <typename ExceptionT, typename RetT>
113-
RetT *waitUntilBuilt(
114-
KernelProgramCache &Cache,
115-
KernelProgramCache::EntityWithBuildResult<RetT> *WithBuildState) {
113+
RetT *waitUntilBuilt(KernelProgramCache &Cache,
114+
KernelProgramCache::BuildResult<RetT> *BuildResult) {
116115
// any thread which will find nullptr in cache will wait until the pointer
117116
// is not null anymore
118-
Cache.waitUntilBuilt([WithBuildState]() {
119-
int State = WithBuildState->State.load();
117+
Cache.waitUntilBuilt([BuildResult]() {
118+
int State = BuildResult->State.load();
120119

121120
return State == BS_Done || State == BS_Failed;
122121
});
123122

124-
if (WithBuildState->BuildResult.get()) {
125-
using BuildResult = KernelProgramCache::BuildResultT;
126-
const BuildResult &Res = *WithBuildState->BuildResult.get();
127-
throw ExceptionT(Res.Msg, Res.Code);
123+
if (BuildResult->Error.FilledIn) {
124+
const KernelProgramCache::BuildError &Error = BuildResult->Error;
125+
throw ExceptionT(Error.Msg, Error.Code);
128126
}
129127

130-
RetT *Result = WithBuildState->Ptr.load();
128+
RetT *Result = BuildResult->Ptr.load();
131129

132130
assert(Result && "An exception should have been thrown");
133131

@@ -156,7 +154,7 @@ template <typename RetT, typename ExceptionT, typename KeyT, typename AcquireFT,
156154
RetT *getOrBuild(KernelProgramCache &KPCache, const KeyT &CacheKey,
157155
AcquireFT &&Acquire, GetCacheFT &&GetCache, BuildFT &&Build) {
158156
bool InsertionTookPlace;
159-
KernelProgramCache::EntityWithBuildResult<RetT> *WithState;
157+
KernelProgramCache::BuildResult<RetT> *BuildResult;
160158

161159
{
162160
auto LockedCache = Acquire(KPCache);
@@ -166,13 +164,13 @@ RetT *getOrBuild(KernelProgramCache &KPCache, const KeyT &CacheKey,
166164
std::forward_as_tuple(nullptr, BS_InProgress));
167165

168166
InsertionTookPlace = Inserted.second;
169-
WithState = &Inserted.first->second;
167+
BuildResult = &Inserted.first->second;
170168
}
171169

172170
// no insertion took place, thus some other thread has already inserted smth
173171
// in the cache
174172
if (!InsertionTookPlace) {
175-
return waitUntilBuilt<ExceptionT>(KPCache, WithState);
173+
return waitUntilBuilt<ExceptionT>(KPCache, BuildResult);
176174
}
177175

178176
// only the building thread will run this, and only once.
@@ -182,28 +180,30 @@ RetT *getOrBuild(KernelProgramCache &KPCache, const KeyT &CacheKey,
182180
#ifndef NDEBUG
183181
RetT *Expected = nullptr;
184182

185-
if (!WithState->Ptr.compare_exchange_strong(Expected, Desired))
183+
if (!BuildResult->Ptr.compare_exchange_strong(Expected, Desired))
186184
// We've got a funny story here
187185
assert(false && "We've build an entity that is already have been built.");
188186
#else
189187
WithState->Ptr.store(Desired);
190188
#endif
191189

192-
WithState->State.store(BS_Done);
190+
BuildResult->State.store(BS_Done);
193191

194192
KPCache.notifyAllBuild();
195193

196194
return Desired;
197195
} catch (const exception &Ex) {
198-
using BuildResultT = KernelProgramCache::BuildResultT;
199-
WithState->BuildResult.reset(new BuildResultT{Ex.what(), Ex.get_cl_code()});
200-
WithState->State.store(BS_Failed);
196+
BuildResult->Error.Msg = Ex.what();
197+
BuildResult->Error.Code = Ex.get_cl_code();
198+
BuildResult->Error.FilledIn = true;
199+
200+
BuildResult->State.store(BS_Failed);
201201

202202
KPCache.notifyAllBuild();
203203

204204
std::rethrow_exception(std::current_exception());
205205
} catch (...) {
206-
WithState->State.store(BS_Failed);
206+
BuildResult->State.store(BS_Failed);
207207

208208
KPCache.notifyAllBuild();
209209

0 commit comments

Comments
 (0)