Skip to content

Commit 20f3a62

Browse files
authored
Introduce text format for compilation hints
Alternatively to the binary representation as a string value, one can use a human readable syntax to express compilation hints. These hints will have the same binary representation int eh module and can therefore also be translated into the string format if conversion tools don't support the alternative format. This implements the suggestion from issue #14.
2 parents 61bad0f + 88b3c60 commit 20f3a62

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

proposals/compilation-hints/Overview.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The following contains a list of hints to be included in the first version of th
3737
The section `metadata.code.compilation_order` contains the order in which functions should be compiled in order to minimize wait times until the compilation is completed. This is especially relevant during instantiation and startup but might also be relevant later.
3838
* *byte offset* |U32| with value 0 (function level only)
3939
* *hint length* |U32| in bytes
40-
* *compilation order* |U32| starting at 0 (functions with the same order value will be compiled in an arbitrary order but before functions with a higher order value)
40+
* *compilation priority* |U32| starting at 0 (functions with the same priority value will be compiled in an arbitrary order but before functions with a higher priority value)
4141
* *hotness* |U32| defining how often this function is called
4242

4343
If a length of larger than required to store 2 values is present, only the first two values of the following hint data is evalued while the rest is ignored. This leaves space for future extensions, e.g. grouping functions. Similarly, the *hotness* can be dropped if a length corresponds to only 1 value is given.
@@ -49,6 +49,19 @@ It is expected and even desired that not all functions are annotated to keep thi
4949
*Note: This should be moved to `metadata.function.compilation_order` without the byte offset if such a namespace will be supported by custom annotations.*
5050

5151

52+
#### Text format
53+
54+
Instead of a binary string representation, these hints can also be provided using a more human readable notation in text format:
55+
```
56+
(@metadata.code.compilation_order (priority 1) (hotness 100))
57+
```
58+
The above example is equivalent to
59+
```
60+
(@metadata.code.compilation_order "\01\64")
61+
```
62+
and tools can produce one or the other.
63+
64+
5265
### Instruction frequencies
5366

5467
Instruction frequencies might be useful to guide optimizations like inlining, loop unrolling, block deferrals, etc. Within a function, these frequencies inform which blocks lie on the hot path and deserve more expensive optimizations, as well as which are on the cold path and might even allow very expensive steps to even execute the code within (e.g. outlining or de-optimization). An engine can take those decisisions based on the instruction frequency observed, but cannot assume that any part of the code is unreachable based on the instruction frequency.
@@ -88,6 +101,21 @@ Special values of 0 and 127 indicate that a function should never or always be i
88101
| 127| |*always optimize* |
89102

90103

104+
#### Text format
105+
106+
The alternative text format representation for such a section would look as follows
107+
```
108+
(@metadata.code.instr_freq (freq 123.45))
109+
```
110+
The given frequency $\frac{n}{N}$ is then converted into the equivalent binary representation
111+
```
112+
(@metadata.code.instr_freq "\26")
113+
```
114+
according to the formula above.
115+
116+
Alternatively to `freq` followed by a numeric value, one can provide `never_opt` and `always_opt` with binary representations of `"\00"` and `"\7f"` respectively.
117+
118+
91119
### Call targets
92120

93121
When dealing with `call_indirect` or `call_ref`, often inefficient code is generated, because the call target is unknown. With code that e.g. uses virtual function calls, there are often very few commonly called targets which a compiler could optimize for. It still needs to have the ability to handle other call targets, but that can then happen at a much lower performance in favor of optimizing for the more commonly called target.
@@ -104,3 +132,16 @@ The `metadata.code.call_targets` section contains instruction level annotations
104132
The accumulated call frequency must add up to 100 or less. If it is less than 100, then other call targets that are not listed are responsible for the missing calls. Together with the inline hints on call frequency, this can information can be used to inline function calls as well. The effective call frequency for each call target is then the inlining call frequency multiplied by the fractional call frequency encoded in this section.
105133

106134
Similarly to the compilation order section, not all call sites need to be annotated and not all call targets be listed. However, if other call targets are known but not emitted, then the frequency must be below 100 to inform the engine of the missing information.
135+
136+
137+
#### Text format
138+
139+
The text representation allows for multiple targets
140+
```
141+
(@metadata.code.call_targets (target $func1 0.73) (target $func2 0.21))
142+
```
143+
which would be converted into binary format as
144+
```
145+
(@metadata.code.call_targets "\01\49\02\15)
146+
```
147+
under the assumption that `$func1` and `$func2` have function indices 1 and 2 respectively.

0 commit comments

Comments
 (0)