Generate ValTrees in DataflowConstProp.

This commit is contained in:
Camille GILLOT 2023-09-12 16:44:53 +00:00
parent 249624b504
commit 31d101093c
16 changed files with 234 additions and 50 deletions

View File

@ -557,11 +557,102 @@ impl<'tcx, 'locals> Collector<'tcx, 'locals> {
state: &State<FlatSet<Scalar>>,
map: &Map,
) -> Option<Const<'tcx>> {
let FlatSet::Elem(Scalar::Int(value)) = state.get(place.as_ref(), &map) else {
return None;
};
let ty = place.ty(self.local_decls, self.patch.tcx).ty;
Some(Const::Val(ConstValue::Scalar(value.into()), ty))
let place = map.find(place.as_ref())?;
if let FlatSet::Elem(Scalar::Int(value)) = state.get_idx(place, map) {
Some(Const::Val(ConstValue::Scalar(value.into()), ty))
} else {
let valtree = self.try_make_valtree(place, ty, state, map)?;
let constant = ty::Const::new_value(self.patch.tcx, valtree, ty);
Some(Const::Ty(constant))
}
}
fn try_make_valtree(
&self,
place: PlaceIndex,
ty: Ty<'tcx>,
state: &State<FlatSet<Scalar>>,
map: &Map,
) -> Option<ty::ValTree<'tcx>> {
let tcx = self.patch.tcx;
match ty.kind() {
// ZSTs.
ty::FnDef(..) => Some(ty::ValTree::zst()),
// Scalars.
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
if let FlatSet::Elem(Scalar::Int(value)) = state.get_idx(place, map) {
Some(ty::ValTree::Leaf(value))
} else {
None
}
}
// Unsupported for now.
ty::Array(_, _) => None,
ty::Tuple(elem_tys) => {
let branches = elem_tys
.iter()
.enumerate()
.map(|(i, ty)| {
let field = map.apply(place, TrackElem::Field(FieldIdx::from_usize(i)))?;
self.try_make_valtree(field, ty, state, map)
})
.collect::<Option<Vec<_>>>()?;
Some(ty::ValTree::Branch(tcx.arena.alloc_from_iter(branches.into_iter())))
}
ty::Adt(def, args) => {
if def.is_union() {
return None;
}
let (variant_idx, variant_def, variant_place) = if def.is_enum() {
let discr = map.apply(place, TrackElem::Discriminant)?;
let FlatSet::Elem(Scalar::Int(discr)) = state.get_idx(discr, map) else {
return None;
};
let discr_bits = discr.assert_bits(discr.size());
let (variant, _) =
def.discriminants(tcx).find(|(_, var)| discr_bits == var.val)?;
let variant_place = map.apply(place, TrackElem::Variant(variant))?;
let variant_int = ty::ValTree::Leaf(variant.as_u32().into());
(Some(variant_int), def.variant(variant), variant_place)
} else {
(None, def.non_enum_variant(), place)
};
let branches = variant_def
.fields
.iter_enumerated()
.map(|(i, field)| {
let ty = field.ty(tcx, args);
let field = map.apply(variant_place, TrackElem::Field(i))?;
self.try_make_valtree(field, ty, state, map)
})
.collect::<Option<Vec<_>>>()?;
Some(ty::ValTree::Branch(
tcx.arena.alloc_from_iter(variant_idx.into_iter().chain(branches)),
))
}
// Do not attempt to support indirection in constants.
ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Str | ty::Slice(_) => None,
ty::Never
| ty::Foreign(..)
| ty::Alias(..)
| ty::Param(_)
| ty::Bound(..)
| ty::Placeholder(..)
| ty::Closure(..)
| ty::Coroutine(..)
| ty::Dynamic(..) => None,
ty::Error(_) | ty::Infer(..) | ty::CoroutineWitness(..) => bug!(),
}
}
}

View File

@ -41,7 +41,8 @@
+ debug ((f: (bool, bool, u32)).2: u32) => const 123_u32;
let _10: std::option::Option<u16>;
scope 7 {
debug o => _10;
- debug o => _10;
+ debug o => const Option::<u16>::Some(99);
let _17: u32;
let _18: u32;
scope 8 {
@ -81,7 +82,7 @@
_15 = const false;
_16 = const 123_u32;
StorageLive(_10);
_10 = Option::<u16>::Some(const 99_u16);
_10 = const Option::<u16>::Some(99);
_17 = const 32_u32;
_18 = const 32_u32;
StorageLive(_11);

View File

@ -43,7 +43,7 @@
- _6 = CheckedAdd(_4, _5);
- assert(!move (_6.1: bool), "attempt to compute `{} + {}`, which would overflow", move _4, move _5) -> [success: bb1, unwind unreachable];
+ _5 = const 2_i32;
+ _6 = CheckedAdd(const 1_i32, const 2_i32);
+ _6 = const (3, false);
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind unreachable];
}
@ -60,7 +60,7 @@
- _10 = CheckedAdd(_9, const 1_i32);
- assert(!move (_10.1: bool), "attempt to compute `{} + {}`, which would overflow", move _9, const 1_i32) -> [success: bb2, unwind unreachable];
+ _9 = const i32::MAX;
+ _10 = CheckedAdd(const i32::MAX, const 1_i32);
+ _10 = const (i32::MIN, true);
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const i32::MAX, const 1_i32) -> [success: bb2, unwind unreachable];
}

View File

@ -43,7 +43,7 @@
- _6 = CheckedAdd(_4, _5);
- assert(!move (_6.1: bool), "attempt to compute `{} + {}`, which would overflow", move _4, move _5) -> [success: bb1, unwind continue];
+ _5 = const 2_i32;
+ _6 = CheckedAdd(const 1_i32, const 2_i32);
+ _6 = const (3, false);
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 1_i32, const 2_i32) -> [success: bb1, unwind continue];
}
@ -60,7 +60,7 @@
- _10 = CheckedAdd(_9, const 1_i32);
- assert(!move (_10.1: bool), "attempt to compute `{} + {}`, which would overflow", move _9, const 1_i32) -> [success: bb2, unwind continue];
+ _9 = const i32::MAX;
+ _10 = CheckedAdd(const i32::MAX, const 1_i32);
+ _10 = const (i32::MIN, true);
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const i32::MAX, const 1_i32) -> [success: bb2, unwind continue];
}

View File

@ -23,7 +23,8 @@
bb0: {
StorageLive(_1);
_1 = E::V1(const 0_i32);
- _1 = E::V1(const 0_i32);
+ _1 = const E::V1(0);
StorageLive(_2);
- _3 = discriminant(_1);
- switchInt(move _3) -> [0: bb3, 1: bb1, otherwise: bb2];

View File

@ -23,7 +23,8 @@
bb0: {
StorageLive(_1);
_1 = E::V1(const 0_i32);
- _1 = E::V1(const 0_i32);
+ _1 = const E::V1(0);
StorageLive(_2);
- _3 = discriminant(_1);
- switchInt(move _3) -> [0: bb3, 1: bb1, otherwise: bb2];

View File

@ -44,7 +44,8 @@
StorageLive(_1);
StorageLive(_2);
_2 = const {ALLOC1: &E};
_1 = (*_2);
- _1 = (*_2);
+ _1 = const E::V1(0);
StorageDead(_2);
StorageLive(_3);
- _4 = discriminant(_1);

View File

@ -44,7 +44,8 @@
StorageLive(_1);
StorageLive(_2);
_2 = const {ALLOC1: &E};
_1 = (*_2);
- _1 = (*_2);
+ _1 = const E::V1(0);
StorageDead(_2);
StorageLive(_3);
- _4 = discriminant(_1);

View File

@ -23,7 +23,7 @@
StorageLive(_4);
- _4 = CheckedAdd(_2, _3);
- assert(!move (_4.1: bool), "attempt to compute `{} + {}`, which would overflow", _2, _3) -> [success: bb1, unwind unreachable];
+ _4 = CheckedAdd(const u8::MAX, const 1_u8);
+ _4 = const (0, true);
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const u8::MAX, const 1_u8) -> [success: bb1, unwind unreachable];
}

View File

@ -23,7 +23,7 @@
StorageLive(_4);
- _4 = CheckedAdd(_2, _3);
- assert(!move (_4.1: bool), "attempt to compute `{} + {}`, which would overflow", _2, _3) -> [success: bb1, unwind continue];
+ _4 = CheckedAdd(const u8::MAX, const 1_u8);
+ _4 = const (0, true);
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const u8::MAX, const 1_u8) -> [success: bb1, unwind continue];
}

View File

@ -17,7 +17,8 @@
bb0: {
StorageLive(_1);
_1 = I32(const 0_i32);
- _1 = I32(const 0_i32);
+ _1 = const I32(0);
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
@ -31,7 +32,7 @@
StorageDead(_5);
StorageDead(_4);
- _2 = I32(move _3);
+ _2 = I32(const 0_i32);
+ _2 = const I32(0);
StorageDead(_3);
_0 = const ();
StorageDead(_2);

View File

@ -9,11 +9,15 @@
let mut _6: i32;
let mut _11: BigStruct;
let mut _16: &&BigStruct;
let mut _17: &BigStruct;
let mut _18: &BigStruct;
let mut _19: &BigStruct;
let mut _20: &BigStruct;
let mut _21: &BigStruct;
let mut _18: S;
let mut _19: u8;
let mut _20: f32;
let mut _21: S;
let mut _22: &BigStruct;
let mut _23: &BigStruct;
let mut _24: &BigStruct;
let mut _25: &BigStruct;
let mut _26: &BigStruct;
scope 1 {
debug s => _1;
let _2: i32;
@ -40,6 +44,10 @@
debug b => _13;
debug c => _14;
debug d => _15;
let _17: BigStruct;
scope 6 {
debug bs => _17;
}
}
}
}
@ -48,7 +56,8 @@
bb0: {
StorageLive(_1);
_1 = S(const 1_i32);
- _1 = S(const 1_i32);
+ _1 = const S(1);
StorageLive(_2);
StorageLive(_3);
- _3 = (_1.0: i32);
@ -85,25 +94,45 @@
StorageDead(_11);
StorageLive(_16);
_16 = const {ALLOC1: &&BigStruct};
_17 = deref_copy (*_16);
_22 = deref_copy (*_16);
StorageLive(_12);
_18 = deref_copy (*_16);
- _12 = ((*_18).0: S);
_23 = deref_copy (*_16);
- _12 = ((*_23).0: S);
+ _12 = const S(1_i32);
StorageLive(_13);
_19 = deref_copy (*_16);
- _13 = ((*_19).1: u8);
_24 = deref_copy (*_16);
- _13 = ((*_24).1: u8);
+ _13 = const 5_u8;
StorageLive(_14);
_20 = deref_copy (*_16);
- _14 = ((*_20).2: f32);
_25 = deref_copy (*_16);
- _14 = ((*_25).2: f32);
+ _14 = const 7f32;
StorageLive(_15);
_21 = deref_copy (*_16);
- _15 = ((*_21).3: S);
_26 = deref_copy (*_16);
- _15 = ((*_26).3: S);
+ _15 = const S(13_i32);
StorageDead(_16);
StorageLive(_17);
StorageLive(_18);
- _18 = _12;
+ _18 = const S(1_i32);
StorageLive(_19);
- _19 = _13;
+ _19 = const 5_u8;
StorageLive(_20);
- _20 = _14;
+ _20 = const 7f32;
StorageLive(_21);
- _21 = _15;
- _17 = BigStruct(move _18, move _19, move _20, move _21);
+ _21 = const S(13_i32);
+ _17 = const BigStruct(S(1), 5, 7f32, S(13));
StorageDead(_21);
StorageDead(_20);
StorageDead(_19);
StorageDead(_18);
_0 = const ();
StorageDead(_17);
StorageDead(_15);
StorageDead(_14);
StorageDead(_13);

View File

@ -9,11 +9,15 @@
let mut _6: i32;
let mut _11: BigStruct;
let mut _16: &&BigStruct;
let mut _17: &BigStruct;
let mut _18: &BigStruct;
let mut _19: &BigStruct;
let mut _20: &BigStruct;
let mut _21: &BigStruct;
let mut _18: S;
let mut _19: u8;
let mut _20: f32;
let mut _21: S;
let mut _22: &BigStruct;
let mut _23: &BigStruct;
let mut _24: &BigStruct;
let mut _25: &BigStruct;
let mut _26: &BigStruct;
scope 1 {
debug s => _1;
let _2: i32;
@ -40,6 +44,10 @@
debug b => _13;
debug c => _14;
debug d => _15;
let _17: BigStruct;
scope 6 {
debug bs => _17;
}
}
}
}
@ -48,7 +56,8 @@
bb0: {
StorageLive(_1);
_1 = S(const 1_i32);
- _1 = S(const 1_i32);
+ _1 = const S(1);
StorageLive(_2);
StorageLive(_3);
- _3 = (_1.0: i32);
@ -85,25 +94,45 @@
StorageDead(_11);
StorageLive(_16);
_16 = const {ALLOC1: &&BigStruct};
_17 = deref_copy (*_16);
_22 = deref_copy (*_16);
StorageLive(_12);
_18 = deref_copy (*_16);
- _12 = ((*_18).0: S);
_23 = deref_copy (*_16);
- _12 = ((*_23).0: S);
+ _12 = const S(1_i32);
StorageLive(_13);
_19 = deref_copy (*_16);
- _13 = ((*_19).1: u8);
_24 = deref_copy (*_16);
- _13 = ((*_24).1: u8);
+ _13 = const 5_u8;
StorageLive(_14);
_20 = deref_copy (*_16);
- _14 = ((*_20).2: f32);
_25 = deref_copy (*_16);
- _14 = ((*_25).2: f32);
+ _14 = const 7f32;
StorageLive(_15);
_21 = deref_copy (*_16);
- _15 = ((*_21).3: S);
_26 = deref_copy (*_16);
- _15 = ((*_26).3: S);
+ _15 = const S(13_i32);
StorageDead(_16);
StorageLive(_17);
StorageLive(_18);
- _18 = _12;
+ _18 = const S(1_i32);
StorageLive(_19);
- _19 = _13;
+ _19 = const 5_u8;
StorageLive(_20);
- _20 = _14;
+ _20 = const 7f32;
StorageLive(_21);
- _21 = _15;
- _17 = BigStruct(move _18, move _19, move _20, move _21);
+ _21 = const S(13_i32);
+ _17 = const BigStruct(S(1), 5, 7f32, S(13));
StorageDead(_21);
StorageDead(_20);
StorageDead(_19);
StorageDead(_18);
_0 = const ();
StorageDead(_17);
StorageDead(_15);
StorageDead(_14);
StorageDead(_13);

View File

@ -20,4 +20,6 @@ fn main() {
static STAT: &BigStruct = &BigStruct(S(1), 5, 7., S(13));
let BigStruct(a, b, c, d) = *STAT;
let bs = BigStruct(a, b, c, d);
}

View File

@ -11,6 +11,9 @@
let mut _8: i32;
let mut _9: i32;
let mut _10: i32;
let mut _12: i32;
let mut _13: (i32, i32);
let mut _14: i32;
scope 1 {
debug a => _1;
let _2: i32;
@ -19,13 +22,18 @@
let _6: i32;
scope 3 {
debug c => _6;
let _11: (i32, (i32, i32), i32);
scope 4 {
debug d => _11;
}
}
}
}
bb0: {
StorageLive(_1);
_1 = (const 1_i32, const 2_i32);
- _1 = (const 1_i32, const 2_i32);
+ _1 = const (1, 2);
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
@ -41,7 +49,8 @@
- _2 = Add(move _3, const 3_i32);
+ _2 = const 6_i32;
StorageDead(_3);
_1 = (const 2_i32, const 3_i32);
- _1 = (const 2_i32, const 3_i32);
+ _1 = const (2, 3);
StorageLive(_6);
StorageLive(_7);
StorageLive(_8);
@ -61,7 +70,23 @@
+ _6 = const 11_i32;
StorageDead(_10);
StorageDead(_7);
StorageLive(_11);
StorageLive(_12);
- _12 = _2;
+ _12 = const 6_i32;
StorageLive(_13);
- _13 = _1;
+ _13 = const (2, 3);
StorageLive(_14);
- _14 = _6;
- _11 = (move _12, move _13, move _14);
+ _14 = const 11_i32;
+ _11 = const (6, (2, 3), 11);
StorageDead(_14);
StorageDead(_13);
StorageDead(_12);
_0 = const ();
StorageDead(_11);
StorageDead(_6);
StorageDead(_2);
StorageDead(_1);

View File

@ -7,4 +7,6 @@ fn main() {
let b = a.0 + a.1 + 3;
a = (2, 3);
let c = a.0 + a.1 + b;
let d = (b, a, c);
}