2024-04-20 06:19:34 -05:00
|
|
|
//@ test-mir-pass: ScalarReplacementOfAggregates
|
2022-11-15 12:06:45 -06:00
|
|
|
//@ compile-flags: -Cpanic=abort
|
|
|
|
//@ no-prefer-dynamic
|
2022-05-23 02:27:44 -05:00
|
|
|
|
|
|
|
struct Tag(usize);
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct S(Tag, Tag, Tag);
|
|
|
|
|
|
|
|
impl Drop for Tag {
|
|
|
|
#[inline(never)]
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that SROA excludes structs with a `Drop` implementation.
|
2022-05-23 02:27:44 -05:00
|
|
|
pub fn dropping() {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn dropping(
|
|
|
|
|
|
|
|
// CHECK: [[aggregate:_[0-9]+]]: S;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[aggregate]] = S
|
2022-05-23 02:27:44 -05:00
|
|
|
S(Tag(0), Tag(1), Tag(2)).1;
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that SROA excludes enums.
|
2022-05-23 02:27:44 -05:00
|
|
|
pub fn enums(a: usize) -> usize {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn enums(
|
|
|
|
|
|
|
|
// CHECK: [[enum:_[0-9]+]]: std::option::Option<usize>;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[enum]] = Option::<usize>::Some
|
2024-08-18 17:51:53 -05:00
|
|
|
// CHECK: _5 = copy (([[enum]] as Some).0: usize)
|
|
|
|
// CHECK: _0 = copy _5
|
2022-05-23 02:27:44 -05:00
|
|
|
if let Some(a) = Some(a) { a } else { 0 }
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that SROA destructures `U`.
|
2022-05-23 02:27:44 -05:00
|
|
|
pub fn structs(a: f32) -> f32 {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn structs(
|
2022-05-23 02:27:44 -05:00
|
|
|
struct U {
|
|
|
|
_foo: usize,
|
|
|
|
a: f32,
|
|
|
|
}
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK: [[ret:_0]]: f32;
|
|
|
|
// CHECK: [[struct:_[0-9]+]]: structs::U;
|
|
|
|
// CHECK: [[a_tmp:_[0-9]+]]: f32;
|
|
|
|
// CHECK: [[foo:_[0-9]+]]: usize;
|
|
|
|
// CHECK: [[a_ret:_[0-9]+]]: f32;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK-NOT: [[struct]]
|
2024-08-18 17:51:53 -05:00
|
|
|
// CHECK: [[a_tmp]] = copy _1;
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-NOT: [[struct]]
|
|
|
|
// CHECK: [[foo]] = const 0_usize;
|
|
|
|
// CHECK-NOT: [[struct]]
|
|
|
|
// CHECK: [[a_ret]] = move [[a_tmp]];
|
|
|
|
// CHECK-NOT: [[struct]]
|
2024-08-18 17:51:53 -05:00
|
|
|
// CHECK: _0 = copy [[a_ret]];
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-NOT: [[struct]]
|
2022-05-23 02:27:44 -05:00
|
|
|
U { _foo: 0, a }.a
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that SROA excludes unions.
|
2022-05-23 02:27:44 -05:00
|
|
|
pub fn unions(a: f32) -> u32 {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn unions(
|
2022-05-23 02:27:44 -05:00
|
|
|
union Repr {
|
|
|
|
f: f32,
|
|
|
|
u: u32,
|
|
|
|
}
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK: [[union:_[0-9]+]]: unions::Repr;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[union]] = Repr {
|
2024-08-18 17:51:53 -05:00
|
|
|
// CHECK: _0 = copy ([[union]].1: u32)
|
2022-05-23 02:27:44 -05:00
|
|
|
unsafe { Repr { f: a }.u }
|
|
|
|
}
|
|
|
|
|
2023-02-05 03:31:27 -06:00
|
|
|
#[derive(Copy, Clone)]
|
2022-05-23 02:27:44 -05:00
|
|
|
struct Foo {
|
|
|
|
a: u8,
|
|
|
|
b: (),
|
|
|
|
c: &'static str,
|
|
|
|
d: Option<isize>,
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that non-escaping uses of a struct are destructured.
|
2022-05-23 02:27:44 -05:00
|
|
|
pub fn flat() {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn flat(
|
|
|
|
|
|
|
|
// CHECK: [[struct:_[0-9]+]]: Foo;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[init_unit:_[0-9]+]] = ();
|
|
|
|
// CHECK: [[init_opt_isize:_[0-9]+]] = Option::<isize>::Some
|
|
|
|
|
|
|
|
// CHECK: [[destr_five:_[0-9]+]] = const 5_u8;
|
|
|
|
// CHECK: [[destr_unit:_[0-9]+]] = move [[init_unit]];
|
|
|
|
// CHECK: [[destr_a:_[0-9]+]] = const "a";
|
|
|
|
// CHECK: [[destr_opt_isize:_[0-9]+]] = move [[init_opt_isize]];
|
|
|
|
|
2022-05-23 02:27:44 -05:00
|
|
|
let Foo { a, b, c, d } = Foo { a: 5, b: (), c: "a", d: Some(-4) };
|
|
|
|
let _ = a;
|
|
|
|
let _ = b;
|
|
|
|
let _ = c;
|
|
|
|
let _ = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct Escaping {
|
|
|
|
a: u32,
|
|
|
|
b: u32,
|
|
|
|
c: u32,
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
fn g() -> u32 {
|
|
|
|
3
|
|
|
|
}
|
|
|
|
|
2022-05-23 02:27:44 -05:00
|
|
|
fn f(a: *const u32) {
|
|
|
|
println!("{}", unsafe { *a.add(2) });
|
|
|
|
}
|
|
|
|
|
2023-02-12 13:37:09 -06:00
|
|
|
// `f` uses the `&e.a` to access `e.c`. This is UB according to Miri today; however,
|
|
|
|
// T-opsem has not finalized that decision and as such rustc should not rely on
|
|
|
|
// it. If SROA were to rely on it, it would be (almost) correct to turn `e` into
|
|
|
|
// three distinct locals - one for each field - and pass a reference to only one
|
|
|
|
// of them to `f`. However, this would lead to a miscompilation because `b` and `c`
|
|
|
|
// might no longer appear right after `a` in memory.
|
2022-05-23 02:27:44 -05:00
|
|
|
pub fn escaping() {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn escaping(
|
|
|
|
|
|
|
|
// CHECK: [[ptr:_[0-9]+]]: *const u32;
|
|
|
|
// CHECK: [[ref:_[0-9]+]]: &u32;
|
|
|
|
// CHECK: [[struct:_[0-9]+]]: Escaping;
|
|
|
|
// CHECK: [[a:_[0-9]+]]: u32;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[struct]] = Escaping {
|
|
|
|
// CHECK: [[ref]] = &([[struct]].0
|
|
|
|
// CHECK: [[ptr]] = &raw const (*[[ref]]);
|
2022-05-23 02:27:44 -05:00
|
|
|
f(&Escaping { a: 1, b: 2, c: g() }.a);
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that copies from an internal struct are destructured and reassigned to
|
|
|
|
/// the original struct.
|
2023-02-05 03:31:27 -06:00
|
|
|
fn copies(x: Foo) {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn copies(
|
|
|
|
|
|
|
|
// CHECK: [[external:_[0-9]+]]: Foo) ->
|
|
|
|
// CHECK: [[internal:_[0-9]+]]: Foo;
|
|
|
|
// CHECK: [[byte:_[0-9]+]]: u8;
|
|
|
|
// CHECK: [[unit:_[0-9]+]]: ();
|
|
|
|
// CHECK: [[str:_[0-9]+]]: &str;
|
|
|
|
// CHECK: [[opt_isize:_[0-9]+]]: std::option::Option<isize>;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
2024-08-18 17:51:53 -05:00
|
|
|
// CHECK: [[byte]] = copy ([[external]].0
|
|
|
|
// CHECK: [[unit]] = copy ([[external]].1
|
|
|
|
// CHECK: [[str]] = copy ([[external]].2
|
|
|
|
// CHECK: [[opt_isize]] = copy ([[external]].3
|
2024-02-04 12:04:39 -06:00
|
|
|
|
2023-02-05 03:31:27 -06:00
|
|
|
let y = x;
|
|
|
|
let t = y.a;
|
|
|
|
let u = y.c;
|
2023-02-05 05:37:44 -06:00
|
|
|
let z = y;
|
|
|
|
let a = z.b;
|
2023-02-05 03:31:27 -06:00
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that copies from an internal struct are destructured and reassigned to
|
|
|
|
/// the original struct.
|
2023-02-05 03:31:27 -06:00
|
|
|
fn ref_copies(x: &Foo) {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn ref_copies(
|
|
|
|
|
|
|
|
// CHECK: [[external:_[0-9]+]]: &Foo) ->
|
|
|
|
// CHECK: [[internal:_[0-9]+]]: Foo;
|
|
|
|
// CHECK: [[byte:_[0-9]+]]: u8;
|
|
|
|
// CHECK: [[unit:_[0-9]+]]: ();
|
|
|
|
// CHECK: [[str:_[0-9]+]]: &str;
|
|
|
|
// CHECK: [[opt_isize:_[0-9]+]]: std::option::Option<isize>;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
2024-08-18 17:51:53 -05:00
|
|
|
// CHECK: [[byte]] = copy ((*[[external]]).0
|
|
|
|
// CHECK: [[unit]] = copy ((*[[external]]).1
|
|
|
|
// CHECK: [[str]] = copy ((*[[external]]).2
|
|
|
|
// CHECK: [[opt_isize]] = copy ((*[[external]]).3
|
2024-02-04 12:04:39 -06:00
|
|
|
|
2023-02-05 03:31:27 -06:00
|
|
|
let y = *x;
|
|
|
|
let t = y.a;
|
|
|
|
let u = y.c;
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:04:39 -06:00
|
|
|
/// Check that deaggregated assignments from constants are placed after the constant's
|
|
|
|
/// assignment. Also check that copying field accesses from the copy of the constant are
|
|
|
|
/// reassigned to copy from the constant.
|
2023-02-09 11:27:31 -06:00
|
|
|
fn constant() {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: constant(
|
|
|
|
|
|
|
|
// CHECK: [[constant:_[0-9]+]]: (usize, u8);
|
|
|
|
// CHECK: [[t:_[0-9]+]]: usize;
|
|
|
|
// CHECK: [[u:_[0-9]+]]: u8;
|
|
|
|
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK-NOT: [[constant]]
|
|
|
|
// CHECK: [[constant]] = const
|
|
|
|
// CHECK: [[t]] = move ([[constant]].0: usize)
|
|
|
|
// CHECK: [[u]] = move ([[constant]].1: u8)
|
2023-02-09 11:27:31 -06:00
|
|
|
const U: (usize, u8) = (5, 9);
|
|
|
|
let y = U;
|
|
|
|
let t = y.0;
|
|
|
|
let u = y.1;
|
|
|
|
}
|
|
|
|
|
2022-05-23 02:27:44 -05:00
|
|
|
fn main() {
|
2024-02-04 12:04:39 -06:00
|
|
|
// CHECK-LABEL: fn main(
|
2022-05-23 02:27:44 -05:00
|
|
|
dropping();
|
|
|
|
enums(5);
|
|
|
|
structs(5.);
|
|
|
|
unions(5.);
|
|
|
|
flat();
|
|
|
|
escaping();
|
2023-02-05 03:31:27 -06:00
|
|
|
copies(Foo { a: 5, b: (), c: "a", d: Some(-4) });
|
|
|
|
ref_copies(&Foo { a: 5, b: (), c: "a", d: Some(-4) });
|
2023-02-09 11:27:31 -06:00
|
|
|
constant();
|
2022-05-23 02:27:44 -05:00
|
|
|
}
|
2023-02-05 03:31:27 -06:00
|
|
|
|
2023-03-05 12:06:55 -06:00
|
|
|
// EMIT_MIR structs.dropping.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.enums.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.structs.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.unions.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.flat.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.escaping.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.copies.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.ref_copies.ScalarReplacementOfAggregates.diff
|
|
|
|
// EMIT_MIR structs.constant.ScalarReplacementOfAggregates.diff
|