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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Verify that compile-opts/link-opts recorded in an OpenMP SPIR-V device
// image's offload metadata survive clang-linker-wrapper instead of being
// silently dropped when the image is re-containerized for the OpenMP JIT
// runtime.
// REQUIRES: system-linux
// REQUIRES: spirv-registered-target
// REQUIRES: x86-registered-target

// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -emit-llvm-bc -o %t.bc
// RUN: %clang_cc1 %s -triple spirv64-intel -fopenmp-is-target-device -emit-obj -o %t.device.o
// RUN: llvm-offload-binary -o %t.bundle "--image=file=%t.device.o,triple=spirv64-intel,arch=generic,kind=openmp,compile-opts=-cl-opt-disable,link-opts=-cl-take-global-address"
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-obj -fembed-offload-object=%t.bundle -x ir %t.bc -o %t.host.o
// RUN: clang-linker-wrapper --dry-run --print-wrapped-module -host-triple=x86_64-unknown-linux-gnu %t.host.o -o %t.out --linker-path="/usr/bin/ld" > %t.ir 2>&1
//
// The embedded strings are serialized through a string-table builder that
// may reorder them relative to each other, so each is checked with a
// separate FileCheck run rather than relying on their relative order.
// RUN: FileCheck %s --input-file=%t.ir --check-prefix=COMPILE-OPTS
Comment thread
bviyer marked this conversation as resolved.
Resolved
// RUN: FileCheck %s --input-file=%t.ir --check-prefix=LINK-OPTS

// COMPILE-OPTS: @.omp_offloading.device_image = {{.*}}compile-opts{{.*}}-cl-opt-disable
// LINK-OPTS: @.omp_offloading.device_image = {{.*}}link-opts{{.*}}-cl-take-global-address
32 changes: 24 additions & 8 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2447,12 +2447,14 @@ Expected<StringRef> linkDevice(ArrayRef<StringRef> InputFiles,
}

Error containerizeRawImage(std::unique_ptr<MemoryBuffer> &Img, OffloadKind Kind,
const ArgList &Args) {
const ArgList &Args, StringRef CompileOpts = "",
StringRef LinkOpts = "") {
llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ));
if (Kind == OFK_OpenMP && Triple.isSPIRV() &&
Triple.getVendor() == llvm::Triple::Intel &&
!Args.hasArg(OPT_embed_bitcode))
return offloading::intel::containerizeOpenMPSPIRVImage(Img, Triple);
return offloading::intel::containerizeOpenMPSPIRVImage(
Img, Triple, CompileOpts, LinkOpts);
return Error::success();
}

Expand Down Expand Up @@ -2914,11 +2916,12 @@ Error handleOverrideImages(
return Error::success();
}

/// Function looks for compile/link options encoded in SYCL images. If they are
/// found, then they are returned. If it is found that images have different
/// compile options or different link options, then an error is returned.
/// Function looks for compile/link options encoded in offload images. If they
/// are found, then they are returned. If it is found that images have
/// different compile options or different link options, then an error is
/// returned.
Expected<std::pair<std::string, std::string>>
extractSYCLCompileLinkOptions(ArrayRef<OffloadFile> OffloadFiles) {
extractCompileLinkOptions(ArrayRef<OffloadFile> OffloadFiles) {
if (OffloadFiles.size() == 0)
return std::pair<std::string, std::string>{};

Expand Down Expand Up @@ -3007,7 +3010,7 @@ linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
if (HasSYCLOffloadKind) {
auto LinkerArgs = getLinkerArgs(Input, BaseArgs, OFK_SYCL);
Expected<std::pair<std::string, std::string>> CompileLinkOptionsOrErr =
extractSYCLCompileLinkOptions(Input);
extractCompileLinkOptions(Input);
if (!CompileLinkOptionsOrErr)
return CompileLinkOptionsOrErr.takeError();

Expand Down Expand Up @@ -3136,6 +3139,17 @@ linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
InputFiles.emplace_back(*FileNameOrErr);
}

// Recover the compile/link options the driver recorded in each input's
// offload metadata; the device linker invocation below does not
// preserve them in its output, so they must be forwarded explicitly
// when containerizing below.
Expected<std::pair<std::string, std::string>> CompileLinkOptionsOrErr =
extractCompileLinkOptions(Input);
if (!CompileLinkOptionsOrErr)
return CompileLinkOptionsOrErr.takeError();
std::pair<std::string, std::string> &CompileLinkOptions =
*CompileLinkOptionsOrErr;

// Link the remaining device files using the device linker.
auto OutputOrErr =
linkDevice(InputFiles, LinkerArgs, ActiveOffloadKindMask);
Expand All @@ -3157,7 +3171,9 @@ linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
}

// Manually containerize offloading images not in ELF format.
if (Error E = containerizeRawImage(*FileOrErr, Kind, LinkerArgs))
if (Error E = containerizeRawImage(*FileOrErr, Kind, LinkerArgs,
CompileLinkOptions.first,
CompileLinkOptions.second))
return E;

std::scoped_lock<decltype(ImageMtx)> Guard(ImageMtx);
Expand Down
Loading