-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Allow read
ing a *mut
without an internal cast
#111233
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
_2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
- _0 = read_via_copy::<i32>(move _2) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
_2 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
- _0 = read_via_copy::<&i32, i32>(move _2) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this also allows directly reading a &
, though it's not used in library
in this PR.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The Miri subtree was changed cc @rust-lang/miri |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks reasonable to me, but I'd like to see if we actually gain something tangible.
StorageDead(_6); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
StorageDead(_5); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
StorageDead(_4); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL | ||
goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR; I'm just curious: why do we goto -> bb1
instead of simply return
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LLVM prefers having a single return terminator. So have a pass to replace such "go to return", but disabled by default. @saethlin did a perf run recently IIRC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#106362 is the "just enable the pass" perf runs
#106550 is enabling the pass then cleaning up after it later in codegen
I don't think this is particularly worth pursuing based on the data available in those PRs, but I have also noticed that there are cases where we can end up with multiple return terminators heading into codegen. Perhaps that itself would be interesting to look into.
- // + span: $DIR/lower_intrinsics.rs:154:5: 154:29 | ||
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*mut i32, usize) -> *mut i32 {offset::<*mut i32, usize>}, val: Value(<ZST>) } | ||
+ _0 = Offset(move _3, move _4); // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:35 | ||
+ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:35 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly to the other comment, why do we goto
to bb1
, where bb1
can be simply inlined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separation of concerns: LowerIntrinsics
is just tasked with replacing the call terminator. Merging the blocks is left to SimplifyCfg
pass.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
⌛ Trying commit 1464345 with merge 1abbc984fa15fd4b95f10542fb07d730767dcd77... |
// SAFETY: We read from `dest` but directly write `src` into it afterwards, | ||
// such that the old value is not duplicated. Nothing is dropped and | ||
// nothing here can panic. | ||
unsafe { | ||
let result = ptr::read(dest); | ||
let result = ptr::read_mut(dest); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compared to ptr::read
, this just avoids a single MIR statement to turn the *mut
into a *const
, right? I am quite surprised that this would be a change worth doing. And if it is worth doing, a more holistic approach might be to just not do that cast and instead allow *mut
everywhere that *const
is allowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does that a bit more for the method versions -- *mut
has had read
for 5 years now, and this PR makes that 1 statement shorter, which is certainly unimportant on its own but impacts every time it's mir-inlined. I suppose we can compatibly add impl Trait
parameters to functions now, so we could consider letting ptr::read
take more things too, but I don't know how disruptive that would be -- or if libs-api would even want it.
But I'm also not certain I understand what you were imagining with "allow *mut
everywhere that *const
is allowed". Could you sketch it out a bit more? I'm curious. (AFAIK MIR is typed enough that we can't omit these casts, even though they're NOPs?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking MIR optimizations could remove the *mut
to *const
cast that was introduced by MIR building, and our notion of "well-typed MIR" could be weakened to say that this is fine.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (1abbc984fa15fd4b95f10542fb07d730767dcd77): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Binary sizeResultsThis 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.
Bootstrap: 654.604s -> 654.776s (0.03%) |
Continuing on from #110837 and #111010, another PR to
as pnkfelix said 🙂
r? @WaffleLapkin
cc @saethlin