authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-25 14:47:14+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-25 14:57:33+01:00
log1e8b82f6b4852211b76a54da14fc8cf0351ada09
tree1f5cd8e93a7f99d3010f4086e385dcfe0ac08e80
parentafe2fed34dd960afb44f88cfe6ce088e1817cb5b
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

compiler: Rework LTO settings for some Zig-provided libraries.

* compiler-rt and mingw32 have both run into LLD bugs, and LLVM disables LTO for its compiler-rt, so disable LTO for these. * While we haven't run into any bugs in it, LLVM disables LTO for its libtsan, so follow suit just to be safe. * Allow LTO for libfuzzer as LLVM does.

3 files changed, 19 insertions(+), 4 deletions(-)

src/Compilation.zig+15-4
......@@ -3695,15 +3695,19 @@ fn performAllTheWorkInner(
36953695 // In case it failed last time, try again. `clearMiscFailures` was already
36963696 // called at the start of `update`.
36973697 if (comp.queued_jobs.compiler_rt_lib and comp.compiler_rt_lib == null) {
3698 comp.link_task_wait_group.spawnManager(buildRt, .{ comp, "compiler_rt.zig", .compiler_rt, .Lib, &comp.compiler_rt_lib, main_progress_node });
3698 // LLVM disables LTO for its compiler-rt and we've had various issues with LTO of our
3699 // compiler-rt due to LLD bugs as well, e.g.:
3700 //
3701 // https://github.com/llvm/llvm-project/issues/43698#issuecomment-2542660611
3702 comp.link_task_wait_group.spawnManager(buildRt, .{ comp, "compiler_rt.zig", .compiler_rt, .Lib, false, &comp.compiler_rt_lib, main_progress_node });
36993703 }
37003704
37013705 if (comp.queued_jobs.compiler_rt_obj and comp.compiler_rt_obj == null) {
3702 comp.link_task_wait_group.spawnManager(buildRt, .{ comp, "compiler_rt.zig", .compiler_rt, .Obj, &comp.compiler_rt_obj, main_progress_node });
3706 comp.link_task_wait_group.spawnManager(buildRt, .{ comp, "compiler_rt.zig", .compiler_rt, .Obj, false, &comp.compiler_rt_obj, main_progress_node });
37033707 }
37043708
37053709 if (comp.queued_jobs.fuzzer_lib and comp.fuzzer_lib == null) {
3706 comp.link_task_wait_group.spawnManager(buildRt, .{ comp, "fuzzer.zig", .libfuzzer, .Lib, &comp.fuzzer_lib, main_progress_node });
3710 comp.link_task_wait_group.spawnManager(buildRt, .{ comp, "fuzzer.zig", .libfuzzer, .Lib, true, &comp.fuzzer_lib, main_progress_node });
37073711 }
37083712
37093713 if (comp.queued_jobs.glibc_shared_objects) {
......@@ -4635,12 +4639,14 @@ fn buildRt(
46354639 root_source_name: []const u8,
46364640 misc_task: MiscTask,
46374641 output_mode: std.builtin.OutputMode,
4642 allow_lto: bool,
46384643 out: *?CrtFile,
46394644 prog_node: std.Progress.Node,
46404645) void {
46414646 comp.buildOutputFromZig(
46424647 root_source_name,
46434648 output_mode,
4649 allow_lto,
46444650 out,
46454651 misc_task,
46464652 prog_node,
......@@ -4748,6 +4754,7 @@ fn buildZigLibc(comp: *Compilation, prog_node: std.Progress.Node) void {
47484754 comp.buildOutputFromZig(
47494755 "c.zig",
47504756 .Lib,
4757 true,
47514758 &comp.libc_static_lib,
47524759 .zig_libc,
47534760 prog_node,
......@@ -6453,6 +6460,7 @@ fn buildOutputFromZig(
64536460 comp: *Compilation,
64546461 src_basename: []const u8,
64556462 output_mode: std.builtin.OutputMode,
6463 allow_lto: bool,
64566464 out: *?CrtFile,
64576465 misc_task_tag: MiscTask,
64586466 prog_node: std.Progress.Node,
......@@ -6481,6 +6489,7 @@ fn buildOutputFromZig(
64816489 .root_strip = strip,
64826490 .link_libc = comp.config.link_libc,
64836491 .any_unwind_tables = comp.root_mod.unwind_tables != .none,
6492 .lto = if (allow_lto) comp.config.lto else .none,
64846493 });
64856494
64866495 const root_mod = try Package.Module.create(arena, .{
......@@ -6581,6 +6590,8 @@ pub const CrtFileOptions = struct {
65816590 unwind_tables: ?std.builtin.UnwindTables = null,
65826591 pic: ?bool = null,
65836592 no_builtin: ?bool = null,
6593
6594 allow_lto: bool = true,
65846595};
65856596
65866597pub fn build_crt_file(
......@@ -6619,7 +6630,7 @@ pub fn build_crt_file(
66196630 .link_libc = false,
66206631 .any_unwind_tables = options.unwind_tables != .none,
66216632 .lto = switch (output_mode) {
6622 .Lib => comp.config.lto,
6633 .Lib => if (options.allow_lto) comp.config.lto else .none,
66236634 .Obj, .Exe => .none,
66246635 },
66256636 });
src/libtsan.zig+2
......@@ -65,6 +65,8 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
6565 .root_strip = strip,
6666 .link_libc = true,
6767 .link_libcpp = link_libcpp,
68 // LLVM disables LTO for its libtsan.
69 .lto = .none,
6870 }) catch |err| {
6971 comp.setMiscFailure(
7072 .libtsan,
src/mingw.zig+2
......@@ -175,6 +175,8 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
175175
176176 return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items, .{
177177 .unwind_tables = unwind_tables,
178 // https://github.com/llvm/llvm-project/issues/43698#issuecomment-2542660611
179 .allow_lto = false,
178180 });
179181 },
180182 }