From 6808ce27bdca14d3876ac607c94f75ea054db7b8 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sat, 24 Aug 2024 16:16:53 +0100 Subject: [PATCH] compiler,lib,test,langref: migrate `@setCold` to `@branchHint` --- doc/langref.html.in | 16 +++++++--------- doc/langref/test_functions.zig | 4 ++-- lib/c.zig | 2 +- lib/compiler/aro/aro/Driver/Filesystem.zig | 8 ++++---- lib/compiler/aro/aro/Parser.zig | 10 +++++----- lib/compiler/resinator/main.zig | 8 ++++---- lib/compiler_rt/common.zig | 2 +- lib/std/Thread/Futex.zig | 8 ++++---- lib/std/Thread/Mutex.zig | 2 +- lib/std/Thread/ResetEvent.zig | 2 +- lib/std/builtin.zig | 14 +++++++------- lib/std/debug.zig | 6 +++--- lib/std/fmt/parse_float/convert_slow.zig | 2 +- lib/std/hash/xxhash.zig | 12 ++++++------ lib/std/hash_map.zig | 2 +- lib/std/heap/WasmPageAllocator.zig | 2 +- lib/std/log.zig | 2 +- lib/std/once.zig | 2 +- lib/std/posix.zig | 2 +- lib/std/sort/pdq.zig | 4 ++-- lib/std/zig/AstGen.zig | 8 ++++---- lib/std/zig/Parse.zig | 12 ++++++------ src/Compilation.zig | 8 ++++---- src/Sema.zig | 4 ++-- src/arch/aarch64/CodeGen.zig | 4 ++-- src/arch/aarch64/Emit.zig | 2 +- src/arch/arm/CodeGen.zig | 4 ++-- src/arch/arm/Emit.zig | 2 +- src/arch/riscv64/CodeGen.zig | 4 ++-- src/arch/riscv64/Lower.zig | 2 +- src/arch/sparc64/CodeGen.zig | 2 +- src/arch/sparc64/Emit.zig | 2 +- src/arch/wasm/Emit.zig | 2 +- src/arch/x86_64/CodeGen.zig | 4 ++-- src/arch/x86_64/Lower.zig | 2 +- src/codegen/c.zig | 2 +- src/codegen/llvm.zig | 4 ++-- src/codegen/spirv.zig | 2 +- src/crash_report.zig | 2 +- test/behavior/basic.zig | 2 +- ...ltin_functions_returning_void_or_noreturn.zig | 2 +- .../function-only_builtins_outside_function.zig | 4 ++-- 42 files changed, 94 insertions(+), 96 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 9702f5a3518e7d82505b8109ac62663486af9a5e..73ffbb0de81c233c5593a7ba6c2de19e7270e8ce 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -4340,6 +4340,13 @@ comptime { {#see_also|@sizeOf|@typeInfo#} {#header_close#} + {#header_open|@branchHint#} +
{#syntax#}@branchHint(hint: BranchHint) void{#endsyntax#}
+

Hints to the optimizer how likely a given branch of control flow is to be reached.

+

{#syntax#}BranchHint{#endsyntax#} can be found with {#syntax#}@import("std").builtin.BranchHint{#endsyntax#}.

+

This function is only valid as the first statement in a control flow branch, or the first statement in a function.

+ {#header_close#} + {#header_open|@breakpoint#}
{#syntax#}@breakpoint() void{#endsyntax#}

@@ -5242,15 +5249,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val

{#header_close#} - {#header_open|@setCold#} -
{#syntax#}@setCold(comptime is_cold: bool) void{#endsyntax#}
-

- Tells the optimizer that the current function is (or is not) rarely called. - - This function is only valid within function scope. -

- {#header_close#} - {#header_open|@setEvalBranchQuota#}
{#syntax#}@setEvalBranchQuota(comptime new_quota: u32) void{#endsyntax#}

diff --git a/doc/langref/test_functions.zig b/doc/langref/test_functions.zig index 0c0a136950ba0d559c131eb9d72e2c1b7eba1a96..10e334793e74ef60ecdd94caf597db889b528265 100644 --- a/doc/langref/test_functions.zig +++ b/doc/langref/test_functions.zig @@ -27,9 +27,9 @@ const WINAPI: std.builtin.CallingConvention = if (native_arch == .x86) .Stdcall extern "kernel32" fn ExitProcess(exit_code: u32) callconv(WINAPI) noreturn; extern "c" fn atan2(a: f64, b: f64) f64; -// The @setCold builtin tells the optimizer that a function is rarely called. +// The @branchHint builtin can be used to tell the optimizer that a function is rarely called ("cold"). fn abort() noreturn { - @setCold(true); + @branchHint(.cold); while (true) {} } diff --git a/lib/c.zig b/lib/c.zig index aa54def73df48c941c4530ea46e9dc1bfd30437b..d742258607419f671c950ea6fe729cf6ec67af89 100644 --- a/lib/c.zig +++ b/lib/c.zig @@ -46,7 +46,7 @@ comptime { // Avoid dragging in the runtime safety mechanisms into this .o file, // unless we're trying to test this file. pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { - @setCold(true); + @branchHint(.cold); _ = error_return_trace; if (builtin.is_test) { std.debug.panic("{s}", .{msg}); diff --git a/lib/compiler/aro/aro/Driver/Filesystem.zig b/lib/compiler/aro/aro/Driver/Filesystem.zig index 3889ec73b367563cb81c426bfd0b58d4fa8f46d0..a81f313753060c0c76f13881c257de247c4da42d 100644 --- a/lib/compiler/aro/aro/Driver/Filesystem.zig +++ b/lib/compiler/aro/aro/Driver/Filesystem.zig @@ -4,7 +4,7 @@ const builtin = @import("builtin"); const is_windows = builtin.os.tag == .windows; fn readFileFake(entries: []const Filesystem.Entry, path: []const u8, buf: []u8) ?[]const u8 { - @setCold(true); + @branchHint(.cold); for (entries) |entry| { if (mem.eql(u8, entry.path, path)) { const len = @min(entry.contents.len, buf.len); @@ -16,7 +16,7 @@ fn readFileFake(entries: []const Filesystem.Entry, path: []const u8, buf: []u8) } fn findProgramByNameFake(entries: []const Filesystem.Entry, name: []const u8, path: ?[]const u8, buf: []u8) ?[]const u8 { - @setCold(true); + @branchHint(.cold); if (mem.indexOfScalar(u8, name, '/') != null) { @memcpy(buf[0..name.len], name); return buf[0..name.len]; @@ -35,7 +35,7 @@ fn findProgramByNameFake(entries: []const Filesystem.Entry, name: []const u8, pa } fn canExecuteFake(entries: []const Filesystem.Entry, path: []const u8) bool { - @setCold(true); + @branchHint(.cold); for (entries) |entry| { if (mem.eql(u8, entry.path, path)) { return entry.executable; @@ -45,7 +45,7 @@ fn canExecuteFake(entries: []const Filesystem.Entry, path: []const u8) bool { } fn existsFake(entries: []const Filesystem.Entry, path: []const u8) bool { - @setCold(true); + @branchHint(.cold); var buf: [std.fs.max_path_bytes]u8 = undefined; var fib = std.heap.FixedBufferAllocator.init(&buf); const resolved = std.fs.path.resolvePosix(fib.allocator(), &.{path}) catch return false; diff --git a/lib/compiler/aro/aro/Parser.zig b/lib/compiler/aro/aro/Parser.zig index 1cb5e18934dfbc9d70d0fec55995cf1dfa3de16e..d8d6f9d71c7b0155b809c9a869b75e1bfce1a549 100644 --- a/lib/compiler/aro/aro/Parser.zig +++ b/lib/compiler/aro/aro/Parser.zig @@ -385,12 +385,12 @@ fn errExpectedToken(p: *Parser, expected: Token.Id, actual: Token.Id) Error { } pub fn errStr(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex, str: []const u8) Compilation.Error!void { - @setCold(true); + @branchHint(.cold); return p.errExtra(tag, tok_i, .{ .str = str }); } pub fn errExtra(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex, extra: Diagnostics.Message.Extra) Compilation.Error!void { - @setCold(true); + @branchHint(.cold); const tok = p.pp.tokens.get(tok_i); var loc = tok.loc; if (tok_i != 0 and tok.id == .eof) { @@ -407,12 +407,12 @@ pub fn errExtra(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex, extra: Diag } pub fn errTok(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex) Compilation.Error!void { - @setCold(true); + @branchHint(.cold); return p.errExtra(tag, tok_i, .{ .none = {} }); } pub fn err(p: *Parser, tag: Diagnostics.Tag) Compilation.Error!void { - @setCold(true); + @branchHint(.cold); return p.errExtra(tag, p.tok_i, .{ .none = {} }); } @@ -638,7 +638,7 @@ fn pragma(p: *Parser) Compilation.Error!bool { /// Issue errors for top-level definitions whose type was never completed. fn diagnoseIncompleteDefinitions(p: *Parser) !void { - @setCold(true); + @branchHint(.cold); const node_slices = p.nodes.slice(); const tags = node_slices.items(.tag); diff --git a/lib/compiler/resinator/main.zig b/lib/compiler/resinator/main.zig index e056e80252e1ac69ba154b235c392794bb0c3e2c..c09801096cc25ea41b8ea1ff61d22bec764a7d34 100644 --- a/lib/compiler/resinator/main.zig +++ b/lib/compiler/resinator/main.zig @@ -421,7 +421,7 @@ fn cliDiagnosticsToErrorBundle( gpa: std.mem.Allocator, diagnostics: *cli.Diagnostics, ) !ErrorBundle { - @setCold(true); + @branchHint(.cold); var bundle: ErrorBundle.Wip = undefined; try bundle.init(gpa); @@ -468,7 +468,7 @@ fn diagnosticsToErrorBundle( diagnostics: *Diagnostics, mappings: SourceMappings, ) !ErrorBundle { - @setCold(true); + @branchHint(.cold); var bundle: ErrorBundle.Wip = undefined; try bundle.init(gpa); @@ -559,7 +559,7 @@ fn flushErrorMessageIntoBundle(wip: *ErrorBundle.Wip, msg: ErrorBundle.ErrorMess } fn errorStringToErrorBundle(allocator: std.mem.Allocator, comptime format: []const u8, args: anytype) !ErrorBundle { - @setCold(true); + @branchHint(.cold); var bundle: ErrorBundle.Wip = undefined; try bundle.init(allocator); errdefer bundle.deinit(); @@ -574,7 +574,7 @@ fn aroDiagnosticsToErrorBundle( fail_msg: []const u8, comp: *aro.Compilation, ) !ErrorBundle { - @setCold(true); + @branchHint(.cold); var bundle: ErrorBundle.Wip = undefined; try bundle.init(gpa); diff --git a/lib/compiler_rt/common.zig b/lib/compiler_rt/common.zig index 552fc07efd0888e21bd4eccd5d386ab3a79df0e7..1645a3951deb6498f7f7b5fa08dc23a14de224f7 100644 --- a/lib/compiler_rt/common.zig +++ b/lib/compiler_rt/common.zig @@ -72,7 +72,7 @@ pub const want_sparc_abi = builtin.cpu.arch.isSPARC(); pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { _ = error_return_trace; if (builtin.is_test) { - @setCold(true); + @branchHint(.cold); std.debug.panic("{s}", .{msg}); } else { unreachable; diff --git a/lib/std/Thread/Futex.zig b/lib/std/Thread/Futex.zig index c067368041d0fb4178beffd8a1ab66fa06c204df..fe22fa201123ccf410eef98331ca466dc20877fc 100644 --- a/lib/std/Thread/Futex.zig +++ b/lib/std/Thread/Futex.zig @@ -27,7 +27,7 @@ const atomic = std.atomic; /// The checking of `ptr` and `expect`, along with blocking the caller, is done atomically /// and totally ordered (sequentially consistent) with respect to other wait()/wake() calls on the same `ptr`. pub fn wait(ptr: *const atomic.Value(u32), expect: u32) void { - @setCold(true); + @branchHint(.cold); Impl.wait(ptr, expect, null) catch |err| switch (err) { error.Timeout => unreachable, // null timeout meant to wait forever @@ -43,7 +43,7 @@ pub fn wait(ptr: *const atomic.Value(u32), expect: u32) void { /// The checking of `ptr` and `expect`, along with blocking the caller, is done atomically /// and totally ordered (sequentially consistent) with respect to other wait()/wake() calls on the same `ptr`. pub fn timedWait(ptr: *const atomic.Value(u32), expect: u32, timeout_ns: u64) error{Timeout}!void { - @setCold(true); + @branchHint(.cold); // Avoid calling into the OS for no-op timeouts. if (timeout_ns == 0) { @@ -56,7 +56,7 @@ pub fn timedWait(ptr: *const atomic.Value(u32), expect: u32, timeout_ns: u64) er /// Unblocks at most `max_waiters` callers blocked in a `wait()` call on `ptr`. pub fn wake(ptr: *const atomic.Value(u32), max_waiters: u32) void { - @setCold(true); + @branchHint(.cold); // Avoid calling into the OS if there's nothing to wake up. if (max_waiters == 0) { @@ -1048,7 +1048,7 @@ pub const Deadline = struct { /// - A spurious wake occurs. /// - The deadline expires; In which case `error.Timeout` is returned. pub fn wait(self: *Deadline, ptr: *const atomic.Value(u32), expect: u32) error{Timeout}!void { - @setCold(true); + @branchHint(.cold); // Check if we actually have a timeout to wait until. // If not just wait "forever". diff --git a/lib/std/Thread/Mutex.zig b/lib/std/Thread/Mutex.zig index 032c19b7dd63506809463b5e6938d34c4aaf057a..be421c4c94014ac1b25b5f4ab9c4b678a510bf37 100644 --- a/lib/std/Thread/Mutex.zig +++ b/lib/std/Thread/Mutex.zig @@ -169,7 +169,7 @@ const FutexImpl = struct { } fn lockSlow(self: *@This()) void { - @setCold(true); + @branchHint(.cold); // Avoid doing an atomic swap below if we already know the state is contended. // An atomic swap unconditionally stores which marks the cache-line as modified unnecessarily. diff --git a/lib/std/Thread/ResetEvent.zig b/lib/std/Thread/ResetEvent.zig index b7e5758780817ea6715dd9e94c8ca36f0fd95804..cbc5a2a31c2d6f14f89decc9b842cce51ebae1e9 100644 --- a/lib/std/Thread/ResetEvent.zig +++ b/lib/std/Thread/ResetEvent.zig @@ -107,7 +107,7 @@ const FutexImpl = struct { } fn waitUntilSet(self: *Impl, timeout: ?u64) error{Timeout}!void { - @setCold(true); + @branchHint(.cold); // Try to set the state from `unset` to `waiting` to indicate // to the set() thread that others are blocked on the ResetEvent. diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 9dcefd2b1130b6380a1bca62fadabfbb02be4a03..6028db83a013079a74f6a539475d13d0438cbd02 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -779,7 +779,7 @@ else /// This function is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace, ret_addr: ?usize) noreturn { - @setCold(true); + @branchHint(.cold); // For backends that cannot handle the language features depended on by the // default panic handler, we have a simpler panic handler: @@ -896,27 +896,27 @@ pub fn checkNonScalarSentinel(expected: anytype, actual: @TypeOf(expected)) void } pub fn panicSentinelMismatch(expected: anytype, actual: @TypeOf(expected)) noreturn { - @setCold(true); + @branchHint(.cold); std.debug.panicExtra(null, @returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{ expected, actual }); } pub fn panicUnwrapError(st: ?*StackTrace, err: anyerror) noreturn { - @setCold(true); + @branchHint(.cold); std.debug.panicExtra(st, @returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)}); } pub fn panicOutOfBounds(index: usize, len: usize) noreturn { - @setCold(true); + @branchHint(.cold); std.debug.panicExtra(null, @returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len }); } pub fn panicStartGreaterThanEnd(start: usize, end: usize) noreturn { - @setCold(true); + @branchHint(.cold); std.debug.panicExtra(null, @returnAddress(), "start index {d} is larger than end index {d}", .{ start, end }); } pub fn panicInactiveUnionField(active: anytype, wanted: @TypeOf(active)) noreturn { - @setCold(true); + @branchHint(.cold); std.debug.panicExtra(null, @returnAddress(), "access of union field '{s}' while field '{s}' is active", .{ @tagName(wanted), @tagName(active) }); } @@ -949,7 +949,7 @@ pub const panic_messages = struct { }; pub noinline fn returnError(st: *StackTrace) void { - @setCold(true); + @branchHint(.cold); @setRuntimeSafety(false); addErrRetTraceAddr(st, @returnAddress()); } diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 826978abe42d727f4c4f0c3ed94105a47b6372d6..4672f7ac0610389fb53ff59c2e7e76c8de485737 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -409,7 +409,7 @@ pub fn assertReadable(slice: []const volatile u8) void { } pub fn panic(comptime format: []const u8, args: anytype) noreturn { - @setCold(true); + @branchHint(.cold); panicExtra(@errorReturnTrace(), @returnAddress(), format, args); } @@ -422,7 +422,7 @@ pub fn panicExtra( comptime format: []const u8, args: anytype, ) noreturn { - @setCold(true); + @branchHint(.cold); const size = 0x1000; const trunc_msg = "(msg truncated)"; @@ -450,7 +450,7 @@ threadlocal var panic_stage: usize = 0; // `panicImpl` could be useful in implementing a custom panic handler which // calls the default handler (on supported platforms) pub fn panicImpl(trace: ?*const std.builtin.StackTrace, first_trace_addr: ?usize, msg: []const u8) noreturn { - @setCold(true); + @branchHint(.cold); if (enable_segfault_handler) { // If a segfault happens while panicking, we want it to actually segfault, not trigger diff --git a/lib/std/fmt/parse_float/convert_slow.zig b/lib/std/fmt/parse_float/convert_slow.zig index e4539937bdb3a1d8671b642d7eab0802e18b2686..9232bc8bce40cd29659265a7aa9ff6810071308f 100644 --- a/lib/std/fmt/parse_float/convert_slow.zig +++ b/lib/std/fmt/parse_float/convert_slow.zig @@ -36,7 +36,7 @@ pub fn getShift(n: usize) usize { /// Note that this function needs a lot of stack space and is marked /// cold to hint against inlining into the caller. pub fn convertSlow(comptime T: type, s: []const u8) BiasedFp(T) { - @setCold(true); + @branchHint(.cold); const MantissaT = mantissaType(T); const min_exponent = -(1 << (math.floatExponentBits(T) - 1)) + 1; diff --git a/lib/std/hash/xxhash.zig b/lib/std/hash/xxhash.zig index 1d7c8399fc91661f786d49247e4a6c746023c30a..eb3d4ea2b9164f8a91c48b87a681ea63f52c11ca 100644 --- a/lib/std/hash/xxhash.zig +++ b/lib/std/hash/xxhash.zig @@ -593,7 +593,7 @@ pub const XxHash3 = struct { } fn hash3(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 { - @setCold(true); + @branchHint(.cold); std.debug.assert(input.len > 0 and input.len < 4); const flip: [2]u32 = @bitCast(secret[0..8].*); @@ -609,7 +609,7 @@ pub const XxHash3 = struct { } fn hash8(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 { - @setCold(true); + @branchHint(.cold); std.debug.assert(input.len >= 4 and input.len <= 8); const flip: [2]u64 = @bitCast(secret[8..24].*); @@ -625,7 +625,7 @@ pub const XxHash3 = struct { } fn hash16(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 { - @setCold(true); + @branchHint(.cold); std.debug.assert(input.len > 8 and input.len <= 16); const flip: [4]u64 = @bitCast(secret[24..56].*); @@ -641,7 +641,7 @@ pub const XxHash3 = struct { } fn hash128(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 { - @setCold(true); + @branchHint(.cold); std.debug.assert(input.len > 16 and input.len <= 128); var acc = XxHash64.prime_1 *% @as(u64, input.len); @@ -657,7 +657,7 @@ pub const XxHash3 = struct { } fn hash240(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 { - @setCold(true); + @branchHint(.cold); std.debug.assert(input.len > 128 and input.len <= 240); var acc = XxHash64.prime_1 *% @as(u64, input.len); @@ -676,7 +676,7 @@ pub const XxHash3 = struct { } noinline fn hashLong(seed: u64, input: []const u8) u64 { - @setCold(true); + @branchHint(.cold); std.debug.assert(input.len >= 240); const block_count = ((input.len - 1) / @sizeOf(Block)) * @sizeOf(Block); diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig index 905d44c3687d0c573d505a6ea9e41aeaccdc26d7..c0ae4596dcf6fcc90161d88807ce296c531240ea 100644 --- a/lib/std/hash_map.zig +++ b/lib/std/hash_map.zig @@ -1657,7 +1657,7 @@ pub fn HashMapUnmanaged( } fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) Allocator.Error!void { - @setCold(true); + @branchHint(.cold); const new_cap = @max(new_capacity, minimal_capacity); assert(new_cap > self.capacity()); assert(std.math.isPowerOfTwo(new_cap)); diff --git a/lib/std/heap/WasmPageAllocator.zig b/lib/std/heap/WasmPageAllocator.zig index 0fb45b70d4eeb5e073c3085a0f07c3354eadc060..7cb85561d829c0adf1f9877d9cdf562c08ce3828 100644 --- a/lib/std/heap/WasmPageAllocator.zig +++ b/lib/std/heap/WasmPageAllocator.zig @@ -61,7 +61,7 @@ const FreeBlock = struct { const not_found = maxInt(usize); fn useRecycled(self: FreeBlock, num_pages: usize, log2_align: u8) usize { - @setCold(true); + @branchHint(.cold); for (self.data, 0..) |segment, i| { const spills_into_next = @as(i128, @bitCast(segment)) < 0; const has_enough_bits = @popCount(segment) >= num_pages; diff --git a/lib/std/log.zig b/lib/std/log.zig index b2c05112b029bf3718e84df1191aaa7e3ca3f4d3..1533161cbae4f959a60477accbbe78b1c3b806ce 100644 --- a/lib/std/log.zig +++ b/lib/std/log.zig @@ -171,7 +171,7 @@ pub fn scoped(comptime scope: @Type(.EnumLiteral)) type { comptime format: []const u8, args: anytype, ) void { - @setCold(true); + @branchHint(.cold); log(.err, scope, format, args); } diff --git a/lib/std/once.zig b/lib/std/once.zig index ee3a8b7a357c55ba1f45cf860d14f0981807781e..326487df076dcc242e25cec9d7c40c6fdb87da84 100644 --- a/lib/std/once.zig +++ b/lib/std/once.zig @@ -25,7 +25,7 @@ pub fn Once(comptime f: fn () void) type { } fn callSlow(self: *@This()) void { - @setCold(true); + @branchHint(.cold); self.mutex.lock(); defer self.mutex.unlock(); diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 665ea17788ac0035fe3bcc834feab67ee4aad904..fcd923ad9c1bce6ec27f6fbdbb126e2838e389bf 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -654,7 +654,7 @@ fn getRandomBytesDevURandom(buf: []u8) !void { /// it raises SIGABRT followed by SIGKILL and finally lo /// Invokes the current signal handler for SIGABRT, if any. pub fn abort() noreturn { - @setCold(true); + @branchHint(.cold); // MSVCRT abort() sometimes opens a popup window which is undesirable, so // even when linking libc on Windows we use our own abort implementation. // See https://github.com/ziglang/zig/issues/2071 for more details. diff --git a/lib/std/sort/pdq.zig b/lib/std/sort/pdq.zig index 61d78797c569a3426387b632098070cd99b7a177..55bd17ae93e5483fde063ed0344255fccd878241 100644 --- a/lib/std/sort/pdq.zig +++ b/lib/std/sort/pdq.zig @@ -203,7 +203,7 @@ fn partitionEqual(a: usize, b: usize, pivot: usize, context: anytype) usize { /// /// returns `true` if the slice is sorted at the end. This function is `O(n)` worst-case. fn partialInsertionSort(a: usize, b: usize, context: anytype) bool { - @setCold(true); + @branchHint(.cold); // maximum number of adjacent out-of-order pairs that will get shifted const max_steps = 5; @@ -247,7 +247,7 @@ fn partialInsertionSort(a: usize, b: usize, context: anytype) bool { } fn breakPatterns(a: usize, b: usize, context: anytype) void { - @setCold(true); + @branchHint(.cold); const len = b - a; if (len < 8) return; diff --git a/lib/std/zig/AstGen.zig b/lib/std/zig/AstGen.zig index 83765c8ad4c22d2f8f6e610c3b7bdac0c77beda2..4e36e3e36557aa8f165e91d7abb34c5aafaf263c 100644 --- a/lib/std/zig/AstGen.zig +++ b/lib/std/zig/AstGen.zig @@ -11435,7 +11435,7 @@ fn appendErrorNodeNotes( args: anytype, notes: []const u32, ) Allocator.Error!void { - @setCold(true); + @branchHint(.cold); const string_bytes = &astgen.string_bytes; const msg: Zir.NullTerminatedString = @enumFromInt(string_bytes.items.len); try string_bytes.writer(astgen.gpa).print(format ++ "\x00", args); @@ -11526,7 +11526,7 @@ fn appendErrorTokNotesOff( args: anytype, notes: []const u32, ) !void { - @setCold(true); + @branchHint(.cold); const gpa = astgen.gpa; const string_bytes = &astgen.string_bytes; const msg: Zir.NullTerminatedString = @enumFromInt(string_bytes.items.len); @@ -11563,7 +11563,7 @@ fn errNoteTokOff( comptime format: []const u8, args: anytype, ) Allocator.Error!u32 { - @setCold(true); + @branchHint(.cold); const string_bytes = &astgen.string_bytes; const msg: Zir.NullTerminatedString = @enumFromInt(string_bytes.items.len); try string_bytes.writer(astgen.gpa).print(format ++ "\x00", args); @@ -11582,7 +11582,7 @@ fn errNoteNode( comptime format: []const u8, args: anytype, ) Allocator.Error!u32 { - @setCold(true); + @branchHint(.cold); const string_bytes = &astgen.string_bytes; const msg: Zir.NullTerminatedString = @enumFromInt(string_bytes.items.len); try string_bytes.writer(astgen.gpa).print(format ++ "\x00", args); diff --git a/lib/std/zig/Parse.zig b/lib/std/zig/Parse.zig index c43868a7d4c7b221ec7f1a9c8e676f90c7dd57e5..6f557a0f5526867e66e8ed787d47388cf4fb9c6b 100644 --- a/lib/std/zig/Parse.zig +++ b/lib/std/zig/Parse.zig @@ -81,7 +81,7 @@ fn addExtra(p: *Parse, extra: anytype) Allocator.Error!Node.Index { } fn warnExpected(p: *Parse, expected_token: Token.Tag) error{OutOfMemory}!void { - @setCold(true); + @branchHint(.cold); try p.warnMsg(.{ .tag = .expected_token, .token = p.tok_i, @@ -90,12 +90,12 @@ fn warnExpected(p: *Parse, expected_token: Token.Tag) error{OutOfMemory}!void { } fn warn(p: *Parse, error_tag: AstError.Tag) error{OutOfMemory}!void { - @setCold(true); + @branchHint(.cold); try p.warnMsg(.{ .tag = error_tag, .token = p.tok_i }); } fn warnMsg(p: *Parse, msg: Ast.Error) error{OutOfMemory}!void { - @setCold(true); + @branchHint(.cold); switch (msg.tag) { .expected_semi_after_decl, .expected_semi_after_stmt, @@ -141,12 +141,12 @@ fn warnMsg(p: *Parse, msg: Ast.Error) error{OutOfMemory}!void { } fn fail(p: *Parse, tag: Ast.Error.Tag) error{ ParseError, OutOfMemory } { - @setCold(true); + @branchHint(.cold); return p.failMsg(.{ .tag = tag, .token = p.tok_i }); } fn failExpected(p: *Parse, expected_token: Token.Tag) error{ ParseError, OutOfMemory } { - @setCold(true); + @branchHint(.cold); return p.failMsg(.{ .tag = .expected_token, .token = p.tok_i, @@ -155,7 +155,7 @@ fn failExpected(p: *Parse, expected_token: Token.Tag) error{ ParseError, OutOfMe } fn failMsg(p: *Parse, msg: Ast.Error) error{ ParseError, OutOfMemory } { - @setCold(true); + @branchHint(.cold); try p.warnMsg(msg); return error.ParseError; } diff --git a/src/Compilation.zig b/src/Compilation.zig index 61f1a30a6b5464ff3332688e1b7f17495d95e6a9..3ba53083ea7485e7b6f4cb2ca4e57c7120c3da1d 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -5785,7 +5785,7 @@ fn failCObj( comptime format: []const u8, args: anytype, ) SemaError { - @setCold(true); + @branchHint(.cold); const diag_bundle = blk: { const diag_bundle = try comp.gpa.create(CObject.Diag.Bundle); diag_bundle.* = .{}; @@ -5809,7 +5809,7 @@ fn failCObjWithOwnedDiagBundle( c_object: *CObject, diag_bundle: *CObject.Diag.Bundle, ) SemaError { - @setCold(true); + @branchHint(.cold); assert(diag_bundle.diags.len > 0); { comp.mutex.lock(); @@ -5825,7 +5825,7 @@ fn failCObjWithOwnedDiagBundle( } fn failWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, comptime format: []const u8, args: anytype) SemaError { - @setCold(true); + @branchHint(.cold); var bundle: ErrorBundle.Wip = undefined; try bundle.init(comp.gpa); errdefer bundle.deinit(); @@ -5852,7 +5852,7 @@ fn failWin32ResourceWithOwnedBundle( win32_resource: *Win32Resource, err_bundle: ErrorBundle, ) SemaError { - @setCold(true); + @branchHint(.cold); { comp.mutex.lock(); defer comp.mutex.unlock(); diff --git a/src/Sema.zig b/src/Sema.zig index ef72016b7b2ccaa9bf186117a7520b6fb1bc19c1..7f39762cb207f2ebf9a2761fa7502674c5242e98 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2471,7 +2471,7 @@ fn addFieldErrNote( comptime format: []const u8, args: anytype, ) !void { - @setCold(true); + @branchHint(.cold); const type_src = container_ty.srcLocOrNull(sema.pt.zcu) orelse return; const field_src: LazySrcLoc = .{ .base_node_inst = type_src.base_node_inst, @@ -2507,7 +2507,7 @@ pub fn fail( } pub fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Zcu.ErrorMsg) error{ AnalysisFail, OutOfMemory } { - @setCold(true); + @branchHint(.cold); const gpa = sema.gpa; const zcu = sema.pt.zcu; diff --git a/src/arch/aarch64/CodeGen.zig b/src/arch/aarch64/CodeGen.zig index 9472bdecfd95db9be4cfc68a6135fa866bd091a8..0ed37b79d7d99adc02e54b5c90d843151f710763 100644 --- a/src/arch/aarch64/CodeGen.zig +++ b/src/arch/aarch64/CodeGen.zig @@ -6357,14 +6357,14 @@ fn wantSafety(self: *Self) bool { } fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); return error.CodegenFail; } fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); return error.CodegenFail; diff --git a/src/arch/aarch64/Emit.zig b/src/arch/aarch64/Emit.zig index 641a6fd920a35af03928f19ad403debfd6e81dff..6c5630718365ca7f026ab5713c7da2bd1223e171 100644 --- a/src/arch/aarch64/Emit.zig +++ b/src/arch/aarch64/Emit.zig @@ -430,7 +430,7 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void { } fn fail(emit: *Emit, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(emit.err_msg == null); const comp = emit.bin_file.comp; const gpa = comp.gpa; diff --git a/src/arch/arm/CodeGen.zig b/src/arch/arm/CodeGen.zig index 9e71bff381bc7c43458086b07c43601820d63be8..90bd3606300a204b7e0091f88d2f174ea7bb57ce 100644 --- a/src/arch/arm/CodeGen.zig +++ b/src/arch/arm/CodeGen.zig @@ -6313,7 +6313,7 @@ fn wantSafety(self: *Self) bool { } fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); const gpa = self.gpa; self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); @@ -6321,7 +6321,7 @@ fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { } fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); const gpa = self.gpa; self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); diff --git a/src/arch/arm/Emit.zig b/src/arch/arm/Emit.zig index dd68f2397da52f5bf24d23acd0040515a69be3df..c1b5baad578f779132d75168f0b46150b6b4d240 100644 --- a/src/arch/arm/Emit.zig +++ b/src/arch/arm/Emit.zig @@ -348,7 +348,7 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void { } fn fail(emit: *Emit, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(emit.err_msg == null); const comp = emit.bin_file.comp; const gpa = comp.gpa; diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig index d7e882ddbeb480a7c00c1ee452dad1b36e5ca4ad..2df0dbc3284abc87295c39ca5522871435fe574f 100644 --- a/src/arch/riscv64/CodeGen.zig +++ b/src/arch/riscv64/CodeGen.zig @@ -8223,14 +8223,14 @@ fn wantSafety(func: *Func) bool { } fn fail(func: *Func, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(func.err_msg == null); func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); return error.CodegenFail; } fn failSymbol(func: *Func, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(func.err_msg == null); func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); return error.CodegenFail; diff --git a/src/arch/riscv64/Lower.zig b/src/arch/riscv64/Lower.zig index 0b96868bdd8be66697095c61425ce0b35aa0f8bd..a9ae9df898d68e504494952f9f5e5d5953fbb95a 100644 --- a/src/arch/riscv64/Lower.zig +++ b/src/arch/riscv64/Lower.zig @@ -583,7 +583,7 @@ fn pushPopRegList(lower: *Lower, comptime spilling: bool, reg_list: Mir.Register } pub fn fail(lower: *Lower, comptime format: []const u8, args: anytype) Error { - @setCold(true); + @branchHint(.cold); assert(lower.err_msg == null); lower.err_msg = try ErrorMsg.create(lower.allocator, lower.src_loc, format, args); return error.LowerFail; diff --git a/src/arch/sparc64/CodeGen.zig b/src/arch/sparc64/CodeGen.zig index 06611a28049f9284d7a1aab880bc8dbe81ab57c1..e6c5a6d2176a334b1804b6c961a481dae0a2beb4 100644 --- a/src/arch/sparc64/CodeGen.zig +++ b/src/arch/sparc64/CodeGen.zig @@ -3533,7 +3533,7 @@ fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) } fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); const gpa = self.gpa; self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); diff --git a/src/arch/sparc64/Emit.zig b/src/arch/sparc64/Emit.zig index 165ee1c45e784782140db74b27554e61161ee5bd..4e49bcf5b42e3878702de547fcd11588e134ca5f 100644 --- a/src/arch/sparc64/Emit.zig +++ b/src/arch/sparc64/Emit.zig @@ -511,7 +511,7 @@ fn dbgAdvancePCAndLine(emit: *Emit, line: u32, column: u32) !void { } fn fail(emit: *Emit, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(emit.err_msg == null); const comp = emit.bin_file.comp; const gpa = comp.gpa; diff --git a/src/arch/wasm/Emit.zig b/src/arch/wasm/Emit.zig index 087f0dff6b0859499ff8b0ff92aec302e3fbd16b..e02067f8ca72e86a3a029d36189e66bfd9cc84d9 100644 --- a/src/arch/wasm/Emit.zig +++ b/src/arch/wasm/Emit.zig @@ -252,7 +252,7 @@ fn offset(self: Emit) u32 { } fn fail(emit: *Emit, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); std.debug.assert(emit.error_msg == null); const comp = emit.bin_file.base.comp; const zcu = comp.zcu.?; diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index d5661fc1937869847a43870844159c4641d898f3..5693a6da23a299c47de04780b5db30df4c0c40c0 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -19203,7 +19203,7 @@ fn resolveCallingConventionValues( } fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); const gpa = self.gpa; self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); @@ -19211,7 +19211,7 @@ fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { } fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { - @setCold(true); + @branchHint(.cold); assert(self.err_msg == null); const gpa = self.gpa; self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); diff --git a/src/arch/x86_64/Lower.zig b/src/arch/x86_64/Lower.zig index 69cd548cc3e9944e001e05376e9a15f4cf45669f..33ba0c7629dbc4873a1311aeffe8c7adc6ac53b7 100644 --- a/src/arch/x86_64/Lower.zig +++ b/src/arch/x86_64/Lower.zig @@ -293,7 +293,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct { } pub fn fail(lower: *Lower, comptime format: []const u8, args: anytype) Error { - @setCold(true); + @branchHint(.cold); assert(lower.err_msg == null); lower.err_msg = try Zcu.ErrorMsg.create(lower.allocator, lower.src_loc, format, args); return error.LowerFail; diff --git a/src/codegen/c.zig b/src/codegen/c.zig index eb5d2921adeef041d73ff7e5aeb0fb74bdac09f1..8703e9b1244804ecf7e27e8c802b66d14d9eef5e 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -626,7 +626,7 @@ pub const DeclGen = struct { } fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { - @setCold(true); + @branchHint(.cold); const zcu = dg.pt.zcu; const src_loc = zcu.navSrcLoc(dg.pass.nav); dg.error_msg = try Zcu.ErrorMsg.create(dg.gpa, src_loc, format, args); diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index f49e1c333bac29b2447cd78bd99fdbb1e50efa4b..eec46b83798a50c73f11f93ebca517b6f2cb8516 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -4618,7 +4618,7 @@ pub const NavGen = struct { } fn todo(ng: *NavGen, comptime format: []const u8, args: anytype) Error { - @setCold(true); + @branchHint(.cold); assert(ng.err_msg == null); const o = ng.object; const gpa = o.gpa; @@ -4784,7 +4784,7 @@ pub const FuncGen = struct { } fn todo(self: *FuncGen, comptime format: []const u8, args: anytype) Error { - @setCold(true); + @branchHint(.cold); return self.ng.todo(format, args); } diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 68961d22412d086da5434b428767cdcf4da676d0..d1df1ba77eadd5fd0ace1d91936c86f8fbd36789 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -410,7 +410,7 @@ const NavGen = struct { } pub fn fail(self: *NavGen, comptime format: []const u8, args: anytype) Error { - @setCold(true); + @branchHint(.cold); const zcu = self.pt.zcu; const src_loc = zcu.navSrcLoc(self.owner_nav); assert(self.error_msg == null); diff --git a/src/crash_report.zig b/src/crash_report.zig index 67ec0e0eb0460ff32626ec01b9d04b6fa72989a9..c0a90d8705725f066d5d0d3e1f0c7fd9079bcfee 100644 --- a/src/crash_report.zig +++ b/src/crash_report.zig @@ -153,8 +153,8 @@ fn writeFilePath(file: *Zcu.File, writer: anytype) !void { } pub fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, maybe_ret_addr: ?usize) noreturn { + @branchHint(.cold); PanicSwitch.preDispatch(); - @setCold(true); const ret_addr = maybe_ret_addr orelse @returnAddress(); const stack_ctx: StackContext = .{ .current = .{ .ret_addr = ret_addr } }; PanicSwitch.dispatch(error_return_trace, stack_ctx, msg); diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index ebe0468ca2d6fb0ac7e114ce0110f3979a3326ae..b96ab2fb06cb2b2b4179d7029c752f6346a42c3f 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -113,7 +113,7 @@ test "cold function" { } fn thisIsAColdFn() void { - @setCold(true); + @branchHint(.cold); } test "unicode escape in character literal" { diff --git a/test/behavior/builtin_functions_returning_void_or_noreturn.zig b/test/behavior/builtin_functions_returning_void_or_noreturn.zig index 721a3f0320cf7812910eb5772c8b1c73701f4109..c69d0f66a89a559d3e232e93734562c4f57286b8 100644 --- a/test/behavior/builtin_functions_returning_void_or_noreturn.zig +++ b/test/behavior/builtin_functions_returning_void_or_noreturn.zig @@ -22,7 +22,7 @@ test { try testing.expectEqual(noreturn, @TypeOf(if (true) @panic("") else {})); try testing.expectEqual({}, @prefetch(&val, .{})); try testing.expectEqual({}, @setAlignStack(16)); - try testing.expectEqual({}, @setCold(true)); + try testing.expectEqual({}, @branchHint(.cold)); try testing.expectEqual({}, @setEvalBranchQuota(0)); try testing.expectEqual({}, @setFloatMode(.optimized)); try testing.expectEqual({}, @setRuntimeSafety(true)); diff --git a/test/cases/compile_errors/function-only_builtins_outside_function.zig b/test/cases/compile_errors/function-only_builtins_outside_function.zig index 3484b5ca4665f710c0e511c96e0845b7d954282e..a9871e2b5956305dbf3716f369c474a74832ccdf 100644 --- a/test/cases/compile_errors/function-only_builtins_outside_function.zig +++ b/test/cases/compile_errors/function-only_builtins_outside_function.zig @@ -3,7 +3,7 @@ comptime { } comptime { - @setCold(true); + @branchHint(.cold); } comptime { @@ -55,7 +55,7 @@ comptime { // target=native // // :2:5: error: '@setAlignStack' outside function scope -// :6:5: error: '@setCold' outside function scope +// :6:5: error: '@branchHint' outside function scope // :10:5: error: '@src' outside function scope // :14:5: error: '@returnAddress' outside function scope // :18:5: error: '@frameAddress' outside function scope -- 2.54.0