Repository navigation
[clang-nvlink-wrapper] Keep LTO task outputs distinct #238883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| 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 | ||
| } |
| 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; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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"))); | ||
|
|
@@ -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"); | ||
| 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(); | ||
|
|
||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A mutex shouldn't be necessary.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> { | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Referenced this from the existing LTO handling in StringRef Stem = sys::path::stem(Args.getLastArgValue(OPT_o, "a.out"));
auto TempFileOrErr = createTempFile(
Args, Stem + (Task == 0 ? Twine("") : Twine('.') + Twine(Task)), "s")
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched to the lld scheme: task 0 gets |
||
| 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); | ||
|
|
@@ -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); | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.