Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use env vars instead of CMake vars for Dawn/Node bindings #7422

Merged
merged 21 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ac41747
Split WebGPU runtime into two variants (#7248 workaround)
steven-johnson Mar 14, 2023
fa0ae85
A few more fixes
steven-johnson Mar 14, 2023
2b34e19
Use env vars instead of CMake vars for Dawn/Node bindings
steven-johnson Mar 15, 2023
2f55173
Merge branch 'main' into srj/webgpu-two-variants
steven-johnson Mar 15, 2023
9743ddd
Merge branch 'srj/webgpu-two-variants' into srj/webgpu-env-vars
steven-johnson Mar 15, 2023
944555e
Merge branch 'main' into srj/webgpu-env-vars
steven-johnson Mar 16, 2023
0103abc
Merge branch 'main' into srj/webgpu-env-vars
steven-johnson Mar 17, 2023
aa49544
Merge branch 'main' into srj/webgpu-env-vars
steven-johnson Mar 28, 2023
91c3fc5
Improve "Could not find a native WebGPU library" error msg
steven-johnson Mar 28, 2023
f3bcf0c
Update README_webgpu.md
steven-johnson Mar 28, 2023
76e024e
Use a launcher script for wasm (with or without webgpu)
steven-johnson Mar 28, 2023
6735fc3
Merge branch 'main' into srj/webgpu-env-vars
steven-johnson Apr 18, 2023
8f09166
Use a find-module to locate Dawn/WebGPU (#7525)
alexreinking Apr 20, 2023
c00440a
Merge branch 'main' into srj/webgpu-env-vars
steven-johnson Apr 20, 2023
e441425
Merge branch 'srj/webgpu-env-vars' of https://github.com/halide/Halid…
steven-johnson Apr 20, 2023
9a3312b
trigger buildbots
steven-johnson Apr 20, 2023
98b98bf
Merge branch 'main' into srj/webgpu-env-vars
steven-johnson Apr 24, 2023
78a0226
Update FindHalide_WebGPU.cmake
steven-johnson Apr 27, 2023
8379c53
trigger buildbots
steven-johnson Apr 28, 2023
bb01f3c
Update FindHalide_WebGPU.cmake
steven-johnson May 1, 2023
24eb78c
Update FindHalide_WebGPU.cmake
steven-johnson May 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions README_webgpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ When invoking `emcc` to link Halide-generated objects, include these flags:

Tests that use AOT compilation can be run using a native WebGPU implementation
that has Node.js bindings, such as [Dawn](dawn.googlesource.com/dawn/).
When configuring Halide, use `-DWEBGPU_NODE_BINDINGS=/path/to/dawn.node` to
enable these tests.
You must set an environment variable named `HL_WEBGPU_NODE_BINDINGS` that
has an absolute path to the bindings to run these tests, e.g. `HL_WEBGPU_NODE_BINDINGS=/path/to/dawn.node`.

See [below](#setting-up-dawn) for instructions on building the Dawn Node.js
bindings.

Expand All @@ -51,9 +52,14 @@ For testing purposes, Halide can also target native WebGPU libraries, such as
This is currently the only path that can run the JIT correctness tests.
See [below](#setting-up-dawn) for instructions on building Dawn.

Pass `-DWEBGPU_NATIVE_LIB=/path/to/native/library.{so,dylib.dll}` to CMake when
configuring Halide to enable this path, which will automatically use this
library for the AOT and JIT tests.
When targeting WebGPU with a native target, Halide defaults to looking for a
build of Dawn (with several common names and suffixes); you can override this
by setting the `HL_WEBGPU_NATIVE_LIB` environment variable to the absolute path
to the library you want.

Note that it is explicitly legal to define both `HL_WEBGPU_NATIVE_LIB` and
`HL_WEBGPU_NODE_BINDINGS` at the same time; the correct executable environment
will be selected based on the Halide target specified.

Note that it is explicitly legal to specify both WEBGPU_NATIVE_LIB and
WEBGPU_NODE_BINDINGS for the same build; the correct executable environment
Expand Down Expand Up @@ -98,5 +104,5 @@ This will produce the following artifacts:
- Node.js bindings: `<build_dir>/dawn.node`
- Native library: `<build_dir>/src/dawn/native/libwebgpu_dawn.{so,dylib,dll}`

These paths can then be used for the `-DWEBGPU_NODE_BINDINGS` and
`-DWEBGPU_NATIVE_LIB` CMake options when configuring Halide.
These paths can then be used for the `HL_WEBGPU_NODE_BINDINGS` and
`HL_WEBGPU_NATIVE_LIB` environment variables when using Halide.
4 changes: 2 additions & 2 deletions cmake/HalideGeneratorHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,8 @@ function(_Halide_target_link_gpu_libs TARGET VISIBILITY)
endif ()

if ("${ARGN}" MATCHES "webgpu")
if (WEBGPU_NATIVE_LIB)
target_link_libraries(${TARGET} INTERFACE ${WEBGPU_NATIVE_LIB})
if (DEFINED ENV{HL_WEBGPU_NATIVE_LIB})
target_link_libraries(${TARGET} INTERFACE $ENV{HL_WEBGPU_NATIVE_LIB})
endif ()
alexreinking marked this conversation as resolved.
Show resolved Hide resolved
endif ()
endfunction()
Expand Down
16 changes: 10 additions & 6 deletions dependencies/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ function(add_wasm_halide_test TARGET)
endif ()

set(script "require('./${TARGET}.js')")
if (WEBGPU_NODE_BINDINGS)
set(script "\
const provider = require('${WEBGPU_NODE_BINDINGS}')\n\
const gpu = provider.create([])\n\
const navigator = { gpu: gpu }\n
${script}")
if (Halide_TARGET MATCHES "webgpu")
if (DEFINED ENV{HL_WEBGPU_NODE_BINDINGS})
set(script "\
const provider = require('$ENV{HL_WEBGPU_NODE_BINDINGS}')\n\
const gpu = provider.create([])\n\
const navigator = { gpu: gpu }\n
${script}")
else ()
message(FATAL_ERROR "You must define the env var HL_WEBGPU_NODE_BINDINGS=/path/to/dawn.node when running wasm tests for Halide.")
endif ()
endif ()

steven-johnson marked this conversation as resolved.
Show resolved Hide resolved
add_halide_test("${TARGET}"
Expand Down
7 changes: 0 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,6 @@ endif ()
option(TARGET_WEBGPU "Include WebGPU target" ON)
if (TARGET_WEBGPU)
target_compile_definitions(Halide PRIVATE WITH_WEBGPU)
set(WEBGPU_NATIVE_LIB "" CACHE STRING
"WebGPU native library to link against")
if (WEBGPU_NATIVE_LIB)
set_property(SOURCE JITModule.cpp PROPERTY COMPILE_DEFINITIONS
WEBGPU_NATIVE_LIB=\"${WEBGPU_NATIVE_LIB}\"
APPEND_STRING)
endif ()
endif()

if (TARGET_SPIRV)
Expand Down
54 changes: 31 additions & 23 deletions src/JITModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,39 @@ void load_metal() {

void load_webgpu() {
debug(1) << "Looking for a native WebGPU implementation...\n";
const char *libnames[] = {
#ifdef WEBGPU_NATIVE_LIB
// Specified via CMake.
WEBGPU_NATIVE_LIB,
#endif
// Dawn (Chromium).
"libwebgpu_dawn.so",
"libwebgpu_dawn.dylib",
"webgpu_dawn.dll",

// wgpu (Firefox).
"libwgpu.so",
"libwgpu.dylib",
"wgpu.dll",
};
string error;
for (const char *libname : libnames) {

const auto try_load = [](const char *libname) -> string {
debug(1) << "Trying " << libname << "... ";
error.clear();
string error;
llvm::sys::DynamicLibrary::LoadLibraryPermanently(libname, &error);
if (error.empty()) {
debug(1) << "found!\n";
break;
} else {
debug(1) << "not found.\n";
debug(1) << (error.empty() ? "found!\n" : "not found.\n");
return error;
};

string error;

auto env_libname = get_env_variable("HL_WEBGPU_NATIVE_LIB");
steven-johnson marked this conversation as resolved.
Show resolved Hide resolved
if (!env_libname.empty()) {
error = try_load(env_libname.c_str());
}
if (!error.empty()) {
const char *libnames[] = {
// Dawn (Chromium).
"libwebgpu_dawn.so",
"libwebgpu_dawn.dylib",
"webgpu_dawn.dll",

// wgpu (Firefox).
"libwgpu.so",
"libwgpu.dylib",
"wgpu.dll",
};

for (const char *libname : libnames) {
error = try_load(libname);
if (error.empty()) {
break;
}
}
}
user_assert(error.empty()) << "Could not find a native WebGPU library: "
Expand Down
Loading