Skip to content

Commit

Permalink
Propagate ScalarPair for any type.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jul 20, 2023
1 parent 895e215 commit ccfa9af
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
50 changes: 16 additions & 34 deletions compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,40 +581,22 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
Some(self.operand_from_scalar(scalar, value.layout.ty))
}
Immediate::ScalarPair(..) => {
// Found a value represented as a pair. For now only do const-prop if the type
// of `rvalue` is also a tuple with two scalars.
// FIXME: enable the general case stated above ^.
let ty = value.layout.ty;
// Only do it for tuples
let ty::Tuple(types) = ty.kind() else { return None };
// Only do it if tuple is also a pair with two scalars
if let [ty1, ty2] = types[..] {
let ty_is_scalar = |ty| {
self.ecx.layout_of(ty).ok().map(|layout| layout.abi.is_scalar())
== Some(true)
};
let alloc = if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
self.ecx
.intern_with_temp_alloc(value.layout, |ecx, dest| {
ecx.write_immediate(*imm, dest)
})
.unwrap()
} else {
return None;
};

// Assign entire constant in a single statement.
// We can't use aggregates, as we run after the aggregate-lowering `MirPhase`.
let const_val = ConstValue::ByRef { alloc, offset: Size::ZERO };
let literal = ConstantKind::Val(const_val, ty);
Some(Operand::Constant(Box::new(Constant {
span: DUMMY_SP,
user_ty: None,
literal,
})))
} else {
None
}
let alloc = self
.ecx
.intern_with_temp_alloc(value.layout, |ecx, dest| {
ecx.write_immediate(*imm, dest)
})
.ok()?;

let literal = ConstantKind::Val(
ConstValue::ByRef { alloc, offset: Size::ZERO },
value.layout.ty,
);
Some(Operand::Constant(Box::new(Constant {
span: DUMMY_SP,
user_ty: None,
literal,
})))
}
// Scalars or scalar pairs that contain undef values are assumed to not have
// successfully evaluated and are thus not propagated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

bb0: {
StorageLive(_2);
_2 = (const (), const 0_u8, const 0_u8);
_1 = encode(move _2) -> [return: bb1, unwind unreachable];
- _2 = (const (), const 0_u8, const 0_u8);
- _1 = encode(move _2) -> [return: bb1, unwind unreachable];
+ _2 = const ((), 0_u8, 0_u8);
+ _1 = encode(const ((), 0_u8, 0_u8)) -> [return: bb1, unwind unreachable];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

bb0: {
StorageLive(_2);
_2 = (const (), const 0_u8, const 0_u8);
_1 = encode(move _2) -> [return: bb1, unwind continue];
- _2 = (const (), const 0_u8, const 0_u8);
- _1 = encode(move _2) -> [return: bb1, unwind continue];
+ _2 = const ((), 0_u8, 0_u8);
+ _1 = encode(const ((), 0_u8, 0_u8)) -> [return: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
- _3 = (const 1_u8, const 2_u8);
- _2 = (move _3,);
+ _3 = const (1_u8, 2_u8);
+ _2 = (const (1_u8, 2_u8),);
+ _2 = const ((1_u8, 2_u8),);
StorageDead(_3);
_1 = test(move _2) -> [return: bb1, unwind unreachable];
- _1 = test(move _2) -> [return: bb1, unwind unreachable];
+ _1 = test(const ((1_u8, 2_u8),)) -> [return: bb1, unwind unreachable];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
- _3 = (const 1_u8, const 2_u8);
- _2 = (move _3,);
+ _3 = const (1_u8, 2_u8);
+ _2 = (const (1_u8, 2_u8),);
+ _2 = const ((1_u8, 2_u8),);
StorageDead(_3);
_1 = test(move _2) -> [return: bb1, unwind continue];
- _1 = test(move _2) -> [return: bb1, unwind continue];
+ _1 = test(const ((1_u8, 2_u8),)) -> [return: bb1, unwind continue];
}

bb1: {
Expand Down

0 comments on commit ccfa9af

Please sign in to comment.