Commit a3a2f839 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Switch thread-in-wasm flag directly

Avoid the C-call to switch the flag, just store to the address
directly. Since js-to-wasm wrappers are still isolate dependent,
we just store the address of the thread-local flag in the isolate
(in ThreadLocalTop) and update it if threads are switched.

R=ahaas@chromium.org, mstarzinger@chromium.org

Bug: chromium:862123, v8:5277
Change-Id: I9e8a40094f11a8b3ba6701dfa7fa026a2d052cb1
Reviewed-on: https://chromium-review.googlesource.com/1136299
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54531}
parent ef284f7b
......@@ -4356,21 +4356,21 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
return num;
}
Node* BuildModifyThreadInWasmFlag(bool new_value) {
// TODO(eholk): generate code to modify the thread-local storage directly,
// rather than calling the runtime.
if (!use_trap_handler()) {
return *control_;
}
// Using two functions instead of taking the new value as a parameter saves
// one instruction on each call to set up the parameter.
ExternalReference ref =
new_value ? ExternalReference::wasm_set_thread_in_wasm_flag()
: ExternalReference::wasm_clear_thread_in_wasm_flag();
MachineSignature sig(0, 0, nullptr);
return BuildCCall(
&sig, graph()->NewNode(mcgraph()->common()->ExternalConstant(ref)));
void BuildModifyThreadInWasmFlag(bool new_value) {
if (!trap_handler::IsTrapHandlerEnabled()) return;
Node* thread_in_wasm_flag_address_address =
graph()->NewNode(mcgraph()->common()->ExternalConstant(
ExternalReference::wasm_thread_in_wasm_flag_address_address(
isolate_)));
Node* thread_in_wasm_flag_address = *effect_ = graph()->NewNode(
mcgraph()->machine()->Load(LoadRepresentation(MachineType::Pointer())),
thread_in_wasm_flag_address_address, mcgraph()->Int32Constant(0),
*effect_, *control_);
*effect_ = graph()->NewNode(
mcgraph()->machine()->Store(StoreRepresentation(
MachineRepresentation::kWord32, kNoWriteBarrier)),
thread_in_wasm_flag_address, mcgraph()->Int32Constant(0),
mcgraph()->Int32Constant(new_value ? 1 : 0), *effect_, *control_);
}
Node* BuildLoadFunctionDataFromExportedFunction(Node* closure) {
......
......@@ -364,16 +364,6 @@ ExternalReference ExternalReference::wasm_float64_pow() {
return ExternalReference(Redirect(FUNCTION_ADDR(wasm::float64_pow_wrapper)));
}
ExternalReference ExternalReference::wasm_set_thread_in_wasm_flag() {
return ExternalReference(
Redirect(FUNCTION_ADDR(wasm::set_thread_in_wasm_flag)));
}
ExternalReference ExternalReference::wasm_clear_thread_in_wasm_flag() {
return ExternalReference(
Redirect(FUNCTION_ADDR(wasm::clear_thread_in_wasm_flag)));
}
static void f64_mod_wrapper(Address data) {
double dividend = ReadUnalignedValue<double>(data);
double divisor = ReadUnalignedValue<double>(data + sizeof(dividend));
......@@ -931,6 +921,12 @@ ExternalReference ExternalReference::debug_restart_fp_address(
return ExternalReference(isolate->debug()->restart_fp_address());
}
ExternalReference ExternalReference::wasm_thread_in_wasm_flag_address_address(
Isolate* isolate) {
return ExternalReference(reinterpret_cast<Address>(
&isolate->thread_local_top()->thread_in_wasm_flag_address_));
}
ExternalReference ExternalReference::fixed_typed_array_base_data_offset() {
return ExternalReference(reinterpret_cast<void*>(
FixedTypedArrayBase::kDataOffset - kHeapObjectTag));
......
......@@ -66,6 +66,8 @@ class StatsCounter;
V(debug_suspended_generator_address, \
"Debug::step_suspended_generator_address()") \
V(debug_restart_fp_address, "Debug::restart_fp_address()") \
V(wasm_thread_in_wasm_flag_address_address, \
"&Isolate::thread_in_wasm_flag_address") \
EXTERNAL_REFERENCE_LIST_NON_INTERPRETED_REGEXP(V)
#define EXTERNAL_REFERENCE_LIST(V) \
......@@ -137,7 +139,6 @@ class StatsCounter;
V(try_internalize_string_function, "try_internalize_string_function") \
V(wasm_call_trap_callback_for_testing, \
"wasm::call_trap_callback_for_testing") \
V(wasm_clear_thread_in_wasm_flag, "wasm::clear_thread_in_wasm_flag") \
V(wasm_f32_ceil, "wasm::f32_ceil_wrapper") \
V(wasm_f32_floor, "wasm::f32_floor_wrapper") \
V(wasm_f32_nearest_int, "wasm::f32_nearest_int_wrapper") \
......@@ -155,7 +156,6 @@ class StatsCounter;
V(wasm_int64_mod, "wasm::int64_mod") \
V(wasm_int64_to_float32, "wasm::int64_to_float32_wrapper") \
V(wasm_int64_to_float64, "wasm::int64_to_float64_wrapper") \
V(wasm_set_thread_in_wasm_flag, "wasm::set_thread_in_wasm_flag") \
V(wasm_uint64_div, "wasm::uint64_div") \
V(wasm_uint64_mod, "wasm::uint64_mod") \
V(wasm_uint64_to_float32, "wasm::uint64_to_float32_wrapper") \
......
......@@ -156,9 +156,10 @@ void ThreadLocalTop::Initialize(Isolate* isolate) {
simulator_ = Simulator::current(isolate);
#endif
thread_id_ = ThreadId::Current();
thread_in_wasm_flag_address_ = reinterpret_cast<Address>(
trap_handler::GetThreadInWasmThreadLocalAddress());
}
void ThreadLocalTop::Free() {
wasm_caught_exception_ = nullptr;
// Match unmatched PopPromise calls.
......
......@@ -396,6 +396,9 @@ class ThreadLocalTop BASE_EMBEDDED {
// Call back function to report unsafe JS accesses.
v8::FailedAccessCheckCallback failed_access_check_callback_ = nullptr;
// Address of the thread-local "thread in wasm" flag.
Address thread_in_wasm_flag_address_ = kNullAddress;
private:
v8::TryCatch* try_catch_handler_ = nullptr;
};
......
......@@ -77,6 +77,13 @@ inline bool IsTrapHandlerEnabled() {
extern THREAD_LOCAL int g_thread_in_wasm_code;
// Return the address of the thread-local {g_thread_in_wasm_code} variable. This
// pointer can be accessed and modified as long as the thread calling this
// function exists. Only use if from the same thread do avoid race conditions.
inline int* GetThreadInWasmThreadLocalAddress() {
return &g_thread_in_wasm_code;
}
inline bool IsThreadInWasm() { return g_thread_in_wasm_code; }
inline void SetThreadInWasm() {
......
......@@ -247,10 +247,6 @@ void float64_pow_wrapper(Address data) {
WriteUnalignedValue<double>(data, Pow(x, y));
}
void set_thread_in_wasm_flag() { trap_handler::SetThreadInWasm(); }
void clear_thread_in_wasm_flag() { trap_handler::ClearThreadInWasm(); }
static WasmTrapCallbackForTesting wasm_trap_callback_for_testing = nullptr;
void set_trap_callback_for_testing(WasmTrapCallbackForTesting callback) {
......
......@@ -67,9 +67,6 @@ uint32_t word32_ror_wrapper(Address data);
void float64_pow_wrapper(Address data);
void set_thread_in_wasm_flag();
void clear_thread_in_wasm_flag();
typedef void (*WasmTrapCallbackForTesting)();
void set_trap_callback_for_testing(WasmTrapCallbackForTesting callback);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment