Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 31d1010

Browse files
committed
Generate ValTrees in DataflowConstProp.
1 parent 249624b commit 31d1010

16 files changed

+234
-50
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,102 @@ impl<'tcx, 'locals> Collector<'tcx, 'locals> {
557557
state: &State<FlatSet<Scalar>>,
558558
map: &Map,
559559
) -> Option<Const<'tcx>> {
560-
let FlatSet::Elem(Scalar::Int(value)) = state.get(place.as_ref(), &map) else {
561-
return None;
562-
};
563560
let ty = place.ty(self.local_decls, self.patch.tcx).ty;
564-
Some(Const::Val(ConstValue::Scalar(value.into()), ty))
561+
let place = map.find(place.as_ref())?;
562+
if let FlatSet::Elem(Scalar::Int(value)) = state.get_idx(place, map) {
563+
Some(Const::Val(ConstValue::Scalar(value.into()), ty))
564+
} else {
565+
let valtree = self.try_make_valtree(place, ty, state, map)?;
566+
let constant = ty::Const::new_value(self.patch.tcx, valtree, ty);
567+
Some(Const::Ty(constant))
568+
}
569+
}
570+
571+
fn try_make_valtree(
572+
&self,
573+
place: PlaceIndex,
574+
ty: Ty<'tcx>,
575+
state: &State<FlatSet<Scalar>>,
576+
map: &Map,
577+
) -> Option<ty::ValTree<'tcx>> {
578+
let tcx = self.patch.tcx;
579+
match ty.kind() {
580+
// ZSTs.
581+
ty::FnDef(..) => Some(ty::ValTree::zst()),
582+
583+
// Scalars.
584+
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
585+
if let FlatSet::Elem(Scalar::Int(value)) = state.get_idx(place, map) {
586+
Some(ty::ValTree::Leaf(value))
587+
} else {
588+
None
589+
}
590+
}
591+
592+
// Unsupported for now.
593+
ty::Array(_, _) => None,
594+
595+
ty::Tuple(elem_tys) => {
596+
let branches = elem_tys
597+
.iter()
598+
.enumerate()
599+
.map(|(i, ty)| {
600+
let field = map.apply(place, TrackElem::Field(FieldIdx::from_usize(i)))?;
601+
self.try_make_valtree(field, ty, state, map)
602+
})
603+
.collect::<Option<Vec<_>>>()?;
604+
Some(ty::ValTree::Branch(tcx.arena.alloc_from_iter(branches.into_iter())))
605+
}
606+
607+
ty::Adt(def, args) => {
608+
if def.is_union() {
609+
return None;
610+
}
611+
612+
let (variant_idx, variant_def, variant_place) = if def.is_enum() {
613+
let discr = map.apply(place, TrackElem::Discriminant)?;
614+
let FlatSet::Elem(Scalar::Int(discr)) = state.get_idx(discr, map) else {
615+
return None;
616+
};
617+
let discr_bits = discr.assert_bits(discr.size());
618+
let (variant, _) =
619+
def.discriminants(tcx).find(|(_, var)| discr_bits == var.val)?;
620+
let variant_place = map.apply(place, TrackElem::Variant(variant))?;
621+
let variant_int = ty::ValTree::Leaf(variant.as_u32().into());
622+
(Some(variant_int), def.variant(variant), variant_place)
623+
} else {
624+
(None, def.non_enum_variant(), place)
625+
};
626+
627+
let branches = variant_def
628+
.fields
629+
.iter_enumerated()
630+
.map(|(i, field)| {
631+
let ty = field.ty(tcx, args);
632+
let field = map.apply(variant_place, TrackElem::Field(i))?;
633+
self.try_make_valtree(field, ty, state, map)
634+
})
635+
.collect::<Option<Vec<_>>>()?;
636+
Some(ty::ValTree::Branch(
637+
tcx.arena.alloc_from_iter(variant_idx.into_iter().chain(branches)),
638+
))
639+
}
640+
641+
// Do not attempt to support indirection in constants.
642+
ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Str | ty::Slice(_) => None,
643+
644+
ty::Never
645+
| ty::Foreign(..)
646+
| ty::Alias(..)
647+
| ty::Param(_)
648+
| ty::Bound(..)
649+
| ty::Placeholder(..)
650+
| ty::Closure(..)
651+
| ty::Coroutine(..)
652+
| ty::Dynamic(..) => None,
653+
654+
ty::Error(_) | ty::Infer(..) | ty::CoroutineWitness(..) => bug!(),
655+
}
565656
}
566657
}
567658

tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
+ debug ((f: (bool, bool, u32)).2: u32) => const 123_u32;
4242
let _10: std::option::Option<u16>;
4343
scope 7 {
44-
debug o => _10;
44+
- debug o => _10;
45+
+ debug o => const Option::<u16>::Some(99);
4546
let _17: u32;
4647
let _18: u32;
4748
scope 8 {
@@ -81,7 +82,7 @@
8182
_15 = const false;
8283
_16 = const 123_u32;
8384
StorageLive(_10);
84-
_10 = Option::<u16>::Some(const 99_u16);
85+
_10 = const Option::<u16>::Some(99);
8586
_17 = const 32_u32;
8687
_18 = const 32_u32;
8788
StorageLive(_11);

tests/mir-opt/dataflow-const-prop/checked.main.DataflowConstProp.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
- _6 = CheckedAdd(_4, _5);
4444
- assert(!move (_6.1: bool), "attempt to compute `{} + {}`, which would overflow", move _4, move _5) -> [success: bb1, unwind unreachable];
4545
+ _5 = const 2_i32;
46-
+ _6 = CheckedAdd(const 1_i32, const 2_i32);
46+
+ _6 = const (3, false);
4747
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind unreachable];
4848
}
4949

@@ -60,7 +60,7 @@
6060
- _10 = CheckedAdd(_9, const 1_i32);
6161
- assert(!move (_10.1: bool), "attempt to compute `{} + {}`, which would overflow", move _9, const 1_i32) -> [success: bb2, unwind unreachable];
6262
+ _9 = const i32::MAX;
63-
+ _10 = CheckedAdd(const i32::MAX, const 1_i32);
63+
+ _10 = const (i32::MIN, true);
6464
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const i32::MAX, const 1_i32) -> [success: bb2, unwind unreachable];
6565
}
6666

tests/mir-opt/dataflow-const-prop/checked.main.DataflowConstProp.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
- _6 = CheckedAdd(_4, _5);
4444
- assert(!move (_6.1: bool), "attempt to compute `{} + {}`, which would overflow", move _4, move _5) -> [success: bb1, unwind continue];
4545
+ _5 = const 2_i32;
46-
+ _6 = CheckedAdd(const 1_i32, const 2_i32);
46+
+ _6 = const (3, false);
4747
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind continue];
4848
}
4949

@@ -60,7 +60,7 @@
6060
- _10 = CheckedAdd(_9, const 1_i32);
6161
- assert(!move (_10.1: bool), "attempt to compute `{} + {}`, which would overflow", move _9, const 1_i32) -> [success: bb2, unwind continue];
6262
+ _9 = const i32::MAX;
63-
+ _10 = CheckedAdd(const i32::MAX, const 1_i32);
63+
+ _10 = const (i32::MIN, true);
6464
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const i32::MAX, const 1_i32) -> [success: bb2, unwind continue];
6565
}
6666

tests/mir-opt/dataflow-const-prop/enum.simple.DataflowConstProp.32bit.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
bb0: {
2525
StorageLive(_1);
26-
_1 = E::V1(const 0_i32);
26+
- _1 = E::V1(const 0_i32);
27+
+ _1 = const E::V1(0);
2728
StorageLive(_2);
2829
- _3 = discriminant(_1);
2930
- switchInt(move _3) -> [0: bb3, 1: bb1, otherwise: bb2];

tests/mir-opt/dataflow-const-prop/enum.simple.DataflowConstProp.64bit.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
bb0: {
2525
StorageLive(_1);
26-
_1 = E::V1(const 0_i32);
26+
- _1 = E::V1(const 0_i32);
27+
+ _1 = const E::V1(0);
2728
StorageLive(_2);
2829
- _3 = discriminant(_1);
2930
- switchInt(move _3) -> [0: bb3, 1: bb1, otherwise: bb2];

tests/mir-opt/dataflow-const-prop/enum.statics.DataflowConstProp.32bit.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
StorageLive(_1);
4545
StorageLive(_2);
4646
_2 = const {ALLOC1: &E};
47-
_1 = (*_2);
47+
- _1 = (*_2);
48+
+ _1 = const E::V1(0);
4849
StorageDead(_2);
4950
StorageLive(_3);
5051
- _4 = discriminant(_1);

tests/mir-opt/dataflow-const-prop/enum.statics.DataflowConstProp.64bit.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
StorageLive(_1);
4545
StorageLive(_2);
4646
_2 = const {ALLOC1: &E};
47-
_1 = (*_2);
47+
- _1 = (*_2);
48+
+ _1 = const E::V1(0);
4849
StorageDead(_2);
4950
StorageLive(_3);
5051
- _4 = discriminant(_1);

tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
StorageLive(_4);
2424
- _4 = CheckedAdd(_2, _3);
2525
- assert(!move (_4.1: bool), "attempt to compute `{} + {}`, which would overflow", _2, _3) -> [success: bb1, unwind unreachable];
26-
+ _4 = CheckedAdd(const u8::MAX, const 1_u8);
26+
+ _4 = const (0, true);
2727
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const u8::MAX, const 1_u8) -> [success: bb1, unwind unreachable];
2828
}
2929

tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
StorageLive(_4);
2424
- _4 = CheckedAdd(_2, _3);
2525
- assert(!move (_4.1: bool), "attempt to compute `{} + {}`, which would overflow", _2, _3) -> [success: bb1, unwind continue];
26-
+ _4 = CheckedAdd(const u8::MAX, const 1_u8);
26+
+ _4 = const (0, true);
2727
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const u8::MAX, const 1_u8) -> [success: bb1, unwind continue];
2828
}
2929

0 commit comments

Comments
 (0)