Skip to content

Commit 01c9bad

Browse files
authored
Fix dynamic tracing support with static loader (#313)
* Fix dynamic tracing support with static loader - To allow for dynamic init of tracing, the static loader cannot optimize out the call to the ze_loader's ze_lib. - Therefore, during init, the function pointer is instead assigned to the dynamic loaded loader's apis to enable tracing. * Add optimizations for static loader calls. * Fix visual studio version for all windows builds Signed-off-by: Neil R. Spruit <[email protected]>
1 parent 96cf377 commit 01c9bad

File tree

10 files changed

+8147
-8
lines changed

10 files changed

+8147
-8
lines changed

.github/docker/windows.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG BASE_IMAGE=mcr.microsoft.com/dotnet/framework/runtime:4.8
44
FROM ${BASE_IMAGE}
55

66
SHELL ["powershell"]
7-
ENV VS_VERSION=2019
7+
ENV VS_VERSION=2022
88
RUN [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
99
Switch ($env:VS_VERSION) { `
1010
"2019" {$url_version = "16"} `

scripts/templates/libapi.cpp.mako

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ ${th.make_func_name(n, tags, obj)}(
160160
}
161161
%endif
162162
%else:
163+
#ifdef DYNAMIC_LOAD_LOADER
164+
ze_result_t result = ${X}_RESULT_SUCCESS;
165+
if(ze_lib::destruction) {
166+
return ${X}_RESULT_ERROR_UNINITIALIZED;
167+
}
168+
%if re.match(r"\w+DriverGet$", th.make_func_name(n, tags, obj)):
169+
if (${x}_lib::context->${n}DdiTable == nullptr) {
170+
return ${X}_RESULT_ERROR_UNINITIALIZED;
171+
}
172+
%endif
173+
static const ${th.make_pfn_type(n, tags, obj)} ${th.make_pfn_name(n, tags, obj)} = [&result] {
174+
auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.load()->${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};
175+
if( nullptr == ${th.make_pfn_name(n, tags, obj)} ) {
176+
result = ${X}_RESULT_ERROR_UNSUPPORTED_FEATURE;
177+
}
178+
return ${th.make_pfn_name(n, tags, obj)};
179+
}();
180+
if (result != ${X}_RESULT_SUCCESS) {
181+
return result;
182+
}
183+
return ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );
184+
#else
163185
if(ze_lib::destruction) {
164186
return ${X}_RESULT_ERROR_UNINITIALIZED;
165187
}
@@ -182,6 +204,7 @@ ${th.make_func_name(n, tags, obj)}(
182204
%endif
183205

184206
return ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );
207+
#endif
185208
}
186209
%endif
187210
%if 'condition' in obj:

scripts/templates/libddi.cpp.mako

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ namespace ${x}_lib
4343
GET_FUNCTION_PTR(loader, "${tbl['export']['name']}") );
4444
result = getTableWithCheck(getTable, version, &initial${n}DdiTable.${tbl['name']} );
4545
%endif
46+
%for obj in tbl['functions']:
47+
initial${n}DdiTable.${tbl['name']}.${th.make_pfn_name(n, tags, obj)} = reinterpret_cast<${th.make_pfn_type(n, tags, obj)}>(
48+
GET_FUNCTION_PTR(loader, "${th.make_func_name(n, tags, obj)}") );
49+
%endfor
4650
}
4751

4852
%endfor

source/lib/ze_lib.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,24 +538,42 @@ zelLoaderContextTeardown()
538538
ze_result_t ZE_APICALL
539539
zelEnableTracingLayer()
540540
{
541+
#ifdef DYNAMIC_LOAD_LOADER
542+
if(nullptr == ze_lib::context->loader)
543+
return ZE_RESULT_ERROR_UNINITIALIZED;
544+
typedef ze_result_t (ZE_APICALL *zelEnableTracingLayerInternal_t)();
545+
auto enableDynamicTracing = reinterpret_cast<zelEnableTracingLayerInternal_t>(
546+
GET_FUNCTION_PTR(ze_lib::context->loader, "zelEnableTracingLayer") );
547+
return enableDynamicTracing();
548+
#else
541549
if (ze_lib::context->dynamicTracingSupported == false) {
542550
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
543551
}
544552
if (ze_lib::context->tracingLayerEnableCounter.fetch_add(1) == 0) {
545553
ze_lib::context->zeDdiTable.exchange(ze_lib::context->pTracingZeDdiTable);
546554
}
555+
#endif
547556
return ZE_RESULT_SUCCESS;
548557
}
549558

550559
ze_result_t ZE_APICALL
551560
zelDisableTracingLayer()
552561
{
562+
#ifdef DYNAMIC_LOAD_LOADER
563+
if(nullptr == ze_lib::context->loader)
564+
return ZE_RESULT_ERROR_UNINITIALIZED;
565+
typedef ze_result_t (ZE_APICALL *zelDisableTracingLayerInternal_t)();
566+
auto disableDynamicTracing = reinterpret_cast<zelDisableTracingLayerInternal_t>(
567+
GET_FUNCTION_PTR(ze_lib::context->loader, "zelDisableTracingLayer") );
568+
return disableDynamicTracing();
569+
#else
553570
if (ze_lib::context->dynamicTracingSupported == false) {
554571
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
555572
}
556573
if (ze_lib::context->tracingLayerEnableCounter.fetch_sub(1) <= 1) {
557574
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
558575
}
576+
#endif
559577
return ZE_RESULT_SUCCESS;
560578
}
561579

0 commit comments

Comments
 (0)