authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-16 10:32:17+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-22 16:58:20+02:00
log4658d857de9198e825f13c136b2342d630e19e62
tree291ecf5d25d29add0ca85e5c081b720db60ff198
parentb380ed6a729d7f0daa6f0ee2b58bf3b54017cc65

macho: fix caching linker line in incremental setting


1 files changed, 45 insertions(+), 36 deletions(-)

src/link/MachO.zig+45-36
...@@ -478,50 +478,59 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -478,50 +478,59 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
478 defer if (!self.base.options.disable_lld_caching) man.deinit();478 defer if (!self.base.options.disable_lld_caching) man.deinit();
479479
480 var digest: [Cache.hex_digest_len]u8 = undefined;480 var digest: [Cache.hex_digest_len]u8 = undefined;
481 var cache_miss: bool = self.cold_start;481 man = comp.cache_parent.obtain();
482 self.base.releaseLock();
482483
483 if (!self.base.options.disable_lld_caching) {484 man.hash.addListOfBytes(libs.keys());
484 man = comp.cache_parent.obtain();
485 self.base.releaseLock();
486485
487 man.hash.addListOfBytes(libs.keys());486 _ = try man.hit();
488487 digest = man.final();
489 _ = try man.hit();
490 digest = man.final();
491488
492 var prev_digest_buf: [digest.len]u8 = undefined;489 var prev_digest_buf: [digest.len]u8 = undefined;
493 const prev_digest: []u8 = Cache.readSmallFile(490 const prev_digest: []u8 = Cache.readSmallFile(
494 cache_dir_handle,491 cache_dir_handle,
495 id_symlink_basename,492 id_symlink_basename,
496 &prev_digest_buf,493 &prev_digest_buf,
497 ) catch |err| blk: {494 ) catch |err| blk: {
498 log.debug("MachO Zld new_digest={s} error: {s}", .{495 log.debug("MachO Zld new_digest={s} error: {s}", .{
499 std.fmt.fmtSliceHexLower(&digest),496 std.fmt.fmtSliceHexLower(&digest),
500 @errorName(err),497 @errorName(err),
501 });498 });
502 // Handle this as a cache miss.499 // Handle this as a cache miss.
503 break :blk prev_digest_buf[0..0];500 break :blk prev_digest_buf[0..0];
504 };501 };
502 const cache_miss: bool = cache_miss: {
505 if (mem.eql(u8, prev_digest, &digest)) {503 if (mem.eql(u8, prev_digest, &digest)) {
506 log.debug("MachO Zld digest={s} match - skipping parsing linker line objects", .{504 log.debug("MachO Zld digest={s} match", .{
507 std.fmt.fmtSliceHexLower(&digest),505 std.fmt.fmtSliceHexLower(&digest),
508 });506 });
509 self.base.lock = man.toOwnedLock();507 if (!self.cold_start) {
510 } else {508 log.debug(" skipping parsing linker line objects", .{});
511 log.debug("MachO Zld prev_digest={s} new_digest={s}", .{509 break :cache_miss false;
512 std.fmt.fmtSliceHexLower(prev_digest),510 } else {
513 std.fmt.fmtSliceHexLower(&digest),511 log.debug(" TODO parse prelinked binary and continue linking where we left off", .{});
514 });512 }
515 // We are about to change the output file to be different, so we invalidate the build hash now.
516 cache_dir_handle.deleteFile(id_symlink_basename) catch |err| switch (err) {
517 error.FileNotFound => {},
518 else => |e| return e,
519 };
520 cache_miss = true;
521 }513 }
522 }514 log.debug("MachO Zld prev_digest={s} new_digest={s}", .{
515 std.fmt.fmtSliceHexLower(prev_digest),
516 std.fmt.fmtSliceHexLower(&digest),
517 });
518 // We are about to change the output file to be different, so we invalidate the build hash now.
519 cache_dir_handle.deleteFile(id_symlink_basename) catch |err| switch (err) {
520 error.FileNotFound => {},
521 else => |e| return e,
522 };
523 break :cache_miss true;
524 };
523525
524 if (cache_miss) {526 if (cache_miss) {
527 for (self.dylibs.items) |*dylib| {
528 dylib.deinit(self.base.allocator);
529 }
530 self.dylibs.clearRetainingCapacity();
531 self.dylibs_map.clearRetainingCapacity();
532 self.referenced_dylibs.clearRetainingCapacity();
533
525 var dependent_libs = std.fifo.LinearFifo(struct {534 var dependent_libs = std.fifo.LinearFifo(struct {
526 id: Dylib.Id,535 id: Dylib.Id,
527 parent: u16,536 parent: u16,
...@@ -607,7 +616,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -607,7 +616,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
607 try self.snapshotState();616 try self.snapshotState();
608 }617 }
609618
610 if (!self.base.options.disable_lld_caching and cache_miss) {619 if (cache_miss) {
611 // Update the file with the digest. If it fails we can continue; it only620 // Update the file with the digest. If it fails we can continue; it only
612 // means that the next invocation will have an unnecessary cache miss.621 // means that the next invocation will have an unnecessary cache miss.
613 Cache.writeSmallFile(cache_dir_handle, id_symlink_basename, &digest) catch |err| {622 Cache.writeSmallFile(cache_dir_handle, id_symlink_basename, &digest) catch |err| {