Skip to content

Commit

Permalink
[FIXUP] Make it clearer that codegen-based steps belong to different …
Browse files Browse the repository at this point in the history
…pipelines
  • Loading branch information
cameel committed Nov 23, 2023
1 parent 15c432c commit 820acff
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions docs/internals/optimizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ the function calls. Similarly, if a function is
side-effect free and its result is multiplied by zero, you can remove the function
call completely.

The codegen-based optimizer consists of two steps, the first of which changes the
order of literals in binary operators, moving them to the right. The second one
enables the compiler to utilize unchecked arithmetic when generating code for
incrementing the counter variable in certain ``for`` loops.
The codegen-based optimizer affects the initial low-level code generated from the AST.
In the legacy pipeline, most of the optimizations of this kind are implicit and not configurable,
the only exception being the optimization which changes the order of literals in binary operations.
The IR-based pipeline takes a different approach and produces Yul IR closely matching the structure
of the Solidity code, with nearly all optimization deferred to the Yul optimizer module.
Codegen-level optimization is done only in very limited cases which are difficult to handle in
Yul IR, but are straightforward with the high-level information from analysis phase at hand.
An example of such an optimization is the bypass of checked arithmetic when incrementing the counter
in certain idiomatic ``for`` loops.

Currently, the parameter ``--optimize`` activates the opcode-based optimizer for the
generated bytecode and the Yul optimizer for the Yul code generated internally, for example for ABI coder v2.
Expand Down Expand Up @@ -1401,16 +1406,17 @@ The LiteralRematerialiser should be run before this step.
Codegen-Based Optimizer Module
==============================

Currently, the codegen-based optimizer module consists of two distinct steps.
Currently, the codegen-based optimizer module currently provides two optimizations.

The first step moves literals to the right of commutative binary operators which
helps exploit associativity.
The first one, available in the legacy code generator, moves literals to the right of commutative
binary operators, which helps exploit their associativity.

The second step uses unchecked arithmetic when generating code for incrementing
the counter variable of specific ``for`` loops. This
avoids wasting gas by identifying some conditions that guarantee
that the counter variable cannot overflow. This eliminates the need to use a verbose
unchecked arithmetic block inside the loop body to increment the counter variable.
The other one, available in the IR-based code generator, enables the use of unchecked arithmetic
when generating code for incrementing the counter variable of certain idiomatic ``for`` loops.
This avoids wasting gas by identifying some conditions that guarantee that the counter variable
cannot overflow.
This eliminates the need to use a verbose unchecked arithmetic block inside the loop body to
increment the counter variable.

.. _unchecked-loop-optimizer:

Expand Down

0 comments on commit 820acff

Please sign in to comment.