Function interferes with compilation optimization?
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
## Description
For the following contract
```solidity
pragma solidity ^0.8.0;
contract RandomProgram {
uint256 constant MAX_LOOP_COUNT = 10;
uint256 constant INITIAL_BALANCE = 100;
struct Person {
string name;
uint256 age;
uint256 balance;
}
uint256 globalCount;
uint256 globalBalance = INITIAL_BALANCE;
function loop() public {
for (uint256 i = 0; i < MAX_LOOP_COUNT; i++) {
require(i < MAX_LOOP_COUNT, "This require statement is redundant");
assert(i >= 0 && i < MAX_LOOP_COUNT); // failed to optimize???
globalCount++;
if (i % 2 == 0) {
globalBalance -= i;
} else {
globalBalance += i;
}
}
}
function conditional(uint256 x, uint256 y) public pure returns (uint256) {
uint256 result;
if (x > y) {
result = x - y;
assert(result > 0);
} else if (x == y) {
result = x + y;
assert(result == 0);
} else {
result = y - x;
assert(result > 0);
}
return result;
}
}
```
The IR optimizer failed to remove `` assert(i >= 0 && i < MAX_LOOP_COUNT);``.
If i remove the function ``conditional``, the `` assert(i >= 0 && i < MAX_LOOP_COUNT);`` can be optimized.
## Environment
- Compiler version: 0.8.19
- Framework/IDE (e.g. Truffle or Remix): Remix
Contributor guide
Research direction
Reproduce the contract in Remix with Solidity 0.8.19 and inspect the IR optimizer output for loop() with and without conditional(). Compare whether assert(i >= 0 && i < MAX_LOOP_COUNT) remains in each case; done means the reported difference is explained and the redundant assertion is optimized consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- solidity
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100