Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.s {{.*}} -o multi.cubin
; MULTI: ptxas{{.*}} -c multi.1.s {{.*}} -o 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

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; }
18 changes: 14 additions & 4 deletions clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"

#include <list>
#include <mutex>

using namespace llvm;
using namespace llvm::opt;
using namespace llvm::object;
Expand Down Expand Up @@ -94,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 @@ -171,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 @@ -342,8 +349,7 @@ 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.

auto TempFileOrErr = createTempFile(Args, sys::path::stem(File), "cubin");
if (!TempFileOrErr)
return TempFileOrErr.takeError();

Expand Down Expand Up @@ -693,8 +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 {
StringRef Stem = sys::path::stem(Args.getLastArgValue(OPT_o, "a.out"));
auto TempFileOrErr = createTempFile(
Args, sys::path::stem(Args.getLastArgValue(OPT_o, "a.out")), "s");
Args, Stem + (Task == 0 ? Twine("") : Twine('.') + Twine(Task)),
"s");
if (!TempFileOrErr)
reportError(TempFileOrErr.takeError());
TempFile = Args.MakeArgString(*TempFileOrErr);
Expand All @@ -712,6 +720,8 @@ getInput(const cl::LibraryOptionsContext &LibraryOptions, const ArgList &Args) {
return Files;

for (StringRef LTOFile : LTOFiles) {
if (LTOFile.empty())
continue;
auto FileOrErr = runPTXAs(LTOFile, Args);
if (!FileOrErr)
return FileOrErr.takeError();
Expand Down
Loading