[PATCH] D133218: [lld-macho] Hardlink -object_path_lto files to cache when possible

Leonard Grey via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 6 12:51:36 PDT 2022


lgrey updated this revision to Diff 458248.
lgrey marked an inline comment as done.
lgrey added a comment.

Add test, clarify control flow


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133218/new/

https://reviews.llvm.org/D133218

Files:
  lld/MachO/LTO.cpp
  lld/test/MachO/lto-object-path.ll


Index: lld/test/MachO/lto-object-path.ll
===================================================================
--- lld/test/MachO/lto-object-path.ll
+++ lld/test/MachO/lto-object-path.ll
@@ -16,12 +16,21 @@
 ;; And that dsymutil can read the result
 ; RUN: dsymutil -f -o - %t/test | llvm-dwarfdump - | FileCheck %s --check-prefix=DSYM
 
+;; Test that the object path hardlinks to the cache when possible
+;; NB: 
+;; 1) Need to delete current object path contents or hardlink will fail.
+;; 2) Note the cache policy is different from the test above (which is crafted
+;;    to ensure that the cache is pruned immediately; we want the opposite here).
+; RUN: rm -rf %t/lto-temps
+; RUN: %lld %t/test.o -o %t/test -object_path_lto %t/lto-temps -prune_interval_lto 20 -cache_path_lto %t/cache --thinlto-cache-policy=cache_size_files=10:cache_size_bytes=10000
+; RUN: ls -l %t/lto-temps/1.x86_64.lto.o | cut -f 3 -d ' ' | FileCheck %s --check-prefix=HARDLINK
+
+
 ;; check that the object path can be an existing file
 ; RUN: touch %t/lto-tmp.o
 ; RUN: %lld %t/test.o -o %t/test -object_path_lto %t/lto-tmp.o
 ; RUN: llvm-nm -pa %t/test | FileCheck %s --check-prefixes CHECK,OBJPATH-FILE -DFILE=%t/lto-tmp.o
 
-
 ; CHECK:             0000000000000000                - 00 0000    SO /tmp/test.cpp
 ; NOOBJPATH-NEXT:    0000000000000000                - 03 0001   OSO /tmp/lto.tmp
 ;; check that modTime is nonzero when `-object_path_lto` is provided
@@ -32,6 +41,7 @@
 ; CHECK-NEXT:        0000000000000000                - 01 0000    SO
 ; CHECK-NEXT:        {{[0-9a-f]+}}                   T _main
 ; DSYM: DW_AT_name ("test.cpp")
+; HARDLINK: 2
 
 target triple = "x86_64-apple-macosx10.15.0"
 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
Index: lld/MachO/LTO.cpp
===================================================================
--- lld/MachO/LTO.cpp
+++ lld/MachO/LTO.cpp
@@ -52,6 +52,18 @@
   return c;
 }
 
+// If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails,
+// or `originalPath` is not set, saves `buffer` to `path`.
+static void saveOrHardlinkBuffer(StringRef buffer, const Twine &path,
+                                 Optional<StringRef> originalPath) {
+  if (originalPath) {
+    auto err = fs::create_hard_link(*originalPath, path);
+    if (!err)
+      return;
+  }
+  saveBuffer(buffer, path);
+}
+
 BitcodeCompiler::BitcodeCompiler() {
   lto::ThinBackend backend = lto::createInProcessThinBackend(
       heavyweight_hardware_concurrency(config->thinLTOJobs));
@@ -154,10 +166,13 @@
     // not use the cached MemoryBuffer directly to ensure dsymutil does not
     // race with the cache pruner.
     StringRef objBuf;
-    if (files[i])
+    Optional<StringRef> cachePath = llvm::None;
+    if (files[i]) {
       objBuf = files[i]->getBuffer();
-    else
+      cachePath = files[i]->getBufferIdentifier();
+    } else {
       objBuf = buf[i];
+    }
     if (objBuf.empty())
       continue;
 
@@ -174,7 +189,7 @@
         path::append(filePath, Twine(i) + "." +
                                    getArchitectureName(config->arch()) +
                                    ".lto.o");
-      saveBuffer(objBuf, filePath);
+      saveOrHardlinkBuffer(objBuf, filePath, cachePath);
       modTime = getModTime(filePath);
     }
     ret.push_back(make<ObjFile>(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133218.458248.patch
Type: text/x-patch
Size: 3357 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220906/2814c5a8/attachment.bin>


More information about the llvm-commits mailing list