Skip to content

[SYCL] Preserve original message and code of kernel/program build result #1108

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 10 commits into from
Feb 18, 2020
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
25 changes: 19 additions & 6 deletions sycl/include/CL/sycl/detail/kernel_program_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,42 @@ namespace detail {
class context_impl;
class KernelProgramCache {
public:
/// Denotes pointer to some entity with its state.
/// Denotes build error data. The data is filled in from cl::sycl::exception
/// class instance.
struct BuildError {
std::string Msg;
pi_int32 Code;

/// Equals to true if the Msg and Code are initialized. This flag is added
/// due to the possibility of error code being equal to zero even in case
/// if build is failed and cl::sycl::exception is thrown.
bool FilledIn;
Copy link
Contributor

@bader bader Feb 18, 2020

Choose a reason for hiding this comment

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

Isn't !Msg.empty() == FilledIn?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it may be. Though, flag is more explicit data and more understandable to read. On the other hand the struct may have a distinct method bool isFilledIn() const.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for replacing this flag with public member function.
FilledIn seems to duplicate information available in two other fields and maintaining is not necessary.

};

/// Denotes pointer to some entity with its general state and build error.
/// The pointer is not null if and only if the entity is usable.
/// State of the entity is provided by the user of cache instance.
/// Currently there is only a single user - ProgramManager class.
template<typename T>
struct EntityWithState {
struct BuildResult {
std::atomic<T *> Ptr;
std::atomic<int> State;
BuildError Error;

EntityWithState(T* P, int S)
: Ptr{P}, State{S}
BuildResult(T* P, int S)
: Ptr{P}, State{S}, Error{"", 0, false}
{}
};

using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
using PiProgramPtrT = std::atomic<PiProgramT *>;
using ProgramWithBuildStateT = EntityWithState<PiProgramT>;
using ProgramWithBuildStateT = BuildResult<PiProgramT>;
using ProgramCacheT = std::map<OSModuleHandle, ProgramWithBuildStateT>;
using ContextPtr = context_impl *;

using PiKernelT = std::remove_pointer<RT::PiKernel>::type;
using PiKernelPtrT = std::atomic<PiKernelT *>;
using KernelWithBuildStateT = EntityWithState<PiKernelT>;
using KernelWithBuildStateT = BuildResult<PiKernelT>;
using KernelByNameT = std::map<string_class, KernelWithBuildStateT>;
using KernelCacheT = std::map<RT::PiProgram, KernelByNameT>;

Expand Down
57 changes: 40 additions & 17 deletions sycl/source/detail/program_manager/program_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,22 @@ DeviceImage &ProgramManager::getDeviceImage(OSModuleHandle M,
}

template <typename ExceptionT, typename RetT>
RetT *
waitUntilBuilt(KernelProgramCache &Cache,
KernelProgramCache::EntityWithState<RetT> *WithBuildState) {
RetT *waitUntilBuilt(KernelProgramCache &Cache,
KernelProgramCache::BuildResult<RetT> *BuildResult) {
// any thread which will find nullptr in cache will wait until the pointer
// is not null anymore
Cache.waitUntilBuilt([WithBuildState]() {
int State = WithBuildState->State.load();
Cache.waitUntilBuilt([BuildResult]() {
int State = BuildResult->State.load();

return State == BS_Done || State == BS_Failed;
});

RetT *Result = WithBuildState->Ptr.load();
if (BuildResult->Error.FilledIn) {
const KernelProgramCache::BuildError &Error = BuildResult->Error;
throw ExceptionT(Error.Msg, Error.Code);
}

if (!Result)
throw ExceptionT("The other thread tried to build the program/kernel but "
"did not succeed.");
RetT *Result = BuildResult->Ptr.load();

return Result;
}
Expand All @@ -152,7 +152,7 @@ template <typename RetT, typename ExceptionT, typename KeyT, typename AcquireFT,
RetT *getOrBuild(KernelProgramCache &KPCache, const KeyT &CacheKey,
AcquireFT &&Acquire, GetCacheFT &&GetCache, BuildFT &&Build) {
bool InsertionTookPlace;
KernelProgramCache::EntityWithState<RetT> *WithState;
KernelProgramCache::BuildResult<RetT> *BuildResult;

{
auto LockedCache = Acquire(KPCache);
Expand All @@ -162,36 +162,59 @@ RetT *getOrBuild(KernelProgramCache &KPCache, const KeyT &CacheKey,
std::forward_as_tuple(nullptr, BS_InProgress));

InsertionTookPlace = Inserted.second;
WithState = &Inserted.first->second;
BuildResult = &Inserted.first->second;
}

// no insertion took place, thus some other thread has already inserted smth
// in the cache
if (!InsertionTookPlace) {
return waitUntilBuilt<ExceptionT>(KPCache, WithState);
for (;;) {
RetT *Result = waitUntilBuilt<ExceptionT>(KPCache, BuildResult);

if (Result)
return Result;

// Previous build is failed. There was no SYCL exception though.
// We might try to build once more.
int Expected = BS_Failed;
int Desired = BS_InProgress;

if (BuildResult->State.compare_exchange_strong(Expected, Desired))
break; // this thread is the building thread now
}
}

// only the building thread will run this, and only once.
// only the building thread will run this
try {
RetT *Desired = Build();

#ifndef NDEBUG
RetT *Expected = nullptr;

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

WithState->State.store(BS_Done);
BuildResult->State.store(BS_Done);

KPCache.notifyAllBuild();

return Desired;
} catch (const exception &Ex) {
BuildResult->Error.Msg = Ex.what();
BuildResult->Error.Code = Ex.get_cl_code();
BuildResult->Error.FilledIn = true;

BuildResult->State.store(BS_Failed);

KPCache.notifyAllBuild();

std::rethrow_exception(std::current_exception());
} catch (...) {
WithState->State.store(BS_Failed);
BuildResult->State.store(BS_Failed);

KPCache.notifyAllBuild();

Expand Down
47 changes: 47 additions & 0 deletions sycl/test/kernel-and-program/cache-build-result.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out

#include <CL/sycl.hpp>

SYCL_EXTERNAL
void undefined();

void test() {
cl::sycl::queue Queue;

auto Kernel = []() {
#ifdef __SYCL_DEVICE_ONLY__
undefined();
#endif
};

std::string Msg;
int Result;

for (int Idx = 0; Idx < 2; ++Idx) {
try {
Queue.submit([&](cl::sycl::handler &CGH) {
CGH.single_task<class SingleTask>(Kernel);
});
assert(false && "There must be compilation error");
} catch (const cl::sycl::compile_program_error &e) {
fprintf(stderr, "Exception: %s, %d\n", e.what(), e.get_cl_code());
if (Idx == 0) {
Msg = e.what();
Result = e.get_cl_code();
} else {
// Exception constantly adds info on its error code in the message
assert(Msg.find_first_of(e.what()) == 0 && "Exception text differs");
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not Msg == e.what()? Can you also check that a message is not empty and Result != 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cl::sycl::exception exception adds appends Code to Msg in form of 0 (CL_SUCCESS). On the second throw it will add the same thing again. I don't do any string operations in this patch.

assert(Result == e.get_cl_code() && "Exception code differs");
}
} catch (...) {
assert(false && "There must be cl::sycl::compile_program_error");
}
}
}

int main() {
test();

return 0;
}