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

Add ability to transmute (somewhat) with generic consts in arrays #106281

Merged
merged 2 commits into from
Apr 8, 2023

Conversation

JulianKnodt
Copy link
Contributor

@JulianKnodt JulianKnodt commented Dec 30, 2022

Previously if the expression contained generic consts and did not have a directly equivalent type, transmuting the type in this way was forbidden, despite the two sizes being identical. Instead, we should be able to lazily tell if the two consts are identical, and if so allow them to be transmuted.

This is done by normalizing the forms of expressions into sorted order of multiplied terms, which is not generic over all expressions, but should handle most cases.

This allows for some basic transmutations between types that are equivalent in size without requiring additional stack space at runtime.

I only see one other location at which SizeSkeleton is being used, and it checks for equality so this shouldn't affect anywhere else that I can tell.

See this Stackoverflow post for what was previously necessary to convert between types. This PR makes converting nested T -> [T; 1] transmutes possible, and [uB*2; N] -> [uB; N * 2] possible as well.

I'm not sure whether this is something that would be wanted, and if it is it definitely should not be insta-stable, so I'd add a feature gate.

@rustbot
Copy link
Collaborator

rustbot commented Dec 30, 2022

r? @nagisa

(rustbot has picked a reviewer for you, use r? to override)

@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. labels Dec 30, 2022
@JulianKnodt JulianKnodt force-pushed the transmute_const_generics branch 2 times, most recently from 98c8e16 to 72f169a Compare December 30, 2022 20:07
@rust-log-analyzer

This comment has been minimized.

@JulianKnodt JulianKnodt force-pushed the transmute_const_generics branch 2 times, most recently from 80f2358 to 42ca47c Compare December 30, 2022 21:06
@rust-log-analyzer

This comment has been minimized.

@nagisa nagisa added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jan 28, 2023
@nagisa
Copy link
Member

nagisa commented Jan 28, 2023

cc @eddyb may have some thoughts on the changes to the layout code.

Copy link
Member

@eddyb eddyb left a comment

Choose a reason for hiding this comment

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

Overall the approach looks sound (if compiler/lang teams are fine with it etc.), left some comments, don't really have much to say other than nitpicking though.

compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
Comment on lines +394 to +453
// constants are always pre-normalized into a canonical form so this
// only needs to check if their pointers are identical.
(SizeSkeleton::Generic(a), SizeSkeleton::Generic(b)) => a == b,
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need to be cheap, we could plausibly run a SAT/SMT solver here (since this is for "proving" two types have the same size). Feels weird to canonicalize ahead of time when ty::Const itself doesn't do it, but I don't know much about "generic consts".

Copy link
Contributor Author

@JulianKnodt JulianKnodt Feb 2, 2023

Choose a reason for hiding this comment

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

I'm not sure whether putting a whole SMT solver here would be reasonable, that seems like a lot of extra baggage for something that only gets rarely invoked.

I think just normalizing it for this specific use case is alright. I was also thinking about e graphs, but I'm not too familiar with it, and seems their library is a bit heavy

compiler/rustc_hir_typeck/src/intrinsicck.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
@JulianKnodt
Copy link
Contributor Author

JulianKnodt commented Jan 29, 2023

will update this after I get back from vacation in a week or so

edit: am back!

@JulianKnodt JulianKnodt force-pushed the transmute_const_generics branch 2 times, most recently from 264454d to 15ef66d Compare February 3, 2023 00:17
@JulianKnodt JulianKnodt marked this pull request as ready for review February 3, 2023 00:21
@nagisa
Copy link
Member

nagisa commented Feb 26, 2023

r? rust-lang/compiler

@rustbot rustbot assigned jackh726 and unassigned nagisa Feb 26, 2023
@apiraino
Copy link
Contributor

r? rust-lang/compiler

@rustbot rustbot assigned b-naber and unassigned jackh726 Mar 22, 2023
Copy link
Contributor

@b-naber b-naber left a comment

Choose a reason for hiding this comment

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

Probably need to take a second look at this in a couple of days, but have added some thoughts now.

compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/layout.rs Outdated Show resolved Hide resolved
@JulianKnodt
Copy link
Contributor Author

Should be good now!

@b-naber
Copy link
Contributor

b-naber commented Apr 8, 2023

Can anybody please explain why this needs CURRENT_RUSTC_VERSION? Not too familiar with the feature system... But I find it kind of weird that all other active features have a specific version number and the only other feature that uses CURRENT_RUSTC_VERSION is an incomplete feature.

@b-naber
Copy link
Contributor

b-naber commented Apr 8, 2023

nvm, just saw @klensy's post.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 8, 2023

📌 Commit b76dd8c has been approved by b-naber

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 8, 2023
@bors
Copy link
Contributor

bors commented Apr 8, 2023

⌛ Testing commit b76dd8c with merge af06dce...

@bors
Copy link
Contributor

bors commented Apr 8, 2023

☀️ Test successful - checks-actions
Approved by: b-naber
Pushing af06dce to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 8, 2023
@bors bors merged commit af06dce into rust-lang:master Apr 8, 2023
@rustbot rustbot added this to the 1.70.0 milestone Apr 8, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (af06dce): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.1% [1.1%, 1.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [1.1%, 1.1%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.1% [-3.3%, -2.9%] 2
All ❌✅ (primary) - - 0

@JulianKnodt JulianKnodt deleted the transmute_const_generics branch April 9, 2023 02:02
@scottmcm
Copy link
Member

@b-naber The meta-reason is that it avoids the "oops, the fork happened, need to unapprove all the PRs with the version in them so they get updated with the new number" problem. A new or stabilized gate always just uses the magic constant, and the release branching scripts make sure they get the real number.

scottmcm added a commit to scottmcm/rust that referenced this pull request Apr 23, 2023
This takes a whole 3 lines in `compiler/` since it lowers to `CastKind::Transmute` in MIR *exactly* the same as the existing `intrinsics::transmute` does, it just doesn't have the fancy checking in `hir_typeck`.

Added to enable experimenting with the request in <rust-lang#106281 (comment)> and because the portable-simd folks might be interested for dependently-sized array-vector conversions.

It also simplifies a couple places in `core`.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 24, 2023
…i-obk

Add `intrinsics::transmute_unchecked`

This takes a whole 3 lines in `compiler/` since it lowers to `CastKind::Transmute` in MIR *exactly* the same as the existing `intrinsics::transmute` does, it just doesn't have the fancy checking in `hir_typeck`.

Added to enable experimenting with the request in <rust-lang#106281 (comment)> and because the portable-simd folks might be interested for dependently-sized array-vector conversions.

It also simplifies a couple places in `core`.

See also rust-lang#108442 (comment), where `CastKind::Transmute` was added having exactly these semantics before the lang meeting (which I wasn't in) independently expressed interest.
@Mark-Simulacrum Mark-Simulacrum removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Apr 30, 2023
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request Jul 18, 2023
This takes a whole 3 lines in `compiler/` since it lowers to `CastKind::Transmute` in MIR *exactly* the same as the existing `intrinsics::transmute` does, it just doesn't have the fancy checking in `hir_typeck`.

Added to enable experimenting with the request in <rust-lang/rust#106281 (comment)> and because the portable-simd folks might be interested for dependently-sized array-vector conversions.

It also simplifies a couple places in `core`.
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request Jul 18, 2023
Add `intrinsics::transmute_unchecked`

This takes a whole 3 lines in `compiler/` since it lowers to `CastKind::Transmute` in MIR *exactly* the same as the existing `intrinsics::transmute` does, it just doesn't have the fancy checking in `hir_typeck`.

Added to enable experimenting with the request in <rust-lang/rust#106281 (comment)> and because the portable-simd folks might be interested for dependently-sized array-vector conversions.

It also simplifies a couple places in `core`.

See also rust-lang/rust#108442 (comment), where `CastKind::Transmute` was added having exactly these semantics before the lang meeting (which I wasn't in) independently expressed interest.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 22, 2024
Stop using `<DefId as Ord>` in various diagnostic situations

work towards rust-lang#90317

Reverts part of rust-lang#106281, as it sorts constants and that's problematic since it can contain `ParamConst`, which contains `DefId`s
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2024
Rollup merge of rust-lang#122820 - oli-obk:no_ord_def_id, r=estebank

Stop using `<DefId as Ord>` in various diagnostic situations

work towards rust-lang#90317

Reverts part of rust-lang#106281, as it sorts constants and that's problematic since it can contain `ParamConst`, which contains `DefId`s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.