authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-15 03:31:17-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-15 14:19:40-04:00
logf9192adaba0eb344ed12aad9c675cd73b740d2a2
treef06afc534fa97ca50c1e8d56189d5697d160b095
parentc7f98332383d71a57699426027e82c435e91addc

llvm: fix lowering of non-byte-aligned field pointers

* When a field starts at some bit offset within a byte you need to load starting from that byte and shift, not starting from the next byte, so a rounded-down divide is required here, not a rounded-up one. * Remove paragraph from doc that no longer relates to anything. Closes #12363

2 files changed, 1 insertions(+), 6 deletions(-)

doc/langref.html.in-5
...@@ -3341,7 +3341,6 @@ fn doTheTest() !void {...@@ -3341,7 +3341,6 @@ fn doTheTest() !void {
3341 Zig allows the address to be taken of a non-byte-aligned field:3341 Zig allows the address to be taken of a non-byte-aligned field:
3342 </p>3342 </p>
3343 {#code_begin|test|pointer_to_non-byte_aligned_field#}3343 {#code_begin|test|pointer_to_non-byte_aligned_field#}
3344 {#backend_stage1#}
3345const std = @import("std");3344const std = @import("std");
3346const expect = std.testing.expect;3345const expect = std.testing.expect;
33473346
...@@ -3398,7 +3397,6 @@ fn bar(x: *const u3) u3 {...@@ -3398,7 +3397,6 @@ fn bar(x: *const u3) u3 {
3398 Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:3397 Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
3399 </p>3398 </p>
3400 {#code_begin|test|packed_struct_field_addrs#}3399 {#code_begin|test|packed_struct_field_addrs#}
3401 {#backend_stage1#}
3402const std = @import("std");3400const std = @import("std");
3403const expect = std.testing.expect;3401const expect = std.testing.expect;
34043402
...@@ -3463,9 +3461,6 @@ test "overaligned pointer to packed struct" {...@@ -3463,9 +3461,6 @@ test "overaligned pointer to packed struct" {
3463 try expect(ptr_to_b.* == 2);3461 try expect(ptr_to_b.* == 2);
3464}3462}
3465 {#code_end#}3463 {#code_end#}
3466 <p>When this bug is fixed, the above test in the documentation will unexpectedly pass, which will
3467 cause the test suite to fail, notifying the bug fixer to update these docs.
3468 </p>
3469 <p>3464 <p>
3470 It's also possible to set alignment of struct fields:3465 It's also possible to set alignment of struct fields:
3471 </p>3466 </p>
src/codegen/llvm.zig+1-1
...@@ -3943,7 +3943,7 @@ pub const DeclGen = struct {...@@ -3943,7 +3943,7 @@ pub const DeclGen = struct {
3943 }3943 }
3944 break :b b;3944 break :b b;
3945 };3945 };
3946 const byte_offset = llvm_usize.constInt((prev_bits + 7) / 8, .False);3946 const byte_offset = llvm_usize.constInt(prev_bits / 8, .False);
3947 const field_addr = base_addr.constAdd(byte_offset);3947 const field_addr = base_addr.constAdd(byte_offset);
3948 bitcast_needed = false;3948 bitcast_needed = false;
3949 const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);3949 const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);