Skip to content

Commit ccfa9af

Browse files
committed
Propagate ScalarPair for any type.
1 parent 895e215 commit ccfa9af

5 files changed

+30
-42
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+16-34
Original file line numberDiff line numberDiff line change
@@ -581,40 +581,22 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
581581
Some(self.operand_from_scalar(scalar, value.layout.ty))
582582
}
583583
Immediate::ScalarPair(..) => {
584-
// Found a value represented as a pair. For now only do const-prop if the type
585-
// of `rvalue` is also a tuple with two scalars.
586-
// FIXME: enable the general case stated above ^.
587-
let ty = value.layout.ty;
588-
// Only do it for tuples
589-
let ty::Tuple(types) = ty.kind() else { return None };
590-
// Only do it if tuple is also a pair with two scalars
591-
if let [ty1, ty2] = types[..] {
592-
let ty_is_scalar = |ty| {
593-
self.ecx.layout_of(ty).ok().map(|layout| layout.abi.is_scalar())
594-
== Some(true)
595-
};
596-
let alloc = if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
597-
self.ecx
598-
.intern_with_temp_alloc(value.layout, |ecx, dest| {
599-
ecx.write_immediate(*imm, dest)
600-
})
601-
.unwrap()
602-
} else {
603-
return None;
604-
};
605-
606-
// Assign entire constant in a single statement.
607-
// We can't use aggregates, as we run after the aggregate-lowering `MirPhase`.
608-
let const_val = ConstValue::ByRef { alloc, offset: Size::ZERO };
609-
let literal = ConstantKind::Val(const_val, ty);
610-
Some(Operand::Constant(Box::new(Constant {
611-
span: DUMMY_SP,
612-
user_ty: None,
613-
literal,
614-
})))
615-
} else {
616-
None
617-
}
584+
let alloc = self
585+
.ecx
586+
.intern_with_temp_alloc(value.layout, |ecx, dest| {
587+
ecx.write_immediate(*imm, dest)
588+
})
589+
.ok()?;
590+
591+
let literal = ConstantKind::Val(
592+
ConstValue::ByRef { alloc, offset: Size::ZERO },
593+
value.layout.ty,
594+
);
595+
Some(Operand::Constant(Box::new(Constant {
596+
span: DUMMY_SP,
597+
user_ty: None,
598+
literal,
599+
})))
618600
}
619601
// Scalars or scalar pairs that contain undef values are assumed to not have
620602
// successfully evaluated and are thus not propagated.

tests/mir-opt/const_prop/issue_66971.main.ConstProp.panic-abort.diff

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = (const (), const 0_u8, const 0_u8);
12-
_1 = encode(move _2) -> [return: bb1, unwind unreachable];
11+
- _2 = (const (), const 0_u8, const 0_u8);
12+
- _1 = encode(move _2) -> [return: bb1, unwind unreachable];
13+
+ _2 = const ((), 0_u8, 0_u8);
14+
+ _1 = encode(const ((), 0_u8, 0_u8)) -> [return: bb1, unwind unreachable];
1315
}
1416

1517
bb1: {

tests/mir-opt/const_prop/issue_66971.main.ConstProp.panic-unwind.diff

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = (const (), const 0_u8, const 0_u8);
12-
_1 = encode(move _2) -> [return: bb1, unwind continue];
11+
- _2 = (const (), const 0_u8, const 0_u8);
12+
- _1 = encode(move _2) -> [return: bb1, unwind continue];
13+
+ _2 = const ((), 0_u8, 0_u8);
14+
+ _1 = encode(const ((), 0_u8, 0_u8)) -> [return: bb1, unwind continue];
1315
}
1416

1517
bb1: {

tests/mir-opt/const_prop/issue_67019.main.ConstProp.panic-abort.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
- _3 = (const 1_u8, const 2_u8);
1414
- _2 = (move _3,);
1515
+ _3 = const (1_u8, 2_u8);
16-
+ _2 = (const (1_u8, 2_u8),);
16+
+ _2 = const ((1_u8, 2_u8),);
1717
StorageDead(_3);
18-
_1 = test(move _2) -> [return: bb1, unwind unreachable];
18+
- _1 = test(move _2) -> [return: bb1, unwind unreachable];
19+
+ _1 = test(const ((1_u8, 2_u8),)) -> [return: bb1, unwind unreachable];
1920
}
2021

2122
bb1: {

tests/mir-opt/const_prop/issue_67019.main.ConstProp.panic-unwind.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
- _3 = (const 1_u8, const 2_u8);
1414
- _2 = (move _3,);
1515
+ _3 = const (1_u8, 2_u8);
16-
+ _2 = (const (1_u8, 2_u8),);
16+
+ _2 = const ((1_u8, 2_u8),);
1717
StorageDead(_3);
18-
_1 = test(move _2) -> [return: bb1, unwind continue];
18+
- _1 = test(move _2) -> [return: bb1, unwind continue];
19+
+ _1 = test(const ((1_u8, 2_u8),)) -> [return: bb1, unwind continue];
1920
}
2021

2122
bb1: {

0 commit comments

Comments
 (0)