Skip to content

Commit

Permalink
[LinkerWrapper] Correctly handle multiple image wrappers (#67679)
Browse files Browse the repository at this point in the history
Summary:
We use these image wrappers to do runtime specifica registration of
variables and to load the device image that was compiled. This was
intended to support multiple of these running at the same time, e.g. you
can have a CUDA instance running with OpenMP and they should both
function so long as you do not share state between the two. However,
because we did not use a unique name for this file it would cause
conflicts when included. This patch names the image based off of the
language runtime it's using so that they remain separate.

Fixes: #67583
  • Loading branch information
jhuber6 committed Sep 28, 2023
1 parent c43aa46 commit 7485d36
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions clang/test/Driver/linker-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
// CUDA: clang{{.*}} -o [[IMG_SM52:.+]] --target=nvptx64-nvidia-cuda -march=sm_52
// CUDA: clang{{.*}} -o [[IMG_SM70:.+]] --target=nvptx64-nvidia-cuda -march=sm_70
// CUDA: fatbinary{{.*}}-64 --create {{.*}}.fatbin --image=profile=sm_70,file=[[IMG_SM70]] --image=profile=sm_52,file=[[IMG_SM52]]
// CUDA: usr/bin/ld{{.*}} {{.*}}.openmp.image.{{.*}}.o {{.*}}.cuda.image.{{.*}}.o

// RUN: clang-offload-packager -o %t.out \
// RUN: --image=file=%t.elf.o,kind=cuda,triple=nvptx64-nvidia-cuda,arch=sm_80 \
Expand Down
10 changes: 6 additions & 4 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ Expected<StringRef> writeOffloadFile(const OffloadFile &File) {

// Compile the module to an object file using the appropriate target machine for
// the host triple.
Expected<StringRef> compileModule(Module &M) {
Expected<StringRef> compileModule(Module &M, OffloadKind Kind) {
llvm::TimeTraceScope TimeScope("Compile module");
std::string Msg;
const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
Expand All @@ -829,8 +829,10 @@ Expected<StringRef> compileModule(Module &M) {
M.setDataLayout(TM->createDataLayout());

int FD = -1;
auto TempFileOrErr = createOutputFile(
sys::path::filename(ExecutableName) + ".image.wrapper", "o");
auto TempFileOrErr =
createOutputFile(sys::path::filename(ExecutableName) + "." +
getOffloadKindName(Kind) + ".image.wrapper",
"o");
if (!TempFileOrErr)
return TempFileOrErr.takeError();
if (std::error_code EC = sys::fs::openFileForWrite(*TempFileOrErr, FD))
Expand Down Expand Up @@ -902,7 +904,7 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
WriteBitcodeToFile(M, OS);
}

auto FileOrErr = compileModule(M);
auto FileOrErr = compileModule(M, Kind);
if (!FileOrErr)
return FileOrErr.takeError();
return *FileOrErr;
Expand Down

0 comments on commit 7485d36

Please sign in to comment.