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

Update EIP-663: Move to Draft #7819

Merged
merged 13 commits into from
Mar 7, 2024
Merged

Conversation

charles-cooper
Copy link
Contributor

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:

  • The PR edits only existing draft PRs.
  • The build passes.
  • Your GitHub username or email address is listed in the 'author' header of all affected PRs, inside .
  • If matching on email address, the email address is the one publicly listed on your GitHub profile.

@github-actions github-actions bot added c-update Modifies an existing proposal s-review This EIP is in Review t-core labels Oct 6, 2023
@eth-bot
Copy link
Collaborator

eth-bot commented Oct 6, 2023

✅ All reviewers have approved.

@eth-bot eth-bot changed the title replace SWAPN with EXCHANGE Update EIP-663: replace SWAPN with EXCHANGE Oct 6, 2023
@eth-bot eth-bot added the a-review Waiting on author to review label Oct 6, 2023
EIPS/eip-663.md Outdated Show resolved Hide resolved
@charles-cooper
Copy link
Contributor Author

if stack depth addressability is a concern, EXCHANGE2 can be added (which can address at least up to 512 stack depth)

@ekpyron
Copy link
Member

ekpyron commented Oct 12, 2023

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.
You're trying to solve quite a different problem with EXCHANGE, so this seems more like hijacking the EIP to fully repurpose it, rather than an amendment :-).

I do appreciate that there are reasons why one may want an EXCHANGE opcode - stack scheduling does require more creative algorithms without it - but I'd argue that it should be proposed as a separate EIP. Other than both having something to do with stack, there is little relation between EIP-663 and the proposed EXCHANGE. I'd even be in support of such a new EIP, but as an (for us optional) addition to, not as replacement of, EIP-663 (and contingently on the acceptance of such a new EIP, EIP-663 could then be amended by an additional EXCHANGE2 - or conversely the new EIP should add that, if EIP-663 is accepted).

@charles-cooper
Copy link
Contributor Author

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. You're trying to solve quite a different problem with EXCHANGE, so this seems more like hijacking the EIP to fully repurpose it, rather than an amendment :-).

I do appreciate that there are reasons why one may want an EXCHANGE opcode - stack scheduling does require more creative algorithms without it - but I'd argue that it should be proposed as a separate EIP. Other than both having something to do with stack, there is little relation between EIP-663 and the proposed EXCHANGE. I'd even be in support of such a new EIP, but as an (for us optional) addition to, not as replacement of, EIP-663 (and contingently on the acceptance of such a new EIP, EIP-663 could then be amended by an additional EXCHANGE2 - or conversely the new EIP should add that, if EIP-663 is accepted).

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) EXCHANGE and its EXCHANGE2 variant (with 2-byte immediate) fully subsume the functionality of the original proposed SWAPN opcode, with an opcode slot vs bytecode size tradeoff.

@ekpyron
Copy link
Member

ekpyron commented Oct 13, 2023

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) EXCHANGE and its EXCHANGE2 variant (with 2-byte immediate) fully subsume the functionality of the original proposed SWAPN opcode, with an opcode slot vs bytecode size tradeoff.

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 EXCHANGE2) is not an option. It clearly changes the EIP in such a way that it no longer solves the issue this EIP is meant to address, i.e. limited reach on stack.

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 EXCHANGE2 instead or additionally would be a different story.

EIPS/eip-663.md Outdated
Comment on lines 76 to 79
### 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.

Copy link
Member

@ekpyron ekpyron Oct 14, 2023

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.

Copy link
Contributor Author

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.

@charles-cooper
Copy link
Contributor Author

Note that with this PR, the EIP's content does not even match the EIP title anymore.

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 EXCHANGE opcode also increases addressability, the same as the original EIP, it just increases the reach somewhat less (in exchange for the other useful semantics as already described).

@ekpyron
Copy link
Member

ekpyron commented Oct 18, 2023

The easiest thing to do and to avoid having to senselessly split hairs here would be to just make the introduction of EXCHANGE a separate EIP, since it's obviously a radically different change compared to the unmodified EIP-663 (you cannot reasonably deny that).
As I said, I'd grant that that EIP would have (independent) merit.

@github-actions github-actions bot added the w-ci Waiting on CI to pass label Nov 1, 2023
@charles-cooper
Copy link
Contributor Author

modified the PR so it adds EXCHANGE in addition to(!) SWAPN.

EIPS/eip-663.md Outdated Show resolved Hide resolved
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.*)
Copy link
Member

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?

Copy link
Contributor Author

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.

@github-actions github-actions bot removed the w-ci Waiting on CI to pass label Nov 8, 2023
Copy link
Contributor

@pdobacz pdobacz left a 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`.
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n + m + 1

EIPS/eip-663.md Show resolved Hide resolved
@github-actions github-actions bot added the w-ci Waiting on CI to pass label Feb 13, 2024
@eth-bot eth-bot changed the title Update EIP-663: replace SWAPN with EXCHANGE Update EIP-663: Move to Draft Feb 13, 2024
@github-actions github-actions bot removed the w-ci Waiting on CI to pass label Feb 13, 2024
pdobacz added a commit to ipsilon/eof that referenced this pull request Feb 14, 2024
EIPS/eip-663.md Outdated
discussions-to: https://ethereum-magicians.org/t/eip-663-unlimited-swap-and-dup-instructions/3346
status: Review
status: Draft
Copy link
Member

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

Copy link
Contributor

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.

@github-actions github-actions bot added c-update Modifies an existing proposal s-review This EIP is in Review and removed c-status Changes a proposal's status s-draft This EIP is a Draft labels Feb 19, 2024
SamWilsn
SamWilsn previously approved these changes Feb 20, 2024
Copy link
Contributor

@SamWilsn SamWilsn left a 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 Show resolved Hide resolved
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
Copy link
Contributor

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.

EIPS/eip-663.md Outdated Show resolved Hide resolved
EIPS/eip-663.md Outdated Show resolved Hide resolved
Copy link

The commit b602f71 (as a parent of 4462c22) contains errors.
Please inspect the Run Summary for details.

@github-actions github-actions bot removed the w-ci Waiting on CI to pass label Feb 21, 2024
@axic axic closed this Mar 7, 2024
@axic axic reopened this Mar 7, 2024
@eth-bot eth-bot enabled auto-merge (squash) March 7, 2024 08:19
Copy link
Collaborator

@eth-bot eth-bot left a 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...

Copy link
Collaborator

@eth-bot eth-bot left a 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...

@eth-bot eth-bot merged commit 6aed382 into ethereum:master Mar 7, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-review Waiting on author to review c-update Modifies an existing proposal s-review This EIP is in Review t-core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants