authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-06 17:41:50-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-07 14:41:49-08:00
log7360be19a461175d1a50dfb1d7c086bcc650b3c1
treee2c3d5c04b48c4c14597e9ffc1cd4b6152067756
parent84bf7a6701d5f51a8307736d310d4049c9158921

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.

4 files changed, 23 insertions(+), 22 deletions(-)

bootstrap.c+1-1
......@@ -139,7 +139,7 @@ int main(int argc, char **argv) {
139139 "pub const enable_tracy = false;\n"
140140 "pub const value_tracing = false;\n"
141141 "pub const skip_non_native = false;\n"
142 "pub const force_gpa = false;\n"
142 "pub const debug_gpa = false;\n"
143143 "pub const dev = .core;\n"
144144 "pub const value_interpret_mode = .direct;\n"
145145 , zig_version);
build.zig+3-3
......@@ -171,7 +171,7 @@ pub fn build(b: *std.Build) !void {
171171 const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
172172 const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
173173 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;
174 const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false;
174 const debug_gpa = b.option(bool, "debug-allocator", "Force the compiler to use DebugAllocator") orelse false;
175175 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);
176176 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
177177 const strip = b.option(bool, "strip", "Omit debug information");
......@@ -233,7 +233,7 @@ pub fn build(b: *std.Build) !void {
233233 exe_options.addOption(bool, "llvm_has_csky", llvm_has_csky);
234234 exe_options.addOption(bool, "llvm_has_arc", llvm_has_arc);
235235 exe_options.addOption(bool, "llvm_has_xtensa", llvm_has_xtensa);
236 exe_options.addOption(bool, "force_gpa", force_gpa);
236 exe_options.addOption(bool, "debug_gpa", debug_gpa);
237237 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);
238238 exe_options.addOption(ValueInterpretMode, "value_interpret_mode", value_interpret_mode);
239239
......@@ -608,7 +608,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
608608
609609 exe_options.addOption(u32, "mem_leak_frames", 0);
610610 exe_options.addOption(bool, "have_llvm", false);
611 exe_options.addOption(bool, "force_gpa", false);
611 exe_options.addOption(bool, "debug_gpa", false);
612612 exe_options.addOption([:0]const u8, "version", version);
613613 exe_options.addOption(std.SemanticVersion, "semver", semver);
614614 exe_options.addOption(bool, "enable_debug_extensions", false);
src/main.zig+18-17
......@@ -171,30 +171,31 @@ pub fn log(
171171 std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
172172}
173173
174var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{
174var debug_allocator: std.heap.DebugAllocator(.{
175175 .stack_trace_frames = build_options.mem_leak_frames,
176}){};
176}) = .init;
177177
178178pub fn main() anyerror!void {
179179 crash_report.initialize();
180180
181 const use_gpa = (build_options.force_gpa or !builtin.link_libc) and native_os != .wasi;
182 const gpa = gpa: {
183 if (native_os == .wasi) {
184 break :gpa std.heap.wasm_allocator;
185 }
186 if (use_gpa) {
187 break :gpa general_purpose_allocator.allocator();
188 }
189 // We would prefer to use raw libc allocator here, but cannot
190 // use it if it won't support the alignment we need.
191 if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
192 break :gpa std.heap.c_allocator;
181 const gpa, const is_debug = gpa: {
182 if (build_options.debug_gpa) break :gpa .{ debug_allocator.allocator(), true };
183 if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
184 if (builtin.link_libc) {
185 // We would prefer to use raw libc allocator here, but cannot use
186 // it if it won't support the alignment we need.
187 if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
188 break :gpa .{ std.heap.c_allocator, false };
189 }
190 break :gpa .{ std.heap.raw_c_allocator, false };
193191 }
194 break :gpa std.heap.raw_c_allocator;
192 break :gpa switch (builtin.mode) {
193 .Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
194 .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
195 };
195196 };
196 defer if (use_gpa) {
197 _ = general_purpose_allocator.deinit();
197 defer if (is_debug) {
198 _ = debug_allocator.deinit();
198199 };
199200 var arena_instance = std.heap.ArenaAllocator.init(gpa);
200201 defer arena_instance.deinit();
stage1/config.zig.in+1-1
......@@ -11,6 +11,6 @@ pub const enable_link_snapshots = false;
1111pub const enable_tracy = false;
1212pub const value_tracing = false;
1313pub const skip_non_native = false;
14pub const force_gpa = false;
14pub const debug_gpa = false;
1515pub const dev = .core;
1616pub const value_interpret_mode = .direct;