[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
Fri Sep 2 10:59:44 PDT 2022


lgrey created this revision.
lgrey added a reviewer: lld-macho.
lgrey added a project: lld-macho.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion.
Herald added a reviewer: ributzka.
Herald added a project: All.
lgrey requested review of this revision.
Herald added a project: LLVM.

This is a follow-up to https://reviews.llvm.org/D131624 (specifically to https://reviews.llvm.org/D131624#3716584)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133218

Files:
  lld/MachO/LTO.cpp


Index: lld/MachO/LTO.cpp
===================================================================
--- lld/MachO/LTO.cpp
+++ lld/MachO/LTO.cpp
@@ -52,6 +52,17 @@
   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) {
+    if (!fs::create_hard_link(*originalPath, path))
+      return;
+  }
+  saveBuffer(buffer, path);
+}
+
 BitcodeCompiler::BitcodeCompiler() {
   lto::ThinBackend backend = lto::createInProcessThinBackend(
       heavyweight_hardware_concurrency(config->thinLTOJobs));
@@ -154,10 +165,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 +188,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.457641.patch
Type: text/x-patch
Size: 1552 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220902/7736bc11/attachment.bin>


More information about the llvm-commits mailing list