You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/tail-call/Overview.md
+30-36Lines changed: 30 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,13 +27,13 @@ This can be applied to any form of call, that is:
27
27
28
28
### Instructions
29
29
30
-
* Tail calls should be separate, explicit call instructions (current instructions explicitly disallow TCE)
30
+
* Tail calls are performed via separate, explicit call instructions (existing call instructions explicitly disallow TCE)
31
31
32
-
* Two possible schemes:
33
-
1. introduce tail version of every call instruction
34
-
2. introduce single prefix instruction that can be applied to every call instruction
32
+
* The proposal thus introduces a tail version of every call instruction
35
33
36
-
* Consideration: WebAssembly will likely get more call instructions in the future, e.g., `call_ref`
34
+
* An alternative scheme introducing a single instruction prefix applicable to every call instruction was considered but rejected by the CG
35
+
- considerations: WebAssembly will likely get a few more call instructions in the future, e.g., `call_ref`
36
+
- but instruction prefixes as modifiers re not used anywhere else in Wasm
37
37
38
38
39
39
### Execution
@@ -44,61 +44,64 @@ This can be applied to any form of call, that is:
44
44
45
45
* Only keeps the necessary call arguments
46
46
47
+
* Tail calls to host functions cannot guarantee tail behaviour (outside the scope of the spec)
48
+
47
49
48
50
### Typing
49
51
50
-
* Because tail calls transfer control and unwind the stack they are stack-polymorphic:
52
+
* Typing rule for tail call instruction is derived by their nature of merging call and return
53
+
54
+
* Because tail calls transfer control and unwind the stack they are stack-polymorphic
51
55
52
-
*Open question: distinguish tail-calls in function type? Possibilities:
56
+
*Previously open question: should tailcalls induce different function types? Possibilities:
53
57
1. Distinguish tail-callees by type
54
58
2. Distinguish tail-callers by type
55
59
3. Both
56
60
4. Neither
57
61
58
62
* Considerations:
59
-
- Option 1 (and 3) allows different calling conventions for non-tail-callable functions, which may be reduce constraints on ABIs.
63
+
- Option 1 (and 3) allows different calling conventions for non-tail-callable functions, which may reduce constraints on ABIs.
60
64
- On the other hand, it creates a bifurcated function space, which can lead to difficulties e.g. when using function tables or other forms of dynamic indirection.
61
-
- Benefit of option 2 (and 3) unclear.
65
+
- Benefit of option 2 (and thus 3) unclear.
66
+
- Experimental validation revealed that there isn't a notable performance benefit to option 1 either.
67
+
68
+
* CG resolution was to go with option 4 as the conceptually simplest.
62
69
63
70
64
71
## Examples
65
72
66
73
A simple boring example of a tail-recursive factorial funciton.
For now, we assume that separate instructions are introduced.
90
-
It is not difficult to adapt the rules to an alternative design with instruction prefixes.
91
-
92
-
The details of possible typing refinements to distinguish tail-callers/callees are to be discussed and not yet included.
93
-
94
-
95
96
### Structure
96
97
97
-
Add two instructions (for now):
98
+
Add two instructions:
98
99
99
100
*`return_call <funcidx>`, the tail-call version of `call`
100
101
*`return_call_indirect <tableidx> <typeidx>`, the tail-call version of `call_indirect`
101
102
103
+
Other language extensions like [typed function refereces](https://github.com/WebAssembly/function-references/blob/master/proposals/function-references/Overview.md) that introduce new call instructions will also introduce tail versions of these new instructions.
104
+
102
105
103
106
### Validation
104
107
@@ -142,13 +145,4 @@ The text format is extended with two new instructions in the obvious manner.
142
145
143
146
## Open Questions
144
147
145
-
* Which instruction scheme should be picked?
146
-
147
-
* Differentiate tail-callers or callees by type?
148
-
149
-
* What about tail calls to host functions?
150
-
- treat as tail-calling a wrapper, use type distinction, or trap?
151
-
- note: cannot distinguish statically without type system support, e.g. with indirect calls
152
-
153
-
* Instruction name bikeshedding
154
-
148
+
* Can tail calls across module boundaries guarantee tail behaviour?
0 commit comments