Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
#[instrument(level = "trace", skip(self), ret)]
fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
use Value::*;
let op = match *self.get(value) {
let vvalue = self.get(value);
debug!(?vvalue);
let op = match *vvalue {
Opaque(_) => return None,
// Do not bother evaluating repeat expressions. This would uselessly consume memory.
Repeat(..) => return None,
Expand All @@ -376,10 +378,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
self.ecx.eval_mir_constant(value, DUMMY_SP, None).ok()?
}
Aggregate(kind, variant, ref fields) => {
debug!(?kind, ?variant, ?fields);
let fields = fields
.iter()
.map(|&f| self.evaluated[f].as_ref())
.collect::<Option<Vec<_>>>()?;
.collect::<Option<Vec<&OpTy<'_>>>>()?;
let ty = match kind {
AggregateTy::Array => {
assert!(fields.len() > 0);
Expand Down Expand Up @@ -407,6 +410,26 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
};
let ptr_imm = Immediate::new_pointer_with_meta(data, meta, &self.ecx);
ImmTy::from_immediate(ptr_imm, ty).into()
} else if matches!(kind, AggregateTy::Array) {
let mut mplace = None;
let alloc_id = self.ecx.intern_with_temp_alloc(ty, |ecx, dest| {
for (field_index, op) in fields.iter().copied().enumerate() {
let field_dest = ecx.project_field(dest, field_index)?;
ecx.copy_op(op, &field_dest)?;
}

let dest = dest.assert_mem_place();
//ecx.alloc_mark_immutable(dest.ptr().provenance.unwrap().alloc_id()).unwrap();
let dest = dest.map_provenance(|prov| prov.as_immutable());
mplace.replace(dest);
Ok(())
}).ok()?;
let GlobalAlloc::Memory(_alloc) = self.tcx.global_alloc(alloc_id) else {
bug!()
};
let mplace = mplace.unwrap();
debug!(?mplace);
return Some(mplace.into());
} else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
let dest = self.ecx.allocate(ty, MemoryKind::Stack).ok()?;
let variant_dest = if let Some(variant) = variant {
Expand All @@ -429,6 +452,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}

Projection(base, elem) => {
debug!(?base, ?elem);
let value = self.evaluated[base].as_ref()?;
let elem = match elem {
ProjectionElem::Deref => ProjectionElem::Deref,
Expand All @@ -450,6 +474,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
self.ecx.project(value, elem).ok()?
}
Address { place, kind, provenance: _ } => {
debug!(?place, ?kind);
if !place.is_indirect_first_projection() {
return None;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/mir-opt/const_array_locals.main.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
- // MIR for `main` before GVN
+ // MIR for `main` after GVN

fn main() -> () {
let mut _0: ();
let _1: [i32; 32];
let mut _4: &[i32; 32];
let _5: [i32; 32];
scope 1 {
debug _arr => _1;
let _2: [i32; 32];
scope 2 {
debug _barr => _2;
let _3: [i32; 32];
let mut _6: &[i32; 32];
scope 3 {
debug _darr => _3;
}
}
}

bb0: {
- StorageLive(_1);
+ nop;
_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];
StorageLive(_2);
- _2 = [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];
+ _2 = _1;
StorageLive(_3);
StorageLive(_4);
_6 = const main::promoted[0];
_4 = &(*_6);
- _3 = (*_4);
+ _3 = (*_6);
StorageDead(_4);
_0 = const ();
StorageDead(_3);
StorageDead(_2);
- StorageDead(_1);
+ nop;
return;
}
}

46 changes: 46 additions & 0 deletions tests/mir-opt/const_array_locals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//@ test-mir-pass: GVN
#![feature(repr_simd)]

// #[repr(simd)]
// struct F32x8([f32; 8]);

// EMIT_MIR const_array_locals.main.GVN.diff
// CHECK-LABEL: fn main(
#[rustfmt::skip]
pub fn main() {
// CHECK: let [[array_lit:_.*]]: [i32; 32];
// CHECK: debug _arr => [[arr:_.*]];

let _arr = [
Copy link
Owner Author

@tesuji tesuji Jun 12, 2024

Choose a reason for hiding this comment

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

With commits[3], this now returns as a immutable memplace: Some(OpTy { op: Indirect(MemPlace { ptr: alloc3<imm>, meta: None, misaligned: None }), ty: [i32; 32] }).

But in the mir diff, _arr still is not promoted.

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 _barr = [
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 = *&[
Copy link
Owner Author

Choose a reason for hiding this comment

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

This returns Some(OpTy { op: Indirect(MemPlace { ptr: alloc2<imm>, meta: None, misaligned: None }), ty: [i32; 32] })

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,
];

// 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!()
// }