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) {...@@ -139,7 +139,7 @@ int main(int argc, char **argv) {
139 "pub const enable_tracy = false;\n"139 "pub const enable_tracy = false;\n"
140 "pub const value_tracing = false;\n"140 "pub const value_tracing = false;\n"
141 "pub const skip_non_native = false;\n"141 "pub const skip_non_native = false;\n"
142 "pub const force_gpa = false;\n"142 "pub const debug_gpa = false;\n"
143 "pub const dev = .core;\n"143 "pub const dev = .core;\n"
144 "pub const value_interpret_mode = .direct;\n"144 "pub const value_interpret_mode = .direct;\n"
145 , zig_version);145 , zig_version);
build.zig+3-3
...@@ -171,7 +171,7 @@ pub fn build(b: *std.Build) !void {...@@ -171,7 +171,7 @@ pub fn build(b: *std.Build) !void {
171 const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);171 const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
172 const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);172 const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
173 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;173 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;
175 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);175 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);
176 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;176 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
177 const strip = b.option(bool, "strip", "Omit debug information");177 const strip = b.option(bool, "strip", "Omit debug information");
...@@ -233,7 +233,7 @@ pub fn build(b: *std.Build) !void {...@@ -233,7 +233,7 @@ pub fn build(b: *std.Build) !void {
233 exe_options.addOption(bool, "llvm_has_csky", llvm_has_csky);233 exe_options.addOption(bool, "llvm_has_csky", llvm_has_csky);
234 exe_options.addOption(bool, "llvm_has_arc", llvm_has_arc);234 exe_options.addOption(bool, "llvm_has_arc", llvm_has_arc);
235 exe_options.addOption(bool, "llvm_has_xtensa", llvm_has_xtensa);235 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);
237 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);237 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);
238 exe_options.addOption(ValueInterpretMode, "value_interpret_mode", value_interpret_mode);238 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 {...@@ -608,7 +608,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
608608
609 exe_options.addOption(u32, "mem_leak_frames", 0);609 exe_options.addOption(u32, "mem_leak_frames", 0);
610 exe_options.addOption(bool, "have_llvm", false);610 exe_options.addOption(bool, "have_llvm", false);
611 exe_options.addOption(bool, "force_gpa", false);611 exe_options.addOption(bool, "debug_gpa", false);
612 exe_options.addOption([:0]const u8, "version", version);612 exe_options.addOption([:0]const u8, "version", version);
613 exe_options.addOption(std.SemanticVersion, "semver", semver);613 exe_options.addOption(std.SemanticVersion, "semver", semver);
614 exe_options.addOption(bool, "enable_debug_extensions", false);614 exe_options.addOption(bool, "enable_debug_extensions", false);
src/main.zig+18-17
...@@ -171,30 +171,31 @@ pub fn log(...@@ -171,30 +171,31 @@ pub fn log(
171 std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);171 std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
172}172}
173173
174var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{174var debug_allocator: std.heap.DebugAllocator(.{
175 .stack_trace_frames = build_options.mem_leak_frames,175 .stack_trace_frames = build_options.mem_leak_frames,
176}){};176}) = .init;
177177
178pub fn main() anyerror!void {178pub fn main() anyerror!void {
179 crash_report.initialize();179 crash_report.initialize();
180180
181 const use_gpa = (build_options.force_gpa or !builtin.link_libc) and native_os != .wasi;181 const gpa, const is_debug = gpa: {
182 const gpa = gpa: {182 if (build_options.debug_gpa) break :gpa .{ debug_allocator.allocator(), true };
183 if (native_os == .wasi) {183 if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
184 break :gpa std.heap.wasm_allocator;184 if (builtin.link_libc) {
185 }185 // We would prefer to use raw libc allocator here, but cannot use
186 if (use_gpa) {186 // it if it won't support the alignment we need.
187 break :gpa general_purpose_allocator.allocator();187 if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
188 }188 break :gpa .{ std.heap.c_allocator, false };
189 // We would prefer to use raw libc allocator here, but cannot189 }
190 // use it if it won't support the alignment we need.190 break :gpa .{ std.heap.raw_c_allocator, false };
191 if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
192 break :gpa std.heap.c_allocator;
193 }191 }
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 };
195 };196 };
196 defer if (use_gpa) {197 defer if (is_debug) {
197 _ = general_purpose_allocator.deinit();198 _ = debug_allocator.deinit();
198 };199 };
199 var arena_instance = std.heap.ArenaAllocator.init(gpa);200 var arena_instance = std.heap.ArenaAllocator.init(gpa);
200 defer arena_instance.deinit();201 defer arena_instance.deinit();
stage1/config.zig.in+1-1
...@@ -11,6 +11,6 @@ pub const enable_link_snapshots = false;...@@ -11,6 +11,6 @@ pub const enable_link_snapshots = false;
11pub const enable_tracy = false;11pub const enable_tracy = false;
12pub const value_tracing = false;12pub const value_tracing = false;
13pub const skip_non_native = false;13pub const skip_non_native = false;
14pub const force_gpa = false;14pub const debug_gpa = false;
15pub const dev = .core;15pub const dev = .core;
16pub const value_interpret_mode = .direct;16pub const value_interpret_mode = .direct;