authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-19 22:44:09+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-22 12:06:02+02:00
log617f89f0823d766f2276651fc1bf467f54a22d35
treeb7b4ed520025cd9ae54349a84dbb4cc92e63dc0f
parentcba3389d906ff36f7913c7497d55ce1bf3164022

macho: parse input files in parallel


1 files changed, 28 insertions(+), 15 deletions(-)

src/link/MachO.zig+28-15
...@@ -96,6 +96,7 @@ debug_line_sect_index: ?u8 = null,...@@ -96,6 +96,7 @@ debug_line_sect_index: ?u8 = null,
96has_tlv: bool = false,96has_tlv: bool = false,
97binds_to_weak: bool = false,97binds_to_weak: bool = false,
98weak_defines: bool = false,98weak_defines: bool = false,
99has_errors: AtomicBool = AtomicBool.init(false),
99100
100/// Options101/// Options
101/// SDK layout102/// SDK layout
...@@ -469,8 +470,6 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n...@@ -469,8 +470,6 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
469 };470 };
470 }471 }
471472
472 if (self.base.hasErrors()) return error.FlushFailure;
473
474 try self.parseInputFiles();473 try self.parseInputFiles();
475 self.parseDependentDylibs() catch |err| {474 self.parseDependentDylibs() catch |err| {
476 switch (err) {475 switch (err) {
...@@ -900,24 +899,36 @@ pub fn parseInputFiles(self: *MachO) !void {...@@ -900,24 +899,36 @@ pub fn parseInputFiles(self: *MachO) !void {
900 const tracy = trace(@src());899 const tracy = trace(@src());
901 defer tracy.end();900 defer tracy.end();
902901
903 for (self.objects.items) |index| {902 const tp = self.base.comp.thread_pool;
904 self.getFile(index).?.parse(self) catch |err| switch (err) {903 var wg: WaitGroup = .{};
905 error.MalformedObject,904
906 error.InvalidCpuArch,905 {
907 error.InvalidTarget,906 wg.reset();
908 => {}, // already reported907 defer wg.wait();
909 else => |e| try self.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}),908
910 };909 for (self.objects.items) |index| {
910 tp.spawnWg(&wg, parseInputFileWorker, .{ self, index });
911 }
912 for (self.dylibs.items) |index| {
913 tp.spawnWg(&wg, parseInputFileWorker, .{ self, index });
914 }
911 }915 }
912 for (self.dylibs.items) |index| {916
913 self.getFile(index).?.parse(self) catch |err| switch (err) {917 if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure;
918}
919
920fn parseInputFileWorker(self: *MachO, index: File.Index) void {
921 self.getFile(index).?.parse(self) catch |err| {
922 switch (err) {
923 error.MalformedObject,
914 error.MalformedDylib,924 error.MalformedDylib,
915 error.InvalidCpuArch,925 error.InvalidCpuArch,
916 error.InvalidTarget,926 error.InvalidTarget,
917 => {}, // already reported927 => {}, // already reported
918 else => |e| try self.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}),928 else => |e| self.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}) catch {},
919 };929 }
920 }930 _ = self.has_errors.swap(true, .seq_cst);
931 };
921}932}
922933
923fn addArchive(self: *MachO, lib: SystemLib, must_link: bool, handle: File.HandleIndex, fat_arch: ?fat.Arch) !void {934fn addArchive(self: *MachO, lib: SystemLib, must_link: bool, handle: File.HandleIndex, fat_arch: ?fat.Arch) !void {
...@@ -4436,6 +4447,7 @@ const Alignment = Atom.Alignment;...@@ -4436,6 +4447,7 @@ const Alignment = Atom.Alignment;
4436const Allocator = mem.Allocator;4447const Allocator = mem.Allocator;
4437const Archive = @import("MachO/Archive.zig");4448const Archive = @import("MachO/Archive.zig");
4438pub const Atom = @import("MachO/Atom.zig");4449pub const Atom = @import("MachO/Atom.zig");
4450const AtomicBool = std.atomic.Value(bool);
4439const Bind = bind.Bind;4451const Bind = bind.Bind;
4440const Cache = std.Build.Cache;4452const Cache = std.Build.Cache;
4441const CodeSignature = @import("MachO/CodeSignature.zig");4453const CodeSignature = @import("MachO/CodeSignature.zig");
...@@ -4471,6 +4483,7 @@ const Thunk = thunks.Thunk;...@@ -4471,6 +4483,7 @@ const Thunk = thunks.Thunk;
4471const TlvPtrSection = synthetic.TlvPtrSection;4483const TlvPtrSection = synthetic.TlvPtrSection;
4472const Value = @import("../Value.zig");4484const Value = @import("../Value.zig");
4473const UnwindInfo = @import("MachO/UnwindInfo.zig");4485const UnwindInfo = @import("MachO/UnwindInfo.zig");
4486const WaitGroup = std.Thread.WaitGroup;
4474const WeakBind = bind.WeakBind;4487const WeakBind = bind.WeakBind;
4475const ZigGotSection = synthetic.ZigGotSection;4488const ZigGotSection = synthetic.ZigGotSection;
4476const ZigObject = @import("MachO/ZigObject.zig");4489const ZigObject = @import("MachO/ZigObject.zig");