-
Notifications
You must be signed in to change notification settings - Fork 852
/
coroutines.go
1106 lines (936 loc) · 38.5 KB
/
coroutines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package transform
// This file lowers asynchronous functions and goroutine starts when using the coroutines scheduler.
// This is accomplished by inserting LLVM intrinsics which are used in order to save the states of functions.
import (
"errors"
"go/token"
"strconv"
"github.com/tinygo-org/tinygo/compiler/llvmutil"
"tinygo.org/x/go-llvm"
)
// LowerCoroutines turns async functions into coroutines.
// This must be run with the coroutines scheduler.
//
// Before this pass, goroutine starts are expressed as a call to an intrinsic called "internal/task.start".
// This intrinsic accepts the function pointer and a pointer to a struct containing the function's arguments.
//
// Before this pass, an intrinsic called "internal/task.Pause" is used to express suspensions of the current goroutine.
//
// This pass first accumulates a list of blocking functions.
// A function is considered "blocking" if it calls "internal/task.Pause" or any other blocking function.
//
// Blocking calls are implemented by turning blocking functions into a coroutine.
// The body of each blocking function is modified to start a new coroutine, and to return after the first suspend.
// After calling a blocking function, the caller coroutine suspends.
// The caller also provides a buffer to store the return value into.
// When a blocking function returns, the return value is written into this buffer and then the caller is queued to run.
//
// Goroutine starts which invoke non-blocking functions are implemented as direct calls.
// Goroutine starts are replaced with the creation of a new task data structure followed by a call to the start of the blocking function.
// The task structure is populated with a "noop" coroutine before invoking the blocking function.
// When the blocking function returns, it resumes this "noop" coroutine which does nothing.
// The goroutine starter is able to continue after the first suspend of the started goroutine.
//
// The transformation of a function to a coroutine is accomplished using LLVM's coroutines system (https://llvm.org/docs/Coroutines.html).
// The simplest implementation of a coroutine inserts a suspend point after every blocking call.
//
// Transforming blocking functions into coroutines and calls into suspend points is extremely expensive.
// In many cases, a blocking call is followed immediately by a function terminator (a return or an "unreachable" instruction).
// This is a blocking "tail call".
// In a non-returning tail call (call to a non-returning function, such as an infinite loop), the coroutine can exit without any extra work.
// In a returning tail call, the returned value must either be the return of the call or a value known before the call.
// If the return value of the caller is the return of the callee, the coroutine can exit without any extra work and the tailed call will instead return to the caller of the caller.
// If the return value is known in advance, this result can be stored into the parent's return buffer before the call so that a suspend is unnecessary.
// If the callee returns an unnecessary value, a return buffer can be allocated on the heap so that it will outlive the coroutine.
//
// In the implementation of time.Sleep, the current task is pushed onto a timer queue and then suspended.
// Since the only suspend point is a call to "internal/task.Pause" followed by a return, there is no need to transform this into a coroutine.
// This generalizes to all blocking functions in which all suspend points can be elided.
// This optimization saves a substantial amount of binary size.
func LowerCoroutines(mod llvm.Module, needStackSlots bool) error {
ctx := mod.Context()
builder := ctx.NewBuilder()
defer builder.Dispose()
target := llvm.NewTargetData(mod.DataLayout())
defer target.Dispose()
pass := &coroutineLoweringPass{
mod: mod,
ctx: ctx,
builder: builder,
target: target,
needStackSlots: needStackSlots,
}
err := pass.load()
if err != nil {
return err
}
// Supply task operands to async calls.
pass.supplyTaskOperands()
// Analyze async returns.
pass.returnAnalysisPass()
// Categorize async calls.
pass.categorizeCalls()
// Lower async functions.
pass.lowerFuncsPass()
// Lower calls to internal/task.Current.
pass.lowerCurrent()
// Lower goroutine starts.
pass.lowerStartsPass()
// Fix annotations on async call params.
pass.fixAnnotations()
if needStackSlots {
// Set up garbage collector tracking of tasks at start.
err = pass.trackGoroutines()
if err != nil {
return err
}
}
return nil
}
// CoroutinesError is an error returned when coroutine lowering failed, for
// example because an async function is exported.
type CoroutinesError struct {
Msg string
Pos token.Position
Traceback []CoroutinesErrorLine
}
// CoroutinesErrorLine is a single line of a CoroutinesError traceback.
type CoroutinesErrorLine struct {
Name string // function name
Position token.Position // position in the function
}
// Error implements the error interface by returning a simple error message
// without the stack.
func (err CoroutinesError) Error() string {
return err.Msg
}
type asyncCallInfo struct {
fn llvm.Value
call llvm.Value
}
// asyncFunc is a metadata container for an asynchronous function.
type asyncFunc struct {
// fn is the underlying function pointer.
fn llvm.Value
// rawTask is the parameter where the task pointer is passed in.
rawTask llvm.Value
// callers is a set of all functions which call this async function.
callers map[llvm.Value]struct{}
// returns is a list of returns in the function, along with metadata.
returns []asyncReturn
// calls is a list of all calls in the asyncFunc.
// normalCalls is a list of all intermideate suspending calls in the asyncFunc.
// tailCalls is a list of all tail calls in the asyncFunc.
calls, normalCalls, tailCalls []llvm.Value
}
// asyncReturn is a metadata container for a return from an asynchronous function.
type asyncReturn struct {
// block is the basic block terminated by the return.
block llvm.BasicBlock
// kind is the kind of the return.
kind returnKind
}
// coroutineLoweringPass is a goroutine lowering pass which is used with the "coroutines" scheduler.
type coroutineLoweringPass struct {
mod llvm.Module
ctx llvm.Context
builder llvm.Builder
target llvm.TargetData
// asyncFuncs is a map of all asyncFuncs.
// The map keys are function pointers.
asyncFuncs map[llvm.Value]*asyncFunc
asyncFuncsOrdered []*asyncFunc
// calls is a slice of all of the async calls in the module.
calls []llvm.Value
i8ptr llvm.Type
// memory management functions from the runtime
alloc, free llvm.Value
// coroutine intrinsics
start, pause, current llvm.Value
setState, setRetPtr, getRetPtr, returnTo, returnCurrent llvm.Value
createTask llvm.Value
// llvm.coro intrinsics
coroId, coroSize, coroBegin, coroSuspend, coroEnd, coroFree, coroSave llvm.Value
trackPointer llvm.Value
needStackSlots bool
}
// findAsyncFuncs finds all asynchronous functions.
// A function is considered asynchronous if it calls an asynchronous function or intrinsic.
func (c *coroutineLoweringPass) findAsyncFuncs() error {
asyncs := map[llvm.Value]*asyncFunc{}
asyncsOrdered := []llvm.Value{}
calls := []llvm.Value{}
callsAsyncFunction := map[llvm.Value]asyncCallInfo{}
// Use a breadth-first search to find all async functions.
worklist := []llvm.Value{c.pause}
for len(worklist) > 0 {
// Pop a function off the worklist.
fn := worklist[0]
worklist = worklist[1:]
// Get task pointer argument.
task := fn.LastParam()
if fn != c.pause && (task.IsNil() || task.Name() != "parentHandle") {
// Exported functions must not do async operations.
err := CoroutinesError{
Msg: "blocking operation in exported function: " + fn.Name(),
Pos: getPosition(fn),
}
f := fn
for !f.IsNil() && f != c.pause {
data := callsAsyncFunction[f]
err.Traceback = append(err.Traceback, CoroutinesErrorLine{f.Name(), getPosition(data.call)})
f = data.fn
}
return err
}
// Search all uses of the function while collecting callers.
callers := map[llvm.Value]struct{}{}
for use := fn.FirstUse(); !use.IsNil(); use = use.NextUse() {
user := use.User()
if user.IsACallInst().IsNil() {
// User is not a call instruction, so this is irrelevant.
continue
}
if user.CalledValue() != fn {
// Not the called value.
continue
}
// Add to calls list.
calls = append(calls, user)
// Get the caller.
caller := user.InstructionParent().Parent()
// Add as caller.
callers[caller] = struct{}{}
if _, ok := asyncs[caller]; ok {
// Already marked caller as async.
continue
}
// Mark the caller as async.
// Use nil as a temporary value. It will be replaced later.
asyncs[caller] = nil
asyncsOrdered = append(asyncsOrdered, caller)
// Track which calls caused this function to be marked async (for
// better diagnostics).
callsAsyncFunction[caller] = asyncCallInfo{
fn: fn,
call: user,
}
// Put the caller on the worklist.
worklist = append(worklist, caller)
}
asyncs[fn] = &asyncFunc{
fn: fn,
rawTask: task,
callers: callers,
}
}
// Flip the order of the async functions so that the top ones are lowered first.
for i := 0; i < len(asyncsOrdered)/2; i++ {
asyncsOrdered[i], asyncsOrdered[len(asyncsOrdered)-(i+1)] = asyncsOrdered[len(asyncsOrdered)-(i+1)], asyncsOrdered[i]
}
// Map the elements of asyncsOrdered to *asyncFunc.
asyncFuncsOrdered := make([]*asyncFunc, len(asyncsOrdered))
for i, v := range asyncsOrdered {
asyncFuncsOrdered[i] = asyncs[v]
}
c.asyncFuncs = asyncs
c.asyncFuncsOrdered = asyncFuncsOrdered
c.calls = calls
return nil
}
func (c *coroutineLoweringPass) load() error {
// Find memory management functions from the runtime.
c.alloc = c.mod.NamedFunction("runtime.alloc")
if c.alloc.IsNil() {
return ErrMissingIntrinsic{"runtime.alloc"}
}
c.free = c.mod.NamedFunction("runtime.free")
if c.free.IsNil() {
return ErrMissingIntrinsic{"runtime.free"}
}
// Find intrinsics.
c.pause = c.mod.NamedFunction("internal/task.Pause")
if c.pause.IsNil() {
return ErrMissingIntrinsic{"internal/task.Pause"}
}
c.start = c.mod.NamedFunction("internal/task.start")
if c.start.IsNil() {
return ErrMissingIntrinsic{"internal/task.start"}
}
c.current = c.mod.NamedFunction("internal/task.Current")
if c.current.IsNil() {
return ErrMissingIntrinsic{"internal/task.Current"}
}
c.setState = c.mod.NamedFunction("(*internal/task.Task).setState")
if c.setState.IsNil() {
return ErrMissingIntrinsic{"(*internal/task.Task).setState"}
}
c.setRetPtr = c.mod.NamedFunction("(*internal/task.Task).setReturnPtr")
if c.setRetPtr.IsNil() {
return ErrMissingIntrinsic{"(*internal/task.Task).setReturnPtr"}
}
c.getRetPtr = c.mod.NamedFunction("(*internal/task.Task).getReturnPtr")
if c.getRetPtr.IsNil() {
return ErrMissingIntrinsic{"(*internal/task.Task).getReturnPtr"}
}
c.returnTo = c.mod.NamedFunction("(*internal/task.Task).returnTo")
if c.returnTo.IsNil() {
return ErrMissingIntrinsic{"(*internal/task.Task).returnTo"}
}
c.returnCurrent = c.mod.NamedFunction("(*internal/task.Task).returnCurrent")
if c.returnCurrent.IsNil() {
return ErrMissingIntrinsic{"(*internal/task.Task).returnCurrent"}
}
c.createTask = c.mod.NamedFunction("internal/task.createTask")
if c.createTask.IsNil() {
return ErrMissingIntrinsic{"internal/task.createTask"}
}
if c.needStackSlots {
c.trackPointer = c.mod.NamedFunction("runtime.trackPointer")
if c.trackPointer.IsNil() {
return ErrMissingIntrinsic{"runtime.trackPointer"}
}
}
// Find async functions.
err := c.findAsyncFuncs()
if err != nil {
return err
}
// Get i8* type.
c.i8ptr = llvm.PointerType(c.ctx.Int8Type(), 0)
// Build LLVM coroutine intrinsic.
coroIdType := llvm.FunctionType(c.ctx.TokenType(), []llvm.Type{c.ctx.Int32Type(), c.i8ptr, c.i8ptr, c.i8ptr}, false)
c.coroId = llvm.AddFunction(c.mod, "llvm.coro.id", coroIdType)
sizeT := c.alloc.Param(0).Type()
coroSizeType := llvm.FunctionType(sizeT, nil, false)
c.coroSize = llvm.AddFunction(c.mod, "llvm.coro.size.i"+strconv.Itoa(sizeT.IntTypeWidth()), coroSizeType)
coroBeginType := llvm.FunctionType(c.i8ptr, []llvm.Type{c.ctx.TokenType(), c.i8ptr}, false)
c.coroBegin = llvm.AddFunction(c.mod, "llvm.coro.begin", coroBeginType)
coroSuspendType := llvm.FunctionType(c.ctx.Int8Type(), []llvm.Type{c.ctx.TokenType(), c.ctx.Int1Type()}, false)
c.coroSuspend = llvm.AddFunction(c.mod, "llvm.coro.suspend", coroSuspendType)
coroEndType := llvm.FunctionType(c.ctx.Int1Type(), []llvm.Type{c.i8ptr, c.ctx.Int1Type()}, false)
c.coroEnd = llvm.AddFunction(c.mod, "llvm.coro.end", coroEndType)
coroFreeType := llvm.FunctionType(c.i8ptr, []llvm.Type{c.ctx.TokenType(), c.i8ptr}, false)
c.coroFree = llvm.AddFunction(c.mod, "llvm.coro.free", coroFreeType)
coroSaveType := llvm.FunctionType(c.ctx.TokenType(), []llvm.Type{c.i8ptr}, false)
c.coroSave = llvm.AddFunction(c.mod, "llvm.coro.save", coroSaveType)
return nil
}
func (c *coroutineLoweringPass) track(ptr llvm.Value) {
if c.needStackSlots {
if ptr.Type() != c.i8ptr {
ptr = c.builder.CreateBitCast(ptr, c.i8ptr, "track.bitcast")
}
c.builder.CreateCall(c.trackPointer, []llvm.Value{ptr, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
}
}
// lowerStartSync lowers a goroutine start of a synchronous function to a synchronous call.
func (c *coroutineLoweringPass) lowerStartSync(start llvm.Value) {
c.builder.SetInsertPointBefore(start)
// Get function to call.
fn := start.Operand(0).Operand(0)
// Create the list of params for the call.
paramTypes := fn.Type().ElementType().ParamTypes()
params := llvmutil.EmitPointerUnpack(c.builder, c.mod, start.Operand(1), paramTypes[:len(paramTypes)-1])
params = append(params, llvm.Undef(c.i8ptr))
// Generate call to function.
c.builder.CreateCall(fn, params, "")
// Remove start call.
start.EraseFromParentAsInstruction()
}
// supplyTaskOperands fills in the task operands of async calls.
func (c *coroutineLoweringPass) supplyTaskOperands() {
var curCalls []llvm.Value
for use := c.current.FirstUse(); !use.IsNil(); use = use.NextUse() {
curCalls = append(curCalls, use.User())
}
for _, call := range append(curCalls, c.calls...) {
c.builder.SetInsertPointBefore(call)
task := c.asyncFuncs[call.InstructionParent().Parent()].rawTask
call.SetOperand(call.OperandsCount()-2, task)
}
}
// returnKind is a classification of a type of function terminator.
type returnKind uint8
const (
// returnNormal is a terminator that returns a value normally from a function.
returnNormal returnKind = iota
// returnVoid is a terminator that exits normally without returning a value.
returnVoid
// returnVoidTail is a terminator which is a tail call to a void-returning function in a void-returning function.
returnVoidTail
// returnTail is a terinator which is a tail call to a value-returning function where the value is returned by the callee.
returnTail
// returnDeadTail is a terminator which is a call to a non-returning asynchronous function.
returnDeadTail
// returnAlternateTail is a terminator which is a tail call to a value-returning function where a previously acquired value is returned by the callee.
returnAlternateTail
// returnDitchedTail is a terminator which is a tail call to a value-returning function, where the callee returns void.
returnDitchedTail
// returnDelayedValue is a terminator in which a void-returning tail call is followed by a return of a previous value.
returnDelayedValue
)
// isAsyncCall returns whether the specified call is async.
func (c *coroutineLoweringPass) isAsyncCall(call llvm.Value) bool {
_, ok := c.asyncFuncs[call.CalledValue()]
return ok
}
// analyzeFuncReturns analyzes and classifies the returns of a function.
func (c *coroutineLoweringPass) analyzeFuncReturns(fn *asyncFunc) {
returns := []asyncReturn{}
if fn.fn == c.pause {
// Skip pause.
fn.returns = returns
return
}
for _, bb := range fn.fn.BasicBlocks() {
last := bb.LastInstruction()
switch last.InstructionOpcode() {
case llvm.Ret:
// Check if it is a void return.
isVoid := fn.fn.Type().ElementType().ReturnType().TypeKind() == llvm.VoidTypeKind
// Analyze previous instruction.
prev := llvm.PrevInstruction(last)
switch {
case prev.IsNil():
fallthrough
case prev.IsACallInst().IsNil():
fallthrough
case !c.isAsyncCall(prev):
// This is not any form of asynchronous tail call.
if isVoid {
returns = append(returns, asyncReturn{
block: bb,
kind: returnVoid,
})
} else {
returns = append(returns, asyncReturn{
block: bb,
kind: returnNormal,
})
}
case isVoid:
if prev.CalledValue().Type().ElementType().ReturnType().TypeKind() == llvm.VoidTypeKind {
// This is a tail call to a void-returning function from a function with a void return.
returns = append(returns, asyncReturn{
block: bb,
kind: returnVoidTail,
})
} else {
// This is a tail call to a value-returning function from a function with a void return.
// The returned value will be ditched.
returns = append(returns, asyncReturn{
block: bb,
kind: returnDitchedTail,
})
}
case last.Operand(0) == prev:
// This is a regular tail call. The return of the callee is returned to the parent.
returns = append(returns, asyncReturn{
block: bb,
kind: returnTail,
})
case prev.CalledValue().Type().ElementType().ReturnType().TypeKind() == llvm.VoidTypeKind:
// This is a tail call that returns a previous value after waiting on a void function.
returns = append(returns, asyncReturn{
block: bb,
kind: returnDelayedValue,
})
default:
// This is a tail call that returns a value that is available before the function call.
returns = append(returns, asyncReturn{
block: bb,
kind: returnAlternateTail,
})
}
case llvm.Unreachable:
prev := llvm.PrevInstruction(last)
if prev.IsNil() || prev.IsACallInst().IsNil() || !c.isAsyncCall(prev) {
// This unreachable instruction does not behave as an asynchronous return.
continue
}
// This is an asyncnhronous tail call to function that does not return.
returns = append(returns, asyncReturn{
block: bb,
kind: returnDeadTail,
})
}
}
fn.returns = returns
}
// returnAnalysisPass runs an analysis pass which classifies the returns of all async functions.
func (c *coroutineLoweringPass) returnAnalysisPass() {
for _, async := range c.asyncFuncsOrdered {
c.analyzeFuncReturns(async)
}
}
// categorizeCalls categorizes all asynchronous calls into regular vs. async and matches them to their callers.
func (c *coroutineLoweringPass) categorizeCalls() {
// Sort calls into their respective callers.
for _, call := range c.calls {
caller := c.asyncFuncs[call.InstructionParent().Parent()]
caller.calls = append(caller.calls, call)
}
// Seperate regular and tail calls.
for _, async := range c.asyncFuncsOrdered {
// Search returns for tail calls.
tails := map[llvm.Value]struct{}{}
for _, ret := range async.returns {
switch ret.kind {
case returnVoidTail, returnTail, returnDeadTail, returnAlternateTail, returnDitchedTail, returnDelayedValue:
// This is a tail return. The previous instruction is a tail call.
tails[llvm.PrevInstruction(ret.block.LastInstruction())] = struct{}{}
}
}
// Seperate tail calls and regular calls.
normalCalls, tailCalls := []llvm.Value{}, []llvm.Value{}
for _, call := range async.calls {
if _, ok := tails[call]; ok {
// This is a tail call.
tailCalls = append(tailCalls, call)
} else {
// This is a regular call.
normalCalls = append(normalCalls, call)
}
}
async.normalCalls = normalCalls
async.tailCalls = tailCalls
}
}
// lowerFuncsPass lowers all functions, turning them into coroutines if necessary.
func (c *coroutineLoweringPass) lowerFuncsPass() {
for _, fn := range c.asyncFuncs {
if fn.fn == c.pause {
// Skip. It is an intrinsic.
continue
}
if len(fn.normalCalls) == 0 && fn.fn.FirstBasicBlock().FirstInstruction().IsAAllocaInst().IsNil() {
// No suspend points or stack allocations. Lower without turning it into a coroutine.
c.lowerFuncFast(fn)
} else {
// There are suspend points or stack allocations, so it is necessary to turn this into a coroutine.
c.lowerFuncCoro(fn)
}
}
}
func (async *asyncFunc) hasValueStoreReturn() bool {
for _, ret := range async.returns {
switch ret.kind {
case returnNormal, returnAlternateTail, returnDelayedValue:
return true
}
}
return false
}
// heapAlloc creates a heap allocation large enough to hold the supplied type.
// The allocation is returned as a raw i8* pointer.
// This allocation is not automatically tracked by the garbage collector, and should thus be stored into a tracked memory object immediately.
func (c *coroutineLoweringPass) heapAlloc(t llvm.Type, name string) llvm.Value {
sizeT := c.alloc.FirstParam().Type()
size := llvm.ConstInt(sizeT, c.target.TypeAllocSize(t), false)
return c.builder.CreateCall(c.alloc, []llvm.Value{size, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, name)
}
// lowerFuncFast lowers an async function that has no suspend points.
func (c *coroutineLoweringPass) lowerFuncFast(fn *asyncFunc) {
// Get return type.
retType := fn.fn.Type().ElementType().ReturnType()
// Get task value.
c.insertPointAfterAllocas(fn.fn)
task := c.builder.CreateCall(c.current, []llvm.Value{llvm.Undef(c.i8ptr), fn.rawTask}, "task")
// Get return pointer if applicable.
var rawRetPtr, retPtr llvm.Value
if fn.hasValueStoreReturn() {
rawRetPtr = c.builder.CreateCall(c.getRetPtr, []llvm.Value{task, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "ret.ptr")
retType = fn.fn.Type().ElementType().ReturnType()
retPtr = c.builder.CreateBitCast(rawRetPtr, llvm.PointerType(retType, 0), "ret.ptr.bitcast")
}
// Lower returns.
for _, ret := range fn.returns {
// Get terminator.
terminator := ret.block.LastInstruction()
// Get tail call if applicable.
var call llvm.Value
switch ret.kind {
case returnVoidTail, returnTail, returnDeadTail, returnAlternateTail, returnDitchedTail, returnDelayedValue:
call = llvm.PrevInstruction(terminator)
}
switch ret.kind {
case returnNormal:
c.builder.SetInsertPointBefore(terminator)
// Store value into return pointer.
c.builder.CreateStore(terminator.Operand(0), retPtr)
// Resume caller.
c.builder.CreateCall(c.returnCurrent, []llvm.Value{task, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
// Erase return argument.
terminator.SetOperand(0, llvm.Undef(retType))
case returnVoid:
c.builder.SetInsertPointBefore(terminator)
// Resume caller.
c.builder.CreateCall(c.returnCurrent, []llvm.Value{task, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnVoidTail:
// Nothing to do. There is already a tail call followed by a void return.
case returnTail:
// Erase return argument.
terminator.SetOperand(0, llvm.Undef(retType))
case returnDeadTail:
// Replace unreachable with immediate return, without resuming the caller.
c.builder.SetInsertPointBefore(terminator)
if retType.TypeKind() == llvm.VoidTypeKind {
c.builder.CreateRetVoid()
} else {
c.builder.CreateRet(llvm.Undef(retType))
}
terminator.EraseFromParentAsInstruction()
case returnAlternateTail:
c.builder.SetInsertPointBefore(call)
// Store return value.
c.builder.CreateStore(terminator.Operand(0), retPtr)
// Heap-allocate a return buffer for the discarded return.
alternateBuf := c.heapAlloc(call.Type(), "ret.alternate")
c.builder.CreateCall(c.setRetPtr, []llvm.Value{task, alternateBuf, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
// Erase return argument.
terminator.SetOperand(0, llvm.Undef(retType))
case returnDitchedTail:
c.builder.SetInsertPointBefore(call)
// Heap-allocate a return buffer for the discarded return.
ditchBuf := c.heapAlloc(call.Type(), "ret.ditch")
c.builder.CreateCall(c.setRetPtr, []llvm.Value{task, ditchBuf, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnDelayedValue:
c.builder.SetInsertPointBefore(call)
// Store value into return pointer.
c.builder.CreateStore(terminator.Operand(0), retPtr)
// Erase return argument.
terminator.SetOperand(0, llvm.Undef(retType))
}
// Delete call if it is a pause, because it has already been lowered.
if !call.IsNil() && call.CalledValue() == c.pause {
call.EraseFromParentAsInstruction()
}
}
}
// insertPointAfterAllocas sets the insert point of the builder to be immediately after the last alloca in the entry block.
func (c *coroutineLoweringPass) insertPointAfterAllocas(fn llvm.Value) {
inst := fn.EntryBasicBlock().FirstInstruction()
for !inst.IsAAllocaInst().IsNil() {
inst = llvm.NextInstruction(inst)
}
c.builder.SetInsertPointBefore(inst)
}
// lowerCallReturn lowers the return value of an async call by creating a return buffer and loading the returned value from it.
func (c *coroutineLoweringPass) lowerCallReturn(caller *asyncFunc, call llvm.Value) {
// Get return type.
retType := call.Type()
if retType.TypeKind() == llvm.VoidTypeKind {
// Void return. Nothing to do.
return
}
// Create alloca for return buffer.
alloca := llvmutil.CreateInstructionAlloca(c.builder, c.mod, retType, call, "call.return")
// Store new return buffer into task before call.
c.builder.SetInsertPointBefore(call)
task := c.builder.CreateCall(c.current, []llvm.Value{llvm.Undef(c.i8ptr), caller.rawTask}, "call.task")
retPtr := c.builder.CreateBitCast(alloca, c.i8ptr, "call.return.bitcast")
c.builder.CreateCall(c.setRetPtr, []llvm.Value{task, retPtr, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
// Load return value after call.
c.builder.SetInsertPointBefore(llvm.NextInstruction(call))
ret := c.builder.CreateLoad(alloca, "call.return.load")
// Replace call value with loaded return.
call.ReplaceAllUsesWith(ret)
}
// lowerFuncCoro transforms an async function into a coroutine by lowering async operations to `llvm.coro` intrinsics.
// See https://llvm.org/docs/Coroutines.html for more information on these intrinsics.
func (c *coroutineLoweringPass) lowerFuncCoro(fn *asyncFunc) {
// Ensure that any alloca instructions in the entry block are at the start.
// Otherwise, block splitting would result in unintended behavior.
{
// Skip alloca instructions at the start of the block.
inst := fn.fn.FirstBasicBlock().FirstInstruction()
for !inst.IsAAllocaInst().IsNil() {
inst = llvm.NextInstruction(inst)
}
// Find any other alloca instructions and move them after the other allocas.
c.builder.SetInsertPointBefore(inst)
for !inst.IsNil() {
next := llvm.NextInstruction(inst)
if !inst.IsAAllocaInst().IsNil() {
inst.RemoveFromParentAsInstruction()
c.builder.Insert(inst)
}
inst = next
}
}
returnType := fn.fn.Type().ElementType().ReturnType()
// Prepare coroutine state.
c.insertPointAfterAllocas(fn.fn)
// %coro.id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
coroId := c.builder.CreateCall(c.coroId, []llvm.Value{
llvm.ConstInt(c.ctx.Int32Type(), 0, false),
llvm.ConstNull(c.i8ptr),
llvm.ConstNull(c.i8ptr),
llvm.ConstNull(c.i8ptr),
}, "coro.id")
// %coro.size = call i32 @llvm.coro.size.i32()
coroSize := c.builder.CreateCall(c.coroSize, []llvm.Value{}, "coro.size")
// %coro.alloc = call i8* runtime.alloc(i32 %coro.size)
coroAlloc := c.builder.CreateCall(c.alloc, []llvm.Value{coroSize, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "coro.alloc")
// %coro.state = call noalias i8* @llvm.coro.begin(token %coro.id, i8* %coro.alloc)
coroState := c.builder.CreateCall(c.coroBegin, []llvm.Value{coroId, coroAlloc}, "coro.state")
c.track(coroState)
// Store state into task.
task := c.builder.CreateCall(c.current, []llvm.Value{llvm.Undef(c.i8ptr), fn.rawTask}, "task")
parentState := c.builder.CreateCall(c.setState, []llvm.Value{task, coroState, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "task.state.parent")
// Get return pointer if needed.
var retPtrRaw, retPtr llvm.Value
if returnType.TypeKind() != llvm.VoidTypeKind {
retPtrRaw = c.builder.CreateCall(c.getRetPtr, []llvm.Value{task, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "task.retPtr")
retPtr = c.builder.CreateBitCast(retPtrRaw, llvm.PointerType(fn.fn.Type().ElementType().ReturnType(), 0), "task.retPtr.bitcast")
}
// Build suspend block.
// This is executed when the coroutine is about to suspend.
suspend := c.ctx.AddBasicBlock(fn.fn, "suspend")
c.builder.SetInsertPointAtEnd(suspend)
// %unused = call i1 @llvm.coro.end(i8* %coro.state, i1 false)
c.builder.CreateCall(c.coroEnd, []llvm.Value{coroState, llvm.ConstInt(c.ctx.Int1Type(), 0, false)}, "unused")
// Insert return.
if returnType.TypeKind() == llvm.VoidTypeKind {
c.builder.CreateRetVoid()
} else {
c.builder.CreateRet(llvm.Undef(returnType))
}
// Build cleanup block.
// This is executed before the function returns in order to clean up resources.
cleanup := c.ctx.AddBasicBlock(fn.fn, "cleanup")
c.builder.SetInsertPointAtEnd(cleanup)
// %coro.memFree = call i8* @llvm.coro.free(token %coro.id, i8* %coro.state)
coroMemFree := c.builder.CreateCall(c.coroFree, []llvm.Value{coroId, coroState}, "coro.memFree")
// call i8* runtime.free(i8* %coro.memFree)
c.builder.CreateCall(c.free, []llvm.Value{coroMemFree, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
// Branch to suspend block.
c.builder.CreateBr(suspend)
// Restore old state before tail calls.
for _, call := range fn.tailCalls {
if !llvm.NextInstruction(call).IsAUnreachableInst().IsNil() {
// Callee never returns, so the state restore is ineffectual.
continue
}
c.builder.SetInsertPointBefore(call)
c.builder.CreateCall(c.setState, []llvm.Value{task, parentState, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "coro.state.restore")
}
// Lower returns.
var postTail llvm.BasicBlock
for _, ret := range fn.returns {
// Get terminator instruction.
terminator := ret.block.LastInstruction()
// Get tail call if applicable.
var call llvm.Value
switch ret.kind {
case returnVoidTail, returnTail, returnDeadTail, returnAlternateTail, returnDitchedTail, returnDelayedValue:
call = llvm.PrevInstruction(terminator)
}
switch ret.kind {
case returnNormal:
c.builder.SetInsertPointBefore(terminator)
// Store value into return pointer.
c.builder.CreateStore(terminator.Operand(0), retPtr)
// Resume caller.
c.builder.CreateCall(c.returnTo, []llvm.Value{task, parentState, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnVoid:
c.builder.SetInsertPointBefore(terminator)
// Resume caller.
c.builder.CreateCall(c.returnTo, []llvm.Value{task, parentState, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnVoidTail, returnDeadTail:
// Nothing to do.
case returnTail:
c.builder.SetInsertPointBefore(call)
// Restore the return pointer so that the caller can store into it.
c.builder.CreateCall(c.setRetPtr, []llvm.Value{task, retPtrRaw, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnAlternateTail:
c.builder.SetInsertPointBefore(call)
// Store return value.
c.builder.CreateStore(terminator.Operand(0), retPtr)
// Heap-allocate a return buffer for the discarded return.
alternateBuf := c.heapAlloc(call.Type(), "ret.alternate")
c.builder.CreateCall(c.setRetPtr, []llvm.Value{task, alternateBuf, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnDitchedTail:
c.builder.SetInsertPointBefore(call)
// Heap-allocate a return buffer for the discarded return.
ditchBuf := c.heapAlloc(call.Type(), "ret.ditch")
c.builder.CreateCall(c.setRetPtr, []llvm.Value{task, ditchBuf, llvm.Undef(c.i8ptr), llvm.Undef(c.i8ptr)}, "")
case returnDelayedValue:
c.builder.SetInsertPointBefore(call)
// Store return value.
c.builder.CreateStore(terminator.Operand(0), retPtr)
}
// Delete call if it is a pause, because it has already been lowered.
if !call.IsNil() && call.CalledValue() == c.pause {
call.EraseFromParentAsInstruction()
}
// Replace terminator with a branch to the exit.
var exit llvm.BasicBlock
if ret.kind == returnNormal || ret.kind == returnVoid || fn.fn.FirstBasicBlock().FirstInstruction().IsAAllocaInst().IsNil() {
// Exit through the cleanup path.
exit = cleanup
} else {
if postTail.IsNil() {
// Create a path with a suspend that never reawakens.
postTail = c.ctx.AddBasicBlock(fn.fn, "post.tail")
c.builder.SetInsertPointAtEnd(postTail)
// %coro.save = call token @llvm.coro.save(i8* %coro.state)
save := c.builder.CreateCall(c.coroSave, []llvm.Value{coroState}, "coro.save")
// %call.suspend = llvm.coro.suspend(token %coro.save, i1 false)
// switch i8 %call.suspend, label %suspend [i8 0, label %wakeup
// i8 1, label %cleanup]
suspendValue := c.builder.CreateCall(c.coroSuspend, []llvm.Value{save, llvm.ConstInt(c.ctx.Int1Type(), 0, false)}, "call.suspend")
sw := c.builder.CreateSwitch(suspendValue, suspend, 2)
unreachableBlock := c.ctx.AddBasicBlock(fn.fn, "unreachable")
sw.AddCase(llvm.ConstInt(c.ctx.Int8Type(), 0, false), unreachableBlock)
sw.AddCase(llvm.ConstInt(c.ctx.Int8Type(), 1, false), cleanup)
c.builder.SetInsertPointAtEnd(unreachableBlock)
c.builder.CreateUnreachable()
}
// Exit through a permanent suspend.
exit = postTail
}
terminator.EraseFromParentAsInstruction()
c.builder.SetInsertPointAtEnd(ret.block)
c.builder.CreateBr(exit)
}
// Lower regular calls.
for _, call := range fn.normalCalls {
// Lower return value of call.
c.lowerCallReturn(fn, call)
// Get originating basic block.
bb := call.InstructionParent()
// Split block.
wakeup := llvmutil.SplitBasicBlock(c.builder, call, llvm.NextBasicBlock(bb), "wakeup")
// Insert suspension and switch.
c.builder.SetInsertPointAtEnd(bb)
// %coro.save = call token @llvm.coro.save(i8* %coro.state)
save := c.builder.CreateCall(c.coroSave, []llvm.Value{coroState}, "coro.save")
// %call.suspend = llvm.coro.suspend(token %coro.save, i1 false)
// switch i8 %call.suspend, label %suspend [i8 0, label %wakeup
// i8 1, label %cleanup]
suspendValue := c.builder.CreateCall(c.coroSuspend, []llvm.Value{save, llvm.ConstInt(c.ctx.Int1Type(), 0, false)}, "call.suspend")
sw := c.builder.CreateSwitch(suspendValue, suspend, 2)
sw.AddCase(llvm.ConstInt(c.ctx.Int8Type(), 0, false), wakeup)
sw.AddCase(llvm.ConstInt(c.ctx.Int8Type(), 1, false), cleanup)
// Delete call if it is a pause, because it has already been lowered.
if call.CalledValue() == c.pause {
call.EraseFromParentAsInstruction()
}
c.builder.SetInsertPointBefore(wakeup.FirstInstruction())
c.track(coroState)
}
}
// lowerCurrent lowers calls to internal/task.Current to bitcasts.
func (c *coroutineLoweringPass) lowerCurrent() error {
taskType := c.current.Type().ElementType().ReturnType()
deleteQueue := []llvm.Value{}
for use := c.current.FirstUse(); !use.IsNil(); use = use.NextUse() {
// Get user.
user := use.User()
if user.IsACallInst().IsNil() || user.CalledValue() != c.current {
return errorAt(user, "unexpected non-call use of task.Current")
}
// Replace with bitcast.
c.builder.SetInsertPointBefore(user)
raw := user.Operand(1)
if !raw.IsAUndefValue().IsNil() || raw.IsNull() {
return errors.New("undefined task")
}
task := c.builder.CreateBitCast(raw, taskType, "task.current")
user.ReplaceAllUsesWith(task)
deleteQueue = append(deleteQueue, user)
}
// Delete calls.