diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-07-21 16:50:06 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-07-21 22:51:17 -0700 |
| commit | f550c29c4e76ccfbdbc8ec2159cf90474530a24c (patch) | |
| tree | 66b810de7b303ce71bb7eb538cc8a2b497c11d72 /test/behavior/struct.zig | |
| parent | 460211431f407c9f707e3ac3bbff61610a487926 (diff) | |
| download | zig-f550c29c4e76ccfbdbc8ec2159cf90474530a24c.tar.gz zig-f550c29c4e76ccfbdbc8ec2159cf90474530a24c.zip | |
LLVM: fix lowering of structs with underaligned fields
When lowering a struct type to an LLVM struct type, keep track of
whether there are any underaligned fields. If so, then make it a packed
llvm struct. This works because we already insert manual padding bytes
regardless.
We could unconditionally use an LLVM packed struct; the reason we bother
checking for underaligned fields is that it is a conservative choice, in
case LLVM handles packed structs less optimally. A future improvement
could simplify this code by unconditionally using packed LLVM structs
and then make sure measure perf is unaffected.
closes #12190
Diffstat (limited to 'test/behavior/struct.zig')
| -rw-r--r-- | test/behavior/struct.zig | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 709c73807b..22d09a066d 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1372,3 +1372,26 @@ test "struct field init value is size of the struct" { var s: namespace.S = .{ .blah = 1234 }; try expect(s.size == 4); } + +test "under-aligned struct field" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + const U = extern union { + fd: i32, + u32: u32, + u64: u64, + }; + const S = extern struct { + events: u32, + data: U align(4), + }; + var runtime: usize = 1234; + const ptr = &S{ .events = 0, .data = .{ .u64 = runtime } }; + const array = @ptrCast(*const [12]u8, ptr); + const result = std.mem.readIntNative(u64, array[4..12]); + try expect(result == 1234); +} |
