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 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 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(