Skip to content

Commit 3ed55b4

Browse files
authored
Move dependencies/wasm to use sites (#8377)
Also replace WITH_WABT and WITH_V8 with Halide_WASM_BACKEND, which can be either wabt, V8, or a CMake false-y value such as OFF. Deprecation notices are provided to ease user transitions.
1 parent 8feee81 commit 3ed55b4

File tree

6 files changed

+171
-171
lines changed

6 files changed

+171
-171
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ option(THREADS_PREFER_PTHREAD_FLAG "When enabled, prefer to use the -pthread fla
188188
find_package(Threads REQUIRED)
189189

190190
## Complex dependencies
191-
add_subdirectory(dependencies)
191+
add_subdirectory(dependencies/llvm)
192192

193193
## Image formats
194194

README_cmake.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,9 @@ The following options enable/disable various Halide-specific backends:
458458
The following options are WebAssembly-specific. They only apply when
459459
`TARGET_WEBASSEMBLY=ON`:
460460

461-
| Option | Default | Description |
462-
|-------------|---------|-------------------------------------------|
463-
| `WITH_WABT` | `ON` | Include WABT Interpreter for WASM testing |
461+
| Option | Default | Description |
462+
|-----------------------|---------|------------------------------------------------------------------------------------------|
463+
| `Halide_WASM_BACKEND` | `wabt` | Select the backend for WASM testing. Can be `wabt`, `V8` or a false value such as `OFF`. |
464464

465465
### Find module options
466466

dependencies/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

dependencies/wasm/CMakeLists.txt

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,80 @@ target_compile_definitions(Halide PUBLIC
562562
HALIDE_VERSION_MINOR=${Halide_VERSION_MINOR}
563563
HALIDE_VERSION_PATCH=${Halide_VERSION_PATCH})
564564

565-
if (TARGET Halide_wabt)
566-
target_link_libraries(Halide PRIVATE Halide_wabt)
567-
target_compile_definitions(Halide PRIVATE WITH_WABT)
568-
endif ()
565+
##
566+
# WasmExecutor backend selection
567+
##
568+
569+
if (TARGET_WEBASSEMBLY)
570+
include(FetchContent)
571+
572+
set(Halide_WASM_BACKEND "wabt"
573+
CACHE STRING "Which backend to use for Halide's WASM testing.")
574+
set_property(CACHE Halide_WASM_BACKEND PROPERTY STRINGS "wabt;V8;OFF")
575+
576+
if (WITH_WABT AND NOT WITH_V8)
577+
message(DEPRECATION "WITH_WABT has been replaced by Halide_WASM_BACKEND=\"wabt\"")
578+
set(Halide_WASM_BACKEND "wabt")
579+
elseif (NOT WITH_WABT AND WITH_V8)
580+
message(DEPRECATION "WITH_V8 has been replaced by Halide_WASM_BACKEND=\"V8\"")
581+
set(Halide_WASM_BACKEND "V8")
582+
elseif (WITH_WABT AND WITH_V8)
583+
message(FATAL_ERROR "Cannot use both WABT and V8 at the same time, disable one of them.")
584+
elseif (DEFINED WITH_WABT AND DEFINED WITH_V8 AND NOT WITH_WABT AND NOT WITH_V8)
585+
message(DEPRECATION "Disabling both WITH_WABT and WITH_V8 has been replaced by Halide_WASM_BACKEND=\"OFF\"")
586+
set(Halide_WASM_BACKEND "OFF")
587+
endif ()
588+
589+
if (MSVC AND Halide_WASM_BACKEND STREQUAL "wabt")
590+
message(WARNING "wabt is not yet supported on Windows")
591+
set(Halide_WASM_BACKEND "OFF")
592+
endif ()
569593

570-
if (TARGET V8::V8)
571-
target_link_libraries(Halide PRIVATE V8::V8)
572-
target_compile_definitions(Halide PRIVATE WITH_V8)
594+
if (Halide_WASM_BACKEND STREQUAL "wabt")
595+
set(WABT_VER 1.0.33)
596+
597+
message(STATUS "Fetching WABT ${WABT_VER}...")
598+
FetchContent_Declare(wabt
599+
GIT_REPOSITORY https://github.com/WebAssembly/wabt.git
600+
GIT_TAG ${WABT_VER}
601+
GIT_SHALLOW TRUE)
602+
603+
# configuration for wabt
604+
set(WITH_EXCEPTIONS ${Halide_ENABLE_EXCEPTIONS})
605+
set(BUILD_TESTS OFF)
606+
set(BUILD_TOOLS OFF)
607+
set(BUILD_LIBWASM OFF)
608+
set(USE_INTERNAL_SHA256 ON)
609+
FetchContent_MakeAvailable(wabt)
610+
611+
set_target_properties(wabt PROPERTIES POSITION_INDEPENDENT_CODE ON)
612+
613+
# Disable this very-noisy warning in GCC
614+
target_compile_options(wabt
615+
PRIVATE
616+
$<$<CXX_COMPILER_ID:GNU>:-Wno-alloca-larger-than>)
617+
618+
# TODO: we want to require unique prefixes to include these files, to avoid ambiguity;
619+
# this means we have to prefix with "wabt-src/...", which is less bad than other alternatives,
620+
# but perhaps we could do better (esp. if wabt was smarter about what it exposed?)
621+
add_library(Halide_wabt INTERFACE)
622+
target_sources(Halide_wabt INTERFACE $<BUILD_LOCAL_INTERFACE:$<TARGET_OBJECTS:wabt>>)
623+
target_include_directories(Halide_wabt
624+
SYSTEM # Use -isystem instead of -I; this is a trick so that clang-tidy won't analyze these includes
625+
INTERFACE
626+
$<BUILD_INTERFACE:${wabt_SOURCE_DIR}>/include
627+
$<BUILD_INTERFACE:${wabt_BINARY_DIR}>/include)
628+
set_target_properties(Halide_wabt PROPERTIES EXPORT_NAME wabt)
629+
630+
target_link_libraries(Halide PRIVATE Halide_wabt)
631+
target_compile_definitions(Halide PRIVATE WITH_WABT)
632+
elseif (Halide_WASM_BACKEND STREQUAL "V8")
633+
find_package(V8 REQUIRED)
634+
target_link_libraries(Halide PRIVATE V8::V8)
635+
target_compile_definitions(Halide PRIVATE WITH_V8)
636+
elseif (Halide_WASM_BACKEND)
637+
message(FATAL_ERROR "Unknown Halide_WASM_BACKEND `${Halide_WASM_BACKEND}`")
638+
endif ()
573639
endif ()
574640

575641
##

test/generator/CMakeLists.txt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,100 @@ else()
1212
set(_USING_WASM 0)
1313
endif()
1414

15+
function(add_wasm_executable TARGET)
16+
set(options)
17+
set(oneValueArgs)
18+
set(multiValueArgs SRCS DEPS INCLUDES OPTIONS ENABLE_IF)
19+
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
20+
21+
if (args_ENABLE_IF AND NOT (${args_ENABLE_IF}))
22+
return()
23+
endif ()
24+
25+
# Conceptually, we want something like this:
26+
# add_executable(${TARGET} ${args_SRCS})
27+
# if (args_INCLUDES)
28+
# target_include_directories("${TARGET}" PRIVATE ${args_INCLUDES})
29+
# endif()
30+
# if (args_DEPS)
31+
# target_link_libraries(${TARGET} PRIVATE ${args_DEPS})
32+
# endif ()
33+
34+
find_program(EMCC emcc REQUIRED HINTS "$ENV{EMSDK}/upstream/emscripten")
35+
36+
# TODO: this is currently hardcoded to settings that are sensible for most of Halide's
37+
# internal purposes. Consider adding ways to customize this as appropriate.
38+
set(EMCC_FLAGS
39+
-O3
40+
-std=c++17
41+
-Wall
42+
-Wcast-qual
43+
-Werror
44+
-Wignored-qualifiers
45+
-Wno-comment
46+
-Wno-psabi
47+
-Wno-unknown-warning-option
48+
-Wno-unused-function
49+
-Wsign-compare
50+
-Wsuggest-override
51+
-s ASSERTIONS=1
52+
-s ALLOW_MEMORY_GROWTH=1
53+
-s ENVIRONMENT=node
54+
-s STACK_SIZE=98304
55+
${args_OPTIONS}
56+
)
57+
58+
if ("${Halide_TARGET}" MATCHES "webgpu")
59+
set(EMCC_FLAGS
60+
${EMCC_FLAGS}
61+
-s USE_WEBGPU=1
62+
-s ASYNCIFY
63+
)
64+
endif ()
65+
66+
set(SRCS)
67+
foreach (S IN LISTS args_SRCS)
68+
list(APPEND SRCS "${CMAKE_CURRENT_SOURCE_DIR}/${S}")
69+
endforeach ()
70+
71+
set(INCLUDES)
72+
foreach (I IN LISTS args_INCLUDES)
73+
list(APPEND INCLUDES "-I${I}")
74+
endforeach ()
75+
76+
set(DEPS)
77+
foreach (D IN LISTS args_DEPS)
78+
list(APPEND DEPS $<TARGET_FILE:${D}>)
79+
endforeach ()
80+
81+
add_custom_command(OUTPUT "${TARGET}.wasm" "${TARGET}.js"
82+
COMMAND ${EMCC} ${EMCC_FLAGS} ${INCLUDES} ${SRCS} ${DEPS} -o "${TARGET}.js"
83+
DEPENDS ${SRCS} ${DEPS}
84+
VERBATIM)
85+
86+
add_custom_target("${TARGET}" ALL
87+
DEPENDS "${TARGET}.wasm" "${TARGET}.js")
88+
89+
endfunction()
90+
91+
function(add_wasm_halide_test TARGET)
92+
set(options)
93+
set(oneValueArgs)
94+
set(multiValueArgs GROUPS ENABLE_IF)
95+
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
96+
97+
if (args_ENABLE_IF AND NOT (${args_ENABLE_IF}))
98+
return()
99+
endif ()
100+
101+
find_package(NodeJS 16.13 REQUIRED)
102+
add_halide_test(
103+
"${TARGET}"
104+
GROUPS ${args_GROUPS}
105+
COMMAND "${NodeJS_EXECUTABLE}" "${Halide_SOURCE_DIR}/tools/launch_wasm_test.js" "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.js" "${Halide_TARGET}"
106+
)
107+
endfunction()
108+
15109
# Emit two halide_library targets, one with the default backend with the given name,
16110
# and (optionally) one with the C++ backend with the name NAME_cpp. (The CPP one defaults to being
17111
# emitted, but can be skipped if OMIT_C_BACKEND is specified.)

0 commit comments

Comments
 (0)