Skip to content

Commit 76c17a2

Browse files
ViacheslavRbigcbot
authored andcommitted
Error Handler for FCL interface
Implementing Error Handler for FCL interface to handle abnormal compilation termination.
1 parent 583850e commit 76c17a2

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

IGC/AdaptorOCL/ocl_igc_interface/impl/fcl_ocl_device_ctx_impl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ SPDX-License-Identifier: MIT
1616

1717
#include "cif/export/library_api.h"
1818

19+
#ifndef WIN32
20+
jmp_buf sig_jmp_buf;
21+
#else
22+
#include <excpt.h>
23+
#endif
24+
1925
#include "cif/macros/enable.h"
2026

2127
namespace IGC {
@@ -31,7 +37,9 @@ CodeType::CodeType_t CIF_GET_INTERFACE_CLASS(FclOclDeviceCtx, 2)::GetPreferredIn
3137
FclOclTranslationCtxBase *CIF_GET_INTERFACE_CLASS(FclOclDeviceCtx, 1)::CreateTranslationCtxImpl(CIF::Version_t ver,
3238
CodeType::CodeType_t inType,
3339
CodeType::CodeType_t outType){
40+
EX_GUARD_BEGIN
3441
return CIF_GET_PIMPL()->CreateTranslationCtx(ver, inType, outType);
42+
EX_GUARD_END
3543
}
3644

3745
FclOclTranslationCtxBase *CIF_PIMPL(FclOclDeviceCtx)::CreateTranslationCtx(CIF::Version_t version, CodeType::CodeType_t inType, CodeType::CodeType_t outType)
@@ -46,7 +54,9 @@ FclOclTranslationCtxBase* CIF_GET_INTERFACE_CLASS(FclOclDeviceCtx, 3)::CreateTra
4654
CodeType::CodeType_t inType,
4755
CodeType::CodeType_t outType,
4856
CIF::Builtins::BufferSimple* err) {
57+
EX_GUARD_BEGIN
4958
return CIF_GET_PIMPL()->CreateTranslationCtx(ver, inType, outType, err);
59+
EX_GUARD_END
5060
}
5161

5262
FclOclTranslationCtxBase* CIF_PIMPL(FclOclDeviceCtx)::CreateTranslationCtx(CIF::Version_t version, CodeType::CodeType_t inType, CodeType::CodeType_t outType, CIF::Builtins::BufferSimple* err)
@@ -66,3 +76,15 @@ PlatformBase *CIF_GET_INTERFACE_CLASS(FclOclDeviceCtx, 4)::GetPlatformHandleImpl
6676
CIF_EXPORT_ENTRY_POINTS_STATIC(IGC::FclOclDeviceCtx);
6777

6878
#include "cif/macros/disable.h"
79+
80+
#if defined(WIN32)
81+
int ex_filter(unsigned int code, struct _EXCEPTION_POINTERS* ep)
82+
{
83+
return EXCEPTION_EXECUTE_HANDLER;
84+
}
85+
#else
86+
void signalHandler(int sig, siginfo_t* info, void* ucontext)
87+
{
88+
longjmp(sig_jmp_buf, sig);
89+
}
90+
#endif

IGC/AdaptorOCL/ocl_igc_interface/impl/fcl_ocl_device_ctx_impl.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ SPDX-License-Identifier: MIT
1313

1414
#include <cinttypes>
1515
#include <mutex>
16+
#include <signal.h>
17+
#include <setjmp.h>
1618

1719
#include "cif/common/cif.h"
1820
#include "cif/export/cif_main_impl.h"
@@ -72,3 +74,66 @@ CIF_DEFINE_INTERFACE_TO_PIMPL_FORWARDING_CTOR_DTOR(FclOclDeviceCtx);
7274
}
7375

7476
#include "cif/macros/disable.h"
77+
78+
#if defined(NDEBUG)
79+
#if defined(WIN32)
80+
OCL_API_CALL int ex_filter(unsigned int code, struct _EXCEPTION_POINTERS* ep);
81+
82+
#define EX_GUARD_BEGIN \
83+
__try \
84+
{ \
85+
86+
#define EX_GUARD_END \
87+
} \
88+
__except (ex_filter(GetExceptionCode(), GetExceptionInformation())) \
89+
{ \
90+
return nullptr; \
91+
} \
92+
93+
#else
94+
OCL_API_CALL void signalHandler(int sig, siginfo_t* info, void* ucontext);
95+
96+
#define SET_SIG_HANDLER(SIG) \
97+
struct sigaction saold_##SIG; \
98+
sigaction(SIG, NULL, &saold_##SIG); \
99+
if (saold_##SIG.sa_handler == SIG_DFL) \
100+
{ \
101+
sigaction(SIG, &sa, NULL); \
102+
} \
103+
104+
#define REMOVE_SIG_HANDLER(SIG) \
105+
if (saold_##SIG.sa_handler == SIG_DFL) \
106+
{ \
107+
sigaction(SIG, &saold_##SIG, NULL); \
108+
} \
109+
110+
#define EX_GUARD_BEGIN \
111+
struct sigaction sa; \
112+
sigemptyset(&sa.sa_mask); \
113+
sa.sa_sigaction = signalHandler; \
114+
sa.sa_flags = 0; \
115+
SET_SIG_HANDLER(SIGABRT) \
116+
SET_SIG_HANDLER(SIGFPE) \
117+
SET_SIG_HANDLER(SIGILL) \
118+
SET_SIG_HANDLER(SIGINT) \
119+
SET_SIG_HANDLER(SIGSEGV) \
120+
SET_SIG_HANDLER(SIGTERM) \
121+
int sig = setjmp(sig_jmp_buf); \
122+
if (sig == 0) { \
123+
124+
#define EX_GUARD_END \
125+
} else { \
126+
return nullptr; \
127+
} \
128+
REMOVE_SIG_HANDLER(SIGABRT) \
129+
REMOVE_SIG_HANDLER(SIGFPE) \
130+
REMOVE_SIG_HANDLER(SIGILL) \
131+
REMOVE_SIG_HANDLER(SIGINT) \
132+
REMOVE_SIG_HANDLER(SIGSEGV) \
133+
REMOVE_SIG_HANDLER(SIGTERM) \
134+
135+
#endif
136+
#else
137+
#define EX_GUARD_BEGIN
138+
#define EX_GUARD_END
139+
#endif

0 commit comments

Comments
 (0)