Skip to content

Commit 44950de

Browse files
authored
[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.
1 parent f6a756f commit 44950de

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,11 @@ Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {
655655
}
656656
}
657657

658-
// Copy all of the input files to a new file ending in `.cubin`. The 'nvlink'
658+
// Create a link for each file to a new file ending in `.cubin`. The 'nvlink'
659659
// linker requires all NVPTX inputs to have this extension for some reason.
660+
// Windows cannot create symbolic links so we just copy the whole file.
660661
for (auto &Input : LinkerInput) {
662+
#ifdef _WIN32
661663
auto TempFileOrErr = createTempFile(
662664
Args, sys::path::stem(Input->getBufferIdentifier()), "cubin");
663665
if (!TempFileOrErr)
@@ -671,6 +673,18 @@ Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {
671673
if (Error E = Output->commit())
672674
return E;
673675
Files.emplace_back(Args.MakeArgString(*TempFileOrErr));
676+
#else
677+
SmallString<128> TempFile;
678+
if (std::error_code EC = sys::fs::getPotentiallyUniqueTempFileName(
679+
sys::path::stem(Input->getBufferIdentifier()), "cubin", TempFile))
680+
reportError(createFileError(TempFile, EC));
681+
if (std::error_code EC =
682+
sys::fs::create_link(Input->getBufferIdentifier(), TempFile)) {
683+
reportError(createFileError(TempFile, EC));
684+
}
685+
Files.emplace_back(Args.MakeArgString(TempFile));
686+
TempFiles.emplace_back(std::move(TempFile));
687+
#endif
674688
}
675689

676690
return Files;

0 commit comments

Comments
 (0)