argotorg / argotorg/solidity

Dead stores into a `memoryguard`-derived allocation are not eliminated

Open
#16,927 1 comment 0 reactions 1 assignee Claimed by @rodiazet View on GitHub
bug :bug:
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
2d 19h
Merged PRs (30d)
29

Description

## Description

When analyzing gas usage of a contract I found it curious that accessing a single member was so expensive.
To me it seems like a bug in the optimizer.

## Environment

- Compiler version: 0.8.35
- Compilation pipeline (legacy, IR, SSA CFG): IR
- Target EVM version (as per compiler settings): cancun
- Framework/IDE (e.g. Foundry, Hardhat, Remix): foundry

## Steps to Reproduce

The following code

```solidity
struct OnlyUnvalidated { uint256 tokenId; uint256 b; uint256 c; uint256 d; }

contract C {
function decodeUnvalidatedStruct(bytes calldata data) external pure returns (uint256) {
return abi.decode(data, (OnlyUnvalidated)).tokenId;
}
}
```

will compile to
```yul
let _1 := memoryguard(0x80)
...
mstore(64, newFreePtr) // newFreePtr = add(_1, 128)
let value := calldataload(add(offset, 36))
mstore(_1, value) // dead
mstore(add(_1, 32), calldataload(add(offset, 68))) // dead
mstore(add(_1, 64), calldataload(add(offset, 100))) // dead
mstore(add(_1, 96), calldataload(add(offset, 132))) // dead
let memPos := mload(64)
mstore(memPos, value)
return(memPos, 32)
```

All four `mstore`s are dead. The optimizer has already forwarded the return value.
The struct is `uin256` values so members are not validated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.