Skip to content

Commit

Permalink
Merge pull request 'fibers' (dart-lang#13) from fibers into main
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbashir committed Sep 21, 2024
2 parents 6b3313e + fa97a44 commit 835f8ef
Show file tree
Hide file tree
Showing 41 changed files with 5,057 additions and 7,164 deletions.
14 changes: 2 additions & 12 deletions runtime/lib/fiber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include <sys/mman.h>
#include "platform/globals.h"
#include "vm/bootstrap_natives.h"

#include "vm/compiler/runtime_api.h"
#include "vm/native_entry.h"

namespace dart {
DEFINE_NATIVE_ENTRY(Coroutine_factory, 0, 3) {
DEFINE_NATIVE_ENTRY(Coroutine_factory, 0, 2) {
GET_NON_NULL_NATIVE_ARGUMENT(Smi, size, arguments->NativeArgAt(1));
GET_NON_NULL_NATIVE_ARGUMENT(Closure, entry, arguments->NativeArgAt(2));
intptr_t stack_size = size.Value();
#if defined(DART_TARGET_OS_WINDOWS)
void** stack_base = (void**)((uintptr_t)VirtualAlloc(nullptr, stack_size * kWordSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
#else
void** stack_base = (void**)((uintptr_t)mmap(nullptr, stack_size * kWordSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
#endif
memset(stack_base, 0, stack_size * kWordSize);
return Coroutine::New(stack_base + stack_size , stack_size, Function::Handle(entry.function()).ptr());
return Coroutine::New(size.Value());
}
} // namespace dart
13 changes: 0 additions & 13 deletions runtime/lib/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,6 @@ DEFINE_NATIVE_ENTRY(Internal_nativeEffect, 0, 1) {
UNREACHABLE();
}

DEFINE_NATIVE_ENTRY(Fiber_coroutineInitialize, 0, 1) {
UNREACHABLE();
}

DEFINE_NATIVE_ENTRY(Fiber_coroutineTransfer, 0, 2) {
UNREACHABLE();
}

DEFINE_NATIVE_ENTRY(Fiber_coroutineFork, 0, 2) {
UNREACHABLE();
}


DEFINE_NATIVE_ENTRY(Internal_collectAllGarbage, 0, 0) {
isolate->group()->heap()->CollectAllGarbage(GCReason::kDebugging,
/*compact=*/true);
Expand Down
5 changes: 1 addition & 4 deletions runtime/vm/bootstrap_natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ namespace dart {
V(DartNativeApiFunctionPointer, 1) \
V(TransferableTypedData_factory, 2) \
V(TransferableTypedData_materialize, 1) \
V(Fiber_coroutineInitialize, 1) \
V(Fiber_coroutineTransfer, 2) \
V(Fiber_coroutineFork, 2) \
V(Coroutine_factory, 3)
V(Coroutine_factory, 2)

// List of bootstrap native entry points used in the dart:mirror library.
#define MIRRORS_BOOTSTRAP_NATIVE_LIST(V) \
Expand Down
33 changes: 18 additions & 15 deletions runtime/vm/compiler/backend/il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8574,21 +8574,23 @@ void CoroutineInitializeInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
__ Drop(1);

__ PushRegister(FPREG);
__ StoreFieldToOffset(SPREG, kCoroutine, Coroutine::root_stack_base_offset());
__ StoreFieldToOffset(SPREG, kCoroutine, Coroutine::native_stack_base_offset());

__ LoadFieldFromOffset(SPREG, kCoroutine, Coroutine::stack_base_offset());
__ PushRegister(kCoroutine);
compiler->GenerateStubCall(source(), StubCode::CoroutineEntry(), UntaggedPcDescriptors::kOther, locs(), deopt_id(), env());
__ PopRegister(kCoroutine);
__ StoreFieldToOffset(SPREG, kCoroutine, Coroutine::stack_base_offset());

__ LoadFieldFromOffset(SPREG, kCoroutine, Coroutine::root_stack_base_offset());
__ LoadFieldFromOffset(SPREG, kCoroutine, Coroutine::native_stack_base_offset());
__ PopRegister(FPREG);
if (!FLAG_precompiled_mode) __ RestoreCodePointer();
if (FLAG_precompiled_mode) __ movq(PP, compiler::Address(THR, Thread::global_object_pool_offset()));

__ PushObject(compiler::NullObject());
__ CallRuntime(kExitCoroutineRuntimeEntry, 0);
__ PushRegister(kCoroutine);
__ CallRuntime(kExitCoroutineRuntimeEntry, 1);
__ PopRegister(kCoroutine);
__ Drop(1);
}

Expand All @@ -8606,17 +8608,18 @@ LocationSummary* CoroutineForkInstr::MakeLocationSummary(
void CoroutineForkInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
const Register kCallerCoroutine = CoroutineForkABI::kCallerCoroutineReg;
const Register kForkedCoroutine = CoroutineForkABI::kForkedCoroutineReg;
const Register kStackLimit = CoroutineForkABI::kStackLimitReg;

#if defined(TARGET_ARCH_ARM) || defined(TARGET_ARCH_ARM64)
SPILLS_LR_TO_FRAME({});
#endif
__ StoreCompressedIntoObjectNoBarrier(kForkedCoroutine, compiler::FieldAddress(kForkedCoroutine, Coroutine::caller_offset()), kCallerCoroutine);

__ LoadFieldFromOffset(kStackLimit, kForkedCoroutine, Coroutine::stack_limit_offset());
__ StoreToOffset(kStackLimit, THR, Thread::stack_limit_offset());
__ StoreToOffset(kForkedCoroutine, THR, Thread::coroutine_offset());
__ PushObject(compiler::NullObject());
__ PushRegister(kForkedCoroutine);
__ CallRuntime(kEnterForkedCoroutineRuntimeEntry, 1);
__ PopRegister(kForkedCoroutine);
__ Drop(1);

__ LoadCompressedFieldFromOffset(kCallerCoroutine, kForkedCoroutine, Coroutine::caller_offset());

__ PushRegister(FPREG);
__ StoreFieldToOffset(SPREG, kCallerCoroutine, Coroutine::stack_base_offset());

Expand All @@ -8633,9 +8636,11 @@ void CoroutineForkInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
if (!FLAG_precompiled_mode) __ RestoreCodePointer();
if (FLAG_precompiled_mode) __ movq(PP, compiler::Address(THR, Thread::global_object_pool_offset()));

__ LoadFieldFromOffset(kStackLimit, kCallerCoroutine, Coroutine::stack_limit_offset());
__ StoreToOffset(kStackLimit, THR, Thread::stack_limit_offset());
__ StoreToOffset(kCallerCoroutine, THR, Thread::coroutine_offset());
__ PushObject(compiler::NullObject());
__ PushRegister(kForkedCoroutine);
__ CallRuntime(kExitForkedCoroutineRuntimeEntry, 1);
__ PopRegister(kForkedCoroutine);
__ Drop(1);
}

LocationSummary* CoroutineTransferInstr::MakeLocationSummary(
Expand Down Expand Up @@ -8668,8 +8673,6 @@ void CoroutineTransferInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
__ LoadFieldFromOffset(kToStackLimit, kToCoroutine, Coroutine::stack_limit_offset());
__ StoreToOffset(kToStackLimit, THR, Thread::stack_limit_offset());
__ StoreToOffset(kToCoroutine, THR, Thread::coroutine_offset());

__ StoreCompressedIntoObjectNoBarrier(kToCoroutine, compiler::FieldAddress(kToCoroutine, Coroutine::caller_offset()), kFromCoroutine);
}

Definition* SuspendInstr::Canonicalize(FlowGraph* flow_graph) {
Expand Down
1 change: 1 addition & 0 deletions runtime/vm/compiler/backend/range_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2836,6 +2836,7 @@ void LoadFieldInstr::InferRange(RangeAnalysis* analysis, Range* range) {

case Slot::Kind::kTypedDataBase_length:
case Slot::Kind::kTypedDataView_offset_in_bytes:
case Slot::Kind::kCoroutine_attributes:
*range = Range(RangeBoundary::FromConstant(0), RangeBoundary::MaxSmi());
break;

Expand Down
1 change: 1 addition & 0 deletions runtime/vm/compiler/backend/slot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ bool Slot::IsImmutableLengthSlot() const {
case Slot::Kind::kClosure_hash:
case Slot::Kind::kRecord_shape:
case Slot::Kind::kAbstractType_hash:
case Slot::Kind::kCoroutine_attributes:
return false;
}
UNREACHABLE();
Expand Down
18 changes: 14 additions & 4 deletions runtime/vm/compiler/backend/slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ class ParsedFunction;
V(WeakProperty, UntaggedWeakProperty, key, Dynamic, WEAK) \
V(WeakProperty, UntaggedWeakProperty, value, Dynamic, WEAK) \
V(WeakReference, UntaggedWeakReference, target, Dynamic, WEAK) \
V(WeakReference, UntaggedWeakReference, type_arguments, TypeArguments, FINAL)\
V(WeakReference, UntaggedWeakReference, type_arguments, TypeArguments, \
FINAL) \
V(Coroutine, UntaggedCoroutine, name, Dynamic, VAR) \
V(Coroutine, UntaggedCoroutine, entry, Closure, VAR) \
V(Coroutine, UntaggedCoroutine, trampoline, Function, VAR) \
V(Coroutine, UntaggedCoroutine, processor, Dynamic, VAR) \
V(Coroutine, UntaggedCoroutine, to_processor, Dynamic, VAR) \
V(Coroutine, UntaggedCoroutine, caller, Coroutine, VAR) \
V(Coroutine, UntaggedCoroutine, entry, Coroutine, VAR) \
V(Coroutine, UntaggedCoroutine, scheduler, Coroutine, VAR)

// The list of slots that correspond to non-nullable boxed fields of native
// Dart objects that contain integers in the following format:
Expand Down Expand Up @@ -118,7 +124,8 @@ class ParsedFunction;
V(Record, UntaggedRecord, shape, Smi, FINAL) \
V(TypeArguments, UntaggedTypeArguments, hash, Smi, VAR) \
V(TypeArguments, UntaggedTypeArguments, length, Smi, FINAL) \
V(AbstractType, UntaggedTypeArguments, hash, Smi, VAR)
V(AbstractType, UntaggedTypeArguments, hash, Smi, VAR) \
V(Coroutine, UntaggedCoroutine, attributes, Smi, VAR)

// The list of slots that correspond to non-nullable boxed fields of native
// Dart objects that do not contain integers in the following format:
Expand Down Expand Up @@ -149,7 +156,9 @@ class ParsedFunction;
FINAL) \
V(TypeParameters, UntaggedTypeParameters, names, Array, FINAL) \
V(UnhandledException, UntaggedUnhandledException, exception, Dynamic, FINAL) \
V(UnhandledException, UntaggedUnhandledException, stacktrace, Dynamic, FINAL)
V(UnhandledException, UntaggedUnhandledException, stacktrace, Dynamic, \
FINAL) \
V(Coroutine, UntaggedCoroutine, arguments, Array, VAR)

// List of slots that correspond to fields of native objects that contain
// unboxed values in the following format:
Expand Down Expand Up @@ -233,6 +242,7 @@ class ParsedFunction;
V(Isolate, _, finalizers, GrowableObjectArray, VAR) \
V(LocalHandle, _, ptr, Dynamic, VAR) \
V(ObjectStore, _, record_field_names, Array, VAR) \
V(Thread, _, coroutine, Coroutine, VAR) \
V(PersistentHandle, _, ptr, Dynamic, VAR)

// List of slots that correspond to fields of non-Dart objects containing
Expand Down
23 changes: 22 additions & 1 deletion runtime/vm/compiler/frontend/kernel_to_il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,14 @@ const Function& TypedListGetNativeFunction(Thread* thread, classid_t cid) {
V(ObjectArrayLength, Array_length) \
V(Record_shape, Record_shape) \
V(SuspendState_getFunctionData, SuspendState_function_data) \
V(Coroutine_getCaller, Coroutine_caller) \
V(Coroutine_getName, Coroutine_name) \
V(Coroutine_getEntry, Coroutine_entry) \
V(Coroutine_getTrampoline, Coroutine_trampoline) \
V(Coroutine_getArguments, Coroutine_arguments) \
V(Coroutine_getAttributes, Coroutine_attributes) \
V(Coroutine_getScheduler, Coroutine_scheduler) \
V(Coroutine_getProcessor, Coroutine_processor) \
V(Coroutine_getToProcessor, Coroutine_to_processor) \
V(SuspendState_getThenCallback, SuspendState_then_callback) \
V(SuspendState_getErrorCallback, SuspendState_error_callback) \
V(TypedDataViewOffsetInBytes, TypedDataView_offset_in_bytes) \
Expand All @@ -944,6 +951,14 @@ const Function& TypedListGetNativeFunction(Thread* thread, classid_t cid) {
V(SuspendState_setFunctionData, SuspendState_function_data) \
V(SuspendState_setThenCallback, SuspendState_then_callback) \
V(SuspendState_setErrorCallback, SuspendState_error_callback) \
V(Coroutine_setName, Coroutine_name) \
V(Coroutine_setEntry, Coroutine_entry) \
V(Coroutine_setTrampoline, Coroutine_trampoline) \
V(Coroutine_setArguments, Coroutine_arguments) \
V(Coroutine_setAttributes, Coroutine_attributes) \
V(Coroutine_setScheduler, Coroutine_scheduler) \
V(Coroutine_setProcessor, Coroutine_processor) \
V(Coroutine_setToProcessor, Coroutine_to_processor) \
V(WeakProperty_setKey, WeakProperty_key) \
V(WeakProperty_setValue, WeakProperty_value) \
V(WeakReference_setTarget, WeakReference_target)
Expand Down Expand Up @@ -1143,6 +1158,7 @@ bool FlowGraphBuilder::IsRecognizedMethodForFlowGraph(
case MethodRecognizer::kCoroutineFork:
case MethodRecognizer::kCoroutineInitialize:
case MethodRecognizer::kCoroutineTransfer:
case MethodRecognizer::kCoroutine_getCurrent:
return true;
default:
return false;
Expand Down Expand Up @@ -1935,6 +1951,11 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfRecognizedMethod(
body += NullConstant();
break;
}
case MethodRecognizer::kCoroutine_getCurrent: {
body += LoadThread();
body += LoadNativeField(Slot::Thread_coroutine());
break;
}
#define IL_BODY(method, slot) \
case MethodRecognizer::k##method: \
ASSERT_EQUAL(function.NumParameters(), 1); \
Expand Down
27 changes: 22 additions & 5 deletions runtime/vm/compiler/recognized_methods_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,28 @@ namespace dart {
V(::, _memCopy, MemCopy, 0x51939aa6) \
V(::, debugger, Debugger, 0xf0aaff14) \
V(::, _checkNotDeeplyImmutable, CheckNotDeeplyImmutable, 0x34e4da90) \
V(::, _coroutineInitialize, CoroutineInitialize, 0x797da468) \
V(::, _coroutineTransfer, CoroutineTransfer, 0x821c1d82) \
V(::, _coroutineFork, CoroutineFork, 0xaca4c449) \
V(_Coroutine, get:_caller, Coroutine_getCaller, \
0x786cc73b) \
V(_Coroutine, _initialize, CoroutineInitialize, 0xa75fb4b4) \
V(_Coroutine, _transfer, CoroutineTransfer, 0x94684214) \
V(_Coroutine, _fork, CoroutineFork, 0x9e657623) \
V(_Coroutine, get:_name, Coroutine_getName,0x2b1f1c32) \
V(_Coroutine, get:_entry, Coroutine_getEntry, 0xc825e938) \
V(_Coroutine, get:_trampoline, Coroutine_getTrampoline, 0xc5b7b65a) \
V(_Coroutine, get:_arguments, Coroutine_getArguments, 0x4df70fc1) \
V(_Coroutine, get:_attributes, Coroutine_getAttributes, 0x4bba9d49) \
V(_Coroutine, get:_caller, Coroutine_getCaller, 0xe388183b) \
V(_Coroutine, get:_scheduler, Coroutine_getScheduler, 0xd5f06e7c) \
V(_Coroutine, get:_processor, Coroutine_getProcessor, 0x4391e70d) \
V(_Coroutine, get:_toProcessor, Coroutine_getToProcessor, 0x69c3b244) \
V(_Coroutine, set:_name, Coroutine_setName, 0x45ff0fef) \
V(_Coroutine, set:_entry, Coroutine_setEntry, 0x896541f5) \
V(_Coroutine, set:_trampoline, Coroutine_setTrampoline, 0x86f70f17) \
V(_Coroutine, set:_arguments, Coroutine_setArguments, 0x8c8ec23e) \
V(_Coroutine, set:_attributes, Coroutine_setAttributes, 0xb87a94c6) \
V(_Coroutine, set:_caller, Coroutine_setCaller, 0xbae0fb78) \
V(_Coroutine, set:_scheduler, Coroutine_setScheduler, 0xad4951b9) \
V(_Coroutine, set:_processor, Coroutine_setProcessor, 0x57c426ca) \
V(_Coroutine, set:_toProcessor, Coroutine_setToProcessor, 0x6b557dc1) \
V(_Coroutine, get:_current, Coroutine_getCurrent, 0xc845245c) \


// List of intrinsics:
Expand Down
18 changes: 13 additions & 5 deletions runtime/vm/compiler/runtime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,17 @@ class SuspendState : public AllStatic {

class Coroutine : public AllStatic {
public:
static word caller_offset();
static word name_offset();
static word entry_offset();
static word root_stack_base_offset();
static word trampoline_offset();
static word arguments_offset();
static word attributes_offset();
static word caller_offset();
static word scheduler_offset();
static word processor_offset();
static word to_processor_offset();
static word index_offset();
static word native_stack_base_offset();
static word stack_root_offset();
static word stack_base_offset();
static word stack_limit_offset();
Expand Down Expand Up @@ -1266,9 +1274,9 @@ class Thread : public AllStatic {
static word bootstrap_native_wrapper_entry_point_offset();
static word no_scope_native_wrapper_entry_point_offset();
static word auto_scope_native_wrapper_entry_point_offset();

static word coroutine_offset();

#define THREAD_XMM_CONSTANT_LIST(V) \
V(float_not) \
V(float_negate) \
Expand Down Expand Up @@ -1299,7 +1307,7 @@ class Thread : public AllStatic {
static word suspend_state_suspend_sync_star_at_start_entry_point_offset();

static word suspend_state_handle_exception_entry_point_offset();

static word OffsetFromThread(const dart::Object& object);
static intptr_t OffsetFromThread(const dart::RuntimeEntry* runtime_entry);
};
Expand Down
Loading

0 comments on commit 835f8ef

Please sign in to comment.