Skip to content

Commit d4efd2a

Browse files
romanovvladbader
authored andcommitted
[SYCL] Small fixes and adjusments around sycl exceptions. (intel#539)
* Make cl::sycl::exception inherit from std::exception If exception is not caught by user's try block in the main function it is usually caught by default c++ handler which for exceptions inherited from std::exception prints content returned by "what" method. * Release program object if build fails * Throw friendly exception command groups without operation * Fix lit tests Added dummy parallel_for's as command groups cannot be empty. Removed usage of command group in GetWaitList test. Signed-off-by: Vlad Romanov <vlad.romanov@intel.com>
1 parent 7e97d81 commit d4efd2a

File tree

8 files changed

+40
-25
lines changed

8 files changed

+40
-25
lines changed

sycl/include/CL/sycl/detail/cg.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ class CG {
319319
public:
320320
// Type of the command group.
321321
enum CGTYPE {
322+
NONE,
322323
KERNEL,
323324
COPY_ACC_TO_PTR,
324325
COPY_PTR_TO_ACC,

sycl/include/CL/sycl/detail/program_manager/program_manager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ProgramManager {
6565
private:
6666
RT::PiProgram loadProgram(OSModuleHandle M, const context &Context,
6767
DeviceImage **I = nullptr);
68-
void build(RT::PiProgram &Program, const string_class &Options = "",
68+
void build(RT::PiProgram Program, const string_class &Options = "",
6969
std::vector<RT::PiDevice> Devices = std::vector<RT::PiDevice>());
7070

7171
ProgramManager() = default;

sycl/include/CL/sycl/exception.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
#include <CL/sycl/detail/common.hpp>
1414
#include <CL/sycl/stl.hpp>
1515

16+
#include <exception>
17+
1618
namespace cl {
1719
namespace sycl {
1820

1921
// Forward declaration
2022
class context;
2123

22-
struct exception {
24+
// Derive from std::exception so uncaught exceptions are printed in c++ default
25+
// exception handler.
26+
class exception: public std::exception {
27+
public:
2328
exception() = default;
2429

25-
const char *what() const noexcept;
30+
const char *what() const noexcept final;
2631

2732
bool has_context() const;
2833

sycl/include/CL/sycl/handler.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class handler {
159159
// Storage for a sycl::kernel object.
160160
std::shared_ptr<detail::kernel_impl> MSyclKernel;
161161
// Type of the command group, e.g. kernel, fill.
162-
detail::CG::CGTYPE MCGType;
162+
detail::CG::CGTYPE MCGType = detail::CG::NONE;
163163
// Pointer to the source host memory or accessor(depending on command type).
164164
void *MSrcPtr = nullptr;
165165
// Pointer to the dest host memory or accessor(depends on command type).
@@ -382,6 +382,9 @@ class handler {
382382
std::move(MSharedPtrStorage), std::move(MRequirements),
383383
std::move(MEvents)));
384384
break;
385+
case detail::CG::NONE:
386+
throw runtime_error("Command group submitted without a kernel or a "
387+
"explicit memory operation.");
385388
default:
386389
throw runtime_error("Unhandled type of command group");
387390
}

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
#include <CL/sycl/detail/common.hpp>
1111
#include <CL/sycl/detail/os_util.hpp>
1212
#include <CL/sycl/detail/program_manager/program_manager.hpp>
13+
#include <CL/sycl/detail/type_traits.hpp>
1314
#include <CL/sycl/detail/util.hpp>
1415
#include <CL/sycl/device.hpp>
1516
#include <CL/sycl/exception.hpp>
1617
#include <CL/sycl/stl.hpp>
1718

18-
#include <assert.h>
19+
#include <cassert>
1920
#include <cstdlib>
2021
#include <fstream>
2122
#include <memory>
@@ -87,12 +88,19 @@ RT::PiProgram ProgramManager::getBuiltOpenCLProgram(OSModuleHandle M,
8788
std::shared_ptr<context_impl> Ctx = getSyclObjImpl(Context);
8889
std::map<OSModuleHandle, RT::PiProgram> &CachedPrograms =
8990
Ctx->getCachedPrograms();
90-
RT::PiProgram &Program = CachedPrograms[M];
91-
if (!Program) {
92-
DeviceImage *Img = nullptr;
93-
Program = loadProgram(M, Context, &Img);
94-
build(Program, Img->BuildOptions);
95-
}
91+
auto It = CachedPrograms.find(M);
92+
if (It != CachedPrograms.end())
93+
return It->second;
94+
95+
DeviceImage *Img = nullptr;
96+
using PiProgramT = remove_pointer_t<RT::PiProgram>;
97+
unique_ptr_class<PiProgramT, decltype(RT::piProgramRelease)> ProgramManaged(
98+
loadProgram(M, Context, &Img), RT::piProgramRelease);
99+
100+
build(ProgramManaged.get(), Img->BuildOptions);
101+
RT::PiProgram Program = ProgramManaged.release();
102+
CachedPrograms[M] = Program;
103+
96104
return Program;
97105
}
98106

@@ -150,7 +158,7 @@ string_class ProgramManager::getProgramBuildLog(const RT::PiProgram &Program) {
150158
return Log;
151159
}
152160

153-
void ProgramManager::build(RT::PiProgram &Program, const string_class &Options,
161+
void ProgramManager::build(RT::PiProgram Program, const string_class &Options,
154162
std::vector<RT::PiDevice> Devices) {
155163

156164
if (DbgProgMgr > 0) {

sycl/source/detail/scheduler/commands.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ void ExecCGCommand::printDot(std::ostream &Stream) const {
480480
case detail::CG::COPY_PTR_TO_ACC:
481481
Stream << "CG type: copy ptr to acc\\n";
482482
break;
483+
default:
484+
Stream << "CG type: unknown\\n";
485+
break;
483486
}
484487

485488
Stream << "\"];" << std::endl;
@@ -692,10 +695,10 @@ cl_int ExecCGCommand::enqueueImp() {
692695

693696
return PI_SUCCESS;
694697
}
698+
case CG::CGTYPE::NONE:
699+
default:
700+
throw runtime_error("CG type not implemented.");
695701
}
696-
697-
assert(!"CG type not implemented");
698-
throw runtime_error("CG type not implemented.");
699702
}
700703

701704
} // namespace detail

sycl/test/basic_tests/stream.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ int main() {
3131
stream Out(1024, 80, CGH);
3232
assert(Out.get_size() == 1024);
3333
assert(Out.get_max_statement_size() == 80);
34+
35+
CGH.single_task<class DummyTask1>([=]() {});
3436
});
3537

3638
// Check common reference semantics
@@ -47,6 +49,8 @@ int main() {
4749

4850
assert(Out2 == Out3);
4951
assert(Hasher(Out2) == Hasher(Out3));
52+
53+
CGH.single_task<class DummyTask2>([=]() {});
5054
});
5155

5256
// TODO: support cl::sycl::endl. According to specitification endl should be

sycl/test/scheduler/GetWaitList.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@
1010

1111
#include <CL/sycl.hpp>
1212

13-
void foo() {
13+
int main() {
1414
cl::sycl::event start;
1515
start.wait_and_throw();
16-
return;
17-
}
18-
19-
int main() {
20-
cl::sycl::queue Q;
21-
Q.submit([&](cl::sycl::handler &CGH) {
22-
foo();
23-
});
24-
return 0;
2516
}

0 commit comments

Comments
 (0)