diff --git a/CMakeLists.txt b/CMakeLists.txt index a19cefd718a8..0685a95c05ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,11 +118,9 @@ else() endif() # WebGPU is disabled by default as it's experimental. -# TODO(benvanik): FindPackage the deps or default to on when under emscripten? +# TODO(benvanik): FindPackage the deps or default to on when under Emscripten? +# (also see iree_copts.cmake and anywhere that uses IREE_HAL_DRIVER_WEBGPU) set(IREE_HAL_DRIVER_WEBGPU_DEFAULT OFF) -set(IREE_HAL_WEBGPU_PLATFORM_DEFAULT "dawn") -set(IREE_HAL_WEBGPU_PLATFORM "${IREE_HAL_WEBGPU_PLATFORM_DEFAULT}" - CACHE STRING "Native WebGPU implementation ('dawn' or 'wgpu-native').") option(IREE_HAL_DRIVER_CUDA "Enables the 'cuda' runtime HAL driver" ${IREE_HAL_DRIVER_CUDA_DEFAULT}) option(IREE_HAL_DRIVER_LOCAL_SYNC "Enables the 'local-sync' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS}) @@ -659,7 +657,6 @@ include(flatbuffer_c_library) add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL) add_subdirectory(build_tools/third_party/llvm-project EXCLUDE_FROM_ALL) add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL) -add_subdirectory(build_tools/third_party/wgpu-native EXCLUDE_FROM_ALL) iree_set_googletest_cmake_options() add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL) @@ -706,35 +703,27 @@ if(IREE_BUILD_PYTHON_BINDINGS) endif() if(IREE_TARGET_BACKEND_METAL_SPIRV) - iree_set_spirv_cross_cmake_options() # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code. + iree_set_spirv_cross_cmake_options() add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL) endif() -if(IREE_TARGET_BACKEND_WEBGPU OR - (IREE_HAL_DRIVER_WEBGPU AND ("${IREE_HAL_WEBGPU_PLATFORM}" STREQUAL "dawn"))) - # NOTE: these all need to be synchronized with Dawn. - # set(IREE_SPIRV_TOOLS_TAG "37d2396cabe56b29d37551ea55d0d745d5748ded") - set(IREE_SPIRV_TOOLS_TAG "ff07cfd86fa229525659f6b81058b3171a67bef1") # 2021-12-10 - # https://dawn.googlesource.com/dawn/+/refs/heads/main/DEPS - # https://chromium.googlesource.com/vulkan-deps/+/main/DEPS - # set(IREE_TINT_TAG "5aca651c524caca8ac85962d1dff378238671eba") - set(IREE_TINT_TAG "188b1fb8f5be52299fb7fbc6db17dbb0c07dbb7e") # 2021-12-16 - set(IREE_DAWN_ABSEIL_TAG "789af048b388657987c59d4da406859034fe310f") - set(IREE_DAWN_GLFW_TAG "62e175ef9fae75335575964c845a302447c012c7") - set(IREE_DAWN_TAG "2f1b0dc47d8316f3db6e5b9a55b873cd3079cea5") - +if(IREE_TARGET_BACKEND_WEBGPU) # Tint is needed to compile SPIR-V into WGSL source code. # Tint also requires SPIRV-Tools, which requires SPIRV-Headers. - # Dawn needs Tint (and thus SPIRV-Tools), but only at runtime. - # Order matters here, unfortunately. + + # NOTE: these can be synced by referencing one of these repositories: + # * https://dawn.googlesource.com/dawn/ + # * https://dawn.googlesource.com/tint/ + # * https://chromium.googlesource.com/vulkan-deps/+/refs/heads/main/DEPS + # or they can be updated independently + set(IREE_TINT_TAG "200492e32b94f042d9942154fb4fa7f93bb8289a") # 2022-08-17 + set(IREE_SPIRV_TOOLS_TAG "ff07cfd86fa229525659f6b81058b3171a67bef1") # 2021-12-10 + iree_set_spirv_headers_cmake_options() add_subdirectory(third_party/spirv_headers EXCLUDE_FROM_ALL) add_subdirectory(build_tools/third_party/spirv-tools EXCLUDE_FROM_ALL) add_subdirectory(build_tools/third_party/tint EXCLUDE_FROM_ALL) - if(IREE_HAL_DRIVER_WEBGPU AND ("${IREE_HAL_WEBGPU_PLATFORM}" STREQUAL "dawn")) - add_subdirectory(build_tools/third_party/dawn EXCLUDE_FROM_ALL) - endif() endif() #------------------------------------------------------------------------------- diff --git a/build_tools/bazel/workspace.bzl b/build_tools/bazel/workspace.bzl index 244d5e37e0f0..7d5c8644d466 100644 --- a/build_tools/bazel/workspace.bzl +++ b/build_tools/bazel/workspace.bzl @@ -94,3 +94,10 @@ def configure_iree_submodule_deps(iree_repo_alias = "@", iree_path = "./"): build_file = iree_repo_alias + "//:build_tools/third_party/torch-mlir-dialects/BUILD.overlay", path = paths.join(iree_path, "third_party/torch-mlir-dialects"), ) + + maybe( + native.new_local_repository, + name = "webgpu_headers", + build_file = iree_repo_alias + "//:build_tools/third_party/webgpu-headers/BUILD.overlay", + path = paths.join(iree_path, "third_party/webgpu-headers"), + ) diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py b/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py index a104fc76da45..c6e08c0e2ecc 100644 --- a/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py +++ b/build_tools/bazel_to_cmake/bazel_to_cmake_targets.py @@ -158,7 +158,6 @@ "@cpuinfo": ["${IREE_CPUINFO_TARGET}"], "@vulkan_memory_allocator//:impl_header_only": ["vulkan_memory_allocator"], "@webgpu_headers": [], - "@wgpu-native//:static": ["wgpu-native-static"], } diff --git a/build_tools/cmake/iree_copts.cmake b/build_tools/cmake/iree_copts.cmake index 25b70c585312..6fb559f0417b 100644 --- a/build_tools/cmake/iree_copts.cmake +++ b/build_tools/cmake/iree_copts.cmake @@ -358,6 +358,7 @@ iree_select_compiler_opts(IREE_DEFAULT_LINKOPTS if(EMSCRIPTEN AND IREE_HAL_DRIVER_WEBGPU) iree_select_compiler_opts(IREE_DEFAULT_LINKOPTS ALL + # TODO(scotttodd): Only add when using WebGPU in a library/binary? "-sUSE_WEBGPU" # Hack: Used to create sync versions of requestAdapter and requestDevice # TODO(scotttodd): Only set for test binaries, avoid sync code in apps diff --git a/build_tools/third_party/dawn/CMakeLists.txt b/build_tools/third_party/dawn/CMakeLists.txt deleted file mode 100644 index 9796c031ad5d..000000000000 --- a/build_tools/third_party/dawn/CMakeLists.txt +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright 2021 The IREE Authors -# -# Licensed under the Apache License v2.0 with LLVM Exceptions. -# See https://llvm.org/LICENSE.txt for license information. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -if(NOT (${IREE_HAL_WEBGPU_PLATFORM} STREQUAL "dawn")) - return() -endif() - -include(FetchContent) - -FetchContent_Declare( - abseil-cpp - GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git - GIT_TAG ${IREE_DAWN_ABSEIL_TAG} -) - -set(ABSL_BUILD_TESTING OFF CACHE BOOL "" FORCE) -set(ABSL_PROPAGATE_CXX_STD ON CACHE BOOL "" FORCE) - -FetchContent_MakeAvailable(abseil-cpp) -FetchContent_GetProperties(abseil-cpp SOURCE_DIR ABSEIL_CPP_SOURCE) - -# FetchContent_Declare( -# glfw -# GIT_REPOSITORY https://github.com/glfw/glfw.git -# GIT_TAG ${IREE_DAWN_GLFW_TAG} -# ) -# FetchContent_MakeAvailable(glfw) - -FetchContent_Declare( - dawn - GIT_REPOSITORY https://dawn.googlesource.com/dawn - GIT_TAG ${IREE_DAWN_TAG} -) - -set(DAWN_ENABLE_OPENGL OFF) -set(DAWN_ENABLE_DESKTOP_GL OFF) -set(DAWN_ENABLE_OPENGLES OFF) -set(DAWN_USE_X11 OFF) -set(DAWN_SUPPORTS_GLFW_FOR_WINDOWING OFF) -set(DAWN_BUILD_EXAMPLES OFF) - -set(DAWN_THIRD_PARTY_DIR "${PROJECT_SOURCE_DIR}/third_party") -set(DAWN_ABSEIL_DIR "${ABSEIL_CPP_SOURCE}") -set(DAWN_SPIRV_CROSS_DIR "${DAWN_THIRD_PARTY_DIR}/spirv_cross") -set(DAWN_SPIRV_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/spirv_headers") -set(DAWN_SPIRV_TOOLS_DIR "${SPIRV_TOOLS_SOURCE}") -set(DAWN_TINT_DIR "${TINT_SOURCE}") - -# HACK: dawn looks for these targets instead of trusting the dirs set above, -# leading to weird double inclusion of things like abseil. I have no idea why -# it requires glfw even when opengl and examples are off. -add_library(libabsl STATIC EXCLUDE_FROM_ALL dummy.c) - -FetchContent_MakeAvailable(dawn) -FetchContent_GetProperties(dawn SOURCE_DIR DAWN_SOURCE) diff --git a/build_tools/third_party/dawn/dummy.c b/build_tools/third_party/dawn/dummy.c deleted file mode 100644 index 0b48ddc9bd01..000000000000 --- a/build_tools/third_party/dawn/dummy.c +++ /dev/null @@ -1 +0,0 @@ -static int x = 0; diff --git a/build_tools/third_party/tint/CMakeLists.txt b/build_tools/third_party/tint/CMakeLists.txt index 0b4b6ba1fb8c..c0e3e4c19964 100644 --- a/build_tools/third_party/tint/CMakeLists.txt +++ b/build_tools/third_party/tint/CMakeLists.txt @@ -16,18 +16,16 @@ set(TINT_BUILD_SAMPLES OFF CACHE BOOL "" FORCE) set(TINT_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(TINT_BUILD_TESTS OFF CACHE BOOL "" FORCE) -# Our usage at compile time primarily needs the SPIR-V reader and WGSL writer, -# but usage at runtime through Dawn needs a broader set. WebGPU on native is -# not a supported production use case and is only for development/testing and -# as such we don't get too particular about minimizing its size here. +# Our usage at compile time primarily needs the SPIR-V reader and WGSL writer. +# Other readers and writers could be enabled, but we want to keep this small. set(TINT_BUILD_SPV_READER ON CACHE BOOL "" FORCE) -set(TINT_BUILD_WGSL_READER ON CACHE BOOL "" FORCE) +set(TINT_BUILD_WGSL_READER OFF CACHE BOOL "" FORCE) set(TINT_BUILD_GLSL_WRITER OFF CACHE BOOL "" FORCE) -set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "" FORCE) -set(TINT_BUILD_MSL_WRITER ON CACHE BOOL "" FORCE) -set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "" FORCE) +set(TINT_BUILD_HLSL_WRITER OFF CACHE BOOL "" FORCE) +set(TINT_BUILD_MSL_WRITER OFF CACHE BOOL "" FORCE) +set(TINT_BUILD_SPV_WRITER OFF CACHE BOOL "" FORCE) set(TINT_BUILD_WGSL_WRITER ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(tint) diff --git a/build_tools/third_party/webgpu-headers/BUILD.overlay b/build_tools/third_party/webgpu-headers/BUILD.overlay new file mode 100644 index 000000000000..8feff9c07eb4 --- /dev/null +++ b/build_tools/third_party/webgpu-headers/BUILD.overlay @@ -0,0 +1,13 @@ +# Copyright 2022 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "webgpu_headers", + hdrs = ["webgpu.h"], + include_prefix = "third_party/webgpu-headers", +) diff --git a/build_tools/third_party/wgpu-native/CMakeLists.txt b/build_tools/third_party/wgpu-native/CMakeLists.txt deleted file mode 100644 index 2c9ef0d45dde..000000000000 --- a/build_tools/third_party/wgpu-native/CMakeLists.txt +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2021 The IREE Authors -# -# Licensed under the Apache License v2.0 with LLVM Exceptions. -# See https://llvm.org/LICENSE.txt for license information. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -if(NOT (${IREE_HAL_WEBGPU_PLATFORM} STREQUAL "wgpu-native")) - return() -endif() - -include(FetchContent) - -FetchContent_Declare( - wgpu-native - GIT_REPOSITORY https://github.com/gfx-rs/wgpu-native.git - GIT_TAG a2e81dc1271b876ebf0c0d35857040838ed58297 # 2021-12-04 -) -FetchContent_MakeAvailable(wgpu-native) -FetchContent_GetProperties(wgpu-native SOURCE_DIR WGPU_NATIVE_SOURCE) - -FetchContent_Declare( - Corrosion - GIT_REPOSITORY https://github.com/AndrewGaspar/corrosion.git - GIT_TAG f679545a63a8b214a415e086f910126ab66714fa # 2021-12-02 -) -FetchContent_MakeAvailable(Corrosion) - -corrosion_import_crate(MANIFEST_PATH ${WGPU_NATIVE_SOURCE}/Cargo.toml) - -set(LIBCLANG_PATH "D:\\Tools\\LLVM\\lib") -set_property( - TARGET wgpu-native APPEND - PROPERTY CORROSION_ENVIRONMENT_VARIABLES - "LIBCLANG_PATH=${LIBCLANG_PATH}" -) - -target_include_directories(wgpu-native-static SYSTEM INTERFACE - ${WGPU_NATIVE_SOURCE}/ffi -) -if(MSVC) - add_definitions(-DWGPU_TARGET=WGPU_TARGET_WINDOWS) - target_link_libraries(wgpu-native-static INTERFACE - "userenv" - "ws2_32" - "Dwmapi" - "dbghelp" - "d3dcompiler" - "D3D12" - "D3D11" - "DXGI" - "setupapi" - "bcrypt" - ) -elseif(APPLE) - add_definitions(-DWGPU_TARGET=WGPU_TARGET_MACOS) - target_link_libraries(wgpu-native-static INTERFACE - "-framework Cocoa" - "-framework CoreVideo" - "-framework IOKit" - "-framework QuartzCore" - ) -else() - add_definitions(-DWGPU_TARGET=WGPU_TARGET_LINUX) -endif() diff --git a/experimental/web/sample_webgpu/build_sample.sh b/experimental/web/sample_webgpu/build_sample.sh index 7187a50fd2b7..5e0d38d186c9 100644 --- a/experimental/web/sample_webgpu/build_sample.sh +++ b/experimental/web/sample_webgpu/build_sample.sh @@ -77,7 +77,6 @@ emcmake "${CMAKE_BIN?}" -G Ninja .. \ -DIREE_HAL_DRIVER_LOCAL_SYNC=OFF \ -DIREE_HAL_DRIVER_LOCAL_TASK=OFF \ -DIREE_HAL_DRIVER_WEBGPU=ON \ - -DIREE_HAL_WEBGPU_PLATFORM=emscripten \ -DIREE_BUILD_COMPILER=OFF \ -DIREE_BUILD_TESTS=OFF diff --git a/experimental/web/testing/build_tests.sh b/experimental/web/testing/build_tests.sh index fab2835ab335..9d814422611d 100644 --- a/experimental/web/testing/build_tests.sh +++ b/experimental/web/testing/build_tests.sh @@ -54,7 +54,6 @@ emcmake "${CMAKE_BIN?}" -G Ninja .. \ -DIREE_BUILD_COMPILER=OFF \ -DIREE_HAL_DRIVER_DEFAULTS=OFF \ -DIREE_HAL_DRIVER_WEBGPU=ON \ - -DIREE_HAL_WEBGPU_PLATFORM=emscripten \ -DIREE_HAL_DRIVER_LOCAL_SYNC=ON \ -DIREE_HAL_DRIVER_LOCAL_TASK=ON \ -DIREE_HAL_EXECUTABLE_LOADER_DEFAULTS=OFF \ diff --git a/runtime/src/iree/hal/allocator_heap.c b/runtime/src/iree/hal/allocator_heap.c index 570e004a723f..0946a077f120 100644 --- a/runtime/src/iree/hal/allocator_heap.c +++ b/runtime/src/iree/hal/allocator_heap.c @@ -34,7 +34,6 @@ IREE_API_EXPORT iree_status_t iree_hal_allocator_create_heap( IREE_ASSERT_ARGUMENT(out_allocator); *out_allocator = NULL; IREE_TRACE_ZONE_BEGIN(z0); - *out_allocator = NULL; iree_hal_heap_allocator_t* allocator = NULL; iree_host_size_t total_size = diff --git a/runtime/src/iree/hal/drivers/webgpu/BUILD b/runtime/src/iree/hal/drivers/webgpu/BUILD index 1f90f2856f98..a615c5d90c82 100644 --- a/runtime/src/iree/hal/drivers/webgpu/BUILD +++ b/runtime/src/iree/hal/drivers/webgpu/BUILD @@ -4,7 +4,7 @@ # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -load("//iree:build_defs.oss.bzl", "iree_runtime_cc_library") +load("//build_tools/bazel:build_defs.oss.bzl", "iree_runtime_cc_library") package( default_visibility = ["//visibility:public"], diff --git a/runtime/src/iree/hal/drivers/webgpu/builtins.c b/runtime/src/iree/hal/drivers/webgpu/builtins.c index a8b2b8d41ba6..4ee880996f3a 100644 --- a/runtime/src/iree/hal/drivers/webgpu/builtins.c +++ b/runtime/src/iree/hal/drivers/webgpu/builtins.c @@ -70,13 +70,20 @@ static iree_status_t iree_hal_webgpu_builtins_initialize_fill_buffer( "failed to create fill_buffer builtin pipeline layout"); } + const char* code = iree_hal_webgpu_builtins_find_code("fill_buffer.wgsl"); const WGPUShaderModuleWGSLDescriptor wgsl_descriptor = { - .chain = - { - .next = NULL, - .sType = WGPUSType_ShaderModuleWGSLDescriptor, - }, - .source = iree_hal_webgpu_builtins_find_code("fill_buffer.wgsl"), + .chain = + { + .next = NULL, + .sType = WGPUSType_ShaderModuleWGSLDescriptor, + }, +#if defined(__EMSCRIPTEN__) + // Emscripten uses this older name. + .source = code, +#else + // Spec uses this name: https://www.w3.org/TR/webgpu/#shader-module-creation + .code = code, +#endif }; const WGPUShaderModuleDescriptor module_descriptor = { .nextInChain = &wgsl_descriptor.chain, diff --git a/runtime/src/iree/hal/drivers/webgpu/executable.c b/runtime/src/iree/hal/drivers/webgpu/executable.c index 324f0c7c0442..4d80b6c6f0c9 100644 --- a/runtime/src/iree/hal/drivers/webgpu/executable.c +++ b/runtime/src/iree/hal/drivers/webgpu/executable.c @@ -106,12 +106,18 @@ static iree_status_t iree_hal_webgpu_create_wgsl_shader_module( const char* code = iree_WGSLShaderModuleDef_code_get(shader_module_def); const WGPUShaderModuleWGSLDescriptor descriptor = { - .chain = - { - .next = NULL, - .sType = WGPUSType_ShaderModuleWGSLDescriptor, - }, - .source = code, + .chain = + { + .next = NULL, + .sType = WGPUSType_ShaderModuleWGSLDescriptor, + }, +#if defined(__EMSCRIPTEN__) + // Emscripten uses this older name. + .source = code, +#else + // Spec uses this name: https://www.w3.org/TR/webgpu/#shader-module-creation + .code = code, +#endif }; const WGPUShaderModuleDescriptor module_descriptor = { .nextInChain = &descriptor.chain, diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/BUILD b/runtime/src/iree/hal/drivers/webgpu/platform/BUILD index 9c8bc795cbbf..63994b42cd03 100644 --- a/runtime/src/iree/hal/drivers/webgpu/platform/BUILD +++ b/runtime/src/iree/hal/drivers/webgpu/platform/BUILD @@ -4,13 +4,15 @@ # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +load("//build_tools/bazel:build_defs.oss.bzl", "iree_runtime_cc_library") + package( default_visibility = ["//visibility:public"], features = ["layering_check"], licenses = ["notice"], # Apache 2.0 ) -cc_library( +iree_runtime_cc_library( name = "platform", hdrs = [ "webgpu.h", diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/emscripten/emscripten_util.c b/runtime/src/iree/hal/drivers/webgpu/platform/emscripten/emscripten_util.c index 9c6e84e8d651..c8fc2d283b7b 100644 --- a/runtime/src/iree/hal/drivers/webgpu/platform/emscripten/emscripten_util.c +++ b/runtime/src/iree/hal/drivers/webgpu/platform/emscripten/emscripten_util.c @@ -82,10 +82,6 @@ IREEWGPUBufferMapSyncStatus iree_wgpuBufferMapSync(WGPUDevice device, WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size) { - IREEWGPUBufferMapSyncStatus status = IREEWGPUBufferMapSyncStatus_Unknown; - wgpuBufferMapAsync(buffer, mode, offset, size, - iree_hal_webgpu_buffer_map_sync_callback, &status); - // TODO(scotttodd): poll / wait somehow, or implement sync mapping differently - // wgpuDevicePoll(device, /*force_wait=*/true); - return status; + // WebGPU (browser/Emscripten) does not support synchronous buffer mapping. + return IREEWGPUBufferMapSyncStatus_Error; } diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/native/CMakeLists.txt b/runtime/src/iree/hal/drivers/webgpu/platform/native/CMakeLists.txt index af4cba4f14d5..93eae6812083 100644 --- a/runtime/src/iree/hal/drivers/webgpu/platform/native/CMakeLists.txt +++ b/runtime/src/iree/hal/drivers/webgpu/platform/native/CMakeLists.txt @@ -20,45 +20,3 @@ iree_cc_library( iree::hal::drivers::webgpu::platform PUBLIC ) - -if(${IREE_HAL_WEBGPU_PLATFORM} STREQUAL "dawn") - target_compile_definitions( - iree_hal_drivers_webgpu_platform_native_native - PUBLIC - "IREE_HAL_WEBGPU_PLATFORM_DAWN=1" - ) - target_sources( - iree_hal_drivers_webgpu_platform_native_native - PRIVATE - "dawn_util.cc" - "native_driver_dawn.cc" - ) - target_link_libraries( - iree_hal_drivers_webgpu_platform_native_native - PUBLIC - dawn_internal_config - dawncpp - dawn_proc - dawn_common - dawn_native - dawn_wire - dawn_utils - ) -elseif(${IREE_HAL_WEBGPU_PLATFORM} STREQUAL "wgpu-native") - target_compile_definitions( - iree_hal_drivers_webgpu_platform_native_native - PUBLIC - "IREE_HAL_WEBGPU_PLATFORM_WGPU_NATIVE=1" - ) - target_sources( - iree_hal_drivers_webgpu_platform_native_native - PRIVATE - "native_driver_wgpu.cc" - "wgpu_util.c" - ) - target_link_libraries( - iree_hal_drivers_webgpu_platform_native_native - PUBLIC - wgpu-native-static - ) -endif() diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/native/dawn_util.cc b/runtime/src/iree/hal/drivers/webgpu/platform/native/dawn_util.cc deleted file mode 100644 index b118f9db6371..000000000000 --- a/runtime/src/iree/hal/drivers/webgpu/platform/native/dawn_util.cc +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2021 The IREE Authors -// -// Licensed under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -// DO NOT SUBMIT -// #include // wgpu-native implementation only - -#include "iree/hal/drivers/webgpu/platform/webgpu.h" - -extern "C" { - -//===----------------------------------------------------------------------===// -// Implementation compatibility layer -//===----------------------------------------------------------------------===// - -void iree_wgpuBindGroupDrop(WGPUBindGroup bindGroup) { - // wgpuBindGroupDrop(bindGroup); -} - -void iree_wgpuBindGroupLayoutDrop(WGPUBindGroupLayout bindGroupLayout) { - // wgpuBindGroupLayoutDrop(bindGroupLayout); -} - -void iree_wgpuBufferDrop(WGPUBuffer buffer) { - // NOTE: drop != destroy (destroy is immediate) but it's all dawn has. - // wgpuBufferDrop(buffer); - wgpuBufferDestroy(buffer); -} - -void iree_wgpuCommandBufferDrop(WGPUCommandBuffer commandBuffer) { - // wgpuCommandBufferDrop(commandBuffer); -} - -void iree_wgpuCommandEncoderDrop(WGPUCommandEncoder commandEncoder) { - // wgpuCommandEncoderDrop(commandEncoder); -} - -void iree_wgpuComputePipelineDrop(WGPUComputePipeline computePipeline) { - // wgpuComputePipelineDrop(computePipeline); -} - -void iree_wgpuPipelineLayoutDrop(WGPUPipelineLayout pipelineLayout) { - // wgpuPipelineLayoutDrop(pipelineLayout); -} - -void iree_wgpuQuerySetDrop(WGPUQuerySet querySet) { - // wgpuQuerySetDrop(querySet); -} - -void iree_wgpuShaderModuleDrop(WGPUShaderModule shaderModule) { - // wgpuShaderModuleDrop(shaderModule); -} - -//===----------------------------------------------------------------------===// -// Speculative WebGPU API additions -//===----------------------------------------------------------------------===// - -static void iree_hal_webgpu_buffer_map_sync_callback( - WGPUBufferMapAsyncStatus status, void* userdata) { - IREEWGPUBufferMapSyncStatus* sync_status = - (IREEWGPUBufferMapSyncStatus*)userdata; - switch (status) { - case WGPUBufferMapAsyncStatus_Success: - *sync_status = IREEWGPUBufferMapSyncStatus_Success; - break; - case WGPUBufferMapAsyncStatus_Error: - *sync_status = IREEWGPUBufferMapSyncStatus_Error; - break; - default: - case WGPUBufferMapAsyncStatus_Unknown: - *sync_status = IREEWGPUBufferMapSyncStatus_Unknown; - break; - case WGPUBufferMapAsyncStatus_DeviceLost: - *sync_status = IREEWGPUBufferMapSyncStatus_DeviceLost; - break; - } -} - -IREEWGPUBufferMapSyncStatus iree_wgpuBufferMapSync(WGPUDevice device, - WGPUBuffer buffer, - WGPUMapModeFlags mode, - size_t offset, size_t size) { - IREEWGPUBufferMapSyncStatus status = IREEWGPUBufferMapSyncStatus_Unknown; - wgpuBufferMapAsync(buffer, mode, offset, size, - iree_hal_webgpu_buffer_map_sync_callback, &status); - // DO NOT SUBMIT - // wgpuDevicePoll(device, /*force_wait=*/true); - return status; -} - -} // extern "C" diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver.h b/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver.h index 37bd933e063d..8b4e750fa422 100644 --- a/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver.h +++ b/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver.h @@ -16,6 +16,8 @@ extern "C" { #endif // __cplusplus +// A stub webgpu driver that builds on native platforms, for testing +// compilation without Emscripten. iree_status_t iree_hal_webgpu_native_driver_create( iree_string_view_t identifier, const iree_hal_webgpu_driver_options_t* options, diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver_dawn.cc b/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver_dawn.cc deleted file mode 100644 index 49a757f2b328..000000000000 --- a/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver_dawn.cc +++ /dev/null @@ -1,298 +0,0 @@ -// Copyright 2021 The IREE Authors -// -// Licensed under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include -#include -#include - -#include - -#include "iree/base/tracing.h" -#include "iree/hal/drivers/webgpu/platform/native/native_driver.h" - -#define IREE_HAL_WEBGPU_DEVICE_ID_DEFAULT 0 - -extern "C" { - -//===----------------------------------------------------------------------===// -// wgpu-native callbacks -//===----------------------------------------------------------------------===// - -/* -typedef struct iree_hal_webgpu_native_adapter_data_t { - iree_status_t status; - WGPUAdapter handle; -} iree_hal_webgpu_native_adapter_data_t; -static void iree_hal_webgpu_native_request_adapter_callback( - WGPURequestAdapterStatus status, WGPUAdapter received, const char* message, - void* user_data_ptr) { - iree_hal_webgpu_native_adapter_data_t* data = - (iree_hal_webgpu_native_adapter_data_t*)user_data_ptr; - IREE_TRACE_ZONE_BEGIN(z0); - - switch (status) { - case WGPURequestAdapterStatus_Success: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "success"); - data->status = iree_ok_status(); - data->handle = received; - break; - case WGPURequestAdapterStatus_Unavailable: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "unavailable"); - data->status = - iree_make_status(IREE_STATUS_UNAVAILABLE, - "wgpuInstanceRequestAdapter failed: %s", message); - break; - case WGPURequestAdapterStatus_Error: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "error"); - data->status = - iree_make_status(IREE_STATUS_INTERNAL, - "wgpuInstanceRequestAdapter failed: %s", message); - break; - default: - case WGPURequestAdapterStatus_Unknown: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "unknown"); - data->status = - iree_make_status(IREE_STATUS_UNKNOWN, - "wgpuInstanceRequestAdapter failed: %s", message); - break; - } - - IREE_TRACE_ZONE_END(z0); -} - -typedef struct iree_hal_webgpu_native_device_data_t { - iree_status_t status; - WGPUDevice handle; -} iree_hal_webgpu_native_device_data_t; -static void iree_hal_webgpu_native_request_device_callback( - WGPURequestDeviceStatus status, WGPUDevice received, const char* message, - void* user_data_ptr) { - iree_hal_webgpu_native_device_data_t* data = - (iree_hal_webgpu_native_device_data_t*)user_data_ptr; - IREE_TRACE_ZONE_BEGIN(z0); - - switch (status) { - case WGPURequestDeviceStatus_Success: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "success"); - data->status = iree_ok_status(); - data->handle = received; - break; - case WGPURequestDeviceStatus_Error: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "error"); - data->status = iree_make_status( - IREE_STATUS_INTERNAL, "wgpuAdapterRequestDevice failed: %s", message); - break; - default: - case WGPURequestDeviceStatus_Unknown: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "unknown"); - data->status = iree_make_status( - IREE_STATUS_UNKNOWN, "wgpuAdapterRequestDevice failed: %s", message); - break; - } - - IREE_TRACE_ZONE_END(z0); -} -*/ - -//===----------------------------------------------------------------------===// -// iree_hal_webgpu_native_driver_t -//===----------------------------------------------------------------------===// - -typedef struct iree_hal_webgpu_native_driver_t { - iree_hal_resource_t resource; - iree_allocator_t host_allocator; - - iree_string_view_t identifier; - iree_hal_webgpu_device_options_t default_options; - - WGPUInstance instance; - WGPUAdapter adapter; - dawn_native::Instance* dawn_instance; - dawn_native::Adapter* dawn_adapter; -} iree_hal_webgpu_native_driver_t; - -extern const iree_hal_driver_vtable_t iree_hal_webgpu_native_driver_vtable; - -static iree_hal_webgpu_native_driver_t* iree_hal_webgpu_native_driver_cast( - iree_hal_driver_t* base_value) { - IREE_HAL_ASSERT_TYPE(base_value, &iree_hal_webgpu_native_driver_vtable); - return (iree_hal_webgpu_native_driver_t*)base_value; -} - -iree_status_t iree_hal_webgpu_native_driver_create( - iree_string_view_t identifier, - const iree_hal_webgpu_driver_options_t* options, - iree_allocator_t host_allocator, iree_hal_driver_t** out_driver) { - IREE_ASSERT_ARGUMENT(options); - IREE_ASSERT_ARGUMENT(out_driver); - *out_driver = NULL; - IREE_TRACE_ZONE_BEGIN(z0); - - iree_hal_webgpu_native_driver_t* driver = NULL; - iree_host_size_t total_size = sizeof(*driver) + identifier.size + /*NUL=*/1; - IREE_RETURN_AND_END_ZONE_IF_ERROR( - z0, iree_allocator_malloc(host_allocator, total_size, (void**)&driver)); - iree_hal_resource_initialize(&iree_hal_webgpu_native_driver_vtable, - &driver->resource); - driver->host_allocator = host_allocator; - - iree_string_view_append_to_buffer(identifier, &driver->identifier, - (char*)driver + sizeof(*driver)); - memcpy(&driver->default_options, &options->device_options, - sizeof(driver->default_options)); - - // Dawn requires this to happen before we can call any WebGPU function, - // including wgpuCreateInstance. Hopefully that changes. - auto procs = dawn_native::GetProcs(); - dawnProcSetProcs(&procs); - - WGPUInstanceDescriptor instance_descriptor = {0}; - driver->instance = wgpuCreateInstance(&instance_descriptor); - if (!driver->instance) { - iree_hal_driver_release((iree_hal_driver_t*)driver); - IREE_TRACE_ZONE_END(z0); - return iree_make_status( - IREE_STATUS_UNAVAILABLE, - "WebGPU implementation not present or failed to load"); - } - - auto instance = std::make_unique(); - instance->DiscoverDefaultAdapters(); - std::vector adapters = instance->GetAdapters(); - dawn_native::Adapter chosenAdapter; - for (dawn_native::Adapter& adapter : adapters) { - wgpu::AdapterProperties properties; - adapter.GetProperties(&properties); - if (properties.backendType != wgpu::BackendType::Null) { - chosenAdapter = adapter; - break; - } - } - - driver->adapter = (WGPUAdapter)((void*)0x1u); // HACK - driver->dawn_instance = instance.release(); - driver->dawn_adapter = new dawn_native::Adapter(chosenAdapter); - - // Request an adapter from the implementation. We only get one of these and it - // may expose multiple devices so it's effectively what we consider a driver. - // DO NOT SUBMIT - // WGPURequestAdapterOptions adapter_options = {0}; - // adapter_options.powerPreference = options->power_preference; - // iree_hal_webgpu_native_adapter_data_t adapter_data; - // memset(&adapter_data, 0, sizeof(adapter_data)); - // wgpuInstanceRequestAdapter(driver->instance, &adapter_options, - // iree_hal_webgpu_native_request_adapter_callback, - // (void*)&adapter_data); - // if (iree_status_is_ok(adapter_data.status)) { - // IREE_ASSERT_NE(adapter_data.handle, NULL); - // driver->adapter = adapter_data.handle; - - // WGPUAdapterProperties adapter_props; - // memset(&adapter_props, 0, sizeof(adapter_props)); - // wgpuAdapterGetProperties(adapter_data.handle, &adapter_props); - - *out_driver = (iree_hal_driver_t*)driver; - // } else { - // iree_hal_driver_release((iree_hal_driver_t*)driver); - // } - - IREE_TRACE_ZONE_END(z0); - // return adapter_data.status; - return iree_ok_status(); -} - -static void iree_hal_webgpu_native_driver_destroy( - iree_hal_driver_t* base_driver) { - iree_hal_webgpu_native_driver_t* driver = - iree_hal_webgpu_native_driver_cast(base_driver); - iree_allocator_t host_allocator = driver->host_allocator; - IREE_TRACE_ZONE_BEGIN(z0); - - // NOTE: there's no wgpu-native teardown for adapters and instances. - driver->adapter = NULL; - driver->instance = NULL; - delete driver->dawn_adapter; - delete driver->dawn_instance; - - iree_allocator_free(host_allocator, driver); - - IREE_TRACE_ZONE_END(z0); -} - -static iree_status_t iree_hal_webgpu_native_driver_query_available_devices( - iree_hal_driver_t* base_driver, iree_allocator_t allocator, - iree_hal_device_info_t** out_device_infos, - iree_host_size_t* out_device_info_count) { - // Unfortunately no queries in WebGPU; we can only request a single device. - static const iree_hal_device_info_t device_infos[1] = { - { - IREE_HAL_WEBGPU_DEVICE_ID_DEFAULT, - {"default", IREE_ARRAYSIZE("default")}, - }, - }; - *out_device_info_count = IREE_ARRAYSIZE(device_infos); - return iree_allocator_clone( - allocator, iree_make_const_byte_span(device_infos, sizeof(device_infos)), - (void**)out_device_infos); -} - -static iree_status_t iree_hal_webgpu_native_driver_create_device( - iree_hal_driver_t* base_driver, iree_hal_device_id_t device_id, - iree_allocator_t host_allocator, iree_hal_device_t** out_device) { - iree_hal_webgpu_native_driver_t* driver = - iree_hal_webgpu_native_driver_cast(base_driver); - - WGPURequiredLimits required_limits = {0}; - required_limits.limits.maxBindGroups = 4; - required_limits.limits.maxStorageBuffersPerShaderStage = 8; - // WGPUDeviceDescriptor device_descriptor = {0}; - // device_descriptor.requiredLimits = &required_limits; - // iree_hal_webgpu_native_device_data_t device_data; - // memset(&device_data, 0, sizeof(device_data)); - // wgpuAdapterRequestDevice(driver->adapter, &device_descriptor, - // iree_hal_webgpu_native_request_device_callback, - // (void*)&device_data); - // IREE_RETURN_IF_ERROR(device_data.status); - // IREE_ASSERT_NE(device_data.handle, NULL); - - WGPUDevice handle = driver->dawn_adapter->CreateDevice(); - auto device = wgpu::Device::Acquire(handle); - device.SetUncapturedErrorCallback( - [](WGPUErrorType error_type, const char* message, void*) { - const char* error_type_name = ""; - switch (error_type) { - case WGPUErrorType_Validation: - error_type_name = "Validation"; - break; - case WGPUErrorType_OutOfMemory: - error_type_name = "Out of memory"; - break; - default: - case WGPUErrorType_Unknown: - error_type_name = "Unknown"; - break; - case WGPUErrorType_DeviceLost: - error_type_name = "Device lost"; - break; - } - fprintf(stderr, "[DAWN %s] %s", error_type_name, message); - }, - nullptr); - device.Release(); - - return iree_hal_webgpu_wrap_device(driver->identifier, - &driver->default_options, handle, - driver->host_allocator, out_device); -} - -static const iree_hal_driver_vtable_t iree_hal_webgpu_native_driver_vtable = { - /*.destroy=*/iree_hal_webgpu_native_driver_destroy, - /*.query_available_devices=*/ - iree_hal_webgpu_native_driver_query_available_devices, - /*.create_device=*/iree_hal_webgpu_native_driver_create_device, -}; - -} // extern "C" diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver_wgpu.c b/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver_wgpu.c deleted file mode 100644 index 11a8e321ee8b..000000000000 --- a/runtime/src/iree/hal/drivers/webgpu/platform/native/native_driver_wgpu.c +++ /dev/null @@ -1,292 +0,0 @@ -// Copyright 2021 The IREE Authors -// -// Licensed under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include // wgpu-native implementation only - -#include "iree/base/tracing.h" -#include "iree/hal/drivers/webgpu/platform/native/native_driver.h" - -#define IREE_HAL_WEBGPU_DEVICE_ID_DEFAULT 0 - -//===----------------------------------------------------------------------===// -// wgpu-native callbacks -//===----------------------------------------------------------------------===// - -static void iree_hal_webgpu_native_log_callback(WGPULogLevel level, - const char* message) { - fprintf(stderr, "WGPU: %s", message); -} - -static void iree_hal_webgpu_native_setup_logging( - iree_hal_webgpu_driver_log_level_t log_level) { - WGPULogLevel wgpu_log_level = WGPULogLevel_Off; - switch (log_level) { - default: - case IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_OFF: - wgpu_log_level = WGPULogLevel_Off; - break; - case IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_ERROR: - wgpu_log_level = WGPULogLevel_Error; - break; - case IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_WARNING: - wgpu_log_level = WGPULogLevel_Warn; - break; - case IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_INFO: - wgpu_log_level = WGPULogLevel_Info; - break; - case IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_DEBUG: - wgpu_log_level = WGPULogLevel_Debug; - break; - case IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_TRACE: - wgpu_log_level = WGPULogLevel_Trace; - break; - } - if (wgpu_log_level == WGPULogLevel_Off) return; - wgpuSetLogCallback(iree_hal_webgpu_native_log_callback); - wgpuSetLogLevel(wgpu_log_level); -} - -typedef struct iree_hal_webgpu_native_adapter_data_t { - iree_status_t status; - WGPUAdapter handle; -} iree_hal_webgpu_native_adapter_data_t; -static void iree_hal_webgpu_native_request_adapter_callback( - WGPURequestAdapterStatus status, WGPUAdapter received, const char* message, - void* user_data_ptr) { - iree_hal_webgpu_native_adapter_data_t* data = - (iree_hal_webgpu_native_adapter_data_t*)user_data_ptr; - IREE_TRACE_ZONE_BEGIN(z0); - - switch (status) { - case WGPURequestAdapterStatus_Success: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "success"); - data->status = iree_ok_status(); - data->handle = received; - break; - case WGPURequestAdapterStatus_Unavailable: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "unavailable"); - data->status = - iree_make_status(IREE_STATUS_UNAVAILABLE, - "wgpuInstanceRequestAdapter failed: %s", message); - break; - case WGPURequestAdapterStatus_Error: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "error"); - data->status = - iree_make_status(IREE_STATUS_INTERNAL, - "wgpuInstanceRequestAdapter failed: %s", message); - break; - default: - case WGPURequestAdapterStatus_Unknown: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "unknown"); - data->status = - iree_make_status(IREE_STATUS_UNKNOWN, - "wgpuInstanceRequestAdapter failed: %s", message); - break; - } - - IREE_TRACE_ZONE_END(z0); -} - -typedef struct iree_hal_webgpu_native_device_data_t { - iree_status_t status; - WGPUDevice handle; -} iree_hal_webgpu_native_device_data_t; -static void iree_hal_webgpu_native_request_device_callback( - WGPURequestDeviceStatus status, WGPUDevice received, const char* message, - void* user_data_ptr) { - iree_hal_webgpu_native_device_data_t* data = - (iree_hal_webgpu_native_device_data_t*)user_data_ptr; - IREE_TRACE_ZONE_BEGIN(z0); - - switch (status) { - case WGPURequestDeviceStatus_Success: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "success"); - data->status = iree_ok_status(); - data->handle = received; - break; - case WGPURequestDeviceStatus_Error: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "error"); - data->status = iree_make_status( - IREE_STATUS_INTERNAL, "wgpuAdapterRequestDevice failed: %s", message); - break; - default: - case WGPURequestDeviceStatus_Unknown: - IREE_TRACE_ZONE_APPEND_TEXT_CSTRING(z0, "unknown"); - data->status = iree_make_status( - IREE_STATUS_UNKNOWN, "wgpuAdapterRequestDevice failed: %s", message); - break; - } - - IREE_TRACE_ZONE_END(z0); -} - -//===----------------------------------------------------------------------===// -// iree_hal_webgpu_native_driver_t -//===----------------------------------------------------------------------===// - -typedef struct iree_hal_webgpu_native_driver_t { - iree_hal_resource_t resource; - iree_allocator_t host_allocator; - - iree_string_view_t identifier; - iree_hal_webgpu_device_options_t default_options; - - WGPUInstance instance; - WGPUAdapter adapter; -} iree_hal_webgpu_native_driver_t; - -static const iree_hal_driver_vtable_t iree_hal_webgpu_native_driver_vtable; - -static iree_hal_webgpu_native_driver_t* iree_hal_webgpu_native_driver_cast( - iree_hal_driver_t* base_value) { - IREE_HAL_ASSERT_TYPE(base_value, &iree_hal_webgpu_native_driver_vtable); - return (iree_hal_webgpu_native_driver_t*)base_value; -} - -iree_status_t iree_hal_webgpu_native_driver_create( - iree_string_view_t identifier, - const iree_hal_webgpu_driver_options_t* options, - iree_allocator_t host_allocator, iree_hal_driver_t** out_driver) { - IREE_ASSERT_ARGUMENT(options); - IREE_ASSERT_ARGUMENT(out_driver); - *out_driver = NULL; - IREE_TRACE_ZONE_BEGIN(z0); - - iree_hal_webgpu_native_driver_t* driver = NULL; - iree_host_size_t total_size = sizeof(*driver) + identifier.size + /*NUL=*/1; - IREE_RETURN_AND_END_ZONE_IF_ERROR( - z0, iree_allocator_malloc(host_allocator, total_size, (void**)&driver)); - iree_hal_resource_initialize(&iree_hal_webgpu_native_driver_vtable, - &driver->resource); - driver->host_allocator = host_allocator; - - iree_string_view_append_to_buffer(identifier, &driver->identifier, - (char*)driver + sizeof(*driver)); - memcpy(&driver->default_options, &options->device_options, - sizeof(driver->default_options)); - - // wgpu-native doesn't currently expose (or need) this. Other implementations - // may and they may require it in the future. - const WGPUInstanceDescriptor instance_descriptor = { - .nextInChain = NULL, - }; - driver->instance = wgpuCreateInstance(&instance_descriptor); - if (!driver->instance) { - iree_hal_driver_release((iree_hal_driver_t*)driver); - IREE_TRACE_ZONE_END(z0); - return iree_make_status( - IREE_STATUS_UNAVAILABLE, - "WebGPU implementation not present or failed to load"); - } - - // Setup logging first as the adapter queries and such we perform immediately - // after this may log things. - iree_hal_webgpu_native_setup_logging(options->log_level); - - // Request an adapter from the implementation. We only get one of these and it - // may expose multiple devices so it's effectively what we consider a driver. - const WGPURequestAdapterOptions adapter_options = { - .nextInChain = NULL, - .compatibleSurface = NULL, - .powerPreference = options->power_preference, - .forceFallbackAdapter = false, - }; - iree_hal_webgpu_native_adapter_data_t adapter_data; - memset(&adapter_data, 0, sizeof(adapter_data)); - wgpuInstanceRequestAdapter(driver->instance, &adapter_options, - iree_hal_webgpu_native_request_adapter_callback, - (void*)&adapter_data); - if (iree_status_is_ok(adapter_data.status)) { - IREE_ASSERT_NE(adapter_data.handle, NULL); - driver->adapter = adapter_data.handle; - - WGPUAdapterProperties adapter_props; - memset(&adapter_props, 0, sizeof(adapter_props)); - wgpuAdapterGetProperties(adapter_data.handle, &adapter_props); - - *out_driver = (iree_hal_driver_t*)driver; - } else { - iree_hal_driver_release((iree_hal_driver_t*)driver); - } - - IREE_TRACE_ZONE_END(z0); - return adapter_data.status; -} - -static void iree_hal_webgpu_native_driver_destroy( - iree_hal_driver_t* base_driver) { - iree_hal_webgpu_native_driver_t* driver = - iree_hal_webgpu_native_driver_cast(base_driver); - iree_allocator_t host_allocator = driver->host_allocator; - IREE_TRACE_ZONE_BEGIN(z0); - - // NOTE: there's no wgpu-native teardown for adapters and instances. - driver->adapter = NULL; - driver->instance = NULL; - - iree_allocator_free(host_allocator, driver); - - IREE_TRACE_ZONE_END(z0); -} - -static iree_status_t iree_hal_webgpu_native_driver_query_available_devices( - iree_hal_driver_t* base_driver, iree_allocator_t allocator, - iree_hal_device_info_t** out_device_infos, - iree_host_size_t* out_device_info_count) { - // Unfortunately no queries in WebGPU; we can only request a single device. - static const iree_hal_device_info_t device_infos[1] = { - { - .device_id = IREE_HAL_WEBGPU_DEVICE_ID_DEFAULT, - .name = iree_string_view_literal("default"), - }, - }; - *out_device_info_count = IREE_ARRAYSIZE(device_infos); - return iree_allocator_clone( - allocator, iree_make_const_byte_span(device_infos, sizeof(device_infos)), - (void**)out_device_infos); -} - -static iree_status_t iree_hal_webgpu_native_driver_create_device( - iree_hal_driver_t* base_driver, iree_hal_device_id_t device_id, - iree_allocator_t host_allocator, iree_hal_device_t** out_device) { - iree_hal_webgpu_native_driver_t* driver = - iree_hal_webgpu_native_driver_cast(base_driver); - - const WGPURequiredLimits required_limits = { - .nextInChain = NULL, - .limits = - { - .maxBindGroups = 4, - .maxStorageBuffersPerShaderStage = 8, - }, - }; - const WGPUDeviceDescriptor device_descriptor = { - .nextInChain = NULL, - // TODO(benvanik): pull from NUL terminated driver storage. - // .label = device->identifier, - .requiredFeaturesCount = 0, - .requiredFeatures = NULL, - .requiredLimits = &required_limits, - }; - iree_hal_webgpu_native_device_data_t device_data; - memset(&device_data, 0, sizeof(device_data)); - wgpuAdapterRequestDevice(driver->adapter, &device_descriptor, - iree_hal_webgpu_native_request_device_callback, - (void*)&device_data); - IREE_RETURN_IF_ERROR(device_data.status); - IREE_ASSERT_NE(device_data.handle, NULL); - - return iree_hal_webgpu_wrap_device( - driver->identifier, &driver->default_options, device_data.handle, - driver->host_allocator, out_device); -} - -static const iree_hal_driver_vtable_t iree_hal_webgpu_native_driver_vtable = { - .destroy = iree_hal_webgpu_native_driver_destroy, - .query_available_devices = - iree_hal_webgpu_native_driver_query_available_devices, - .create_device = iree_hal_webgpu_native_driver_create_device, -}; diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/native/wgpu_util.c b/runtime/src/iree/hal/drivers/webgpu/platform/native/wgpu_util.c deleted file mode 100644 index 270f5bfe32d9..000000000000 --- a/runtime/src/iree/hal/drivers/webgpu/platform/native/wgpu_util.c +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2021 The IREE Authors -// -// Licensed under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include // wgpu-native implementation only - -#include "iree/hal/drivers/webgpu/platform/webgpu.h" - -//===----------------------------------------------------------------------===// -// Implementation compatibility layer -//===----------------------------------------------------------------------===// - -#define WGPU_NATIVE_INSTANCE ((WGPUInstance)((uintptr_t)0xABADD00Du)) - -WGPUInstance wgpuCreateInstance(WGPUInstanceDescriptor const* descriptor) { - // wgpu-native does not have instances and just uses globals for everything :( - // We use a sentinel value here so that we can do null checks in places for - // implementations that do use instances. - return WGPU_NATIVE_INSTANCE; -} - -void wgpuDeviceDestroy(WGPUDevice device) { - // wgpu-native does not export this but does have a drop to deref. - wgpuDeviceDrop(device); -} - -void const* wgpuBufferGetConstMappedRange(WGPUBuffer buffer, size_t offset, - size_t size) { - // wgpu-native doesn't have this, for some reason. - return wgpuBufferGetMappedRange(buffer, offset, size); -} - -void wgpuCommandEncoderPopDebugGroup(WGPUCommandEncoder commandEncoder) { - // No-op; wgpu-native does not export this symbol. -} - -void wgpuCommandEncoderPushDebugGroup(WGPUCommandEncoder commandEncoder, - char const* groupLabel) { - // No-op; wgpu-native does not export this symbol. -} - -void iree_wgpuBindGroupDrop(WGPUBindGroup bindGroup) { - wgpuBindGroupDrop(bindGroup); -} - -void iree_wgpuBindGroupLayoutDrop(WGPUBindGroupLayout bindGroupLayout) { - wgpuBindGroupLayoutDrop(bindGroupLayout); -} - -void iree_wgpuBufferDrop(WGPUBuffer buffer) { wgpuBufferDrop(buffer); } - -void iree_wgpuCommandBufferDrop(WGPUCommandBuffer commandBuffer) { - // wgpu-native crashes if this method is called. I have no idea why - rust... - // wgpuCommandBufferDrop(commandBuffer); -} - -void iree_wgpuCommandEncoderDrop(WGPUCommandEncoder commandEncoder) { - wgpuCommandEncoderDrop(commandEncoder); -} - -void iree_wgpuComputePipelineDrop(WGPUComputePipeline computePipeline) { - wgpuComputePipelineDrop(computePipeline); -} - -void iree_wgpuPipelineLayoutDrop(WGPUPipelineLayout pipelineLayout) { - wgpuPipelineLayoutDrop(pipelineLayout); -} - -void iree_wgpuQuerySetDrop(WGPUQuerySet querySet) { - wgpuQuerySetDrop(querySet); -} - -void iree_wgpuShaderModuleDrop(WGPUShaderModule shaderModule) { - wgpuShaderModuleDrop(shaderModule); -} - -//===----------------------------------------------------------------------===// -// Speculative WebGPU API additions -//===----------------------------------------------------------------------===// - -static void iree_hal_webgpu_buffer_map_sync_callback( - WGPUBufferMapAsyncStatus status, void* userdata) { - IREEWGPUBufferMapSyncStatus* sync_status = - (IREEWGPUBufferMapSyncStatus*)userdata; - switch (status) { - case WGPUBufferMapAsyncStatus_Success: - *sync_status = IREEWGPUBufferMapAsyncStatus_Success; - break; - case WGPUBufferMapAsyncStatus_Error: - *sync_status = IREEWGPUBufferMapAsyncStatus_Error; - break; - default: - case WGPUBufferMapAsyncStatus_Unknown: - *sync_status = IREEWGPUBufferMapAsyncStatus_Unknown; - break; - case WGPUBufferMapAsyncStatus_DeviceLost: - *sync_status = IREEWGPUBufferMapAsyncStatus_DeviceLost; - break; - } -} - -IREEWGPUBufferMapSyncStatus iree_wgpuBufferMapSync(WGPUDevice device, - WGPUBuffer buffer, - WGPUMapModeFlags mode, - size_t offset, size_t size) { - IREEWGPUBufferMapSyncStatus status = IREEWGPUBufferMapSyncStatus_Unknown; - wgpuBufferMapAsync(buffer, mode, offset, size, - iree_hal_webgpu_buffer_map_sync_callback, &status); - wgpuDevicePoll(device, /*force_wait=*/true); - return status; -} diff --git a/runtime/src/iree/hal/drivers/webgpu/platform/webgpu.h b/runtime/src/iree/hal/drivers/webgpu/platform/webgpu.h index 1ee07102592c..e48d72ec34aa 100644 --- a/runtime/src/iree/hal/drivers/webgpu/platform/webgpu.h +++ b/runtime/src/iree/hal/drivers/webgpu/platform/webgpu.h @@ -11,16 +11,11 @@ #include "iree/hal/api.h" #define WGPU_SKIP_PROCS 1 -#if defined(IREE_HAL_WEBGPU_PLATFORM_WGPU_NATIVE) -#include -#include // wgpu-native implementation only -#elif defined(IREE_HAL_WEBGPU_PLATFORM_DAWN) -#include -#elif defined(__EMSCRIPTEN__) +#if defined(__EMSCRIPTEN__) #include #else #include "third_party/webgpu-headers/webgpu.h" // IWYU pragma: export -#endif // IREE_HAL_WEBGPU_PLATFORM_WGPU_NATIVE +#endif #ifdef __cplusplus extern "C" { diff --git a/runtime/src/iree/hal/drivers/webgpu/registration/driver_module_native.c b/runtime/src/iree/hal/drivers/webgpu/registration/driver_module_native.c index ebb5265fa639..ef23d5980cc5 100644 --- a/runtime/src/iree/hal/drivers/webgpu/registration/driver_module_native.c +++ b/runtime/src/iree/hal/drivers/webgpu/registration/driver_module_native.c @@ -4,104 +4,26 @@ // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include -#include - #include "iree/base/api.h" -#include "iree/base/internal/flags.h" #include "iree/hal/drivers/webgpu/platform/native/native_driver.h" #include "iree/hal/drivers/webgpu/registration/driver_module.h" // TODO(#4298): remove this driver registration and wrapper. -#define IREE_HAL_WEBGPU_DRIVER_ID 0x57475055u // WGPU - -IREE_FLAG(string, webgpu_backend, "", - "[any, d3d12, metal, vulkan]; indicates a preference for the WebGPU " - "backend implementation to use, if possible."); - -IREE_FLAG(string, webgpu_log_level, "warning", - "[off, error, warning, info, debug, trace]; controls the verbosity " - "level of the logging from the WebGPU " - "implementation."); - -IREE_FLAG(string, webgpu_power_preference, "high-performance", - "[(empty), low-power, high-performance]; biases adapter selection " - "based on the assumed power usage of the device."); - static iree_status_t iree_hal_webgpu_native_driver_factory_enumerate( void* self, const iree_hal_driver_info_t** out_driver_infos, iree_host_size_t* out_driver_info_count) { - static const iree_hal_driver_info_t driver_infos[1] = { - { - .driver_id = IREE_HAL_WEBGPU_DRIVER_ID, - .driver_name = iree_string_view_literal("webgpu"), - .full_name = iree_string_view_literal("Experimental WebGPU"), - }, - }; - *out_driver_info_count = IREE_ARRAYSIZE(driver_infos); - *out_driver_infos = driver_infos; - return iree_ok_status(); + return iree_make_status( + IREE_STATUS_UNIMPLEMENTED, + "WebGPU native driver is only for testing compilation"); } static iree_status_t iree_hal_webgpu_native_driver_factory_try_create( void* self, iree_hal_driver_id_t driver_id, iree_allocator_t host_allocator, iree_hal_driver_t** out_driver) { - if (driver_id != IREE_HAL_WEBGPU_DRIVER_ID) { - return iree_make_status(IREE_STATUS_UNAVAILABLE, - "no driver with ID %016" PRIu64 - " is provided by this factory", - driver_id); - } - - iree_hal_webgpu_driver_options_t options; - iree_hal_webgpu_driver_options_initialize(&options); - - // Let the user override the backend that will be used or choose based on - // platform; if on Windows we want D3D12 and on Apple OS' we want Metal. - if (strcmp(FLAG_webgpu_backend, "d3d12") == 0) { - options.backend_preference = IREE_HAL_WEBGPU_DRIVER_BACKEND_D3D12; - } else if (strcmp(FLAG_webgpu_backend, "metal") == 0) { - options.backend_preference = IREE_HAL_WEBGPU_DRIVER_BACKEND_METAL; - } else if (strcmp(FLAG_webgpu_backend, "vulkan") == 0) { - options.backend_preference = IREE_HAL_WEBGPU_DRIVER_BACKEND_VULKAN; - } else { -#if defined(IREE_PLATFORM_WINDOWS) - options.backend_preference = IREE_HAL_WEBGPU_DRIVER_BACKEND_D3D12; -#elif defined(IREE_PLATFORM_APPLE) - options.backend_preference = IREE_HAL_WEBGPU_DRIVER_BACKEND_METAL; -#else - options.backend_preference = IREE_HAL_WEBGPU_DRIVER_BACKEND_VULKAN; -#endif - } - - if (strcmp(FLAG_webgpu_log_level, "error") == 0) { - options.log_level = IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_ERROR; - } else if (strcmp(FLAG_webgpu_log_level, "warning") == 0) { - options.log_level = IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_WARNING; - } else if (strcmp(FLAG_webgpu_log_level, "info") == 0) { - options.log_level = IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_INFO; - } else if (strcmp(FLAG_webgpu_log_level, "debug") == 0) { - options.log_level = IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_DEBUG; - } else if (strcmp(FLAG_webgpu_log_level, "trace") == 0) { - options.log_level = IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_TRACE; - } else { - options.log_level = IREE_HAL_WEBGPU_DRIVER_LOG_LEVEL_OFF; - } - - if (strcmp(FLAG_webgpu_power_preference, "low-power") == 0) { - options.power_preference = WGPUPowerPreference_LowPower; - } else if (strcmp(FLAG_webgpu_power_preference, "high-performance") == 0) { - options.power_preference = WGPUPowerPreference_HighPerformance; - } else { - // TODO(benvanik): coming in future spec update. For now go high-perf. - // options.power_preference = WGPUPowerPreference_Undefined; - options.power_preference = WGPUPowerPreference_HighPerformance; - } - - return iree_hal_webgpu_native_driver_create( - iree_make_cstring_view("webgpu-native"), &options, host_allocator, - out_driver); + return iree_make_status( + IREE_STATUS_UNIMPLEMENTED, + "WebGPU native driver is only for testing compilation"); } IREE_API_EXPORT iree_status_t diff --git a/third_party/webgpu-headers b/third_party/webgpu-headers index 402ee247cfbd..fa1c6ab4927e 160000 --- a/third_party/webgpu-headers +++ b/third_party/webgpu-headers @@ -1 +1 @@ -Subproject commit 402ee247cfbd9a65673e0f9141d80ccf38e79959 +Subproject commit fa1c6ab4927ef1fa5731907b42b62ea93119866c