``require`` can be optimized but ``assert`` cannot
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
## Description
For the following contract, the IR optimizer failed to optimize ``assert(i < 10)``.
```solidity
pragma solidity ^0.8.0;
contract RandomContract {
function loop() public pure {
for (uint256 i = 0; i < 10; i++) {
assert(i < 10); //failed to optimize
}
}
}
```
But if we use require, then it can be optimized.
```solidity
pragma solidity ^0.8.0;
contract RandomContract {
function loop() public pure {
for (uint256 i = 0; i < 10; i++) {
require(i < 10); //optimized
}
}
}
```
## Environment
- Compiler version: 0.8.19
- Framework/IDE (e.g. Truffle or Remix): Remix
```json
{
"language": "Solidity",
"settings": {
"viaIR": true,
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"": ["ast"],
"*": ["abi", "metadata", "devdoc", "userdoc", "storageLayout", "evm.legacyAssembly", "evm.bytecode", "evm.deployedBytecode", "evm.methodIdentifiers", "evm.gasEstimates", "evm.assembly"]
}
}
}
}
```
Contributor guide
Research direction
Reproduce both Solidity contracts with compiler version 0.8.19, viaIR enabled, and the optimizer configured for 200 runs, then compare their generated output. Start by tracing the IR optimizer handling for assert and require; done means assert(i < 10) is optimized in the loop as require(i < 10) is.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- blockchain, compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100