Skip to content

compiler,runtime: implement stack-based scheduler #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2019

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Aug 15, 2019

This scheduler is intended to live along the (stackless) coroutine based
scheduler which is needed for WebAssembly and unsupported platforms. The
stack based scheduler is somewhat simpler in implementation as it does
not require full program transform passes and supports things like
function pointers and interface methods out of the box with no changes.

Code size is reduced in most cases, even in the case where no scheduler
scheduler is used at all. I'm not exactly sure why but these changes
likely allowed some further optimizations somewhere. Even RAM is
slightly reduced, perhaps some global was elminated in the process as
well.

This scheduler is intended to live along the (stackless) coroutine based
scheduler which is needed for WebAssembly and unsupported platforms. The
stack based scheduler is somewhat simpler in implementation as it does
not require full program transform passes and supports things like
function pointers and interface methods out of the box with no changes.

Code size is reduced in most cases, even in the case where no scheduler
scheduler is used at all. I'm not exactly sure why but these changes
likely allowed some further optimizations somewhere. Even RAM is
slightly reduced, perhaps some global was elminated in the process as
well.
@aykevl aykevl force-pushed the stackful-scheduler branch from 2d46e95 to 11b818e Compare August 15, 2019 14:02
@aykevl
Copy link
Member Author

aykevl commented Aug 15, 2019

Note that this will conflict with #487. Either could be merged first, I'll fix the conflicts in the other PR.

@aykevl
Copy link
Member Author

aykevl commented Aug 15, 2019

This PR sets a fixed goroutine stack size of 1kB by default. I am not sure what a good default size would be, that should probably be made configurable at some point. But a default of 1kB may be good enough for most embedded systems. Also: TinyGo likely requires less stack size anyway than the main Go implementation (untested!) because of the different calling convention and the fact it is usually used for 32-bit systems (halving int and pointer sizes).
There is a check in place that should catch most stack overflows when switching between stacks, but other than that a stack overflow may corrupt memory at the moment. I have a long-term goal of using the .stack_size section to determine the stack size whenever possible at compile time and inserting runtime checks when that is impossible, but that will require a decent amount of work.

This new goroutine implementation can be switched off by passing the -scheduler=coroutines option. The default for Cortex-M is -scheduler=tasks and elsewhere -scheduler=coroutines.

@deadprogram
Copy link
Member

I can confirm that this PR does address the issue I encountered with my use of multiple go routines.

Merging this PR, so that #487 can have the conflicts resolved.

@deadprogram deadprogram merged commit 542135c into dev Aug 15, 2019
@deadprogram deadprogram deleted the stackful-scheduler branch August 15, 2019 15:36
@deadprogram
Copy link
Member

OK spoke too soon. It did solve my compilation program, but then code did not operate.

I then went and looked more closely, and discovered that the USB CDC code on the SAMD21-based boards does not function after this commit.

This code no longer works, specifically the USB port does not appear on my machine as expected:

tinygo flash --target arduino-nano33 examples/serial

However this works:

tinygo flash -scheduler=coroutines -target arduino-nano33 examples/serial

Would appear to be the new scheduler...

@deadprogram deadprogram restored the stackful-scheduler branch August 15, 2019 18:56
@aykevl
Copy link
Member Author

aykevl commented Aug 15, 2019

Ouch, I should have investigated why there was a code size change at all. Because I probably introduced a bug somewhere that led to LLVM optimizing something it shouldn't.

I will investigate tomorrow.

UPDATE: see #507

@deadprogram deadprogram deleted the stackful-scheduler branch September 25, 2019 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants