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
Next Next commit
[clang-nvlink-wrapper] Keep LTO task outputs distinct
  • Loading branch information
0z5a committed Nov 4, 2026
commit 426f387d0b390bbe9ca1e1e99e7756137a9bea1d
25 changes: 25 additions & 0 deletions clang/test/OffloadTools/clang-nvlink-wrapper/lto-save-temps.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; REQUIRES: nvptx-registered-target
;
; RUN: llvm-as %s -o %t.bc
; RUN: mkdir -p %t.dir
; 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.1.s {{.*}} -o multi.1.cubin
; MULTI: nvlink{{.*}} -o multi multi.0.cubin multi.1.cubin
; SINGLE: ptxas{{.*}} -c single.s {{.*}} -o single.cubin
; SINGLE: nvlink{{.*}} -o single single.cubin

target datalayout = "e-p6:32:32-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64"
target triple = "nvptx64-nvidia-cuda"

define i32 @foo(i32 %x) {
%r = add i32 %x, 1
ret i32 %r
}

define i32 @bar(i32 %x) {
%r = mul i32 %x, 3
ret i32 %r
}
10 changes: 10 additions & 0 deletions clang/test/OffloadTools/clang-nvlink-wrapper/thinlto-save-temps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// REQUIRES: nvptx-registered-target
// RUN: %clang -cc1 -triple nvptx64-nvidia-cuda -flto=thin -funified-lto -emit-llvm-bc %s -o %t.bc
// RUN: mkdir -p %t.dir
// RUN: cd %t.dir && clang-nvlink-wrapper --dry-run --save-temps --plugin-opt=thinlto -arch sm_90 %t.bc -o thin 2>&1 | FileCheck %s
// CHECK-NOT: ptxas{{.*}} -c -O
// CHECK: ptxas{{.*}} -c thin.1.s {{.*}} -o thin.1.cubin
// CHECK-NEXT: {{.*}}nvlink{{.*}} -o thin thin.1.cubin

int foo(int x) { return x + 1; }
int bar(int x) { return x * 3; }
27 changes: 20 additions & 7 deletions clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"

#include <mutex>

using namespace llvm;
using namespace llvm::opt;
using namespace llvm::object;
Expand Down Expand Up @@ -327,7 +329,8 @@ struct Symbol {
bool UsedInRegularObj;
};

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

auto TempFileOrErr = createTempFile(
Args, sys::path::stem(Args.getLastArgValue(OPT_o, "a.out")), "cubin");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to instead derive its name from the input file (i.e. foo.s -> foo.cubin). That should fix this and make the signature changes unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 56ceb02: runPTXAs names the cubin after its input (foo.s -> foo.cubin) and keeps its old signature.

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");
if (!TempFileOrErr)
return TempFileOrErr.takeError();

Expand Down Expand Up @@ -685,6 +691,7 @@ 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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A mutex shouldn't be necessary. createTempFile is intended to be thread safe on its own and everything else should have an index in an array of size MaxTasks like above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In clang-linker-wrapper it is: createOutputFile takes TempFilesMutex and TempFiles is a std::list. The copy here appends to a SmallVector without a lock, and AddStream runs on the LTO codegen thread pool (splitCodeGen), so two tasks can push to TempFiles at once. I moved the lock into createTempFile and made TempFiles a std::list to match, and dropped the mutex at the call site.

auto AddStream =
[&](size_t Task,
const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
Expand All @@ -693,8 +700,12 @@ getInput(const cl::LibraryOptionsContext &LibraryOptions, const ArgList &Args) {
if (Args.hasArg(OPT_lto_emit_asm))
TempFile = Args.getLastArgValue(OPT_o, "a.out");
else {
auto TempFileOrErr = createTempFile(
Args, sys::path::stem(Args.getLastArgValue(OPT_o, "a.out")), "s");
Comment on lines -696 to -697

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referenced this from the existing LTO handling in lld.

  StringRef Stem = sys::path::stem(Args.getLastArgValue(OPT_o, "a.out"));
  auto TempFileOrErr = createTempFile(
      Args, Stem + (Task == 0 ? Twine("") : Twine('.') + Twine(Task)), "s")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to the lld scheme: task 0 gets <stem>.s, task N gets <stem>.N.s. The tests now expect multi.s/multi.cubin and multi.1.s/multi.1.cubin.

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");
if (!TempFileOrErr)
reportError(TempFileOrErr.takeError());
TempFile = Args.MakeArgString(*TempFileOrErr);
Expand All @@ -711,8 +722,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 (StringRef LTOFile : LTOFiles) {
auto FileOrErr = runPTXAs(LTOFile, Args);
for (auto [Task, LTOFile] : enumerate(LTOFiles)) {
if (LTOFile.empty())
continue;
auto FileOrErr = runPTXAs(LTOFile, Args, Task, MaxTasks);
if (!FileOrErr)
return FileOrErr.takeError();
Files.emplace_back(*FileOrErr);
Expand Down