Skip to content

Commit be7e5c0

Browse files
committed
Add optional code location entry point variants
1 parent dbfe0b5 commit be7e5c0

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

offload/new-api/API/Common.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,14 @@ def : Macro {
8989
let desc = "Success condition";
9090
let value = "NULL";
9191
}
92+
93+
def : Struct {
94+
let name = "offload_code_location_t";
95+
let desc = "Code location information that can optionally be associated with an API call";
96+
let members = [
97+
StructMember<"const char*", "FunctionName", "Function name">,
98+
StructMember<"const char*", "SourceFile", "Source code file">,
99+
StructMember<"uint32_t", "LineNumber", "Source code line number">,
100+
StructMember<"uint32_t", "ColumnNumber", "Source code column number">
101+
];
102+
}

offload/new-api/include/offload_api.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ typedef const offload_error_struct_t *offload_result_t;
129129
#endif // !defined(OFFLOAD_SUCCESS)
130130
#endif // OFFLOAD_SUCCESS
131131

132+
///////////////////////////////////////////////////////////////////////////////
133+
/// @brief Code location information that can optionally be associated with an
134+
/// API call
135+
typedef struct offload_code_location_t {
136+
const char *FunctionName; /// Function name
137+
const char *SourceFile; /// Source code file
138+
uint32_t LineNumber; /// Source code line number
139+
uint32_t ColumnNumber; /// Source code column number
140+
} offload_code_location_t;
141+
132142
///////////////////////////////////////////////////////////////////////////////
133143
/// @brief Retrieves all available platforms
134144
///
@@ -397,6 +407,46 @@ typedef struct offload_device_get_info_params_t {
397407
size_t **ppPropSizeRet;
398408
} offload_device_get_info_params_t;
399409

410+
///////////////////////////////////////////////////////////////////////////////
411+
/// @brief Variant of offloadPlatformGet that also sets source code location
412+
/// information
413+
/// @details See also ::offloadPlatformGet
414+
OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL
415+
offloadPlatformGetWithCodeLoc(uint32_t NumEntries,
416+
offload_platform_handle_t *phPlatforms,
417+
uint32_t *pNumPlatforms,
418+
offload_code_location_t *pCodeLocation);
419+
420+
///////////////////////////////////////////////////////////////////////////////
421+
/// @brief Variant of offloadPlatformGetInfo that also sets source code location
422+
/// information
423+
/// @details See also ::offloadPlatformGetInfo
424+
OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL
425+
offloadPlatformGetInfoWithCodeLoc(offload_platform_handle_t hPlatform,
426+
offload_platform_info_t propName,
427+
size_t propSize, void *pPropValue,
428+
size_t *pPropSizeRet,
429+
offload_code_location_t *pCodeLocation);
430+
431+
///////////////////////////////////////////////////////////////////////////////
432+
/// @brief Variant of offloadDeviceGet that also sets source code location
433+
/// information
434+
/// @details See also ::offloadDeviceGet
435+
OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL offloadDeviceGetWithCodeLoc(
436+
offload_platform_handle_t hPlatform, offload_device_type_t DeviceType,
437+
uint32_t NumEntries, offload_device_handle_t *phDevices,
438+
uint32_t *pNumDevices, offload_code_location_t *pCodeLocation);
439+
440+
///////////////////////////////////////////////////////////////////////////////
441+
/// @brief Variant of offloadDeviceGetInfo that also sets source code location
442+
/// information
443+
/// @details See also ::offloadDeviceGetInfo
444+
OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL
445+
offloadDeviceGetInfoWithCodeLoc(offload_device_handle_t hDevice,
446+
offload_device_info_t propName, size_t propSize,
447+
void *pPropValue, size_t *pPropSizeRet,
448+
offload_code_location_t *pCodeLocation);
449+
400450
#if defined(__cplusplus)
401451
} // extern "C"
402452
#endif

offload/new-api/include/offload_entry_points.inc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ offloadPlatformGet(uint32_t NumEntries, offload_platform_handle_t *phPlatforms,
4040
}
4141
return result;
4242
}
43+
offload_result_t offloadPlatformGetWithCodeLoc(
44+
uint32_t NumEntries, offload_platform_handle_t *phPlatforms,
45+
uint32_t *pNumPlatforms, offload_code_location_t *pCodeLocation) {
46+
CodeLocation() = pCodeLocation;
47+
offload_result_t result =
48+
offloadPlatformGet(NumEntries, phPlatforms, pNumPlatforms);
49+
50+
CodeLocation() = nullptr;
51+
return result;
52+
}
4353

4454
///////////////////////////////////////////////////////////////////////////////
4555
offload_impl_result_t
@@ -88,6 +98,17 @@ OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL offloadPlatformGetInfo(
8898
}
8999
return result;
90100
}
101+
offload_result_t offloadPlatformGetInfoWithCodeLoc(
102+
offload_platform_handle_t hPlatform, offload_platform_info_t propName,
103+
size_t propSize, void *pPropValue, size_t *pPropSizeRet,
104+
offload_code_location_t *pCodeLocation) {
105+
CodeLocation() = pCodeLocation;
106+
offload_result_t result = offloadPlatformGetInfo(
107+
hPlatform, propName, propSize, pPropValue, pPropSizeRet);
108+
109+
CodeLocation() = nullptr;
110+
return result;
111+
}
91112

92113
///////////////////////////////////////////////////////////////////////////////
93114
offload_impl_result_t offloadDeviceGet_val(offload_platform_handle_t hPlatform,
@@ -134,6 +155,17 @@ offloadDeviceGet(offload_platform_handle_t hPlatform,
134155
}
135156
return result;
136157
}
158+
offload_result_t offloadDeviceGetWithCodeLoc(
159+
offload_platform_handle_t hPlatform, offload_device_type_t DeviceType,
160+
uint32_t NumEntries, offload_device_handle_t *phDevices,
161+
uint32_t *pNumDevices, offload_code_location_t *pCodeLocation) {
162+
CodeLocation() = pCodeLocation;
163+
offload_result_t result = offloadDeviceGet(hPlatform, DeviceType, NumEntries,
164+
phDevices, pNumDevices);
165+
166+
CodeLocation() = nullptr;
167+
return result;
168+
}
137169

138170
///////////////////////////////////////////////////////////////////////////////
139171
offload_impl_result_t offloadDeviceGetInfo_val(offload_device_handle_t hDevice,
@@ -183,3 +215,15 @@ OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL offloadDeviceGetInfo(
183215
}
184216
return result;
185217
}
218+
offload_result_t
219+
offloadDeviceGetInfoWithCodeLoc(offload_device_handle_t hDevice,
220+
offload_device_info_t propName, size_t propSize,
221+
void *pPropValue, size_t *pPropSizeRet,
222+
offload_code_location_t *pCodeLocation) {
223+
CodeLocation() = pCodeLocation;
224+
offload_result_t result = offloadDeviceGetInfo(hDevice, propName, propSize,
225+
pPropValue, pPropSizeRet);
226+
227+
CodeLocation() = nullptr;
228+
return result;
229+
}

offload/new-api/src/offload_lib.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ ErrSetT &Errors() {
2626
return Errors;
2727
}
2828

29+
offload_code_location_t *&CodeLocation() {
30+
thread_local offload_code_location_t *CodeLoc = nullptr;
31+
return CodeLoc;
32+
}
33+
2934
// Pull in the declarations for the implementation funtions. The actual entry
3035
// points in this file wrap these.
3136
#include "offload_impl_func_decls.inc"

offload/tools/offload-tblgen/APIGen.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ typedef struct {1} {{
168168
OS << formatv("} {0};\n", Func.getParamStructName());
169169
}
170170

171+
static void ProcessFuncWithCodeLocVariant(const FunctionRec &Func,
172+
raw_ostream &OS) {
173+
174+
auto FuncWithCodeLocBegin = R"(
175+
///////////////////////////////////////////////////////////////////////////////
176+
/// @brief Variant of {0} that also sets source code location information
177+
/// @details See also ::{0}
178+
OFFLOAD_APIEXPORT offload_result_t OFFLOAD_APICALL {0}WithCodeLoc(
179+
)";
180+
OS << formatv(FuncWithCodeLocBegin, Func.getName());
181+
auto Params = Func.getParams();
182+
for (auto &Param : Params) {
183+
OS << " " << Param.getType() << " " << Param.getName();
184+
OS << ",\n";
185+
}
186+
OS << "offload_code_location_t *pCodeLocation);\n\n";
187+
}
188+
171189
void EmitOffloadAPI(RecordKeeper &Records, raw_ostream &OS) {
172190
OS << GenericHeader;
173191
OS << FileHeader;
@@ -193,5 +211,9 @@ void EmitOffloadAPI(RecordKeeper &Records, raw_ostream &OS) {
193211
ProcessFuncParamStruct(FunctionRec{R}, OS);
194212
}
195213

214+
for (auto *R : Records.getAllDerivedDefinitions("Function")) {
215+
ProcessFuncWithCodeLocVariant(FunctionRec{R}, OS);
216+
}
217+
196218
OS << FileFooter;
197219
}

offload/tools/offload-tblgen/EntryPointGen.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,33 @@ static void EmitEntryPointFunc(const FunctionRec &F, raw_ostream &OS) {
102102
OS << "}\n";
103103
}
104104

105+
static void EmitCodeLocWrapper(const FunctionRec &F, raw_ostream &OS) {
106+
// Emit preamble
107+
OS << formatv("{0}_result_t {1}WithCodeLoc(\n ", PrefixLower, F.getName());
108+
// Emit arguments
109+
std::string ParamNameList = "";
110+
for (auto &Param : F.getParams()) {
111+
OS << Param.getType() << " " << Param.getName() << ", ";
112+
ParamNameList += Param.getName().str();
113+
if (Param != F.getParams().back()) {
114+
ParamNameList += ", ";
115+
}
116+
}
117+
OS << "offload_code_location_t *pCodeLocation";
118+
OS << ") {\n";
119+
OS << TAB_1 "CodeLocation() = pCodeLocation;\n";
120+
OS << formatv(TAB_1 "{0}_result_t result = {1}({2});\n\n", PrefixLower,
121+
F.getName(), ParamNameList);
122+
OS << TAB_1 "CodeLocation() = nullptr;\n";
123+
OS << TAB_1 "return result;\n";
124+
OS << "}\n";
125+
}
126+
105127
void EmitOffloadEntryPoints(RecordKeeper &Records, raw_ostream &OS) {
106128
OS << GenericHeader;
107129
for (auto *R : Records.getAllDerivedDefinitions("Function")) {
108130
EmitValidationFunc(FunctionRec{R}, OS);
109131
EmitEntryPointFunc(FunctionRec{R}, OS);
132+
EmitCodeLocWrapper(FunctionRec{R}, OS);
110133
}
111134
}

0 commit comments

Comments
 (0)