Skip to content

Commit

Permalink
Merge pull request 'Fibers' (dart-lang#16) from fibers into main
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbashir committed Nov 1, 2024
2 parents 1f38b23 + 30746b5 commit 9ffee69
Show file tree
Hide file tree
Showing 30 changed files with 818 additions and 688 deletions.
2 changes: 1 addition & 1 deletion runtime/platform/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <functional>
#include <cassert> // For assert() in constant expressions.

#if defined(_WIN32)
Expand Down
101 changes: 49 additions & 52 deletions runtime/tests/vm/dart/fiber/fiber_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import 'package:expect/expect.dart';
var globalState = "";

void main() {
// testBase();

while (true) {
testClosures();
}
// testRecycle();
testBase();
testClosures();
testRecycle();
}

void testBase() {
Expand Down Expand Up @@ -60,51 +57,51 @@ void testClosures() {
);
Expect.equals("after fiber", localState);

// localState = "localState";
// Fiber.launch(
// () {
// Expect.equals("localState", localState);
// localState = "after main fiber";
// Fiber.schedule(Fiber.current());
// Fiber.spawn(
// () {
// Expect.equals("after main fiber", localState);
// localState = "after child fiber";
// Fiber.reschedule();
// Expect.equals("after child fiber after main fiber", localState);
// localState = "finish";
// },
// name: "child",
// );
// Expect.equals("after child fiber", localState);
// localState = "after child fiber after main fiber";
// Fiber.suspend();
// },
// terminate: true,
// );
// Expect.equals("finish", localState);
localState = "localState";
Fiber.launch(
() {
Expect.equals("localState", localState);
localState = "after main fiber";
Fiber.schedule(Fiber.current());
Fiber.spawn(
() {
Expect.equals("after main fiber", localState);
localState = "after child fiber";
Fiber.reschedule();
Expect.equals("after child fiber after main fiber", localState);
localState = "finish";
},
name: "child",
);
Expect.equals("after child fiber", localState);
localState = "after child fiber after main fiber";
Fiber.suspend();
},
terminate: true,
);
Expect.equals("finish", localState);

// localState = "level 1";
// Fiber.launch(
// () {
// Expect.equals("level 1", localState);
// localState = "level 2";
// Fiber.spawn(
// () {
// Expect.equals("level 2", localState);
// localState = "level 3";
// Fiber.spawn(
// () {
// Expect.equals("level 3", localState);
// localState = "level 4";
// },
// name: "child",
// );
// },
// name: "child",
// );
// },
// terminate: true,
// );
// Expect.equals("level 4", localState);
localState = "level 1";
Fiber.launch(
() {
Expect.equals("level 1", localState);
localState = "level 2";
Fiber.spawn(
() {
Expect.equals("level 2", localState);
localState = "level 3";
Fiber.spawn(
() {
Expect.equals("level 3", localState);
localState = "level 4";
},
name: "child",
);
},
name: "child",
);
},
terminate: true,
);
Expect.equals("level 4", localState);
}
3 changes: 1 addition & 2 deletions runtime/vm/compiler/backend/il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8614,8 +8614,7 @@ void CoroutineTransferInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
__ LoadFieldFromOffset(SPREG, kToCoroutine, Coroutine::stack_base_offset());
__ PopRegister(FPREG);
if (!FLAG_precompiled_mode) __ RestoreCodePointer();
if (FLAG_precompiled_mode)
__ movq(PP, compiler::Address(THR, Thread::global_object_pool_offset()));
if (FLAG_precompiled_mode) __ movq(PP, compiler::Address(THR, Thread::global_object_pool_offset()));

__ LoadFieldFromOffset(kToStackLimit, kToCoroutine, Coroutine::overflow_stack_limit_offset());
__ StoreToOffset(kToCoroutine, THR, Thread::coroutine_offset());
Expand Down
2 changes: 0 additions & 2 deletions runtime/vm/compiler/backend/range_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2836,8 +2836,6 @@ void LoadFieldInstr::InferRange(RangeAnalysis* analysis, Range* range) {

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

Expand Down
2 changes: 0 additions & 2 deletions runtime/vm/compiler/backend/slot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ bool Slot::IsImmutableLengthSlot() const {
case Slot::Kind::kClosure_hash:
case Slot::Kind::kRecord_shape:
case Slot::Kind::kAbstractType_hash:
case Slot::Kind::kCoroutine_attributes:
case Slot::Kind::kCoroutine_index:
return false;
}
UNREACHABLE();
Expand Down
8 changes: 4 additions & 4 deletions runtime/vm/compiler/backend/slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ 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(Coroutine, UntaggedCoroutine, index, Smi, VAR) \
V(Coroutine, UntaggedCoroutine, attributes, Smi, VAR)
V(AbstractType, UntaggedTypeArguments, hash, 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 @@ -189,7 +187,9 @@ class ParsedFunction;
FINAL) \
V(FunctionType, UntaggedFunctionType, packed_type_parameter_counts, Uint16, \
FINAL) \
V(SubtypeTestCache, UntaggedSubtypeTestCache, num_inputs, Uint32, FINAL)
V(SubtypeTestCache, UntaggedSubtypeTestCache, num_inputs, Uint32, FINAL) \
V(Coroutine, UntaggedCoroutine, index, IntPtr, VAR) \
V(Coroutine, UntaggedCoroutine, attributes, IntPtr, VAR)

// Native slots containing untagged addresses that do not exist in JIT mode.
// See UNTAGGED_NATIVE_DART_SLOTS_LIST for the format.
Expand Down
26 changes: 23 additions & 3 deletions runtime/vm/compiler/frontend/kernel_to_il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -926,13 +926,11 @@ const Function& TypedListGetNativeFunction(Thread* thread, classid_t cid) {
V(Coroutine_getEntry, Coroutine_entry) \
V(Coroutine_getTrampoline, Coroutine_trampoline) \
V(Coroutine_getArguments, Coroutine_arguments) \
V(Coroutine_getAttributes, Coroutine_attributes) \
V(Coroutine_getCaller, Coroutine_caller) \
V(Coroutine_getScheduler, Coroutine_scheduler) \
V(Coroutine_getProcessor, Coroutine_processor) \
V(Coroutine_getToProcessorNext, Coroutine_to_processor_next) \
V(Coroutine_getToProcessorPrevious, Coroutine_to_processor_previous) \
V(Coroutine_getIndex, Coroutine_index) \
V(SuspendState_getThenCallback, SuspendState_then_callback) \
V(SuspendState_getErrorCallback, SuspendState_error_callback) \
V(TypedDataViewOffsetInBytes, TypedDataView_offset_in_bytes) \
Expand All @@ -958,7 +956,6 @@ const Function& TypedListGetNativeFunction(Thread* thread, classid_t cid) {
V(Coroutine_setEntry, Coroutine_entry) \
V(Coroutine_setTrampoline, Coroutine_trampoline) \
V(Coroutine_setArguments, Coroutine_arguments) \
V(Coroutine_setAttributes, Coroutine_attributes) \
V(Coroutine_setCaller, Coroutine_caller) \
V(Coroutine_setScheduler, Coroutine_scheduler) \
V(Coroutine_setProcessor, Coroutine_processor) \
Expand Down Expand Up @@ -1165,6 +1162,9 @@ bool FlowGraphBuilder::IsRecognizedMethodForFlowGraph(
case MethodRecognizer::kCoroutineTransfer:
case MethodRecognizer::kCoroutine_getCurrent:
case MethodRecognizer::kCoroutine_atIndex:
case MethodRecognizer::kCoroutine_getAttributes:
case MethodRecognizer::kCoroutine_setAttributes:
case MethodRecognizer::kCoroutine_getIndex:
return true;
default:
return false;
Expand Down Expand Up @@ -1969,6 +1969,26 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfRecognizedMethod(
body += LoadIndexed(kArrayCid);
break;
}
case MethodRecognizer::kCoroutine_getIndex: {
body += LoadLocal(parsed_function_->RawParameterVariable(0));
body += LoadNativeField(Slot::Coroutine_index());
body += Box(kUnboxedInt64);
break;
}
case MethodRecognizer::kCoroutine_getAttributes: {
body += LoadLocal(parsed_function_->RawParameterVariable(0));
body += LoadNativeField(Slot::Coroutine_attributes());
body += Box(kUnboxedInt64);
break;
}
case MethodRecognizer::kCoroutine_setAttributes: {
body += LoadLocal(parsed_function_->RawParameterVariable(0));
body += LoadLocal(parsed_function_->RawParameterVariable(1));
body += UnboxTruncate(kUnboxedInt64);
body += StoreNativeField(Slot::Coroutine_attributes());
body += NullConstant();
break;
}
#define IL_BODY(method, slot) \
case MethodRecognizer::k##method: \
ASSERT_EQUAL(function.NumParameters(), 1); \
Expand Down
Loading

0 comments on commit 9ffee69

Please sign in to comment.