Skip to content

Enabling GVN produces a const allocation with the wrong alignment? #117761

Closed
@saethlin

Description

@saethlin
rustc +nightly ice.rs -Copt-level=0 -Zmir-opt-level=2 -Zinline-mir -Zmir-enable-passes=+GVN
struct Affine2 {
    matrix2: Mat2,
    translation: Vec2,
}

impl Affine2 {
    #[inline]
    fn inverse(&self) {
        mat2_ref(&self.matrix2);
        vec2_move(self.translation);
    }
}

#[derive(Clone, Copy)]
#[repr(align(8))]
struct Mat2([f32; 4]);

#[derive(Clone, Copy)]
struct Vec2 {
    x: f32, 
    y: f32, 
}

#[inline(never)]
fn mat2_ref(_: &Mat2) {
    loop {}
}

#[inline(never)]
fn vec2_move(_: Vec2) {
    loop {}
}

fn main() {
    Affine2 {
        matrix2: Mat2([0.0, 0.0, 0.0, 0.0]),
        translation: Vec2 { x: 0.0, y: 0.0 },
    }.inverse();
}

Error:

thread 'rustc' panicked at /rustc/f967532a47eb728ada44473a5c4c2eca1a45fe30/compiler/rustc_codegen_ssa/src/mir/operand.rs:135:9:
assertion `left == right` failed
  left: Align(8 bytes)
 right: Align(4 bytes)

That's this:

fn from_const_alloc<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
bx: &mut Bx,
layout: TyAndLayout<'tcx>,
alloc: rustc_middle::mir::interpret::ConstAllocation<'tcx>,
offset: Size,
) -> Self {
let alloc_align = alloc.inner().align;
assert_eq!(alloc_align, layout.align.abi);

The GVN MIR diff is this:

diff --git a/ice.main.005-017.GVN.before.mir b/ice.main.005-017.GVN.after.mir
index 512091e..af00cdf 100644
--- a/ice.main.005-017.GVN.before.mir
+++ b/ice.main.005-017.GVN.after.mir
@@ -1,4 +1,4 @@
-// MIR for `main` before GVN
+// MIR for `main` after GVN
 
 fn main() -> () {
     let mut _0: ();
@@ -22,7 +22,12 @@ fn main() -> () {
     }
 
     bb2: {
-        _5 = ((*_1).1: Vec2);
-        _4 = vec2_move(move _5) -> [return: bb1, unwind continue];
+        _5 = const Vec2 {{ x: 0f32, y: 0f32 }};
+        _4 = vec2_move(const Vec2 {{ x: 0f32, y: 0f32 }}) -> [return: bb1, unwind continue];
     }
 }
+
+alloc11 (size: 24, align: 8) {
+    0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
+    0x10 │ 00 00 00 00 00 00 00 00                         │ ........
+}

searched nightlies: from nightly-2023-06-01 to nightly-2023-11-08
regressed nightly: nightly-2023-10-30
searched commit range: e5cfc55...608e968
regressed commit: 83c9732

bisected with cargo-bisect-rustc v0.6.7

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start=2023-06-01 --end=2023-11-08 --script script 
rustc 1.75.0-nightly (fdaaaf9f9 2023-11-08)
binary: rustc
commit-hash: fdaaaf9f923281ab98b865259aa40fbf93d72c7a
commit-date: 2023-11-08
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions