code_gen_options.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. Code Generation Options
  2. =======================
  3. There are compiler flags to control code generation. They can be divided into two categories:
  4. * Optimizer passes are enabled by default and make the generated code more optimal.
  5. * Debugging options are enabled by default. They can be disabled in release builds, using `--release` CLI option, to decrease gas or compute units usage and code size.
  6. Optimizer Passes
  7. ----------------
  8. Solang generates its own internal IR, before the LLVM IR is generated. This internal IR allows us to do
  9. several optimizations which LLVM cannot do, since it is not aware of higher-level language constructs.
  10. Arithmetic of large integers (larger than 64 bit) has special handling, since LLVM cannot generate them.
  11. So we need to do our own optimizations for these types, and we cannot rely on LLVM.
  12. .. _constant-folding:
  13. Constant Folding Pass
  14. +++++++++++++++++++++
  15. There is a constant folding (also called constant propagation) pass done, before all the other passes. This
  16. helps arithmetic of large types, and also means that the functions are constant folded when their arguments
  17. are constant. For example:
  18. .. code-block:: solidity
  19. bytes32 hash = keccak256('foobar');
  20. This is evaluated at compile time. You can see this in the Visual Studio Code extension by hover over `hash`;
  21. the hover will tell you the value of the hash.
  22. .. _strength-reduce:
  23. Strength Reduction Pass
  24. +++++++++++++++++++++++
  25. Strength reduction is when expensive arithmetic is replaced with cheaper ones. So far, the following types
  26. of arithmetic may be replaced:
  27. - 256 or 128 bit multiply maybe replaced by 64 bit multiply or shift
  28. - 256 or 128 bit divide maybe replaced by 64 bit divide or shift
  29. - 256 or 128 bit modulo maybe replaced by 64 bit modulo or bitwise and
  30. .. include:: ./examples/strength_reduce.sol
  31. :code: solidity
  32. Solang uses reaching definitions to track the known bits of the variables; here solang knows that i can have
  33. the values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and the other operand is always 100. So, the multiplication can be
  34. done using a single 64 bit multiply instruction. If you hover over the ``*`` in the Visual Studio Code you
  35. will see this noted.
  36. .. _dead-storage:
  37. Dead Storage pass
  38. +++++++++++++++++
  39. Loading from contract storage, or storing to contract storage is expensive. This optimization removes any
  40. redundant load from and store to contract storage. If the same variable is read twice, then the value from
  41. the first load is re-used. Similarly, if there is are two successive stores to the same variable, the first
  42. one is removed as it is redundant. For example:
  43. .. include:: ./examples/dead_storage_elimination.sol
  44. :code: solidity
  45. This optimization pass can be disabled by running `solang --no-dead-storage`. You can see the difference between
  46. having this optimization pass on by comparing the output of `solang --no-dead-storage --emit cfg foo.sol` with
  47. `solang --emit cfg foo.sol`.
  48. .. _vector-to-slice:
  49. Vector to Slice Pass
  50. ++++++++++++++++++++
  51. A `bytes` or `string` variable can be stored in a vector, which is a modifyable in-memory buffer, and a slice
  52. which is a pointer to readonly memory and an a length. Since a vector is modifyable, each instance requires
  53. a allocation. For example:
  54. .. include:: ./examples/vector_to_slice_optimization.sol
  55. :code: solidity
  56. This optimization pass can be disabled by running `solang --no-vector-to-slice`. You can see the difference between
  57. having this optimization pass on by comparing the output of `solang --no-vector-to-slice --emit cfg foo.sol` with
  58. `solang --emit cfg foo.sol`.
  59. .. _unused-variable-elimination:
  60. Unused Variable Elimination
  61. +++++++++++++++++++++++++++
  62. During the semantic analysis, Solang detects unused variables and raises warnings for them.
  63. During codegen, we remove all assignments that have been made to this unused variable. There is an example below:
  64. .. include:: ./examples/unused_variable_elimination.sol
  65. :code: solidity
  66. The variable 'x' will be removed from the function, as it has never been used. The removal won't affect any
  67. expressions inside the function.
  68. .. _common-subexpression-elimination:
  69. Common Subexpression Elimination
  70. ++++++++++++++++++++++++++++++++
  71. Solang performs common subexpression elimination by doing two passes over the CFG (Control
  72. Flow Graph). During the first one, it builds a graph to track existing expressions and detect repeated ones.
  73. During the second pass, it replaces the repeated expressions by a temporary variable, which assumes the value
  74. of the expression. To disable this feature, use `solang --no-cse`.
  75. Check out the example below. It contains multiple common subexpressions:
  76. .. include:: ./examples/common_subexpression_elimination.sol
  77. :code: solidity
  78. The expression `a*b` is repeated throughout the function and will be saved to a temporary variable.
  79. This temporary will be placed wherever there is an expression `a*b`. You can see the pass in action when you compile
  80. this contract and check the CFG, using `solang --emit cfg`.
  81. .. _Array-Bound-checks-optimizations:
  82. Array Bound checks optimization
  83. +++++++++++++++++++++++++++++++
  84. Whenever an array access is done, there must be a check for ensuring we are not accessing
  85. beyond the end of an array. Sometimes, the array length could be known. For example:
  86. .. include:: ./examples/array_bounds_check_optimization.sol
  87. :code: solidity
  88. In this example we access ``array`` element 1, while the array length is 3. So, no bounds
  89. checks are necessary and the code will more efficient if we do not emit the bounds check in
  90. the compiled contract.
  91. The array length is tracked in an invisible temporary variable, which is always kept up to date when, for example, a ``.pop()`` or ``.push()`` happens on the array
  92. or an assignment happens. Then, when the bounds check happens, rather than retrieving the array length from
  93. the array at runtime, bounds check becomes the constant expression `1 < 3` which is
  94. always true, so the check is omitted.
  95. This also means that, whenever the length of an array is accessed using '.length', it is replaced with a constant.
  96. Note that this optimization does not cover every case. When an array is passed
  97. as a function argument, for instance, the length is unknown.
  98. ``wasm-opt`` optimization passes
  99. --------------------------------
  100. For the Substrate target, optimization passes from the `binaryen <https://github.com/WebAssembly/binaryen>`_ ``wasm-opt``
  101. tool can be applied. This may shrink the Wasm code size and makes it more efficient.
  102. Use the ``--wasm-opt`` compile flag to enable ``wasm-opt`` optimizations. Possible values are
  103. ``0`` - ``4``, ``s`` and ``z``, corresponding to the ``wasm-opt`` flags ``-O0`` - ``-O4``, ``-Os`` and ``-Oz`` respectively.
  104. To learn more about the optimization levels please consult ``wasm-opt --help``.
  105. .. note::
  106. In ``--release`` mode, if ``--wasm-opt`` is not specified, the level ``z`` ("super-focusing on code size") will be used.
  107. Debugging Options
  108. -----------------
  109. It is desirable to have access to debug information regarding the contract execution in the testing phase.
  110. Therefore, by default, debugging options are enabled; however, they can be deactivated by utilizing the command-line interface (CLI) flags.
  111. Debugging options should be disabled in release builds, as debug builds greatly increase contract size and gas consumption.
  112. Solang provides three debugging options, namely debug prints, logging API return codes, and logging runtime errors. For more flexible debugging,
  113. Solang supports disabling each debugging feature on its own, as well as disabling them all at once with the ``--release`` flag.
  114. .. _no-print:
  115. Print Function
  116. ++++++++++++++
  117. Solang provides a :ref:`print_function` which is enabled by default.
  118. The ``no-print`` flag will instruct the compiler not to log debugging prints in the environment.
  119. .. _no-log-api-return-codes:
  120. Log runtime API call results
  121. ++++++++++++++++++++++++++++
  122. Runtime API calls are not guaranteed to succeed.
  123. By design, the low level results of these calls are abstracted away in Solidity.
  124. For development purposes, it can be desirable to observe the low level return code of such calls.
  125. The contract will print the return code of runtime calls by default, and this feature can be disabled by providing the
  126. ``--no-log-api-return-codes`` flag.
  127. .. note::
  128. This is only implemented for the Substrate target.
  129. .. _no-log-runtime-errors:
  130. Log Runtime Errors
  131. ++++++++++++++++++
  132. In most cases, contract execution will emit a human readable error message in case a runtime error is encountered.
  133. The error is printed out alongside with the filename and line number that caused the error.
  134. This feature is enabled by default, and can be disabled by the ``--no-log-runtime-errors`` flag.
  135. .. _release:
  136. Release builds:
  137. +++++++++++++++
  138. Release builds must not contain any debugging related logic. The ``--release`` flag will turn off all debugging features,
  139. thereby reducing the required gas and storage.