Skip to content

Commit

Permalink
llvm: fix lowering of non-byte-aligned field pointers
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
jacobly0 authored and andrewrk committed Oct 15, 2022
1 parent c7f9833 commit f9192ad
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 6 deletions.
5 changes: 0 additions & 5 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -3341,7 +3341,6 @@ fn doTheTest() !void {
Zig allows the address to be taken of a non-byte-aligned field:
</p>
{#code_begin|test|pointer_to_non-byte_aligned_field#}
{#backend_stage1#}
const std = @import("std");
const expect = std.testing.expect;

Expand Down Expand Up @@ -3398,7 +3397,6 @@ fn bar(x: *const u3) u3 {
Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
</p>
{#code_begin|test|packed_struct_field_addrs#}
{#backend_stage1#}
const std = @import("std");
const expect = std.testing.expect;

Expand Down Expand Up @@ -3463,9 +3461,6 @@ test "overaligned pointer to packed struct" {
try expect(ptr_to_b.* == 2);
}
{#code_end#}
<p>When this bug is fixed, the above test in the documentation will unexpectedly pass, which will
cause the test suite to fail, notifying the bug fixer to update these docs.
</p>
<p>
It's also possible to set alignment of struct fields:
</p>
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3943,7 +3943,7 @@ pub const DeclGen = struct {
}
break :b b;
};
const byte_offset = llvm_usize.constInt((prev_bits + 7) / 8, .False);
const byte_offset = llvm_usize.constInt(prev_bits / 8, .False);
const field_addr = base_addr.constAdd(byte_offset);
bitcast_needed = false;
const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);
Expand Down

0 comments on commit f9192ad

Please sign in to comment.