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

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 26 18:48:06 PDT 2024


Author: Joseph Huber
Date: 2024-09-26T18:48:01-07:00
New Revision: 44950de5b1496aaf524b73201579f56ff4325c52

URL: https://github.com/llvm/llvm-project/commit/44950de5b1496aaf524b73201579f56ff4325c52
DIFF: https://github.com/llvm/llvm-project/commit/44950de5b1496aaf524b73201579f56ff4325c52.diff

LOG: [nvlink-wrapper] Use a symbolic link instead of copying the file (#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.

Added: 
    

Modified: 
    clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
index 871fe5e4553ccb..8ec1f722fa8a10 100644
--- a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
+++ b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
@@ -655,9 +655,11 @@ 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.
+  // Windows cannot create symbolic links so we just copy the whole file.
   for (auto &Input : LinkerInput) {
+#ifdef _WIN32
     auto TempFileOrErr = createTempFile(
         Args, sys::path::stem(Input->getBufferIdentifier()), "cubin");
     if (!TempFileOrErr)
@@ -671,6 +673,18 @@ Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {
     if (Error E = Output->commit())
       return E;
     Files.emplace_back(Args.MakeArgString(*TempFileOrErr));
+#else
+    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));
+#endif
   }
 
   return Files;


        


More information about the cfe-commits mailing list