Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the support for sstore/sload with simple slot access #2670

Merged
merged 4 commits into from
Feb 25, 2025

Conversation

montyly
Copy link
Member

@montyly montyly commented Feb 20, 2025

During the IR generation, convert the sstore/sload into direct IR assignement when we have a state_variable.slot direct access

This allow the SSA to be generated correctly, and for the var read/written analyses to use the right variables.

Fix #2642 #2491 #2470 #2160.
Replace #2669
Revert #2329

On current limitation is the lack of type conversion if the variable is not a uint256. I will open a different issue to track this

During the IR generation, convert the sstore/sload into direct IR assignement
when we have a state_variable.slot direct access

This allow the SSA to be generated correctly, and for the var read/written analyses
to use the right variables.

Fix #2642 #2491 #2470 #2160.
Replace #2669
Revert #2329
@montyly montyly requested a review from smonicas as a code owner February 20, 2025 11:02
montyly and others added 2 commits February 20, 2025 12:03
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@montyly
Copy link
Member Author

montyly commented Feb 20, 2025

The IR ssa generated for

contract Test{

    uint variable;

    function read() internal {
        assembly {
            let read_value := sload(variable.slot)
        }
    }

    function read_parameter(uint slot) internal {
        assembly {
            let read_value := sload(slot)
        }
    }

    function write() internal {
        assembly {
            sstore(variable.slot, 1)
        }
    }

    function write_parameter(uint slot) internal {
        assembly {
            sstore(slot, 1)
        }
    }

}

is now

        Function Test.read()
                IRs:
                        variable_1(uint256) := ϕ(['variable_0', 'variable_2'])
                Expression: read_value_read_asm_0 = sload(uint256)(variable)
                IRs:
                        TMP_0(uint256) := variable_1(uint256)
                        read_value_read_asm_0_1(uint256) := TMP_0(uint256)
        Function Test.read_parameter(uint256)
                Expression: read_value_read_parameter_asm_0 = sload(uint256)(slot)
                IRs:
                        TMP_1(uint256) = SOLIDITY_CALL sload(uint256)(slot_1)
                        read_value_read_parameter_asm_0_1(uint256) := TMP_1(uint256)
        Function Test.write()
                Expression: sstore(uint256,uint256)(variable,1)
                IRs:
                        variable_2(uint256) := 1(uint256)
        Function Test.write_parameter(uint256)
                Expression: sstore(uint256,uint256)(slot,1)
                IRs:
                        TMP_2(None) = SOLIDITY_CALL sstore(uint256,uint256)(slot_1,1)

You can see the sstore/sload being converted if they are direct access

@montyly montyly merged commit c2da7d9 into dev Feb 25, 2025
46 checks passed
@montyly montyly deleted the improve-sstore-sload branch February 25, 2025 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant