Skip to content
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
15 changes: 8 additions & 7 deletions cmake/wasi-sdk-sysroot.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ function(define_libcxx_sub sysroot target target_suffix extra_target_flags extra
set(extra_cxxflags_list ${CMAKE_CXX_FLAGS} ${extra_flags})
list(JOIN extra_cxxflags_list " " extra_cxxflags)

set(patches
${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch
${CMAKE_SOURCE_DIR}/src/llvm-pr-186054.patch
${CMAKE_SOURCE_DIR}/src/llvm-undo-part-of-194317.patch
)
list(JOIN patches " " patches)

ExternalProject_Add(libcxx-${target}${target_suffix}-build
SOURCE_DIR ${llvm_proj_dir}/runtimes
CMAKE_ARGS
Expand Down Expand Up @@ -392,13 +399,7 @@ function(define_libcxx_sub sysroot target target_suffix extra_target_flags extra
USES_TERMINAL_PATCH ON
PATCH_COMMAND
${CMAKE_COMMAND} -E chdir .. bash -c
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch -R --check"
COMMAND
${CMAKE_COMMAND} -E chdir .. bash -c
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-186054.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-186054.patch -R --check"
COMMAND
${CMAKE_COMMAND} -E chdir .. bash -c
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-undo-part-of-194317.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-undo-part-of-194317.patch -R --check"
"git apply ${patches} || git apply ${patches} -R --check"
)
add_dependencies(libcxx-${target} libcxx-${target}${target_suffix}-build)
endfunction()
Expand Down
15 changes: 11 additions & 4 deletions cmake/wasi-sdk-toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ endif()
list(TRANSFORM tools PREPEND --target= OUTPUT_VARIABLE build_targets)
list(TRANSFORM tools PREPEND --target=install- OUTPUT_VARIABLE install_targets)

set(patches
${CMAKE_SOURCE_DIR}/src/llvm-pr-206831.patch
${CMAKE_SOURCE_DIR}/src/llvm-pr-208263.patch
${CMAKE_SOURCE_DIR}/src/llvm-pr-208332.patch
${CMAKE_SOURCE_DIR}/src/llvm-pr-208597.patch
)
list(JOIN patches " " patches_apply)
list(REVERSE patches)
list(JOIN patches " " patches_rev)

ExternalProject_Add(llvm-build
SOURCE_DIR "${llvm_proj_dir}/llvm"
CMAKE_ARGS
Expand Down Expand Up @@ -257,10 +267,7 @@ ExternalProject_Add(llvm-build
USES_TERMINAL_INSTALL ON
PATCH_COMMAND
${CMAKE_COMMAND} -E chdir .. bash -c
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-206831.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-206831.patch -R --check"
COMMAND
${CMAKE_COMMAND} -E chdir .. bash -c
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-prs-208263-208332-208597.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-prs-208263-208332-208597.patch -R --check"
"git apply ${patches_apply} || git apply ${patches_rev} -R --check"
)

add_custom_target(build ALL DEPENDS llvm-build)
Expand Down
94 changes: 94 additions & 0 deletions src/llvm-pr-208263.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
diff --git a/lld/test/wasm/cooperative-threading.s b/lld/test/wasm/cooperative-threading.s
index 8b0f7eb1c256f..a4afb01dc2264 100644
--- a/lld/test/wasm/cooperative-threading.s
+++ b/lld/test/wasm/cooperative-threading.s
@@ -2,7 +2,7 @@
# thread-context globals (__init_stack_pointer, __init_tls_base, etc.) and
# works without --shared-memory and atomics.

-# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
+# RUN: llvm-mc -mattr=+call-indirect-overlong -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
# RUN: wasm-ld --cooperative-threading -no-gc-sections -o %t.wasm %t.o
# RUN: obj2yaml %t.wasm | FileCheck %s
# RUN: llvm-objdump -d --no-print-imm-hex --no-show-raw-insn %t.wasm | FileCheck %s --check-prefix=DIS
@@ -11,12 +11,22 @@
# RUN: not wasm-ld --cooperative-threading --shared-memory %t.o -o %t2.wasm 2>&1 | FileCheck %s --check-prefix=INCOMPAT
# INCOMPAT: --cooperative-threading is incompatible with --shared-memory

+.globl __indirect_function_table
+.tabletype __indirect_function_table, funcref
+
.globl __wasm_get_tls_base
__wasm_get_tls_base:
.functype __wasm_get_tls_base () -> (i32)
i32.const 0
end_function

+.globl do_call_indirect
+do_call_indirect:
+ .functype do_call_indirect () -> ()
+ i32.const 1
+ call_indirect __indirect_function_table, () -> ()
+ end_function
+
.globl _start
_start:
.functype _start () -> (i32)
@@ -66,12 +76,23 @@ foo:
.int8 7
.ascii "atomics"

+# CHECK: - Type: TABLE
+# CHECK-NEXT: Tables:
+# CHECK-NEXT: - Index: 0
+# CHECK-NEXT: ElemType: FUNCREF
+
# Memory must NOT be marked as shared.
# CHECK: - Type: MEMORY
# CHECK-NEXT: Memories:
# CHECK-NEXT: - Minimum: 0x2
# CHECK-NOT: Shared

+# The function table is exported by default.
+# CHECK: - Type: EXPORT
+# CHECK: - Name: __indirect_function_table
+# CHECK-NEXT: Kind: TABLE
+# CHECK-NEXT: Index: 0
+
# Only TLS needs a passive data segment; .data stays active and .bss gets no
# segment at all since memory is only instantiated once and starts zeroed.
# CHECK: - Type: DATACOUNT
@@ -118,3 +139,14 @@ foo:
# DIS-NEXT: i32.load 0
# DIS-NEXT: i32.add
# DIS-NEXT: end
+
+# When the table is imported instead there is no need to also export it.
+# RUN: wasm-ld --cooperative-threading --import-table -no-gc-sections -o %t3.wasm %t.o
+# RUN: obj2yaml %t3.wasm | FileCheck %s --check-prefix=IMPORT-TABLE
+
+# When the table is imported instead there is no need to also export it.
+# IMPORT-TABLE: - Type: IMPORT
+# IMPORT-TABLE: - Module: env
+# IMPORT-TABLE-NEXT: Field: __indirect_function_table
+# IMPORT-TABLE-NEXT: Kind: TABLE
+# IMPORT-TABLE-NOT: Kind: TABLE
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 9a2e3a82a9279..c213d7ca0b0f3 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -759,6 +759,14 @@ static void setConfigs() {
if (ctx.arg.sharedMemory)
error("--cooperative-threading is incompatible with --shared-memory");
ctx.arg.libcallThreadContext = true;
+
+ // Cooperative threading requires the table is either imported or exported
+ // or otherwise there's no way for embedders to read spawned functions from
+ // the table. If we've gotten this far and the table isn't otherwise
+ // imported (e.g in `isPic` mode) then export the table instead to ensure
+ // that it's visible to the outside world.
+ if (!ctx.arg.importTable)
+ ctx.arg.exportTable = true;
}
}

Loading
Loading