compiler: use std.heap.smp_allocator

In main, now this allocator is chosen by default when compiling without
libc in ReleaseFast or ReleaseSmall, and not targeting WebAssembly.
This commit is contained in:
Andrew Kelley 2025-02-06 17:41:50 -08:00
parent 84bf7a6701
commit 7360be19a4
4 changed files with 23 additions and 22 deletions

View File

@ -139,7 +139,7 @@ int main(int argc, char **argv) {
"pub const enable_tracy = false;\n"
"pub const value_tracing = false;\n"
"pub const skip_non_native = false;\n"
"pub const force_gpa = false;\n"
"pub const debug_gpa = false;\n"
"pub const dev = .core;\n"
"pub const value_interpret_mode = .direct;\n"
, zig_version);

View File

@ -171,7 +171,7 @@ pub fn build(b: *std.Build) !void {
const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
const tracy_callstack_depth: u32 = b.option(u32, "tracy-callstack-depth", "Declare callstack depth for Tracy data. Does nothing if -Dtracy_callstack is not provided") orelse 10;
const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false;
const debug_gpa = b.option(bool, "debug-allocator", "Force the compiler to use DebugAllocator") orelse false;
const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);
const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
const strip = b.option(bool, "strip", "Omit debug information");
@ -233,7 +233,7 @@ pub fn build(b: *std.Build) !void {
exe_options.addOption(bool, "llvm_has_csky", llvm_has_csky);
exe_options.addOption(bool, "llvm_has_arc", llvm_has_arc);
exe_options.addOption(bool, "llvm_has_xtensa", llvm_has_xtensa);
exe_options.addOption(bool, "force_gpa", force_gpa);
exe_options.addOption(bool, "debug_gpa", debug_gpa);
exe_options.addOption(DevEnv, "dev", b.option(DevEnv, "dev", "Build a compiler with a reduced feature set for development of specific features") orelse if (only_c) .bootstrap else .full);
exe_options.addOption(ValueInterpretMode, "value_interpret_mode", value_interpret_mode);
@ -608,7 +608,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
exe_options.addOption(u32, "mem_leak_frames", 0);
exe_options.addOption(bool, "have_llvm", false);
exe_options.addOption(bool, "force_gpa", false);
exe_options.addOption(bool, "debug_gpa", false);
exe_options.addOption([:0]const u8, "version", version);
exe_options.addOption(std.SemanticVersion, "semver", semver);
exe_options.addOption(bool, "enable_debug_extensions", false);

View File

@ -171,30 +171,31 @@ pub fn log(
std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
}
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{
var debug_allocator: std.heap.DebugAllocator(.{
.stack_trace_frames = build_options.mem_leak_frames,
}){};
}) = .init;
pub fn main() anyerror!void {
crash_report.initialize();
const use_gpa = (build_options.force_gpa or !builtin.link_libc) and native_os != .wasi;
const gpa = gpa: {
if (native_os == .wasi) {
break :gpa std.heap.wasm_allocator;
const gpa, const is_debug = gpa: {
if (build_options.debug_gpa) break :gpa .{ debug_allocator.allocator(), true };
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
if (builtin.link_libc) {
// We would prefer to use raw libc allocator here, but cannot use
// it if it won't support the alignment we need.
if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
break :gpa .{ std.heap.c_allocator, false };
}
break :gpa .{ std.heap.raw_c_allocator, false };
}
if (use_gpa) {
break :gpa general_purpose_allocator.allocator();
}
// We would prefer to use raw libc allocator here, but cannot
// use it if it won't support the alignment we need.
if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
break :gpa std.heap.c_allocator;
}
break :gpa std.heap.raw_c_allocator;
break :gpa switch (builtin.mode) {
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
};
};
defer if (use_gpa) {
_ = general_purpose_allocator.deinit();
defer if (is_debug) {
_ = debug_allocator.deinit();
};
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();

View File

@ -11,6 +11,6 @@ pub const enable_link_snapshots = false;
pub const enable_tracy = false;
pub const value_tracing = false;
pub const skip_non_native = false;
pub const force_gpa = false;
pub const debug_gpa = false;
pub const dev = .core;
pub const value_interpret_mode = .direct;