Skip to content

Commit a90a611

Browse files
committed
Document optimization levels
1 parent 2941809 commit a90a611

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/smart_contract_tour/advanced.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22

33
This section contains a few selected, advanced use cases of OpShin.
44

5+
### Compiler Flags
6+
7+
OpShin provides several flags that enable, control or disable optimizations.
8+
Concretely the following flags are available (visible when calling `opshin --help`):
9+
10+
| Flag | Description |
11+
|------|-------------|
12+
| `-fcompress-patterns` | Enables the compression of re-occurring code patterns. Can reduce memory and CPU steps but increases the size of the compiled contract. |
13+
|`-fiterative-unfold-patterns`| Enables iterative unfolding of patterns. Improves application of pattern optimization but is very slow. |
14+
|`-fconstant-index-access-list` | Replace index accesses with constant parameters with optimized constant accesses. Reduces memory and CPU steps but increases the size of the compiled contract. |
15+
|`-fconstant-folding`, `--cf` | Enables experimental constant folding, including propagation and code execution. See below for a detailed explanation. |
16+
|`-fallow-isinstance-anything` | Enables the use of isinstance(x, D) in the contract where x is of type Anything. This is not recommended as it only checks the constructor id and not the actual type of the data. |
17+
|`-fremove-dead-code`| Removes dead code and variables from the contract. |
18+
|`-ffast-access-skip FAST_ACCESS_SKIP`| How many steps to skip for fast list index access, default None means no steps are skipped (useful if long lists are common). |
19+
20+
### Optimization Levels
21+
22+
OpShin, similar to C-compilers, features optimization levels. These control which optimizations are applied to the generated code to make it smaller and more performant, but at the same time more difficult to debug.
23+
The levels reach from 0 to 3, where higher numbers indicate more optimizations.
24+
However, as opposed to C-programs, there are not yet many debugging tools that require non-optimized ode.
25+
Thus, as a typical user, optimizing less than level 2 will not bring any debugging benefits.
26+
Therefore, OpShin per default optimizes at level 2.
27+
Level 3 enables some slow optimizations and in theory allows optimizations that change the semantics, i.e., the behavior, of the contract.
28+
In practice, this will never have more of an effect than removing print statements or assertion messages.
29+
However, these messages are quite useful in practice and thus level 3 should not be enabled unless the contract has been thoroughly tested.
30+
31+
The following optimizations are enabled by each optimization level (where each optimization level contains all optimizations of the lower optimizations):
32+
33+
- `O0`: None
34+
- `O1`:
35+
- `-fcompress-patterns`
36+
- `-fconstant-index-access-list`
37+
- `-fremove-dead-code`
38+
- `O2`:
39+
- `fconstant-folding`
40+
- `ffast-access-skip 5`
41+
- `O3`:
42+
- `fiterative-unfold-patterns`
43+
44+
Flags can be overridden by passing them specifically after specifying an optimization level, e.g., `opshin build ... -O2 -ffast-access-skip 2`.
45+
46+
547
### Constant Folding
648

749
OpShin supports the command-line flag `--constant-folding` or short `--cf`.

0 commit comments

Comments
 (0)