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

De-LLVM the unchecked shifts [MCP#693] #123226

Merged
merged 3 commits into from
Apr 2, 2024
Merged

Conversation

scottmcm
Copy link
Member

This is just one part of the MCP (rust-lang/compiler-team#693), but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.

r? WaffleLapkin

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 30, 2024
@scottmcm
Copy link
Member Author

Note that I did not change the traits for llvm/gcc to implement in this. Updating the intrinsics -- as rust concepts -- to match what we want them to be definitely makes sense to me. I'm less sure about the codegen_ssa traits, though, so left them alone.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 30, 2024

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

This is just one part of the MCP, but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.
@RalfJung
Copy link
Member

As far as I can tell, CTFE, Miri, and Codegen all supported mixed unchecked shifts already without me doing anything. So this just gives a way to write tests exercising those.

This comment confusingly disappeared, but for CTFE/Miri it is true. The MIR binops support different types for LHS and RHS (and these intrinsics lower to MIR binops).

@WaffleLapkin
Copy link
Member

but it's the one that IMHO removes the most noise from the standard library code.

I was actually stuck on figuring the shifts out. Since you can have integer shifts with any integer rhs, backends will need to cast, no matter what, so I couldn't figure out how to make everything better for everyone.

I have all the other changes ready (I think), so I'll open a PR once we merge this (I'll try to review soon).

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

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

I have some small nits and am somewhat confused why we want to make intrinsics generic wrt lhs. But LGTM on the general approach of moving things from std to the backend.

This does not particularly help us at my dayjob (we would like bx.shl to take rhs which is u32, i.e. we would like to have a different build_shift_expr_rhs), but I do like this change anyway. 😅

compiler/rustc_codegen_ssa/src/base.rs Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/base.rs Outdated Show resolved Hide resolved
#[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
#[rustc_nounwind]
pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
pub fn unchecked_shl<T: Copy, U: Copy>(x: T, y: U) -> T;
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to make shift amount generic? Why not just y: u32?

Copy link
Member Author

Choose a reason for hiding this comment

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

Making the shift amount always u32 was actually what I did first.

However, MIR actually supports arbitrary heterogeneous shifts, even though for the unchecked once those weren't accessible. So when the RHS was always u32, that blew up a whole bunch of tests -- many wouldn't even compile, with -1 not being a valid u32.

So supporting mixed types here is helpful in that we can still write tests that exercise those codepaths in CTFE and Codegen. And it doesn't make the lowering to MIR any harder, nor add any extra work to what the backends already needed to handle. It also turned out convenient in that the align_offset method actually ends up using unchecked_shl<usize, usize> with this this PR, though once you change cttz_nonzero it'll also end up at unchecked_shl<usize, u32>, I think.

Copy link
Member

Choose a reason for hiding this comment

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

A bit weird (given that we don't publicly expose this), but given this does not make backend more complex, ig this is fine.

tests/mir-opt/inline/unchecked_shifts.rs Outdated Show resolved Hide resolved
@@ -27,7 +27,7 @@ const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) };

const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) };
//~^ ERROR evaluation of constant value failed
const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) };
const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_i16, 16) };
Copy link
Member

Choose a reason for hiding this comment

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

💀

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is a fun typo. So much so that I opened a clippy issue for a lint about it *looks* wow, 6 years ago now rust-lang/rust-clippy#3091

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2024
@WaffleLapkin
Copy link
Member

WaffleLapkin commented Apr 2, 2024

Thanks! @bors r+

@bors
Copy link
Contributor

bors commented Apr 2, 2024

📌 Commit 4626521 has been approved by WaffleLapkin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 2, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 2, 2024
De-LLVM the unchecked shifts [MCP#693]

This is just one part of the MCP (rust-lang/compiler-team#693), but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.

r? WaffleLapkin
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 2, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#123198 (Add fn const BuildHasherDefault::new)
 - rust-lang#123226 (De-LLVM the unchecked shifts [MCP#693])
 - rust-lang#123302 (Make sure to insert `Sized` bound first into clauses list)
 - rust-lang#123348 (rustdoc: add a couple of regression tests)
 - rust-lang#123362 (Check that nested statics in thread locals are duplicated per thread.)
 - rust-lang#123368 (CFI: Support non-general coroutines)
 - rust-lang#123375 (rustdoc: synthetic auto trait impls: accept unresolved region vars for now)
 - rust-lang#123378 (Update sysinfo to 0.30.8)

Failed merges:

 - rust-lang#123349 (Fix capture analysis for by-move closure bodies)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1b0e46f into rust-lang:master Apr 2, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 2, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 2, 2024
Rollup merge of rust-lang#123226 - scottmcm:u32-shifts, r=WaffleLapkin

De-LLVM the unchecked shifts [MCP#693]

This is just one part of the MCP (rust-lang/compiler-team#693), but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.

r? WaffleLapkin
@scottmcm scottmcm deleted the u32-shifts branch April 3, 2024 00:29
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 23, 2024
…tmcm,RalfJung,antoyo

Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)

This implements rust-lang/compiler-team#693 minus what was implemented in rust-lang#123226.

Note: I decided to _not_ change `shl`/... builder methods, as it just doesn't seem worth it.

r? `@scottmcm`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 23, 2024
…tmcm,RalfJung,antoyo

Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)

This implements rust-lang/compiler-team#693 minus what was implemented in rust-lang#123226.

Note: I decided to _not_ change `shl`/... builder methods, as it just doesn't seem worth it.

r? ``@scottmcm``
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 23, 2024
Rollup merge of rust-lang#124003 - WaffleLapkin:dellvmization, r=scottmcm,RalfJung,antoyo

Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)

This implements rust-lang/compiler-team#693 minus what was implemented in rust-lang#123226.

Note: I decided to _not_ change `shl`/... builder methods, as it just doesn't seem worth it.

r? ``@scottmcm``
bjorn3 pushed a commit to bjorn3/rust that referenced this pull request May 13, 2024
…tmcm,RalfJung,antoyo

Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)

This implements rust-lang/compiler-team#693 minus what was implemented in rust-lang#123226.

Note: I decided to _not_ change `shl`/... builder methods, as it just doesn't seem worth it.

r? ``@scottmcm``
GuillaumeGomez pushed a commit to GuillaumeGomez/rust that referenced this pull request Jul 10, 2024
…tmcm,RalfJung,antoyo

Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)

This implements rust-lang/compiler-team#693 minus what was implemented in rust-lang#123226.

Note: I decided to _not_ change `shl`/... builder methods, as it just doesn't seem worth it.

r? ``@scottmcm``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants