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

[EVM] Check for logic duplication in stack slots rematerialization logic #718

Open
akiramenai opened this issue Oct 23, 2024 · 1 comment

Comments

@akiramenai
Copy link
Collaborator

There are two places where we rematerialize stack slots:

  • The EVMSingleUseExpression pass handles rematerialization for constants (plus ad-hoc solutions for CALLDATALOAD, ADD).
  • The StackLayoutGenerator::compressStack also does it for constants, function call labels, symbols, and junk slots.

The suggestion is to handle constants exclusively in the EVMSingleUseExpression pass and remove their handling from other areas. Rematerialization of the remaining rematerializable items (e.g., function call labels, symbols, junk slots) should become the default behavior, rather than relying on the compressStack logic.

@akiramenai akiramenai changed the title Check for logic duplication in stack slots rematerialization logic [EVM] Check for logic duplication in stack slots rematerialization logic Oct 25, 2024
@vladimirradosavljevic
Copy link
Contributor

Rematerialization of CALLDATALOAD and ADD is not trivial, since we need to rematerialize 2 instructions where second instruction is a constant. In some cases, it is more profitable to reuse results of CALLDATALOAD and ADD from the stack, instead of doing rematerialization.
One of the tests where this is the case is tests/solidity/simple/yul_instructions/keccak256.sol:

Loop with rematerialization:
.BB0_8:                                 ; %for_body
                                        ; =>This Inner Loop Header: Depth=1
	JUMPDEST
	PUSH1 68
	CALLDATALOAD
	PUSH1 4
	CALLDATALOAD
	DUP3
	ADD
	MSTORE8
	ADD
	PUSH1 36
	CALLDATALOAD
	DUP2
	LT
	PUSH4 @.BB0_17
	JUMPI
Loop without rematerialization:
.BB0_8:                                 ; %for_body
                                        ; =>This Inner Loop Header: Depth=1
	JUMPDEST
	DUP3              <- No remat
	DUP6              <- No remat
	DUP3
	ADD
	MSTORE8
	ADD
	DUP3              <- No remat
	DUP2
	LT
	PUSH4 @.BB0_17
	JUMPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants