Skip to content

Commit baf070f

Browse files
committed
[Offload] Check for initialization
All entry points (except olInit) now check that offload has been initialized. If not, a new `OL_ERRC_UNINITIALIZED` error is returned.
1 parent dd4776d commit baf070f

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

offload/liboffload/API/Common.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def ErrorCode : Enum {
106106
Etor<"ASSEMBLE_FAILURE", "assembler failure while processing binary image">,
107107
Etor<"LINK_FAILURE", "linker failure while processing binary image">,
108108
Etor<"BACKEND_FAILURE", "the plugin backend is in an invalid or unsupported state">,
109+
Etor<"UNINITIALIZED", "not initialized">,
109110

110111
// Handle related errors - only makes sense for liboffload
111112
Etor<"INVALID_NULL_HANDLE", "a handle argument is null when it should not be">,

offload/liboffload/include/OffloadImpl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace llvm {
2626
namespace offload {
2727
bool isTracingEnabled();
2828
bool isValidationEnabled();
29+
bool isOffloadInited();
2930
} // namespace offload
3031
} // namespace llvm
3132

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ struct OffloadContext {
120120

121121
// If the context is uninited, then we assume tracing is disabled
122122
bool isTracingEnabled() {
123-
return OffloadContextVal && OffloadContext::get().TracingEnabled;
123+
return isOffloadInited() && OffloadContext::get().TracingEnabled;
124124
}
125125
bool isValidationEnabled() { return OffloadContext::get().ValidationEnabled; }
126+
bool isOffloadInited() { return OffloadContextVal != nullptr; }
126127

127128
template <typename HandleT> Error olDestroy(HandleT Handle) {
128129
delete Handle;

offload/tools/offload-tblgen/EntryPointGen.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ static void EmitEntryPointFunc(const FunctionRec &F, raw_ostream &OS) {
8282
}
8383
OS << ") {\n";
8484

85+
// Check offload is inited
86+
if (F.getName() != "olInit")
87+
OS << "if (!llvm::offload::isOffloadInited()) return &UninitError;";
88+
8589
// Emit pre-call prints
8690
OS << TAB_1 "if (llvm::offload::isTracingEnabled()) {\n";
8791
OS << formatv(TAB_2 "llvm::errs() << \"---> {0}\";\n", F.getName());
@@ -143,6 +147,14 @@ static void EmitCodeLocWrapper(const FunctionRec &F, raw_ostream &OS) {
143147

144148
void EmitOffloadEntryPoints(const RecordKeeper &Records, raw_ostream &OS) {
145149
OS << GenericHeader;
150+
151+
constexpr const char *UninitMessage =
152+
"liboffload has not been initialized - please call olInit before using "
153+
"this API";
154+
OS << formatv("static {0}_error_struct_t UninitError = "
155+
"{{{1}_ERRC_UNINITIALIZED, \"{2}\"};",
156+
PrefixLower, PrefixUpper, UninitMessage);
157+
146158
for (auto *R : Records.getAllDerivedDefinitions("Function")) {
147159
EmitValidationFunc(FunctionRec{R}, OS);
148160
EmitEntryPointFunc(FunctionRec{R}, OS);

0 commit comments

Comments
 (0)