authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-07 22:47:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-07 22:47:08-07:00
log53c86febcbeee858e9c6536c54adf99ee644147e
tree4d2ab07845f46e98dce59c6fea920f02444d339b
parent3e30ba3f20dce2d406253de3fc0eb86934a3eaa7

stage2: packed struct fixes for big-endian targets


3 files changed, 58 insertions(+), 17 deletions(-)

src/Sema.zig+9-2
......@@ -18708,11 +18708,18 @@ fn structFieldPtrByIndex(
1870818708 // If the field happens to be byte-aligned, simplify the pointer type.
1870918709 // The pointee type bit size must match its ABI byte size so that loads and stores
1871018710 // do not interfere with the surrounding packed bits.
18711 if (parent_align != 0 and ptr_ty_data.bit_offset % 8 == 0) {
18712 const byte_offset = ptr_ty_data.bit_offset / 8;
18711 // We do not attempt this with big-endian targets yet because of nested
18712 // structs and floats. I need to double-check the desired behavior for big endian
18713 // targets before adding the necessary complications to this code. This will not
18714 // cause miscompilations; it only means the field pointer uses bit masking when it
18715 // might not be strictly necessary.
18716 if (parent_align != 0 and ptr_ty_data.bit_offset % 8 == 0 and
18717 target.cpu.arch.endian() == .Little)
18718 {
1871318719 const elem_size_bytes = ptr_ty_data.pointee_type.abiSize(target);
1871418720 const elem_size_bits = ptr_ty_data.pointee_type.bitSize(target);
1871518721 if (elem_size_bytes * 8 == elem_size_bits) {
18722 const byte_offset = ptr_ty_data.bit_offset / 8;
1871618723 const new_align = @as(u32, 1) << @intCast(u5, @ctz(u64, byte_offset | parent_align));
1871718724 ptr_ty_data.bit_offset = 0;
1871818725 ptr_ty_data.host_size = 0;
src/type.zig+4-3
......@@ -5597,17 +5597,18 @@ pub const Type = extern union {
55975597 comptime assert(Type.packed_struct_layout_version == 2);
55985598
55995599 var bit_offset: u16 = undefined;
5600 var elem_size_bits: u16 = undefined;
56005601 var running_bits: u16 = 0;
56015602 for (struct_obj.fields.values()) |f, i| {
56025603 if (!f.ty.hasRuntimeBits()) continue;
56035604
5605 const field_bits = @intCast(u16, f.ty.bitSize(target));
56045606 if (i == field_index) {
56055607 bit_offset = running_bits;
5608 elem_size_bits = field_bits;
56065609 }
5607 running_bits += @intCast(u16, f.ty.bitSize(target));
5610 running_bits += field_bits;
56085611 }
5609 const host_size = (running_bits + 7) / 8;
5610 _ = host_size; // TODO big-endian
56115612 const byte_offset = bit_offset / 8;
56125613 return byte_offset;
56135614 }
test/behavior/packed-struct.zig+45-12
......@@ -289,13 +289,7 @@ test "regular in irregular packed struct" {
289289
290290 const Irregular = packed struct {
291291 bar: Regular = Regular{},
292
293 // This field forces the regular packed struct to be a part of single u48
294 // and thus it all gets represented as an array of 6 bytes in LLVM
295292 _: u24 = 0,
296
297 // This struct on its own can represent its fields directly in LLVM
298 // with no need to use array of bytes as underlaying representation.
299293 pub const Regular = packed struct { a: u16 = 0, b: u8 = 0 };
300294 };
301295
......@@ -335,17 +329,44 @@ test "byte-aligned field pointer offsets" {
335329 .c = 3,
336330 .d = 4,
337331 };
338 comptime assert(@TypeOf(&a.a) == *align(4) u8);
339 comptime assert(@TypeOf(&a.b) == *u8);
340 comptime assert(@TypeOf(&a.c) == *align(2) u8);
341 comptime assert(@TypeOf(&a.d) == *u8);
332 switch (comptime builtin.cpu.arch.endian()) {
333 .Little => {
334 comptime assert(@TypeOf(&a.a) == *align(4) u8);
335 comptime assert(@TypeOf(&a.b) == *u8);
336 comptime assert(@TypeOf(&a.c) == *align(2) u8);
337 comptime assert(@TypeOf(&a.d) == *u8);
338 },
339 .Big => {
340 // TODO re-evaluate packed struct endianness
341 comptime assert(@TypeOf(&a.a) == *align(4:0:4) u8);
342 comptime assert(@TypeOf(&a.b) == *align(4:8:4) u8);
343 comptime assert(@TypeOf(&a.c) == *align(4:16:4) u8);
344 comptime assert(@TypeOf(&a.d) == *align(4:24:4) u8);
345 },
346 }
342347 try expect(a.a == 1);
343348 try expect(a.b == 2);
344349 try expect(a.c == 3);
345350 try expect(a.d == 4);
351
346352 a.a += 1;
353 try expect(a.a == 2);
354 try expect(a.b == 2);
355 try expect(a.c == 3);
356 try expect(a.d == 4);
357
347358 a.b += 1;
359 try expect(a.a == 2);
360 try expect(a.b == 3);
361 try expect(a.c == 3);
362 try expect(a.d == 4);
363
348364 a.c += 1;
365 try expect(a.a == 2);
366 try expect(a.b == 3);
367 try expect(a.c == 4);
368 try expect(a.d == 4);
369
349370 a.d += 1;
350371 try expect(a.a == 2);
351372 try expect(a.b == 3);
......@@ -356,11 +377,23 @@ test "byte-aligned field pointer offsets" {
356377 .a = 1,
357378 .b = 2,
358379 };
359 comptime assert(@TypeOf(&b.a) == *align(4) u16);
360 comptime assert(@TypeOf(&b.b) == *u16);
380 switch (comptime builtin.cpu.arch.endian()) {
381 .Little => {
382 comptime assert(@TypeOf(&b.a) == *align(4) u16);
383 comptime assert(@TypeOf(&b.b) == *u16);
384 },
385 .Big => {
386 comptime assert(@TypeOf(&b.a) == *align(4:0:4) u16);
387 comptime assert(@TypeOf(&b.b) == *align(4:16:4) u16);
388 },
389 }
361390 try expect(b.a == 1);
362391 try expect(b.b == 2);
392
363393 b.a += 1;
394 try expect(b.a == 2);
395 try expect(b.b == 2);
396
364397 b.b += 1;
365398 try expect(b.a == 2);
366399 try expect(b.b == 3);