-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Update EIP-663: Move to Draft #7819
Conversation
✅ All reviewers have approved. |
if stack depth addressability is a concern, |
This is not a reasonable modification to EIP-663. Note that with this PR, the EIP's content does not even match the EIP title anymore. I do appreciate that there are reasons why one may want an |
sorry, i don't think "hijacking" is a fair characterization here. i have been bringing this up for nearly a year now on the discussion thread (https://ethereum-magicians.org/t/eip-663-unlimited-swap-and-dup-instructions/3346/40), and regularly attending EOF meetings, and the suggestion was made that i make a PR directly against EIP-663. additionally, i didn't add different variants (and there are several variants which i think are useful) of SWAP/EXCHANGE since i have been informed that opcode slot space is at a premium. so i tried to make a minimal change. but (and maybe this is just context from regularly attending and participating in the EOF calls, but did not come across expressed clearly in the PR) |
I apologize for the phrasing, I didn't mean to insinuate misintent - and I know that Vyper has been in favour of an exchange opcode in EOF for a long time. But I want it to be as clear as possible that this PR as it is stated here (which markedly does not include If this is to be proposed as amendment of EIP-663 and not as a separate proposal, it has to keep addressing this problem, which the current state of the PR doesn't. A variant of the PR that introduces |
EIPS/eip-663.md
Outdated
### Exchange vs extending swap | ||
|
||
As mentioned before, `EXCHANGE` is important to compilers implementing stack scheduling algorithms. Specifically, in the case that a stack item is scheduled to be consumed deeper in the stack (for instance, the 3rd item in the stack needs to be moved into 2nd position in order to be consumed by the next operation), that currently takes three instructions, `SWAP2 SWAP3 SWAP2`. However, in the EVM implementation, the implementation is just a pointer swap, so it could be implemented in a single instruction at no extra runtime cost to the client. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This argument here also technically doesn't hold up to scrutiny.
Proper EVM stack scheduling can avoid situations in which you need to exchange two slots neither of which are at the stack top. I.e. if you find yourself in the situation of having to swap the 3rd and 2nd position on stack, then you allocated an inefficient position for the values on stack when you produced them in the first place. I've given a talk at Devconnect Amsterdam about the topic, if you're interested.
Conversely, all values in the EVM are produced at the stack top and it thereby cannot be avoided to swap the stack top with a deeper position to create optimal stack layouts in the first place.
So, technically, SWAPN
is actually the more efficient choice, since the more common and unavoidable operation on stack top requires only two bytes instead of three even for deep access, while conversely patterns like SWAP2 SWAP3 SWAP2
can generally be avoided by proper stack scheduling at the compiler level.
That being said, having written stack scheduling like that myself, I know that it is not the most trivial thing to do - that's why I've not immediately tried to shut down the idea of EXCHANGE2
: it does allow for simpler more naive stack scheduling albeit at the cost of an increase in code size cost globally compared to optimal scheduling.
But either way, DUPN
and EXCHANGE
are not a replacement of SWAPN
, so it would have to be EXCHANGE2
as well, no matter what.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally be avoided by proper stack scheduling at the compiler level.
the situation can definitely be improved with better algorithms, but optimal stack scheduling, especially across basic blocks and subroutines, is hard. (although i don't have a proof, i suspect it is NP-complete, in the same way that register allocation is). and, even if such a theoretically optimal stack scheduler were available, it is sometimes simply not possible in the general case to schedule things to be in the right order ahead of time, for instance, when side effects are interleaved with stack item production.
But either way, DUPN and EXCHANGE are not a replacement of SWAPN, so it would have to be EXCHANGE2 as well, no matter what.
having things too deep in the stack is also solvable by better memory spilling algorithms, so i don't think phrasing this choice in absolute terms is very constructive here. but sure, if EXCHANGE
+ EXCHANGE2
are not considered as taking up too many slots, why not.
the original EIP itself did not match the EIP title! the title implies "unlimited" reach, but the opcodes only provide 256- slots of addressability. (and even the three-byte variant, of course, only allows addressing up to the 1024 limit imposed by the EVM). so clearly there is a limit, but it is deemed practically useful. the proposed |
The easiest thing to do and to avoid having to senselessly split hairs here would be to just make the introduction of |
modified the PR so it adds |
EIPS/eip-663.md
Outdated
|
||
4. Execution rules: | ||
4.1. `DUPN`: the `n`'th stack item is duplicated at the top of the stack. (*Note: We use 1-based indexing here.*) | ||
4.2 `SWAPN`: the `n + 1`th stack item is swapped with the top stack item. | ||
4.2. `SWAPN`: the `n`'th stack item is swapped with the top of the stack. (*Note: We use 1-based indexing here.*) | ||
4.3 `EXCHANGE`: the `n`'th stack item is swapped with the `n + m`'th stack item. (*Note: We use 1-based indexing here.*) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This limits the swapping of nth
item to only items [n+1, n+16]
, so it can go 32 items deep but only with max distance 16 between items. E.g. you can't swap 1st with 32d.
Is this acceptable limitation for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we might want to change the encoding still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more off-by-one nitpicks, since we increased the reach of EXCHANGE. Also the stack depths mentioned in the abstract aren't 100% accurate, but maybe that's intended to not overwhelm with detail
EIPS/eip-663.md
Outdated
@@ -72,14 +89,14 @@ This has no effect on backwards compatibility because the opcodes were not previ | |||
Given variable `n`, which equals to `imm + 1`, for `1 <= n <= 256`: | |||
|
|||
- `DUPN imm` to fail validation if `stack_height < n`. | |||
- `SWAPN imm` to fail validation if `stack_height < (n + 1)`. | |||
- `EXCHANGE imm` to fail validation if `stack_height < n + m`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my other comment got lost: SWAPN
cases got lost in merges, this entire section needs a revisit at the end. Also specifically it's n + m + 1
now for EXCHANGE
EIPS/eip-663.md
Outdated
3.2. Before `SWAPN` if the current stack height is less than `n + 1`, code is invalid. After `SWAPN` stack height is not changed. | ||
3.1. Before `DUPN` if the current stack height is less than `n`, code is invalid. After `DUPN`, the stack height is incremented. | ||
3.2. Before `SWAPN` if the current stack height is less than `n + 1`, code is invalid. After `SWAPN`, the stack height is unchanged. | ||
3.2. Before `EXCHANGE` if the current stack height is less than `n + m`, code is invalid. After `EXCHANGE`, the stack height is unchanged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
n + m + 1
Co-authored-by: pdobacz <[email protected]>
EIPS/eip-663.md
Outdated
discussions-to: https://ethereum-magicians.org/t/eip-663-unlimited-swap-and-dup-instructions/3346 | ||
status: Review | ||
status: Draft |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We hope to get EIP-3540 out of Stagnant soon #8217
then this can be left in Review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charles-cooper ☝️ this can be undone now, 3540 is out of Stagnant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This'll still need approval from @axic to add the new authors.
EIPS/eip-663.md
Outdated
description: Introduce SWAPN and DUPN which take an immediate value for the depth | ||
author: Alex Beregszaszi (@axic) | ||
title: Additional stack manipulation instructions | ||
description: Introduce additional stack manipulation instructions which take an immediate arguments for the depth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same kind of comment for the description.
The commit b602f71 (as a parent of 4462c22) contains errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All Reviewers Have Approved; Performing Automatic Merge...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All Reviewers Have Approved; Performing Automatic Merge...
When opening a pull request to submit a new EIP, please use the suggested template: https://github.com/ethereum/EIPs/blob/master/eip-template.md
We have a GitHub bot that automatically merges some PRs. It will merge yours immediately if certain criteria are met: