Possibly unnecessary use of exponentiation in generated bytecode with legacy codegen
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
## Description
I have observed a use of exponentiation instruction that was a bit surprising. Here is a simple contract:
```solidity
contract A {
uint128[1] arr;
function set(uint128 v) external {
arr[0] = v;
}
}
```
Compiling this contract (`--bin-runtime`) with legacy codegen results in a bytecode with an exponentiation instruction, while compiling with `--via-ir` results in bytecode with no exponentiation.
This behaviour has been observed on versions `0.8.26` and `0.8.30`.
### Summary of matrix discussion:
The origin of the exponentiation of the instruction seems to be [here](https://github.com/ethereum/solidity/blob/406bdfb3abc8cfe73d59f9a748d53f9445591323/libsolidity/codegen/LValue.cpp#L315). I confirm that the constant base `0x100` is the value I have seen in the bytecode.
It seems this `EXP` is a substitute for a bit shift, and likely should have been updated for EVMs that support bit shifts.
I would add that exponentiation is difficult to handle by verification tools, so using bit shift would be very much appreciated :)
It would also be good to evaluate the impact of the change in bytecode size and gas consumption.
For this contract specifically, the legacy codegen produces smaller bytecode.
Contributor guide
Research direction
Start at libsolidity/codegen/LValue.cpp around line 315, where the exponentiation originates, and compile the example with --bin-runtime using legacy codegen and --via-ir for comparison. Determine whether replacing the constant-base EXP with a bit shift changes bytecode size or gas consumption, and verify the resulting bytecode for the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100