Skip to content

Commit 6806174

Browse files
authored
fix webgpu delay load test (microsoft#23157)
### Description This change fixes the WebGPU delay load test. <details> <summary>Fix UB in macro</summary> The following C++ code outputs `2, 1` in MSVC, while it outputs `1, 1` in GCC: ```c++ #include <iostream> #define A 1 #define B 1 #define ENABLE defined(A) && defined(B) #if ENABLE int x = 1; #else int x = 2; #endif #if defined(A) && defined(B) int y = 1; #else int y = 2; #endif int main() { std::cout << x << ", " << y << "\n"; } ``` Clang reports `macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]`. </details> <details> <summary>Fix condition of build option onnxruntime_ENABLE_DELAY_LOADING_WIN_DLLS</summary> Delay load is explicitly disabled when python binding is being built. modifies the condition. </details>
1 parent fcc34da commit 6806174

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

cmake/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ option(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS "Dump debug information about node
130130
cmake_dependent_option(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS_ENABLE_DUMP_TO_SQLDB "Build dump debug information about node inputs and outputs with support for sql database." OFF "onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS" OFF)
131131

132132
# When loading a delay loaded DLL, Windows searches the main EXE's folder first.
133-
# In a Python process, it searches where python.exe lives, but it doesn't search the python package's installation folder. Therefore we cannot enable this flag when Python is enabled.
134-
cmake_dependent_option(onnxruntime_ENABLE_DELAY_LOADING_WIN_DLLS "Delay load some of the dependent DLls that are part of the OS" ON "WIN32;NOT GDK_PLATFORM;NOT onnxruntime_ENABLE_PYTHON" OFF)
133+
cmake_dependent_option(onnxruntime_ENABLE_DELAY_LOADING_WIN_DLLS "Delay load some of the dependent DLls that are part of the OS" ON "WIN32;NOT GDK_PLATFORM" OFF)
135134
option(onnxruntime_USE_DML "Build with DirectML support" OFF)
136135
option(onnxruntime_USE_MIGRAPHX "Build with AMDMIGraphX support" OFF)
137136
option(onnxruntime_USE_WINML "Build with WinML support" OFF)

onnxruntime/core/dll/delay_load_hook.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@
2424
// - both USE_WEBGPU and BUILD_DAWN_MONOLITHIC_LIBRARY are defined
2525
// - USE_DML is defined
2626
//
27-
#define ORT_DELAY_LOAD_WEBGPU_DAWN_DLL (defined(USE_WEBGPU) && defined(BUILD_DAWN_MONOLITHIC_LIBRARY))
28-
#define ORT_DELAY_LOAD_DIRECTML_DLL defined(USE_DML)
27+
#if defined(USE_WEBGPU) && defined(BUILD_DAWN_MONOLITHIC_LIBRARY)
28+
#define ORT_DELAY_LOAD_WEBGPU_DAWN_DLL 1
29+
#else
30+
#define ORT_DELAY_LOAD_WEBGPU_DAWN_DLL 0
31+
#endif
32+
#if defined(USE_DML)
33+
#define ORT_DELAY_LOAD_DIRECTML_DLL 1
34+
#else
35+
#define ORT_DELAY_LOAD_DIRECTML_DLL 0
36+
#endif
2937
#if defined(_MSC_VER) && (ORT_DELAY_LOAD_WEBGPU_DAWN_DLL || ORT_DELAY_LOAD_DIRECTML_DLL)
3038

3139
#include <Windows.h>
@@ -59,7 +67,7 @@ FARPROC WINAPI delay_load_hook(unsigned dliNotify, PDelayLoadInfo pdli) {
5967
// Try to load the DLL from the same directory as onnxruntime.dll
6068

6169
// First, get the path to onnxruntime.dll
62-
auto path = Env::Default().GetRuntimePath();
70+
auto path = onnxruntime::Env::Default().GetRuntimePath();
6371
if (path.empty()) {
6472
// Failed to get the path to onnxruntime.dll. In this case, we will just return NULL and let the system
6573
// search for the DLL in the default search order.

onnxruntime/test/webgpu/delay_load/main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ int test_main() {
118118
HMODULE hModule = LoadLibraryA("dlls\\onnxruntime.dll");
119119
if (hModule == NULL) {
120120
std::cout << "Failed to load dlls\\onnxruntime.dll" << std::endl;
121+
std::cout << "Error code: " << GetLastError() << std::endl;
121122
return 1;
122123
}
123124

0 commit comments

Comments
 (0)