Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[clang-nvlink-wrapper] Name task outputs like lld and derive cubin names
Name each LTO task's assembly like lld (`<stem>.s`, then `<stem>.<task>.s`)
and name the cubin after the assembly it comes from, so runPTXAs keeps its
signature. Lock createTempFile itself, as clang-linker-wrapper does, and
keep TempFiles in a std::list so returned references stay valid, instead
of a mutex at the call site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
0z5a and claude committed Nov 10, 2026
commit 56ceb0210cd7adbdb8af33288cf94cab6eaffbd6
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
; RUN: cd %t.dir && clang-nvlink-wrapper --dry-run --save-temps --plugin-opt=lto-partitions=2 -arch sm_90 %t.bc -o multi 2>&1 | FileCheck %s --check-prefix=MULTI
; RUN: cd %t.dir && clang-nvlink-wrapper --dry-run --save-temps -arch sm_90 %t.bc -o single 2>&1 | FileCheck %s --check-prefix=SINGLE
;
; MULTI: ptxas{{.*}} -c multi.0.s {{.*}} -o multi.0.cubin
; MULTI: ptxas{{.*}} -c multi.s {{.*}} -o multi.cubin
; MULTI: ptxas{{.*}} -c multi.1.s {{.*}} -o multi.1.cubin
; MULTI: nvlink{{.*}} -o multi multi.0.cubin multi.1.cubin
; MULTI: nvlink{{.*}} -o multi multi.cubin multi.1.cubin
; SINGLE: ptxas{{.*}} -c single.s {{.*}} -o single.cubin
; SINGLE: nvlink{{.*}} -o single single.cubin

Expand Down
31 changes: 14 additions & 17 deletions clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"

#include <list>
#include <mutex>

using namespace llvm;
Expand Down Expand Up @@ -96,8 +97,11 @@ static void printVersion(raw_ostream &OS) {
/// The value of `argv[0]` when run.
static const char *Executable;

/// Mutex lock to protect writes to shared TempFiles in parallel.
static std::mutex TempFilesMutex;

/// Temporary files to be cleaned up.
static SmallVector<SmallString<128>> TempFiles;
static std::list<SmallString<128>> TempFiles;

/// Codegen flags for LTO backend.
static codegen::RegisterCodeGenFlags CodeGenFlags;
Expand Down Expand Up @@ -173,6 +177,7 @@ bool hasFatBinary(const ArgList &Args, MemoryBufferRef Buffer) {

Expected<StringRef> createTempFile(const ArgList &Args, const Twine &Prefix,
StringRef Extension) {
std::scoped_lock<decltype(TempFilesMutex)> Lock(TempFilesMutex);
SmallString<128> OutputFile;
if (Args.hasArg(OPT_save_temps)) {
(Prefix + "." + Extension).toNullTerminatedStringRef(OutputFile);
Expand Down Expand Up @@ -329,8 +334,7 @@ struct Symbol {
bool UsedInRegularObj;
};

Expected<StringRef> runPTXAs(StringRef File, const ArgList &Args, size_t Task,
size_t MaxTasks) {
Expected<StringRef> runPTXAs(StringRef File, const ArgList &Args) {
SmallVector<StringRef, 1> SearchPaths;
if (Arg *A = Args.getLastArg(OPT_cuda_path_EQ))
SearchPaths.push_back(Args.MakeArgString(A->getValue() + Twine("/bin")));
Expand All @@ -345,11 +349,7 @@ Expected<StringRef> runPTXAs(StringRef File, const ArgList &Args, size_t Task,
return createStringError(
"must pass in an explicit nvptx64 gpu architecture to 'ptxas'");

std::string Prefix =
sys::path::stem(Args.getLastArgValue(OPT_o, "a.out")).str();
if (MaxTasks > 1)
Prefix += "." + std::to_string(Task);
auto TempFileOrErr = createTempFile(Args, Prefix, "cubin");
auto TempFileOrErr = createTempFile(Args, sys::path::stem(File), "cubin");
if (!TempFileOrErr)
return TempFileOrErr.takeError();

Expand Down Expand Up @@ -691,7 +691,6 @@ getInput(const cl::LibraryOptionsContext &LibraryOptions, const ArgList &Args) {
// Run the LTO job to compile the bitcode.
size_t MaxTasks = LTOBackend.getMaxTasks();
SmallVector<StringRef> LTOFiles(MaxTasks);
std::mutex TempFilesMutex;
auto AddStream =
[&](size_t Task,
const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
Expand All @@ -700,12 +699,10 @@ getInput(const cl::LibraryOptionsContext &LibraryOptions, const ArgList &Args) {
if (Args.hasArg(OPT_lto_emit_asm))
TempFile = Args.getLastArgValue(OPT_o, "a.out");
else {
std::lock_guard<std::mutex> Lock(TempFilesMutex);
std::string Prefix =
sys::path::stem(Args.getLastArgValue(OPT_o, "a.out")).str();
if (MaxTasks > 1)
Prefix += "." + std::to_string(Task);
auto TempFileOrErr = createTempFile(Args, Prefix, "s");
StringRef Stem = sys::path::stem(Args.getLastArgValue(OPT_o, "a.out"));
auto TempFileOrErr = createTempFile(
Args, Stem + (Task == 0 ? Twine("") : Twine('.') + Twine(Task)),
"s");
if (!TempFileOrErr)
reportError(TempFileOrErr.takeError());
TempFile = Args.MakeArgString(*TempFileOrErr);
Expand All @@ -722,10 +719,10 @@ getInput(const cl::LibraryOptionsContext &LibraryOptions, const ArgList &Args) {
if (Args.hasArg(OPT_lto_emit_llvm) || Args.hasArg(OPT_lto_emit_asm))
return Files;

for (auto [Task, LTOFile] : enumerate(LTOFiles)) {
for (StringRef LTOFile : LTOFiles) {
if (LTOFile.empty())
continue;
auto FileOrErr = runPTXAs(LTOFile, Args, Task, MaxTasks);
auto FileOrErr = runPTXAs(LTOFile, Args);
if (!FileOrErr)
return FileOrErr.takeError();
Files.emplace_back(*FileOrErr);
Expand Down
Loading