From 0ccdc511cebfa3db1dfcbd05fe849843da117657 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sun, 18 Dec 2022 01:23:36 -0500 Subject: [PATCH 1/2] rand: add pub to next/jump I specifically needed jump for an application and it doesn't appear to be exposed in any way. --- lib/std/rand/Xoroshiro128.zig | 4 ++-- lib/std/rand/Xoshiro256.zig | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/rand/Xoroshiro128.zig b/lib/std/rand/Xoroshiro128.zig index 60907a017b..6ddd2eb89e 100644 --- a/lib/std/rand/Xoroshiro128.zig +++ b/lib/std/rand/Xoroshiro128.zig @@ -20,7 +20,7 @@ pub fn random(self: *Xoroshiro128) Random { return Random.init(self, fill); } -fn next(self: *Xoroshiro128) u64 { +pub fn next(self: *Xoroshiro128) u64 { const s0 = self.s[0]; var s1 = self.s[1]; const r = s0 +% s1; @@ -33,7 +33,7 @@ fn next(self: *Xoroshiro128) u64 { } // Skip 2^64 places ahead in the sequence -fn jump(self: *Xoroshiro128) void { +pub fn jump(self: *Xoroshiro128) void { var s0: u64 = 0; var s1: u64 = 0; diff --git a/lib/std/rand/Xoshiro256.zig b/lib/std/rand/Xoshiro256.zig index b81b5178aa..42ad43c445 100644 --- a/lib/std/rand/Xoshiro256.zig +++ b/lib/std/rand/Xoshiro256.zig @@ -22,7 +22,7 @@ pub fn random(self: *Xoshiro256) Random { return Random.init(self, fill); } -fn next(self: *Xoshiro256) u64 { +pub fn next(self: *Xoshiro256) u64 { const r = math.rotl(u64, self.s[0] +% self.s[3], 23) +% self.s[0]; const t = self.s[1] << 17; @@ -40,7 +40,7 @@ fn next(self: *Xoshiro256) u64 { } // Skip 2^128 places ahead in the sequence -fn jump(self: *Xoshiro256) void { +pub fn jump(self: *Xoshiro256) void { var s: u256 = 0; var table: u256 = 0x39abdc4529b1661ca9582618e03fc9aad5a61266f0c9392c180ec6d33cfd0aba; From e0bc5f65b98d154b4318027d56f780b55605e33c Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sun, 18 Dec 2022 01:25:19 -0500 Subject: [PATCH 2/2] llvm: fix lowering pointer to final zero-width field of a comptime value * Handle a `null` return from `llvmFieldIndex`. * Add a behavior test to test this code path. * Reword this test name, which incorrectly described how pointers to zero-bit fields behave, and instead describe the actual test. --- src/codegen/llvm.zig | 22 +++++++++++++++------- test/behavior/struct.zig | 36 +++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index cd9ef98e44..68d929c5a4 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -4033,16 +4033,24 @@ pub const DeclGen = struct { const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0); break :blk field_addr.constIntToPtr(final_llvm_ty); } - bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module); var ty_buf: Type.Payload.Pointer = undefined; - const llvm_field_index = llvmFieldIndex(parent_ty, field_index, target, &ty_buf).?; - const indices: [2]*llvm.Value = .{ - llvm_u32.constInt(0, .False), - llvm_u32.constInt(llvm_field_index, .False), - }; + const parent_llvm_ty = try dg.lowerType(parent_ty); - break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); + if (llvmFieldIndex(parent_ty, field_index, target, &ty_buf)) |llvm_field_index| { + bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module); + const indices: [2]*llvm.Value = .{ + llvm_u32.constInt(0, .False), + llvm_u32.constInt(llvm_field_index, .False), + }; + break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); + } else { + bitcast_needed = !parent_ty.eql(ptr_child_ty, dg.module); + const indices: [1]*llvm.Value = .{ + llvm_u32.constInt(1, .False), + }; + break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); + } }, .Pointer => { assert(parent_ty.isSlice()); diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index db7092ab82..0984f7d1e4 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1359,23 +1359,33 @@ test "under-aligned struct field" { try expect(result == 1234); } -test "address of zero-bit field is equal to address of only field" { +test "fieldParentPtr of a zero-bit field" { 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 A = struct { b: void = {}, u: u8 }; - var a = A{ .u = 0 }; - const a_ptr = @fieldParentPtr(A, "b", &a.b); - try std.testing.expectEqual(&a, a_ptr); - } - { - const A = struct { u: u8, b: void = {} }; - var a = A{ .u = 0 }; - const a_ptr = @fieldParentPtr(A, "b", &a.b); - try std.testing.expectEqual(&a, a_ptr); - } + const S = struct { + fn testOneType(comptime A: type) !void { + { + const a = A{ .u = 0 }; + const b_ptr = &a.b; + const a_ptr = @fieldParentPtr(A, "b", b_ptr); + try std.testing.expectEqual(&a, a_ptr); + } + { + var a = A{ .u = 0 }; + const b_ptr = &a.b; + const a_ptr = @fieldParentPtr(A, "b", b_ptr); + try std.testing.expectEqual(&a, a_ptr); + } + } + fn doTheTest() !void { + try testOneType(struct { b: void = {}, u: u8 }); + try testOneType(struct { u: u8, b: void = {} }); + } + }; + try S.doTheTest(); + comptime try S.doTheTest(); } test "struct field has a pointer to an aligned version of itself" {