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(...@@ -3695,15 +3695,19 @@ fn performAllTheWorkInner(
3695 // In case it failed last time, try again. `clearMiscFailures` was already3695 // In case it failed last time, try again. `clearMiscFailures` was already
3696 // called at the start of `update`.3696 // called at the start of `update`.
3697 if (comp.queued_jobs.compiler_rt_lib and comp.compiler_rt_lib == null) {3697 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 });
3699 }3703 }
37003704
3701 if (comp.queued_jobs.compiler_rt_obj and comp.compiler_rt_obj == null) {3705 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 });
3703 }3707 }
37043708
3705 if (comp.queued_jobs.fuzzer_lib and comp.fuzzer_lib == null) {3709 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 });
3707 }3711 }
37083712
3709 if (comp.queued_jobs.glibc_shared_objects) {3713 if (comp.queued_jobs.glibc_shared_objects) {
...@@ -4635,12 +4639,14 @@ fn buildRt(...@@ -4635,12 +4639,14 @@ fn buildRt(
4635 root_source_name: []const u8,4639 root_source_name: []const u8,
4636 misc_task: MiscTask,4640 misc_task: MiscTask,
4637 output_mode: std.builtin.OutputMode,4641 output_mode: std.builtin.OutputMode,
4642 allow_lto: bool,
4638 out: *?CrtFile,4643 out: *?CrtFile,
4639 prog_node: std.Progress.Node,4644 prog_node: std.Progress.Node,
4640) void {4645) void {
4641 comp.buildOutputFromZig(4646 comp.buildOutputFromZig(
4642 root_source_name,4647 root_source_name,
4643 output_mode,4648 output_mode,
4649 allow_lto,
4644 out,4650 out,
4645 misc_task,4651 misc_task,
4646 prog_node,4652 prog_node,
...@@ -4748,6 +4754,7 @@ fn buildZigLibc(comp: *Compilation, prog_node: std.Progress.Node) void {...@@ -4748,6 +4754,7 @@ fn buildZigLibc(comp: *Compilation, prog_node: std.Progress.Node) void {
4748 comp.buildOutputFromZig(4754 comp.buildOutputFromZig(
4749 "c.zig",4755 "c.zig",
4750 .Lib,4756 .Lib,
4757 true,
4751 &comp.libc_static_lib,4758 &comp.libc_static_lib,
4752 .zig_libc,4759 .zig_libc,
4753 prog_node,4760 prog_node,
...@@ -6453,6 +6460,7 @@ fn buildOutputFromZig(...@@ -6453,6 +6460,7 @@ fn buildOutputFromZig(
6453 comp: *Compilation,6460 comp: *Compilation,
6454 src_basename: []const u8,6461 src_basename: []const u8,
6455 output_mode: std.builtin.OutputMode,6462 output_mode: std.builtin.OutputMode,
6463 allow_lto: bool,
6456 out: *?CrtFile,6464 out: *?CrtFile,
6457 misc_task_tag: MiscTask,6465 misc_task_tag: MiscTask,
6458 prog_node: std.Progress.Node,6466 prog_node: std.Progress.Node,
...@@ -6481,6 +6489,7 @@ fn buildOutputFromZig(...@@ -6481,6 +6489,7 @@ fn buildOutputFromZig(
6481 .root_strip = strip,6489 .root_strip = strip,
6482 .link_libc = comp.config.link_libc,6490 .link_libc = comp.config.link_libc,
6483 .any_unwind_tables = comp.root_mod.unwind_tables != .none,6491 .any_unwind_tables = comp.root_mod.unwind_tables != .none,
6492 .lto = if (allow_lto) comp.config.lto else .none,
6484 });6493 });
64856494
6486 const root_mod = try Package.Module.create(arena, .{6495 const root_mod = try Package.Module.create(arena, .{
...@@ -6581,6 +6590,8 @@ pub const CrtFileOptions = struct {...@@ -6581,6 +6590,8 @@ pub const CrtFileOptions = struct {
6581 unwind_tables: ?std.builtin.UnwindTables = null,6590 unwind_tables: ?std.builtin.UnwindTables = null,
6582 pic: ?bool = null,6591 pic: ?bool = null,
6583 no_builtin: ?bool = null,6592 no_builtin: ?bool = null,
6593
6594 allow_lto: bool = true,
6584};6595};
65856596
6586pub fn build_crt_file(6597pub fn build_crt_file(
...@@ -6619,7 +6630,7 @@ pub fn build_crt_file(...@@ -6619,7 +6630,7 @@ pub fn build_crt_file(
6619 .link_libc = false,6630 .link_libc = false,
6620 .any_unwind_tables = options.unwind_tables != .none,6631 .any_unwind_tables = options.unwind_tables != .none,
6621 .lto = switch (output_mode) {6632 .lto = switch (output_mode) {
6622 .Lib => comp.config.lto,6633 .Lib => if (options.allow_lto) comp.config.lto else .none,
6623 .Obj, .Exe => .none,6634 .Obj, .Exe => .none,
6624 },6635 },
6625 });6636 });
src/libtsan.zig+2
...@@ -65,6 +65,8 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo...@@ -65,6 +65,8 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
65 .root_strip = strip,65 .root_strip = strip,
66 .link_libc = true,66 .link_libc = true,
67 .link_libcpp = link_libcpp,67 .link_libcpp = link_libcpp,
68 // LLVM disables LTO for its libtsan.
69 .lto = .none,
68 }) catch |err| {70 }) catch |err| {
69 comp.setMiscFailure(71 comp.setMiscFailure(
70 .libtsan,72 .libtsan,
src/mingw.zig+2
...@@ -175,6 +175,8 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -175,6 +175,8 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
175175
176 return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items, .{176 return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items, .{
177 .unwind_tables = unwind_tables,177 .unwind_tables = unwind_tables,
178 // https://github.com/llvm/llvm-project/issues/43698#issuecomment-2542660611
179 .allow_lto = false,
178 });180 });
179 },181 },
180 }182 }