[PATCH] D116543: [OpenMP] Embed device files into the host IR

Joseph Huber via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 3 09:59:32 PST 2022


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, gregrodgers, JonChesterfield, ronlieb.
Herald added subscribers: guansong, yaxunl.
jhuber6 requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

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>`.

Depends on D116542 <https://reviews.llvm.org/D116542>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116543

Files:
  clang/lib/Driver/ToolChains/Clang.cpp


Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -4357,9 +4357,9 @@
       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.
@@ -4376,8 +4376,8 @@
       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");
     }
@@ -6870,6 +6870,32 @@
     }
   }
 
+  // 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()) {
+    SmallString<128> InputFiles("-fembed-offload-binary=");
+    SmallString<128> InputSections("-fembed-offload-section=");
+
+    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);
+      InputSections += TC->getTripleString() + ".";
+      InputSections += TCArgs.getLastArgValue(options::OPT_march_EQ);
+      InputSections += ",";
+
+      InputFiles += C.getArgs().MakeArgString(TC->getInputFilename(*InputFile));
+      InputFiles += ",";
+    }
+    InputSections.pop_back();
+    InputFiles.pop_back();
+
+    CmdArgs.push_back(Args.MakeArgString(InputFiles.str()));
+    CmdArgs.push_back(Args.MakeArgString(InputSections.str()));
+  }
+
   if (Triple.isAMDGPU()) {
     handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116543.397092.patch
Type: text/x-patch
Size: 2251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220103/b40c8a47/attachment.bin>


More information about the cfe-commits mailing list