diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 5c683a965a..1b12add122 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -9761,6 +9761,7 @@ pub const FuncGen = struct { const ptr = try fg.resolveInst(ty_op.operand); elide: { + if (ptr_info.flags.alignment != .none) break :elide; if (!isByRef(Type.fromInterned(ptr_info.child), zcu)) break :elide; if (!canElideLoad(fg, body_tail)) break :elide; return ptr; diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 29e9986e5e..f1c10a1461 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -2136,3 +2136,21 @@ test "field access through mem ptr arg" { &.{ .field = 0x0ced271f }, ) == 0x0ced271f); } + +test "align 1 struct parameter dereferenced and returned" { + if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; + + const S = extern struct { + a: u32, + + fn gimme(p: *align(1) @This()) @This() { + return p.*; + } + }; + var buffer: [5]u8 align(4) = .{ 1, 2, 3, 4, 5 }; + const s = S.gimme(@ptrCast(buffer[1..])); + switch (native_endian) { + .big => try expect(s.a == 0x02030405), + .little => try expect(s.a == 0x05040302), + } +}