[clang] [nvlink-wrapper] Use a symbolic link instead of copying the file (PR #110139)

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 26 12:07:57 PDT 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/110139

>From 393e05145d0c31a3b1b254f97a357c776617898c Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Thu, 26 Sep 2024 11:04:46 -0500
Subject: [PATCH] [nvlink-wrapper] Use a symbolic link instead of copying the
 file

Summary:
We need all inputs to `nvlink` to end in `.cubin` while the rest of the
compiler toolchain wants `.o`. Previously we copied `.o` file to
`.cubin` files, but this is wasteful. Instead, we can just create a link
against it. This saves some disk space during link time.
---
 .../ClangNVLinkWrapper.cpp                    | 25 ++++++++-----------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
index 871fe5e4553ccb..bdc1cc40496370 100644
--- a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
+++ b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
@@ -655,22 +655,19 @@ Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {
     }
   }
 
-  // Copy all of the input files to a new file ending in `.cubin`. The 'nvlink'
+  // Create a link for each file to a new file ending in `.cubin`. The 'nvlink'
   // linker requires all NVPTX inputs to have this extension for some reason.
   for (auto &Input : LinkerInput) {
-    auto TempFileOrErr = createTempFile(
-        Args, sys::path::stem(Input->getBufferIdentifier()), "cubin");
-    if (!TempFileOrErr)
-      return TempFileOrErr.takeError();
-    Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
-        FileOutputBuffer::create(*TempFileOrErr, Input->getBuffer().size());
-    if (!OutputOrErr)
-      return OutputOrErr.takeError();
-    std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
-    llvm::copy(Input->getBuffer(), Output->getBufferStart());
-    if (Error E = Output->commit())
-      return E;
-    Files.emplace_back(Args.MakeArgString(*TempFileOrErr));
+    SmallString<128> TempFile;
+    if (std::error_code EC = sys::fs::getPotentiallyUniqueTempFileName(
+            sys::path::stem(Input->getBufferIdentifier()), "cubin", TempFile))
+      reportError(createFileError(TempFile, EC));
+    if (std::error_code EC =
+            sys::fs::create_link(Input->getBufferIdentifier(), TempFile)) {
+      reportError(createFileError(TempFile, EC));
+    }
+    Files.emplace_back(Args.MakeArgString(TempFile));
+    TempFiles.emplace_back(std::move(TempFile));
   }
 
   return Files;



More information about the cfe-commits mailing list