From 97bfa29147fffaca13e105b52a37bcea0b0c8031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 10 Mar 2025 11:51:37 +0100 Subject: [PATCH 1/4] Revert "std.mem.indexOfSentinel: don't ask the OS the page size" This reverts commit faf256e429914a816dd5ab1555671b7ee69ecc4e. --- lib/std/mem.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 016f3ab9da7a..0fa7deadc3d0 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1098,12 +1098,12 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co // as we don't read into a new page. This should be the case for most architectures // which use paged memory, however should be confirmed before adding a new arch below. .aarch64, .x86, .x86_64 => if (std.simd.suggestVectorLength(T)) |block_len| { - const page_size = std.heap.page_size_min; + const page_size = std.heap.pageSize(); const block_size = @sizeOf(T) * block_len; const Block = @Vector(block_len, T); const mask: Block = @splat(sentinel); - comptime assert(std.heap.page_size_min % @sizeOf(Block) == 0); + comptime assert(std.heap.page_size_max % @sizeOf(Block) == 0); assert(page_size % @sizeOf(Block) == 0); // First block may be unaligned @@ -1153,7 +1153,7 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co test "indexOfSentinel vector paths" { const Types = [_]type{ u8, u16, u32, u64 }; const allocator = std.testing.allocator; - const page_size = std.heap.page_size_min; + const page_size = std.heap.pageSize(); inline for (Types) |T| { const block_len = std.simd.suggestVectorLength(T) orelse continue; From ce82f43799fb790d27402916047c4e7414a03346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 10 Mar 2025 11:52:35 +0100 Subject: [PATCH 2/4] Revert "add parentheses in std.heap.page_size_min" This reverts commit 0367d684fccf8bf011fe8ac1a984820c824871a8. --- lib/std/heap.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/heap.zig b/lib/std/heap.zig index f0ea8d0d467f..0d35c98c1c2f 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -42,8 +42,8 @@ pub var next_mmap_addr_hint: ?[*]align(page_size_min) u8 = null; /// /// On many systems, the actual page size can only be determined at runtime /// with `pageSize`. -pub const page_size_min: usize = std.options.page_size_min orelse (page_size_min_default orelse - @compileError(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " has unknown page_size_min; populate std.options.page_size_min")); +pub const page_size_min: usize = std.options.page_size_min orelse page_size_min_default orelse + @compileError(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " has unknown page_size_min; populate std.options.page_size_min"); /// comptime-known maximum page size of the target. /// From af8c0a191c5234881e47f704623e13ef1c417c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 10 Mar 2025 11:52:41 +0100 Subject: [PATCH 3/4] Revert "std.heap.page_size_min: relax freestanding restriction" This reverts commit 2447b87d98ee4b0fe13938c9b2acf2bb06cab572. --- lib/std/heap.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 0d35c98c1c2f..6fbc3d8b75ff 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -42,8 +42,10 @@ pub var next_mmap_addr_hint: ?[*]align(page_size_min) u8 = null; /// /// On many systems, the actual page size can only be determined at runtime /// with `pageSize`. -pub const page_size_min: usize = std.options.page_size_min orelse page_size_min_default orelse - @compileError(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " has unknown page_size_min; populate std.options.page_size_min"); +pub const page_size_min: usize = std.options.page_size_min orelse (page_size_min_default orelse if (builtin.os.tag == .freestanding or builtin.os.tag == .other) + @compileError("freestanding/other page_size_min must provided with std.options.page_size_min") +else + @compileError(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " has unknown page_size_min; populate std.options.page_size_min")); /// comptime-known maximum page size of the target. /// @@ -829,10 +831,8 @@ const page_size_min_default: ?usize = switch (builtin.os.tag) { .xtensa => 4 << 10, else => null, }, - .freestanding, .other => switch (builtin.cpu.arch) { + .freestanding => switch (builtin.cpu.arch) { .wasm32, .wasm64 => 64 << 10, - .x86, .x86_64 => 4 << 10, - .aarch64, .aarch64_be => 4 << 10, else => null, }, else => null, From 4f439186d0e28273e7781820b15211f6ae542d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 10 Mar 2025 12:28:20 +0100 Subject: [PATCH 4/4] std.mem: Don't use vectorized path for freestanding targets in indexOfSentinel(). --- lib/std/mem.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 0fa7deadc3d0..81f061be1701 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -6,6 +6,7 @@ const math = std.math; const mem = @This(); const testing = std.testing; const Endian = std.builtin.Endian; +const native_os = builtin.target.os.tag; const native_endian = builtin.cpu.arch.endian(); /// The standard library currently thoroughly depends on byte size @@ -1091,7 +1092,8 @@ pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]co if (backend_supports_vectors and !std.debug.inValgrind() and // https://github.com/ziglang/zig/issues/17717 !@inComptime() and - (@typeInfo(T) == .int or @typeInfo(T) == .float) and std.math.isPowerOfTwo(@bitSizeOf(T))) + (@typeInfo(T) == .int or @typeInfo(T) == .float) and std.math.isPowerOfTwo(@bitSizeOf(T)) and + (native_os != .freestanding and native_os != .other and native_os != .uefi)) { switch (@import("builtin").cpu.arch) { // The below branch assumes that reading past the end of the buffer is valid, as long