Skip to content

Commit

Permalink
Merge pull request 'fibers' (#2) from fibers into main
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbashir committed Aug 30, 2024
2 parents f30b97e + 09fa1e9 commit 7df9a5a
Show file tree
Hide file tree
Showing 48 changed files with 6,756 additions and 3,762 deletions.
2 changes: 1 addition & 1 deletion build-sdk-debug.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
./tools/build.py -m debug -a x64 runtime dart_precompiled_runtime create_common_sdk create_platform_sdk
./tools/build.py -m debug -a x64 runtime dart_precompiled_runtime ddc dartanalyzer analysis_server create_common_sdk create_platform_sdk
1 change: 0 additions & 1 deletion dart.S

This file was deleted.

5 changes: 3 additions & 2 deletions runtime/lib/fiber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include "vm/tagged_pointer.h"

namespace dart {
DEFINE_NATIVE_ENTRY(Coroutine_factory, 0, 2) {
DEFINE_NATIVE_ENTRY(Coroutine_factory, 0, 3) {
GET_NON_NULL_NATIVE_ARGUMENT(Smi, stack, arguments->NativeArgAt(1));
return Coroutine::New(stack.Value());
GET_NON_NULL_NATIVE_ARGUMENT(Closure, entry, arguments->NativeArgAt(2));
return Coroutine::New(stack.Value(), Function::Handle(entry.function()).ptr());
}
} // namespace dart
4 changes: 4 additions & 0 deletions runtime/lib/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ 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,
Expand Down
14 changes: 7 additions & 7 deletions runtime/vm/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import("//build_overrides/build.gni")
import("//third_party/protobuf/proto_library.gni")
import("../../build/executable_suffix.gni")
import("../../sdk/lib/async/async_sources.gni")
import("../../sdk/lib/fiber/fiber_sources.gni")
import("../../sdk/lib/collection/collection_sources.gni")
import("../../sdk/lib/convert/convert_sources.gni")
import("../../sdk/lib/core/core_sources.gni")
import("../../sdk/lib/developer/developer_sources.gni")
import("../../sdk/lib/ffi/ffi_sources.gni")
import("../../sdk/lib/fiber/fiber_sources.gni")
import("../../sdk/lib/internal/internal_sources.gni")
import("../../sdk/lib/isolate/isolate_sources.gni")
import("../../sdk/lib/math/math_sources.gni")
Expand All @@ -24,11 +24,11 @@ import("../bin/cli_sources.gni")
import("../bin/io_sources.gni")
import("../configs.gni")
import("../lib/async_sources.gni")
import("../lib/fiber_sources.gni")
import("../lib/convert_sources.gni")
import("../lib/core_sources.gni")
import("../lib/developer_sources.gni")
import("../lib/ffi_sources.gni")
import("../lib/fiber_sources.gni")
import("../lib/isolate_sources.gni")
import("../lib/math_sources.gni")
import("../lib/mirrors_sources.gni")
Expand Down Expand Up @@ -227,11 +227,11 @@ library_for_all_configs("libdart_lib") {
extra_deps += [ "$fuchsia_sdk/pkg/trace-engine" ]
}
include_dirs = [ ".." ]
allsources = async_runtime_cc_files + fiber_runtime_cc_files +
core_runtime_cc_files + developer_runtime_cc_files +
isolate_runtime_cc_files + math_runtime_cc_files +
mirrors_runtime_cc_files + typed_data_runtime_cc_files +
vmservice_runtime_cc_files + ffi_runtime_cc_files
allsources = async_runtime_cc_files + core_runtime_cc_files +
developer_runtime_cc_files + isolate_runtime_cc_files +
math_runtime_cc_files + mirrors_runtime_cc_files +
typed_data_runtime_cc_files + vmservice_runtime_cc_files +
ffi_runtime_cc_files + fiber_runtime_cc_files
sources = [ "bootstrap.cc" ] + rebase_path(allsources, ".", "../lib")
snapshot_sources = []
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/vm/app_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9455,7 +9455,7 @@ void Deserializer::ReadInstructions(CodePtr code, bool deferred) {
instr = image_reader_->GetInstructionsAt(active_offset);
unchecked_offset = ReadUnsigned();
code->untag()->active_instructions_ = instr;
Code::InitializeCachedEntryPointsFrom(code, instr, unchecked_offset);
Code::InitializeCachedEntryPointsFrom(code, instr, unchecked_offset);
#endif // defined(DART_PRECOMPILED_RUNTIME)
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/vm/bootstrap_natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ namespace dart {
V(Internal_nativeEffect, 1) \
V(Fiber_coroutineInitialize, 1) \
V(Fiber_coroutineTransfer, 2) \
V(Fiber_coroutineFork, 2) \
V(Internal_collectAllGarbage, 0) \
V(Internal_makeListFixedLength, 1) \
V(Internal_makeFixedListUnmodifiable, 1) \
Expand Down Expand Up @@ -315,7 +316,7 @@ namespace dart {
V(DartNativeApiFunctionPointer, 1) \
V(TransferableTypedData_factory, 2) \
V(TransferableTypedData_materialize, 1) \
V(Coroutine_factory, 2)
V(Coroutine_factory, 3)

// List of bootstrap native entry points used in the dart:mirror library.
#define MIRRORS_BOOTSTRAP_NATIVE_LIST(V) \
Expand Down
3 changes: 3 additions & 0 deletions runtime/vm/compiler/backend/constant_propagator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,9 @@ void ConstantPropagator::VisitCoroutineTransferStub(CoroutineTransferStubInstr*
SetValue(instr, non_constant_);
}

void ConstantPropagator::VisitCoroutineForkStub(CoroutineForkStubInstr* instr) {
}

void ConstantPropagator::VisitSuspend(SuspendInstr* instr) {
SetValue(instr, non_constant_);
}
Expand Down
1 change: 1 addition & 0 deletions runtime/vm/compiler/backend/flow_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <array>

#include "platform/text_buffer.h"
#include "vm/bit_vector.h"
#include "vm/compiler/backend/dart_calling_conventions.h"
#include "vm/compiler/backend/flow_graph_compiler.h"
Expand Down
46 changes: 20 additions & 26 deletions runtime/vm/compiler/backend/il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8557,27 +8557,15 @@ LocationSummary* CoroutineInitializeStubInstr::MakeLocationSummary(
const intptr_t kNumInputs = 1;
const intptr_t kNumTemps = 0;
LocationSummary* locs = new (zone) LocationSummary(zone, kNumInputs, kNumTemps, LocationSummary::kCall);
locs->set_in(0, Location::RegisterLocation(CoroutineInitializeStubABI::kFromCoroutineReg));
locs->set_in(0, Location::RegisterLocation(CoroutineInitializeStubABI::kCoroutineReg));
return locs;
}

void CoroutineInitializeStubInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
ObjectStore* object_store = compiler->isolate_group()->object_store();
Code& stub = Code::ZoneHandle(compiler->zone());
stub = object_store->coroutine_initialize_stub();
__ PushRegister(FPREG);
__ PushRegister(THR);
__ PushRegister(TMP);
__ PushRegister(PP);
__ PushRegister(CODE_REG);
__ LoadFieldFromOffset(CoroutineTransferStubABI::kFromContextReg, CoroutineTransferStubABI::kFromCoroutineReg, compiler::target::Coroutine::context_offset());
__ StoreToOffset(SPREG, CoroutineTransferStubABI::kFromContextReg, CoroutineTransferStubABI::kContextSpOffset);
compiler->GenerateStubCall(source(), stub, UntaggedPcDescriptors::kOther, locs(), deopt_id(), env());
__ PopRegister(CODE_REG);
__ PopRegister(PP);
__ PopRegister(TMP);
__ PopRegister(THR);
__ PopRegister(FPREG);
compiler->GenerateStubCall(source(), stub, UntaggedPcDescriptors::kOther, locs(), deopt_id(), env());
}

LocationSummary* CoroutineTransferStubInstr::MakeLocationSummary(
Expand All @@ -8595,19 +8583,25 @@ void CoroutineTransferStubInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
ObjectStore* object_store = compiler->isolate_group()->object_store();
Code& stub = Code::ZoneHandle(compiler->zone());
stub = object_store->coroutine_transfer_stub();
__ PushRegister(FPREG);
__ PushRegister(THR);
__ PushRegister(TMP);
__ PushRegister(PP);
__ PushRegister(CODE_REG);
__ LoadFieldFromOffset(CoroutineTransferStubABI::kFromContextReg, CoroutineTransferStubABI::kFromCoroutineReg, compiler::target::Coroutine::context_offset());
__ StoreToOffset(SPREG, CoroutineTransferStubABI::kFromContextReg, CoroutineTransferStubABI::kContextSpOffset);
compiler->GenerateStubCall(source(), stub, UntaggedPcDescriptors::kOther, locs(), deopt_id(), env());
__ PopRegister(CODE_REG);
__ PopRegister(PP);
__ PopRegister(TMP);
__ PopRegister(THR);
__ PopRegister(FPREG);
}

LocationSummary* CoroutineForkStubInstr::MakeLocationSummary(
Zone* zone,
bool opt) const {
const intptr_t kNumInputs = 2;
const intptr_t kNumTemps = 0;
LocationSummary* locs = new (zone) LocationSummary(zone, kNumInputs, kNumTemps, LocationSummary::kCall);
locs->set_in(0, Location::RegisterLocation(CoroutineForkStubABI::kCallerCoroutineReg));
locs->set_in(1, Location::RegisterLocation(CoroutineForkStubABI::kForkedCoroutineReg));
return locs;
}

void CoroutineForkStubInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
ObjectStore* object_store = compiler->isolate_group()->object_store();
Code& stub = Code::ZoneHandle(compiler->zone());
stub = object_store->coroutine_fork_stub();
compiler->GenerateStubCall(source(), stub, UntaggedPcDescriptors::kOther, locs(), deopt_id(), env());
}

Definition* SuspendInstr::Canonicalize(FlowGraph* flow_graph) {
Expand Down
62 changes: 37 additions & 25 deletions runtime/vm/compiler/backend/il.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ struct InstrAttrs {
M(Call1ArgStub, _) \
M(CoroutineInitializeStub, _) \
M(CoroutineTransferStub, _) \
M(CoroutineForkStub, _) \
M(LoadThread, kNoGC) \
M(Deoptimize, kNoGC) \
M(SimdOp, kNoGC) \
Expand Down Expand Up @@ -11477,16 +11478,13 @@ class Call1ArgStubInstr : public TemplateDefinition<1, Throws> {

class CoroutineInitializeStubInstr : public TemplateDefinition<1, NoThrow> {
public:
CoroutineInitializeStubInstr(const InstructionSource& source,
Value* root,
intptr_t deopt_id)
: TemplateDefinition(source, deopt_id), token_pos_(source.token_pos) {
CoroutineInitializeStubInstr(Value* root, intptr_t deopt_id)
: TemplateDefinition(InstructionSource(TokenPosition::kNoSource),
deopt_id) {
SetInputAt(0, root);
}

Value* root() const { return inputs_[0]; }
virtual TokenPosition token_pos() const { return token_pos_; }

virtual bool CanCallDart() const { return true; }
virtual bool ComputeCanDeoptimize() const { return false; }
virtual bool ComputeCanDeoptimizeAfterCall() const { return true; }
Expand All @@ -11497,32 +11495,23 @@ class CoroutineInitializeStubInstr : public TemplateDefinition<1, NoThrow> {

DECLARE_INSTRUCTION(CoroutineInitializeStub);
PRINT_OPERANDS_TO_SUPPORT

#define FIELD_LIST(F) F(const TokenPosition, token_pos_)

DECLARE_INSTRUCTION_SERIALIZABLE_FIELDS(CoroutineInitializeStubInstr,
TemplateDefinition,
FIELD_LIST)
#undef FIELD_LIST
DECLARE_EMPTY_SERIALIZATION(CoroutineInitializeStubInstr, TemplateDefinition)

private:
DISALLOW_COPY_AND_ASSIGN(CoroutineInitializeStubInstr);
};

class CoroutineTransferStubInstr : public TemplateDefinition<2, NoThrow> {
public:
CoroutineTransferStubInstr(const InstructionSource& source,
Value* from,
Value* to,
intptr_t deopt_id)
: TemplateDefinition(source, deopt_id), token_pos_(source.token_pos) {
CoroutineTransferStubInstr(Value* from, Value* to, intptr_t deopt_id)
: TemplateDefinition(InstructionSource(TokenPosition::kNoSource),
deopt_id) {
SetInputAt(0, from);
SetInputAt(1, to);
}

Value* from() const { return inputs_[0]; }
Value* to() const { return inputs_[1]; }
virtual TokenPosition token_pos() const { return token_pos_; }

virtual bool CanCallDart() const { return true; }
virtual bool ComputeCanDeoptimize() const { return false; }
Expand All @@ -11534,16 +11523,39 @@ class CoroutineTransferStubInstr : public TemplateDefinition<2, NoThrow> {

DECLARE_INSTRUCTION(CoroutineTransferStub);
PRINT_OPERANDS_TO_SUPPORT
DECLARE_EMPTY_SERIALIZATION(CoroutineTransferStubInstr, TemplateDefinition)

#define FIELD_LIST(F) F(const TokenPosition, token_pos_)
private:
DISALLOW_COPY_AND_ASSIGN(CoroutineTransferStubInstr);
};

DECLARE_INSTRUCTION_SERIALIZABLE_FIELDS(CoroutineTransferStubInstr,
TemplateDefinition,
FIELD_LIST)
#undef FIELD_LIST
class CoroutineForkStubInstr : public TemplateDefinition<2, NoThrow> {
public:
CoroutineForkStubInstr(Value* from, Value* to, intptr_t deopt_id)
: TemplateDefinition(InstructionSource(TokenPosition::kNoSource),
deopt_id) {
SetInputAt(0, from);
SetInputAt(1, to);
}

Value* from() const { return inputs_[0]; }
Value* to() const { return inputs_[1]; }

virtual bool CanCallDart() const { return true; }
virtual bool ComputeCanDeoptimize() const { return false; }
virtual bool ComputeCanDeoptimizeAfterCall() const { return true; }
virtual bool HasUnknownSideEffects() const { return true; }
virtual intptr_t NumberOfInputsConsumedBeforeCall() const {
return InputCount();
}

DECLARE_INSTRUCTION(CoroutineForkStub);
PRINT_OPERANDS_TO_SUPPORT

DECLARE_EMPTY_SERIALIZATION(CoroutineForkStubInstr, TemplateDefinition)

private:
DISALLOW_COPY_AND_ASSIGN(CoroutineTransferStubInstr);
DISALLOW_COPY_AND_ASSIGN(CoroutineForkStubInstr);
};

// Suspends execution using the suspend stub specified using [StubId].
Expand Down
8 changes: 8 additions & 0 deletions runtime/vm/compiler/backend/il_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,14 @@ void CoroutineTransferStubInstr::PrintOperandsTo(BaseTextBuffer* f) const {
f->AddString(")");
}

void CoroutineForkStubInstr::PrintOperandsTo(BaseTextBuffer* f) const {
const char* name = "CoroutineFork";
f->Printf("%s(", name);
from()->PrintTo(f);
to()->PrintTo(f);
f->AddString(")");
}

void SuspendInstr::PrintOperandsTo(BaseTextBuffer* f) const {
const char* name = "";
switch (stub_id_) {
Expand Down
4 changes: 3 additions & 1 deletion runtime/vm/compiler/backend/slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ 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, caller, Coroutine, VAR) \
V(Coroutine, UntaggedCoroutine, entry, 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
42 changes: 36 additions & 6 deletions runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3375,6 +3375,8 @@ Fragment StreamingFlowGraphBuilder::BuildStaticInvocation(TokenPosition* p) {
return BuildCoroutineInitialize();
case MethodRecognizer::kCoroutineTransfer:
return BuildCoroutineTransfer();
case MethodRecognizer::kCoroutineFork:
return BuildCoroutineFork();
case MethodRecognizer::kNativeEffect:
return BuildNativeEffect();
case MethodRecognizer::kReachabilityFence:
Expand Down Expand Up @@ -6020,22 +6022,50 @@ Fragment StreamingFlowGraphBuilder::BuildNativeEffect() {

Fragment StreamingFlowGraphBuilder::BuildCoroutineInitialize() {
Fragment instructions;
Array& argument_names = Array::ZoneHandle(Z);
instructions += BuildArguments(&argument_names, nullptr /* arg count */, nullptr /* positional arg count */);
ReadUInt();
ReadListLength();
ReadListLength();
instructions += BuildExpression();
ReadListLength();
instructions += B->CoroutineInitialize(TokenPosition::kNoSource);
instructions += NullConstant();
return instructions;
}

Fragment StreamingFlowGraphBuilder::BuildCoroutineTransfer() {
Fragment instructions;
Array& argument_names = Array::ZoneHandle(Z);
instructions += BuildArguments(&argument_names, nullptr /* arg count */, nullptr /* positional arg count */);
ReadUInt();
ReadListLength();
ReadListLength();
instructions += BuildExpression();
LocalVariable* from = MakeTemporary();
instructions += LoadLocal(from);
instructions += BuildExpression();
LocalVariable* to = MakeTemporary();
instructions += LoadLocal(to);
ReadListLength();
instructions += B->CoroutineTransfer(TokenPosition::kNoSource);
instructions += NullConstant();
instructions += Drop();
instructions += Drop();
return instructions;
}

Fragment StreamingFlowGraphBuilder::BuildCoroutineFork() {
Fragment instructions;
ReadUInt();
ReadListLength();
ReadListLength();
instructions += BuildExpression();
LocalVariable* from = MakeTemporary();
instructions += LoadLocal(from);
instructions += BuildExpression();
LocalVariable* to = MakeTemporary();
instructions += LoadLocal(to);
ReadListLength();
instructions += B->CoroutineFork(TokenPosition::kNoSource);
instructions += Drop();
instructions += Drop();
return instructions;
}

Fragment StreamingFlowGraphBuilder::BuildReachabilityFence() {
const intptr_t argc = ReadUInt(); // Read argument count.
Expand Down
2 changes: 2 additions & 0 deletions runtime/vm/compiler/frontend/kernel_binary_flowgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ class StreamingFlowGraphBuilder : public KernelReaderHelper {

Fragment BuildCoroutineTransfer();

Fragment BuildCoroutineFork();

// Build the call-site manually, to avoid doing initialization checks
// for late fields.
Fragment BuildReachabilityFence();
Expand Down
Loading

0 comments on commit 7df9a5a

Please sign in to comment.