-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
[WIP] mir-opt: promoting const read-only arrays #125916
Changes from all commits
80bcc74
1c2d0f5
c7ac773
3facd92
7a97b1c
3e21e5f
b6bf4e4
6b6bc18
19a01fd
dfcae48
e53c231
d11c9e2
1f40fa4
7c4dcca
ea443af
514e4a9
dca7207
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,7 @@ mod normalize_array_len; | |
mod nrvo; | ||
mod prettify; | ||
mod promote_consts; | ||
mod promote_consts_local_arrays; | ||
mod ref_prop; | ||
mod remove_noop_landing_pads; | ||
mod remove_storage_markers; | ||
|
@@ -342,14 +343,22 @@ fn mir_promoted( | |
|
||
// What we need to run borrowck etc. | ||
let promote_pass = promote_consts::PromoteTemps::default(); | ||
let promote_array = promote_consts_local_arrays::PromoteArraysOpt::default(); | ||
pm::run_passes( | ||
tcx, | ||
&mut body, | ||
&[&promote_pass, &simplify::SimplifyCfg::PromoteConsts, &coverage::InstrumentCoverage], | ||
&[ | ||
&promote_pass, | ||
&promote_array, | ||
&simplify::SimplifyCfg::PromoteConsts, | ||
&coverage::InstrumentCoverage, | ||
], | ||
Some(MirPhase::Analysis(AnalysisPhase::Initial)), | ||
); | ||
|
||
let promoted = promote_pass.promoted_fragments.into_inner(); | ||
let mut promoted = promote_pass.promoted_fragments.into_inner(); | ||
let array_promoted = promote_array.promoted_fragments.into_inner(); | ||
promoted.extend(array_promoted); | ||
Comment on lines
+360
to
+361
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which does mean you won't be able to use the existing promotion scheme, but would need to start looking into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then again, if all we're doing is creating non-generic static items, that already has precedent (we do that for nested statics), so likely you can do the same in an optimization. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though in that case I would expect this to fall out of GVN or some similar optimization, not be its own separate path There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With GVN or similar you don't even need to create new constants and MIR bodies, you can just stick the fully evaluated constant into a MIR constant |
||
(tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted)) | ||
} | ||
|
||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
- // MIR for `main` before PromoteArraysOpt | ||
+ // MIR for `main` after PromoteArraysOpt | ||
|
||
fn main() -> () { | ||
let mut _0: (); | ||
let _1: [i32; 32]; | ||
let mut _3: [i32; 12]; | ||
let mut _4: [i32; 12]; | ||
let mut _6: &[i32; 32]; | ||
let _7: [i32; 32]; | ||
let _8: (); | ||
let mut _9: [u16; 32]; | ||
let mut _11: [f32; 8]; | ||
let _12: [[i32; 3]; 3]; | ||
let mut _13: [i32; 3]; | ||
let mut _14: [i32; 3]; | ||
let mut _15: [i32; 3]; | ||
+ let mut _19: &[i32; 32]; | ||
scope 1 { | ||
debug _arr => _1; | ||
let _2: [[i32; 12]; 2]; | ||
+ let mut _18: &[[i32; 12]; 2]; | ||
scope 2 { | ||
debug _foo => _2; | ||
let _5: [i32; 32]; | ||
let mut _16: &[i32; 32]; | ||
scope 3 { | ||
debug _darr => _5; | ||
let _10: F32x8; | ||
+ let mut _17: &[u16; 32]; | ||
scope 4 { | ||
debug _f => _10; | ||
} | ||
} | ||
} | ||
} | ||
|
||
bb0: { | ||
- StorageLive(_1); | ||
- _1 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32, const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 159_i32, const 22_i32, const 157_i32, const 105_i32, const 31_i32, const 96_i32, const 173_i32, const 50_i32, const 1_i32]; | ||
+ _19 = const main::promoted[3]; | ||
+ _1 = (*_19); | ||
FakeRead(ForLet(None), _1); | ||
- StorageLive(_2); | ||
- StorageLive(_3); | ||
- _3 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32]; | ||
- StorageLive(_4); | ||
- _4 = [const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 42_i32]; | ||
- _2 = [move _3, move _4]; | ||
- StorageDead(_4); | ||
- StorageDead(_3); | ||
+ _18 = const main::promoted[2]; | ||
+ _2 = (*_18); | ||
FakeRead(ForLet(None), _2); | ||
StorageLive(_5); | ||
StorageLive(_6); | ||
_16 = const main::promoted[0]; | ||
_6 = &(*_16); | ||
_5 = (*_6); | ||
FakeRead(ForLet(None), _5); | ||
StorageDead(_6); | ||
StorageLive(_8); | ||
- StorageLive(_9); | ||
- _9 = [const 255_u16, const 105_u16, const 15_u16, const 39_u16, const 62_u16, const 251_u16, const 191_u16, const 178_u16, const 9_u16, const 4_u16, const 56_u16, const 221_u16, const 193_u16, const 164_u16, const 194_u16, const 197_u16, const 6_u16, const 243_u16, const 218_u16, const 171_u16, const 87_u16, const 247_u16, const 104_u16, const 159_u16, const 22_u16, const 157_u16, const 105_u16, const 31_u16, const 96_u16, const 173_u16, const 50_u16, const 1_u16]; | ||
+ _17 = const main::promoted[1]; | ||
+ _9 = (*_17); | ||
_8 = consume(move _9) -> [return: bb1, unwind: bb2]; | ||
} | ||
|
||
bb1: { | ||
- StorageDead(_9); | ||
StorageDead(_8); | ||
StorageLive(_10); | ||
StorageLive(_11); | ||
_11 = [const 1f32, const 1f32, const 1f32, const 1f32, const 1f32, const 1f32, const 1f32, const 1f32]; | ||
_10 = F32x8(move _11); | ||
StorageDead(_11); | ||
FakeRead(ForLet(None), _10); | ||
StorageLive(_12); | ||
StorageLive(_13); | ||
_13 = [const 1_i32, const 0_i32, const 0_i32]; | ||
StorageLive(_14); | ||
_14 = [const 0_i32, const 1_i32, const 0_i32]; | ||
StorageLive(_15); | ||
_15 = [const 0_i32, const 0_i32, const 1_i32]; | ||
_12 = [move _13, move _14, move _15]; | ||
StorageDead(_15); | ||
StorageDead(_14); | ||
StorageDead(_13); | ||
StorageDead(_12); | ||
_0 = const (); | ||
StorageDead(_10); | ||
StorageDead(_5); | ||
- StorageDead(_2); | ||
- StorageDead(_1); | ||
return; | ||
} | ||
|
||
bb2 (cleanup): { | ||
resume; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//@ test-mir-pass: PromoteArraysOpt | ||
#![feature(repr_simd)] | ||
|
||
#[repr(simd)] | ||
struct F32x8([f32; 8]); | ||
|
||
// EMIT_MIR const_array_locals.main.PromoteArraysOpt.diff | ||
// CHECK-LABEL: fn main( | ||
#[rustfmt::skip] | ||
pub fn main() { | ||
// CHECK: let [[array_lit:_.*]]: [i32; 32]; | ||
// CHECK: let mut [[moved_array:_.*]]: [u16; 32]; | ||
// CHECK: debug _arr => [[arr:_.*]]; | ||
|
||
// CHECK: [[array_lit]] = (*{{_[0-9]*}}) | ||
let _arr = [ | ||
255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, | ||
193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, | ||
159, 22, 157, 105, 31, 96, 173, 50, 1, | ||
]; | ||
let _foo = [ | ||
[255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221], | ||
[193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, 42], | ||
]; | ||
let _darr = *&[ | ||
255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, | ||
193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, | ||
159, 22, 157, 105, 31, 96, 173, 50, 1, | ||
]; | ||
|
||
// CHECK: [[moved_array]] = (*{{_[0-9]*}}) | ||
consume([255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, | ||
193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, | ||
159, 22, 157, 105, 31, 96, 173, 50, 1,]); | ||
|
||
let _f = F32x8([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]); | ||
|
||
// ice | ||
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]; // 2D array | ||
} | ||
|
||
fn consume(_arr: [u16; 32]) { | ||
unimplemented!() | ||
} |
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.
since this is something that should not have user-visible effects (e.g. affecting dropck, const eval UB or borrowck), it should be run as part of the regular runtime optimization pipeline