argotorg / argotorg/solidity

IR Optimizer failed to remove inline assembly code after ``return``

Open
#14,388 0 comments 0 reactions 0 assignees View on GitHub
bug :bug:
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 UnreachableCode {
uint256 globalVar = 10;

function foo(uint256 x) public {
if (x < 5) {
for (uint i = 0; i < 3; i++) {
globalVar = globalVar + i;
}
} else if (x < 10) {
while (globalVar > 0) {
globalVar = globalVar - 1;
if (globalVar == 5) {
break;
}
}
} else {
return;
}

return ;
unreachableCode();
}

function unreachableCode() private pure {
uint256 y = 5;
return ;
assembly {
let ptr := mload(0x40)
mstore(ptr, y)
}
}
}
```
The optimizer failed to remove the
```solidity
assembly {
let ptr := mload(0x40)
mstore(ptr, y)
}
```
## Environment

- Compiler version: 0.8.19
- Framework/IDE (e.g. Truffle or Remix):Remix
- Compiler config
```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

Open the contributing guide

Research direction

Reproduce the provided UnreachableCode contract with Solidity 0.8.19, viaIR, and the enabled optimizer. Inspect the generated IR and assembly outputs selected in the configuration, focusing on the code after return in unreachableCode(). Done means the optimizer removes the inline assembly after the return while preserving the rest of the generated output.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.