compiler,runtime: fix new task-based scheduler

A bug was introduced in the previous commit that led to miscompilations
in the time.Sleep function when the scheduler was disabled, because
time.Sleep (implemented in the runtime) tried to switch to the scheduler
stack.

This commit restores the binary size of most examples to what it was
before, but still reduces static RAM consumption (.bss) slightly. This
gives me some confidence that it does indeed fix the introduced bug.
Этот коммит содержится в:
Ayke van Laethem 2019-08-16 15:22:12 +02:00 коммит произвёл Ron Evans
родитель 542135c357
коммит e4fc3bb66a
3 изменённых файлов: 11 добавлений и 0 удалений

Просмотреть файл

@ -37,6 +37,7 @@ var functionsUsedInTransforms = []string{
"runtime.alloc", "runtime.alloc",
"runtime.free", "runtime.free",
"runtime.sleepTask", "runtime.sleepTask",
"runtime.sleepCurrentTask",
"runtime.setTaskStatePtr", "runtime.setTaskStatePtr",
"runtime.getTaskStatePtr", "runtime.getTaskStatePtr",
"runtime.activateTask", "runtime.activateTask",

Просмотреть файл

@ -150,6 +150,10 @@ func (c *Compiler) lowerTasks() error {
zero := llvm.ConstInt(c.uintptrType, 0, false) zero := llvm.ConstInt(c.uintptrType, 0, false)
c.createRuntimeCall("startGoroutine", []llvm.Value{realMainWrapper, zero}, "") c.createRuntimeCall("startGoroutine", []llvm.Value{realMainWrapper, zero}, "")
c.createRuntimeCall("scheduler", nil, "") c.createRuntimeCall("scheduler", nil, "")
sleep := c.mod.NamedFunction("time.Sleep")
if !sleep.IsNil() {
sleep.ReplaceAllUsesWith(c.mod.NamedFunction("runtime.sleepCurrentTask"))
}
} else { } else {
// Program doesn't need a scheduler. Call main.main directly. // Program doesn't need a scheduler. Call main.main directly.
c.builder.SetInsertPointBefore(mainCall) c.builder.SetInsertPointBefore(mainCall)

Просмотреть файл

@ -98,6 +98,12 @@ func startGoroutine(fn, args uintptr) {
//go:linkname sleep time.Sleep //go:linkname sleep time.Sleep
func sleep(d int64) { func sleep(d int64) {
sleepTicks(timeUnit(d / tickMicros))
}
// sleepCurrentTask suspends the current goroutine. This is a compiler
// intrinsic. It replaces calls to time.Sleep when a scheduler is in use.
func sleepCurrentTask(d int64) {
sleepTask(currentTask, d) sleepTask(currentTask, d)
swapTask(currentTask, &schedulerState) swapTask(currentTask, &schedulerState)
} }