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

Move dependencies/wasm to use sites #8377

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ option(THREADS_PREFER_PTHREAD_FLAG "When enabled, prefer to use the -pthread fla
find_package(Threads REQUIRED)

## Complex dependencies
add_subdirectory(dependencies)
add_subdirectory(dependencies/llvm)

## Image formats

Expand Down
6 changes: 3 additions & 3 deletions README_cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ The following options enable/disable various Halide-specific backends:
The following options are WebAssembly-specific. They only apply when
`TARGET_WEBASSEMBLY=ON`:

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

### Find module options

Expand Down
8 changes: 0 additions & 8 deletions dependencies/CMakeLists.txt

This file was deleted.

152 changes: 0 additions & 152 deletions dependencies/wasm/CMakeLists.txt

This file was deleted.

80 changes: 73 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,80 @@ target_compile_definitions(Halide PUBLIC
HALIDE_VERSION_MINOR=${Halide_VERSION_MINOR}
HALIDE_VERSION_PATCH=${Halide_VERSION_PATCH})

if (TARGET Halide_wabt)
target_link_libraries(Halide PRIVATE Halide_wabt)
target_compile_definitions(Halide PRIVATE WITH_WABT)
endif ()
##
# WasmExecutor backend selection
##

if (TARGET_WEBASSEMBLY)
include(FetchContent)

set(Halide_WASM_BACKEND "wabt"
CACHE STRING "Which backend to use for Halide's WASM testing.")
set_property(CACHE Halide_WASM_BACKEND PROPERTY STRINGS "wabt;V8;OFF")

if (WITH_WABT AND NOT WITH_V8)
message(DEPRECATION "WITH_WABT has been replaced by Halide_WASM_BACKEND=\"wabt\"")
set(Halide_WASM_BACKEND "wabt")
elseif (NOT WITH_WABT AND WITH_V8)
message(DEPRECATION "WITH_V8 has been replaced by Halide_WASM_BACKEND=\"V8\"")
set(Halide_WASM_BACKEND "V8")
elseif (WITH_WABT AND WITH_V8)
message(FATAL_ERROR "Cannot use both WABT and V8 at the same time, disable one of them.")
elseif (DEFINED WITH_WABT AND DEFINED WITH_V8 AND NOT WITH_WABT AND NOT WITH_V8)
message(DEPRECATION "Disabling both WITH_WABT and WITH_V8 has been replaced by Halide_WASM_BACKEND=\"OFF\"")
set(Halide_WASM_BACKEND "OFF")
endif ()

if (MSVC AND Halide_WASM_BACKEND STREQUAL "wabt")
message(WARNING "wabt is not yet supported on Windows")
set(Halide_WASM_BACKEND "OFF")
endif ()

if (TARGET V8::V8)
target_link_libraries(Halide PRIVATE V8::V8)
target_compile_definitions(Halide PRIVATE WITH_V8)
if (Halide_WASM_BACKEND STREQUAL "wabt")
set(WABT_VER 1.0.33)

message(STATUS "Fetching WABT ${WABT_VER}...")
FetchContent_Declare(wabt
GIT_REPOSITORY https://github.com/WebAssembly/wabt.git
GIT_TAG ${WABT_VER}
GIT_SHALLOW TRUE)

# configuration for wabt
set(WITH_EXCEPTIONS ${Halide_ENABLE_EXCEPTIONS})
set(BUILD_TESTS OFF)
set(BUILD_TOOLS OFF)
set(BUILD_LIBWASM OFF)
set(USE_INTERNAL_SHA256 ON)
FetchContent_MakeAvailable(wabt)

set_target_properties(wabt PROPERTIES POSITION_INDEPENDENT_CODE ON)

# Disable this very-noisy warning in GCC
target_compile_options(wabt
PRIVATE
$<$<CXX_COMPILER_ID:GNU>:-Wno-alloca-larger-than>)

# TODO: we want to require unique prefixes to include these files, to avoid ambiguity;
# this means we have to prefix with "wabt-src/...", which is less bad than other alternatives,
# but perhaps we could do better (esp. if wabt was smarter about what it exposed?)
add_library(Halide_wabt INTERFACE)
target_sources(Halide_wabt INTERFACE $<BUILD_LOCAL_INTERFACE:$<TARGET_OBJECTS:wabt>>)
target_include_directories(Halide_wabt
SYSTEM # Use -isystem instead of -I; this is a trick so that clang-tidy won't analyze these includes
INTERFACE
$<BUILD_INTERFACE:${wabt_SOURCE_DIR}>/include
$<BUILD_INTERFACE:${wabt_BINARY_DIR}>/include)
set_target_properties(Halide_wabt PROPERTIES EXPORT_NAME wabt)

target_link_libraries(Halide PRIVATE Halide_wabt)
target_compile_definitions(Halide PRIVATE WITH_WABT)
elseif (Halide_WASM_BACKEND STREQUAL "V8")
find_package(V8 REQUIRED)
target_link_libraries(Halide PRIVATE V8::V8)
target_compile_definitions(Halide PRIVATE WITH_V8)
elseif (Halide_WASM_BACKEND)
message(FATAL_ERROR "Unknown Halide_WASM_BACKEND `${Halide_WASM_BACKEND}`")
endif ()
endif ()

##
Expand Down
94 changes: 94 additions & 0 deletions test/generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,100 @@ else()
set(_USING_WASM 0)
endif()

function(add_wasm_executable TARGET)
set(options)
set(oneValueArgs)
set(multiValueArgs SRCS DEPS INCLUDES OPTIONS ENABLE_IF)
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if (args_ENABLE_IF AND NOT (${args_ENABLE_IF}))
return()
endif ()

# Conceptually, we want something like this:
# add_executable(${TARGET} ${args_SRCS})
# if (args_INCLUDES)
# target_include_directories("${TARGET}" PRIVATE ${args_INCLUDES})
# endif()
# if (args_DEPS)
# target_link_libraries(${TARGET} PRIVATE ${args_DEPS})
# endif ()

find_program(EMCC emcc REQUIRED HINTS "$ENV{EMSDK}/upstream/emscripten")

# TODO: this is currently hardcoded to settings that are sensible for most of Halide's
# internal purposes. Consider adding ways to customize this as appropriate.
set(EMCC_FLAGS
-O3
-std=c++17
-Wall
-Wcast-qual
-Werror
-Wignored-qualifiers
-Wno-comment
-Wno-psabi
-Wno-unknown-warning-option
-Wno-unused-function
-Wsign-compare
-Wsuggest-override
-s ASSERTIONS=1
-s ALLOW_MEMORY_GROWTH=1
-s ENVIRONMENT=node
-s STACK_SIZE=98304
${args_OPTIONS}
)

if ("${Halide_TARGET}" MATCHES "webgpu")
set(EMCC_FLAGS
${EMCC_FLAGS}
-s USE_WEBGPU=1
-s ASYNCIFY
)
endif ()

set(SRCS)
foreach (S IN LISTS args_SRCS)
list(APPEND SRCS "${CMAKE_CURRENT_SOURCE_DIR}/${S}")
endforeach ()

set(INCLUDES)
foreach (I IN LISTS args_INCLUDES)
list(APPEND INCLUDES "-I${I}")
endforeach ()

set(DEPS)
foreach (D IN LISTS args_DEPS)
list(APPEND DEPS $<TARGET_FILE:${D}>)
endforeach ()

add_custom_command(OUTPUT "${TARGET}.wasm" "${TARGET}.js"
COMMAND ${EMCC} ${EMCC_FLAGS} ${INCLUDES} ${SRCS} ${DEPS} -o "${TARGET}.js"
DEPENDS ${SRCS} ${DEPS}
VERBATIM)

add_custom_target("${TARGET}" ALL
DEPENDS "${TARGET}.wasm" "${TARGET}.js")

endfunction()

function(add_wasm_halide_test TARGET)
set(options)
set(oneValueArgs)
set(multiValueArgs GROUPS ENABLE_IF)
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if (args_ENABLE_IF AND NOT (${args_ENABLE_IF}))
return()
endif ()

find_package(NodeJS 16.13 REQUIRED)
add_halide_test(
"${TARGET}"
GROUPS ${args_GROUPS}
COMMAND "${NodeJS_EXECUTABLE}" "${Halide_SOURCE_DIR}/tools/launch_wasm_test.js" "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.js" "${Halide_TARGET}"
)
endfunction()

# Emit two halide_library targets, one with the default backend with the given name,
# and (optionally) one with the C++ backend with the name NAME_cpp. (The CPP one defaults to being
# emitted, but can be skipped if OMIT_C_BACKEND is specified.)
Expand Down