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

refactor: remove OnceLock #9992

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

JonasKruckenberg
Copy link
Contributor

@JonasKruckenberg JonasKruckenberg commented Jan 13, 2025

This PR is another attempt at #8489 (superseding it) this time by making the necessary changes in regalloc2 (see here) to allow MachineEnv to be created in const expressions and therefore placed into statics without the need for lazy initialization (as mentioned in this comment)
This PR also makes the necessary changes in register creation functions to make them const.

While I agree with @cfallin that this is the "correct" move, it is slightly annoying that Rusts const limitation makes the static initializers so verbose where a 1..=16 range iterator was used previously, any ideas here are welcome :/

Edit: marked the PR as draft until the regalloc2 upstream PR is merged

@JonasKruckenberg JonasKruckenberg requested review from a team as code owners January 13, 2025 11:18
@JonasKruckenberg JonasKruckenberg requested review from fitzgen and removed request for a team January 13, 2025 11:18
@JonasKruckenberg JonasKruckenberg marked this pull request as draft January 13, 2025 11:18
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen labels Jan 13, 2025
@cfallin cfallin requested review from cfallin and removed request for fitzgen January 13, 2025 19:54
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.

Thanks a bunch for working on this! This all looks like the shape I expected; I'm glad there were no other hidden snags. A few minor comments below but otherwise happy to see this merged once we get the RA2 PR merged and released.

env
}
static DEFAULT_MACHINE_ENV: MachineEnv = {
debug_assert!(PINNED_REG == 21); // We assumed this below in hardcoded reg list.
Copy link
Member

Choose a reason for hiding this comment

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

This probably goes in the PINNED_MACHINE_ENV above, no? (It's load-bearing there that we get it right, because we excluded the pinned register from allocation; the default environment is used when pinning is disabled so doesn't care what the pinned reg is)

@@ -48,10 +48,26 @@ pub fn first_user_vreg_index() -> usize {
pub struct Reg(VReg);

impl Reg {
/// Create a register from a physical register
// FIXME(const-hack) remove in favor of `From` impl
Copy link
Member

Choose a reason for hiding this comment

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

Can you say more here -- I'm not that familiar with the current state of constification of everything; is there some feature this is waiting on to stabilize or...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well, the feature that would be waiting for is const associated functions in traits (therefore allowing From::from to be const) but that afaik is in "never type land". I can remove the note as it doesn't make much sense to hold our breath on it

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think that's best -- usually when we have a FIXME we want it to be actionable, or at least give sufficient context so that we know what we're waiting on (i.e.: if someone else walked up to this code tomorrow, would they know what they need in order to fix it?). It's fine to evaluate questions like this once new Rust features land in the future.

Copy link
Contributor Author

@JonasKruckenberg JonasKruckenberg Jan 13, 2025

Choose a reason for hiding this comment

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

the issue with constness is that theres no good way to detect constness workarounds after the fact (ie reevaluating code like this is tricky since it appears as just somewhat odd but regular code) which is the whole reason for the const-hack comments but usually you'd tie them to specific RFCs so yeah i agree getting rid of it is fine

Choose a reason for hiding this comment

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

Relevant RFC fresh off the press: rust-lang/rfcs#3762

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants