Fix malformed transmute handling during mir build#154144
Fix malformed transmute handling during mir build#154144Human9000-bit wants to merge 3 commits intorust-lang:mainfrom
Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
8891ca8 to
192c426
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
192c426 to
e8c1abe
Compare
This comment has been minimized.
This comment has been minimized.
e8c1abe to
2635118
Compare
|
r? mir |
2635118 to
5ccfb12
Compare
This comment has been minimized.
This comment has been minimized.
| @@ -0,0 +1,12 @@ | |||
| error[E0512]: cannot transmute between types of different sizes, or dependently-sized types | |||
There was a problem hiding this comment.
This error is emitted in typeck iirc. It should be tainting the body. If it isnt, it's likely that tcx.dcx() is used instead of infcx.dcx()
Please check if we can avoid changing anything in const prop by properly tainting wherever this error is emitted
There was a problem hiding this comment.
At first glance there isn't infcx where the error is emitted, and no obvious way to get one
There was a problem hiding this comment.
Where is it being emitted? If it is within the typeck query, we can probably change something further up the call stack ( you can try using -Ztreat-err-as-bug to find out where the error is emitted and what the call stack is while emitting it
There was a problem hiding this comment.
It is emitted in check_transmutes ran by run_required_analyses:
rust/compiler/rustc_interface/src/passes.rs
Lines 1146 to 1149 in 981cf69
and the very error emission happens here:
rust/compiler/rustc_hir_typeck/src/intrinsicck.rs
Lines 138 to 142 in 981cf69
There was a problem hiding this comment.
Oh wow. Didn't know that was changed (#145469)
I'll need to think on it. Unsure why it was necessary to live in a separate query
There was a problem hiding this comment.
Yes that would fix this optimizer ICE, but i didn't suggest that because i assumed we'd get the same ICE in CTFE, and the only fix for that is to run the query earlier, where it would likely cycle again
There was a problem hiding this comment.
We could check at the beginning of optimized_mir that check_transmutes does not emit an error.
I tried to do that at the start of run_pass of dataflow const prop. It ICEd for def_id being a typeck child.
So we can't just slap check_transmutes for every def_id (at least in mir opts)
There was a problem hiding this comment.
You can invoke is_typeck_child to invoke check_transmutes on the parent (there's also a method for getting the typeck parent).
This will fix the ICE (if you also make the check_transmute query return a Result<(), ErrorReported> and don't do anything if it's an error).
Tho at that point it may be better to do what cjgillot suggested and check it at the start of thr optimized_mir query and just return a dummy body or the unoptimized body.
I'm just sceptical it fixed the ICE in general as it should be possibe to produce the ICE in const eval by evaluating code that has the same transmute problem
There was a problem hiding this comment.
Hmm apparently not: https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=31291deac2b05c040502d266b33e2f69
It seems very weird that we report an error here, but that's probably an oversight. The ctfe error should be an ICE
There was a problem hiding this comment.
Apparently your example is getting caught in well-formedness checking during HIR analysis, which uses different methods of interpreter compared to dataflow const prop opt
5ccfb12 to
fec9c70
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
Follow-up idea: I am thinking it would be nice to add |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
… r=<try> Fix dataflow const prop behavior when propagating malsized transmutes
|
@oli-obk I tried the approach with checking transmutes another time before optimizing MIR, the perf results should be ready soon |
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (8483d7e): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. @bors rollup=never Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)Results (primary 4.3%, secondary -6.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.3%, secondary 3.8%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 491.114s -> 494.562s (0.70%) |
|
Reminder, once the PR becomes ready for a review, use |
That query modifier is for large query results: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/query/modifiers/struct.arena_cache.html I think using it may even cause perf regressions |
fec9c70 to
67933dd
Compare
This comment has been minimized.
This comment has been minimized.
67933dd to
51a2a9f
Compare
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
It looks like tainting a body inside |
|
Though missing error messages don't seem very much informative to me, so we wouldn't lose much if we just bless these tests ig |
|
☔ The latest upstream changes (presumably #155083) made this pull request unmergeable. Please resolve the merge conflicts. |
View all comments
Running dataflow const prop optimization on
std::mem::transmuteof mismatched sizes failed the assertion in interpreter and caused ICE.So it's better to not let those transmutations into the interpreter at all
Fixes #149920