Skip to content

Commit 6808ce2

Browse files
committed
compiler,lib,test,langref: migrate @setCold to @branchHint
1 parent a3a737e commit 6808ce2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+94
-96
lines changed

doc/langref.html.in

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,6 +4340,13 @@ comptime {
43404340
{#see_also|@sizeOf|@typeInfo#}
43414341
{#header_close#}
43424342

4343+
{#header_open|@branchHint#}
4344+
<pre>{#syntax#}@branchHint(hint: BranchHint) void{#endsyntax#}</pre>
4345+
<p>Hints to the optimizer how likely a given branch of control flow is to be reached.</p>
4346+
<p>{#syntax#}BranchHint{#endsyntax#} can be found with {#syntax#}@import("std").builtin.BranchHint{#endsyntax#}.</p>
4347+
<p>This function is only valid as the first statement in a control flow branch, or the first statement in a function.</p>
4348+
{#header_close#}
4349+
43434350
{#header_open|@breakpoint#}
43444351
<pre>{#syntax#}@breakpoint() void{#endsyntax#}</pre>
43454352
<p>
@@ -5242,15 +5249,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
52425249
</p>
52435250
{#header_close#}
52445251

5245-
{#header_open|@setCold#}
5246-
<pre>{#syntax#}@setCold(comptime is_cold: bool) void{#endsyntax#}</pre>
5247-
<p>
5248-
Tells the optimizer that the current function is (or is not) rarely called.
5249-
5250-
This function is only valid within function scope.
5251-
</p>
5252-
{#header_close#}
5253-
52545252
{#header_open|@setEvalBranchQuota#}
52555253
<pre>{#syntax#}@setEvalBranchQuota(comptime new_quota: u32) void{#endsyntax#}</pre>
52565254
<p>

doc/langref/test_functions.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const WINAPI: std.builtin.CallingConvention = if (native_arch == .x86) .Stdcall
2727
extern "kernel32" fn ExitProcess(exit_code: u32) callconv(WINAPI) noreturn;
2828
extern "c" fn atan2(a: f64, b: f64) f64;
2929

30-
// The @setCold builtin tells the optimizer that a function is rarely called.
30+
// The @branchHint builtin can be used to tell the optimizer that a function is rarely called ("cold").
3131
fn abort() noreturn {
32-
@setCold(true);
32+
@branchHint(.cold);
3333
while (true) {}
3434
}
3535

lib/c.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ comptime {
4646
// Avoid dragging in the runtime safety mechanisms into this .o file,
4747
// unless we're trying to test this file.
4848
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
49-
@setCold(true);
49+
@branchHint(.cold);
5050
_ = error_return_trace;
5151
if (builtin.is_test) {
5252
std.debug.panic("{s}", .{msg});

lib/compiler/aro/aro/Driver/Filesystem.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const builtin = @import("builtin");
44
const is_windows = builtin.os.tag == .windows;
55

66
fn readFileFake(entries: []const Filesystem.Entry, path: []const u8, buf: []u8) ?[]const u8 {
7-
@setCold(true);
7+
@branchHint(.cold);
88
for (entries) |entry| {
99
if (mem.eql(u8, entry.path, path)) {
1010
const len = @min(entry.contents.len, buf.len);
@@ -16,7 +16,7 @@ fn readFileFake(entries: []const Filesystem.Entry, path: []const u8, buf: []u8)
1616
}
1717

1818
fn findProgramByNameFake(entries: []const Filesystem.Entry, name: []const u8, path: ?[]const u8, buf: []u8) ?[]const u8 {
19-
@setCold(true);
19+
@branchHint(.cold);
2020
if (mem.indexOfScalar(u8, name, '/') != null) {
2121
@memcpy(buf[0..name.len], name);
2222
return buf[0..name.len];
@@ -35,7 +35,7 @@ fn findProgramByNameFake(entries: []const Filesystem.Entry, name: []const u8, pa
3535
}
3636

3737
fn canExecuteFake(entries: []const Filesystem.Entry, path: []const u8) bool {
38-
@setCold(true);
38+
@branchHint(.cold);
3939
for (entries) |entry| {
4040
if (mem.eql(u8, entry.path, path)) {
4141
return entry.executable;
@@ -45,7 +45,7 @@ fn canExecuteFake(entries: []const Filesystem.Entry, path: []const u8) bool {
4545
}
4646

4747
fn existsFake(entries: []const Filesystem.Entry, path: []const u8) bool {
48-
@setCold(true);
48+
@branchHint(.cold);
4949
var buf: [std.fs.max_path_bytes]u8 = undefined;
5050
var fib = std.heap.FixedBufferAllocator.init(&buf);
5151
const resolved = std.fs.path.resolvePosix(fib.allocator(), &.{path}) catch return false;

lib/compiler/aro/aro/Parser.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,12 @@ fn errExpectedToken(p: *Parser, expected: Token.Id, actual: Token.Id) Error {
385385
}
386386

387387
pub fn errStr(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex, str: []const u8) Compilation.Error!void {
388-
@setCold(true);
388+
@branchHint(.cold);
389389
return p.errExtra(tag, tok_i, .{ .str = str });
390390
}
391391

392392
pub fn errExtra(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex, extra: Diagnostics.Message.Extra) Compilation.Error!void {
393-
@setCold(true);
393+
@branchHint(.cold);
394394
const tok = p.pp.tokens.get(tok_i);
395395
var loc = tok.loc;
396396
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
407407
}
408408

409409
pub fn errTok(p: *Parser, tag: Diagnostics.Tag, tok_i: TokenIndex) Compilation.Error!void {
410-
@setCold(true);
410+
@branchHint(.cold);
411411
return p.errExtra(tag, tok_i, .{ .none = {} });
412412
}
413413

414414
pub fn err(p: *Parser, tag: Diagnostics.Tag) Compilation.Error!void {
415-
@setCold(true);
415+
@branchHint(.cold);
416416
return p.errExtra(tag, p.tok_i, .{ .none = {} });
417417
}
418418

@@ -638,7 +638,7 @@ fn pragma(p: *Parser) Compilation.Error!bool {
638638

639639
/// Issue errors for top-level definitions whose type was never completed.
640640
fn diagnoseIncompleteDefinitions(p: *Parser) !void {
641-
@setCold(true);
641+
@branchHint(.cold);
642642

643643
const node_slices = p.nodes.slice();
644644
const tags = node_slices.items(.tag);

lib/compiler/resinator/main.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn cliDiagnosticsToErrorBundle(
421421
gpa: std.mem.Allocator,
422422
diagnostics: *cli.Diagnostics,
423423
) !ErrorBundle {
424-
@setCold(true);
424+
@branchHint(.cold);
425425

426426
var bundle: ErrorBundle.Wip = undefined;
427427
try bundle.init(gpa);
@@ -468,7 +468,7 @@ fn diagnosticsToErrorBundle(
468468
diagnostics: *Diagnostics,
469469
mappings: SourceMappings,
470470
) !ErrorBundle {
471-
@setCold(true);
471+
@branchHint(.cold);
472472

473473
var bundle: ErrorBundle.Wip = undefined;
474474
try bundle.init(gpa);
@@ -559,7 +559,7 @@ fn flushErrorMessageIntoBundle(wip: *ErrorBundle.Wip, msg: ErrorBundle.ErrorMess
559559
}
560560

561561
fn errorStringToErrorBundle(allocator: std.mem.Allocator, comptime format: []const u8, args: anytype) !ErrorBundle {
562-
@setCold(true);
562+
@branchHint(.cold);
563563
var bundle: ErrorBundle.Wip = undefined;
564564
try bundle.init(allocator);
565565
errdefer bundle.deinit();
@@ -574,7 +574,7 @@ fn aroDiagnosticsToErrorBundle(
574574
fail_msg: []const u8,
575575
comp: *aro.Compilation,
576576
) !ErrorBundle {
577-
@setCold(true);
577+
@branchHint(.cold);
578578

579579
var bundle: ErrorBundle.Wip = undefined;
580580
try bundle.init(gpa);

lib/compiler_rt/common.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub const want_sparc_abi = builtin.cpu.arch.isSPARC();
7272
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
7373
_ = error_return_trace;
7474
if (builtin.is_test) {
75-
@setCold(true);
75+
@branchHint(.cold);
7676
std.debug.panic("{s}", .{msg});
7777
} else {
7878
unreachable;

lib/std/Thread/Futex.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const atomic = std.atomic;
2727
/// The checking of `ptr` and `expect`, along with blocking the caller, is done atomically
2828
/// and totally ordered (sequentially consistent) with respect to other wait()/wake() calls on the same `ptr`.
2929
pub fn wait(ptr: *const atomic.Value(u32), expect: u32) void {
30-
@setCold(true);
30+
@branchHint(.cold);
3131

3232
Impl.wait(ptr, expect, null) catch |err| switch (err) {
3333
error.Timeout => unreachable, // null timeout meant to wait forever
@@ -43,7 +43,7 @@ pub fn wait(ptr: *const atomic.Value(u32), expect: u32) void {
4343
/// The checking of `ptr` and `expect`, along with blocking the caller, is done atomically
4444
/// and totally ordered (sequentially consistent) with respect to other wait()/wake() calls on the same `ptr`.
4545
pub fn timedWait(ptr: *const atomic.Value(u32), expect: u32, timeout_ns: u64) error{Timeout}!void {
46-
@setCold(true);
46+
@branchHint(.cold);
4747

4848
// Avoid calling into the OS for no-op timeouts.
4949
if (timeout_ns == 0) {
@@ -56,7 +56,7 @@ pub fn timedWait(ptr: *const atomic.Value(u32), expect: u32, timeout_ns: u64) er
5656

5757
/// Unblocks at most `max_waiters` callers blocked in a `wait()` call on `ptr`.
5858
pub fn wake(ptr: *const atomic.Value(u32), max_waiters: u32) void {
59-
@setCold(true);
59+
@branchHint(.cold);
6060

6161
// Avoid calling into the OS if there's nothing to wake up.
6262
if (max_waiters == 0) {
@@ -1048,7 +1048,7 @@ pub const Deadline = struct {
10481048
/// - A spurious wake occurs.
10491049
/// - The deadline expires; In which case `error.Timeout` is returned.
10501050
pub fn wait(self: *Deadline, ptr: *const atomic.Value(u32), expect: u32) error{Timeout}!void {
1051-
@setCold(true);
1051+
@branchHint(.cold);
10521052

10531053
// Check if we actually have a timeout to wait until.
10541054
// If not just wait "forever".

lib/std/Thread/Mutex.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const FutexImpl = struct {
169169
}
170170

171171
fn lockSlow(self: *@This()) void {
172-
@setCold(true);
172+
@branchHint(.cold);
173173

174174
// Avoid doing an atomic swap below if we already know the state is contended.
175175
// An atomic swap unconditionally stores which marks the cache-line as modified unnecessarily.

lib/std/Thread/ResetEvent.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const FutexImpl = struct {
107107
}
108108

109109
fn waitUntilSet(self: *Impl, timeout: ?u64) error{Timeout}!void {
110-
@setCold(true);
110+
@branchHint(.cold);
111111

112112
// Try to set the state from `unset` to `waiting` to indicate
113113
// to the set() thread that others are blocked on the ResetEvent.

0 commit comments

Comments
 (0)