From 640c11171bf8d13776629941f3305cf11c62c1f3 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 28 Aug 2025 18:32:51 -0700 Subject: [PATCH] LLVM backend:fix align 1 sret parameter load returned closes #25067 --- src/codegen/llvm.zig | 1 + test/behavior/struct.zig | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 9cbe1f8c56..e4abfcca43 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -9773,6 +9773,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), + } +}