[clang] 12ae095 - [OpenMP] Embed device files into the host IR
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 31 12:56:18 PST 2022
Author: Joseph Huber
Date: 2022-01-31T15:56:02-05:00
New Revision: 12ae095bbb639c3dda9607a57487b83ccec9d246
URL: https://github.com/llvm/llvm-project/commit/12ae095bbb639c3dda9607a57487b83ccec9d246
DIFF: https://github.com/llvm/llvm-project/commit/12ae095bbb639c3dda9607a57487b83ccec9d246.diff
LOG: [OpenMP] Embed device files into the host IR
This patch adds support for embedding the device object files into the
host IR to create a fat binary. Each offloading file will be inserted
into a section with the following naming format
`.llvm.offloading.<triple>.<arch>.<filename>`.
Depends on D116542
Reviewed By: JonChesterfield
Differential Revision: https://reviews.llvm.org/D116543
Added:
Modified:
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/openmp-offload-gpu.c
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index dfe81954a3be..66943302388d 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4370,9 +4370,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
IsHeaderModulePrecompile ? HeaderModuleInput : Inputs[0];
InputInfoList ModuleHeaderInputs;
+ InputInfoList OpenMPHostInputs;
const InputInfo *CudaDeviceInput = nullptr;
const InputInfo *OpenMPDeviceInput = nullptr;
- const InputInfo *OpenMPHostInput = nullptr;
for (const InputInfo &I : Inputs) {
if (&I == &Input) {
// This is the primary input.
@@ -4389,8 +4389,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CudaDeviceInput = &I;
} else if (IsOpenMPDevice && !OpenMPDeviceInput) {
OpenMPDeviceInput = &I;
- } else if (IsOpenMPHost && !OpenMPHostInput) {
- OpenMPHostInput = &I;
+ } else if (IsOpenMPHost) {
+ OpenMPHostInputs.push_back(I);
} else {
llvm_unreachable("unexpectedly given multiple inputs");
}
@@ -6897,6 +6897,25 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}
+ // Host-side OpenMP offloading recieves the device object files and embeds it
+ // in a named section including the associated target triple and architecture.
+ if (IsOpenMPHost && !OpenMPHostInputs.empty()) {
+ auto InputFile = OpenMPHostInputs.begin();
+ auto OpenMPTCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
+ for (auto TI = OpenMPTCs.first, TE = OpenMPTCs.second; TI != TE;
+ ++TI, ++InputFile) {
+ const ToolChain *TC = TI->second;
+ const ArgList &TCArgs = C.getArgsForToolChain(TC, "", Action::OFK_OpenMP);
+ StringRef File =
+ C.getArgs().MakeArgString(TC->getInputFilename(*InputFile));
+ StringRef InputName = Clang::getBaseInputStem(Args, Inputs);
+
+ CmdArgs.push_back(Args.MakeArgString(
+ "-fembed-offload-object=" + File + "," + TC->getTripleString() + "." +
+ TCArgs.getLastArgValue(options::OPT_march_EQ) + "." + InputName));
+ }
+ }
+
if (Triple.isAMDGPU()) {
handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
diff --git a/clang/test/Driver/openmp-offload-gpu.c b/clang/test/Driver/openmp-offload-gpu.c
index 7489d471c525..b031740a7d92 100644
--- a/clang/test/Driver/openmp-offload-gpu.c
+++ b/clang/test/Driver/openmp-offload-gpu.c
@@ -360,3 +360,10 @@
// NEW_DRIVER: "nvptx64-nvidia-cuda" - "NVPTX::Assembler", inputs: ["[[DEVICE_ASM]]"], output: "[[DEVICE_OBJ:.+]]"
// NEW_DRIVER: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HOST_BC]]", "[[DEVICE_OBJ]]"], output: "[[HOST_OBJ:.+]]"
// NEW_DRIVER: "x86_64-unknown-linux-gnu" - "[[LINKER:.+]]", inputs: ["[[HOST_OBJ]]"], output: "openmp-offload-gpu"
+
+// RUN: %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target=nvptx64-nvida-cuda -march=sm_70 \
+// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-new-nvptx-test.bc \
+// 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
More information about the cfe-commits
mailing list