[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 09:08:19 PDT 2024


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

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.


>From 8c1cd42f2b860f5b9640f98e05e242f2003d35ba 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.
---
 .../clang-nvlink-wrapper/ClangNVLinkWrapper.cpp     | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
index 871fe5e4553ccb..19faef8dc07e45 100644
--- a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
+++ b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
@@ -655,21 +655,16 @@ 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;
+    if (std::error_code EC =
+            sys::fs::create_link(Input->getBufferIdentifier(), *TempFileOrErr))
+      reportError(createFileError(*TempFileOrErr, EC));
     Files.emplace_back(Args.MakeArgString(*TempFileOrErr));
   }
 



More information about the cfe-commits mailing list