Skip to content

Commit

Permalink
[Clang] Add offload kind to embedded offload object
Browse files Browse the repository at this point in the history
This patch adds the offload kind to the embedded section name in
preparation for offloading to different kinda like CUDA or HIP.

Depends on D120288

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D120271
  • Loading branch information
jhuber6 committed Mar 15, 2022
1 parent 06b336c commit 9f89769
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
11 changes: 7 additions & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -6977,7 +6977,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
StringRef InputName = Clang::getBaseInputStem(Args, Inputs);

CmdArgs.push_back(Args.MakeArgString(
"-fembed-offload-object=" + File + "," + TC->getTripleString() + "." +
"-fembed-offload-object=" + File + "," +
Action::GetOffloadKindName(Action::OFK_OpenMP) + "." +
TC->getTripleString() + "." +
TCArgs.getLastArgValue(options::OPT_march_EQ) + "." + InputName));
}
}
Expand Down Expand Up @@ -8297,9 +8299,10 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
TC->getTriple());

if (!BitcodeLibrary.empty())
CmdArgs.push_back(
Args.MakeArgString("-target-library=" + TC->getTripleString() +
"-" + Arch + "=" + BitcodeLibrary.back()));
CmdArgs.push_back(Args.MakeArgString(
"-target-library=" +
Action::GetOffloadKindName(Action::OFK_OpenMP) + "-" +
TC->getTripleString() + "-" + Arch + "=" + BitcodeLibrary.back()));
}

// Pass in the optimization level to use for LTO.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Driver/openmp-offload-gpu.c
Expand Up @@ -345,4 +345,4 @@
// RUN: -fopenmp-new-driver -no-canonical-prefixes -nogpulib %s -o openmp-offload-gpu 2>&1 \
// RUN: | FileCheck -check-prefix=NEW_DRIVER_EMBEDDING %s

// NEW_DRIVER_EMBEDDING: -fembed-offload-object=[[CUBIN:.*\.cubin]],nvptx64-nvidia-cuda.sm_70
// NEW_DRIVER_EMBEDDING: -fembed-offload-object=[[CUBIN:.*\.cubin]],openmp.nvptx64-nvidia-cuda.sm_70
43 changes: 25 additions & 18 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Expand Up @@ -151,9 +151,11 @@ static codegen::RegisterCodeGenFlags CodeGenFlags;

/// Information for a device offloading file extracted from the host.
struct DeviceFile {
DeviceFile(StringRef TheTriple, StringRef Arch, StringRef Filename)
: TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}
DeviceFile(StringRef Kind, StringRef TheTriple, StringRef Arch,
StringRef Filename)
: Kind(Kind), TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}

std::string Kind;
std::string TheTriple;
std::string Arch;
std::string Filename;
Expand All @@ -164,11 +166,13 @@ namespace llvm {
template <> struct DenseMapInfo<DeviceFile> {
static DeviceFile getEmptyKey() {
return {DenseMapInfo<StringRef>::getEmptyKey(),
DenseMapInfo<StringRef>::getEmptyKey(),
DenseMapInfo<StringRef>::getEmptyKey(),
DenseMapInfo<StringRef>::getEmptyKey()};
}
static DeviceFile getTombstoneKey() {
return {DenseMapInfo<StringRef>::getTombstoneKey(),
DenseMapInfo<StringRef>::getTombstoneKey(),
DenseMapInfo<StringRef>::getTombstoneKey(),
DenseMapInfo<StringRef>::getTombstoneKey()};
}
Expand Down Expand Up @@ -213,12 +217,13 @@ std::string getMainExecutable(const char *Name) {
return sys::path::parent_path(COWPath).str();
}

/// Extract the device file from the string '<triple>-<arch>=<library>.bc'.
/// Extract the device file from the string '<kind>-<triple>-<arch>=<library>'.
DeviceFile getBitcodeLibrary(StringRef LibraryStr) {
auto DeviceAndPath = StringRef(LibraryStr).split('=');
auto TripleAndArch = DeviceAndPath.first.rsplit('-');
return DeviceFile(TripleAndArch.first, TripleAndArch.second,
DeviceAndPath.second);
auto StringAndArch = DeviceAndPath.first.rsplit('-');
auto KindAndTriple = StringAndArch.first.split('-');
return DeviceFile(KindAndTriple.first, KindAndTriple.second,
StringAndArch.second, DeviceAndPath.second);
}

/// Get a temporary filename suitable for output.
Expand Down Expand Up @@ -299,16 +304,17 @@ extractFromBinary(const ObjectFile &Obj,

SmallVector<StringRef, 4> SectionFields;
Name->split(SectionFields, '.');
StringRef DeviceTriple = SectionFields[3];
StringRef Arch = SectionFields[4];
StringRef Kind = SectionFields[3];
StringRef DeviceTriple = SectionFields[4];
StringRef Arch = SectionFields[5];

if (Expected<StringRef> Contents = Sec.getContents()) {
SmallString<128> TempFile;
StringRef DeviceExtension = getDeviceFileExtension(
DeviceTriple, identify_magic(*Contents) == file_magic::bitcode);
if (Error Err =
createOutputFile(Prefix + "-device-" + DeviceTriple + "-" + Arch,
DeviceExtension, TempFile))
if (Error Err = createOutputFile(Prefix + "-" + Kind + "-" +
DeviceTriple + "-" + Arch,
DeviceExtension, TempFile))
return std::move(Err);

Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
Expand All @@ -320,7 +326,7 @@ extractFromBinary(const ObjectFile &Obj,
if (Error E = Output->commit())
return std::move(E);

DeviceFiles.emplace_back(DeviceTriple, Arch, TempFile);
DeviceFiles.emplace_back(Kind, DeviceTriple, Arch, TempFile);
ToBeStripped.push_back(*Name);
}
}
Expand Down Expand Up @@ -412,16 +418,17 @@ extractFromBitcode(std::unique_ptr<MemoryBuffer> Buffer,

SmallVector<StringRef, 4> SectionFields;
GV.getSection().split(SectionFields, '.');
StringRef DeviceTriple = SectionFields[3];
StringRef Arch = SectionFields[4];
StringRef Kind = SectionFields[3];
StringRef DeviceTriple = SectionFields[4];
StringRef Arch = SectionFields[5];

StringRef Contents = CDS->getAsString();
SmallString<128> TempFile;
StringRef DeviceExtension = getDeviceFileExtension(
DeviceTriple, identify_magic(Contents) == file_magic::bitcode);
if (Error Err =
createOutputFile(Prefix + "-device-" + DeviceTriple + "-" + Arch,
DeviceExtension, TempFile))
if (Error Err = createOutputFile(Prefix + "-" + Kind + "-" + DeviceTriple +
"-" + Arch,
DeviceExtension, TempFile))
return std::move(Err);

Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
Expand All @@ -433,7 +440,7 @@ extractFromBitcode(std::unique_ptr<MemoryBuffer> Buffer,
if (Error E = Output->commit())
return std::move(E);

DeviceFiles.emplace_back(DeviceTriple, Arch, TempFile);
DeviceFiles.emplace_back(Kind, DeviceTriple, Arch, TempFile);
ToBeDeleted.push_back(&GV);
}

Expand Down

0 comments on commit 9f89769

Please sign in to comment.