Skip to content

Conversation

@abrown
Copy link
Member

@abrown abrown commented Sep 17, 2025

This is a non-functional change to refactor RegTraversalIter to make it more amenable for future commits adding limits to which registers can be allocated. This change does change the API of RegTraversalIter, though.

It boils down to several parts:

  • using Cursor internally: much of the cursor-advancing logic is duplicated between the preferred and non-preferred register list. This change adds a new, private struct--Cursor--to handle this. This refactoring also reduces the scope of the borrow we take down to only what is necessary (i.e., the actual register lists).
  • changing parameter order: this also passes through the arguments constructing a RegTraversalIter in the order they will be checked during iteration: first it emits the fixed register, then the hinted register, then the offset-based lookup. This is a clarity tweak.
  • only allow one hint register: RegTraversalIter allowed for two hint registers but the library never uses the second one. This change switches to only using a single register, with no other change in behavior.
  • minor simplifications: e.g., we can use Option::take to avoid a more verbose access (and removal) of the fixed register.

Another possible change here would be to pass the hinted register as an Option<PReg> with None to indicate no hint. This seems more conventional and a quick audit of the code supports this but I avoided this out of an abundance of caution.

@abrown abrown requested a review from cfallin September 17, 2025 18:34
This is a non-functional change to refactor `RegTraversalIter` to make it more
amenable for future commits adding limits to which registers can be allocated.
This change does change the API of `RegTraversalIter`, though.

It boils down to several parts:
- using `Cursor` internally: much of the cursor-advancing logic is duplicated
  between the preferred and non-preferred register list. This change adds a new,
  private struct--`Cursor`--to handle this. This refactoring also reduces the
  scope of the borrow we take down to only what is necessary (i.e., the actual
  register lists).
- changing parameter order: this also passes through the arguments constructing
  a `RegTraversalIter` in the order they will be checked during iteration: first
  it emits the fixed register, then the hinted register, then the offset-based
  lookup. This is a clarity tweak.
- only allow one hint register: `RegTraversalIter` allowed for two hint
  registers but the library never uses the second one. This change switches to
  only using a single register, with no other change in behavior.
- minor simplifications: e.g., we can use `Option::take` to avoid a more verbose
  access (and removal) of the fixed register.

Another possible change here would be to pass the hinted register as an
`Option<PReg>` with `None` to indicate no hint. This seems more conventional and
a quick audit of the code supports this but I avoided this out of an abundance
of caution.
Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

This is much cleaner -- thanks very much!

I'm not sure why the original implementation didn't use Option<PReg> for the hint field rather than the separate flag and I agree that'd be cleaner -- feel free to make that change as well.

@cfallin
Copy link
Member

cfallin commented Sep 17, 2025

(I'll merge this now but happy to take said additional cleanup as part of another PR.)

@cfallin cfallin merged commit a5d9f50 into bytecodealliance:main Sep 17, 2025
6 checks passed
@abrown abrown deleted the reg-traversal-iter branch September 18, 2025 15:55
abrown added a commit to abrown/regalloc2 that referenced this pull request Sep 18, 2025
As noted in a [comment] to bytecodealliance#235, passing an `Option<PReg>` _is_ a bit
more clear API. To do this, this change adds `PReg::is_valid()` to do
the conversion. This change (rather unnecessarily) also renames this
variable to `hint` everywhere (previously: `hint`, `hint_reg`,
`reg_hint`) and uses variables directly in several close-by `format!`
strings.

This is a non-functional change.

[comment]: bytecodealliance#235 (review)
abrown added a commit to abrown/regalloc2 that referenced this pull request Sep 18, 2025
As noted in a [comment] to bytecodealliance#235, passing an `Option<PReg>` _is_ a bit
more clear API. To do this, this change adds `PReg::is_valid()` to do
the conversion. This change (rather unnecessarily) also renames this
variable to `hint` everywhere (previously: `hint`, `hint_reg`,
`reg_hint`) and uses variables directly in several close-by `format!`
strings.

This is a non-functional change.

[comment]: bytecodealliance#235 (review)
cfallin pushed a commit that referenced this pull request Sep 18, 2025
As noted in a [comment] to #235, passing an `Option<PReg>` _is_ a bit
more clear API. To do this, this change adds `PReg::is_valid()` to do
the conversion. This change (rather unnecessarily) also renames this
variable to `hint` everywhere (previously: `hint`, `hint_reg`,
`reg_hint`) and uses variables directly in several close-by `format!`
strings.

This is a non-functional change.

[comment]:
#235 (review)
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.

2 participants