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

Currently we use the `-fembed-offload-object` option to embed a binary
file into the host as a named section. This is currently only used as a
codegen action, meaning we only handle this option correctly when the
input is a bitcode file. This patch adds the same handling to embed an
offloading object after we complete code generation. This allows us to
embed the object correctly if the input file is source or bitcode.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120270

Files:
  clang/include/clang/CodeGen/BackendUtil.h
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/lib/CodeGen/CodeGenModule.cpp


Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -43,6 +43,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Version.h"
+#include "clang/CodeGen/BackendUtil.h"
 #include "clang/CodeGen/ConstantInitBuilder.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -587,6 +588,9 @@
     EmitModuleLinkOptions();
   }
 
+  // If there is device offloading code embed it in the host now.
+  EmbedObject(getModule(), CodeGenOpts, getDiags());
+
   // On ELF we pass the dependent library specifiers directly to the linker
   // without manipulating them. This is in contrast to other platforms where
   // they are mapped to a specific linker option by the compiler. This
Index: clang/lib/CodeGen/CodeGenAction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenAction.cpp
+++ clang/lib/CodeGen/CodeGenAction.cpp
@@ -1134,7 +1134,7 @@
     TheModule->setTargetTriple(TargetOpts.Triple);
   }
 
-  EmbedObject(TheModule.get(), CodeGenOpts, Diagnostics);
+  EmbedObject(*TheModule.get(), CodeGenOpts, Diagnostics);
   EmbedBitcode(TheModule.get(), CodeGenOpts, *MainFile);
 
   LLVMContext &Ctx = TheModule->getContext();
Index: clang/lib/CodeGen/BackendUtil.cpp
===================================================================
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -1753,7 +1753,7 @@
       CGOpts.CmdArgs);
 }
 
-void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
+void clang::EmbedObject(llvm::Module &M, const CodeGenOptions &CGOpts,
                         DiagnosticsEngine &Diags) {
   if (CGOpts.OffloadObjects.empty())
     return;
@@ -1771,12 +1771,12 @@
     if (std::error_code EC = ObjectOrErr.getError()) {
       auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
                                           "could not open '%0' for embedding");
-      Diags.Report(DiagID) << std::get<0>(FilenameAndSection);
+      Diags.Report(DiagID) << FilenameAndSection.first;
       return;
     }
 
     SmallString<128> SectionName(
-        {".llvm.offloading.", std::get<1>(FilenameAndSection)});
-    llvm::embedBufferInModule(*M, **ObjectOrErr, SectionName);
+        {".llvm.offloading.", FilenameAndSection.second});
+    llvm::embedBufferInModule(M, **ObjectOrErr, SectionName);
   }
 }
Index: clang/include/clang/CodeGen/BackendUtil.h
===================================================================
--- clang/include/clang/CodeGen/BackendUtil.h
+++ clang/include/clang/CodeGen/BackendUtil.h
@@ -45,7 +45,7 @@
   void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
                     llvm::MemoryBufferRef Buf);
 
-  void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
+  void EmbedObject(llvm::Module &M, const CodeGenOptions &CGOpts,
                    DiagnosticsEngine &Diags);
 }
 


Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -43,6 +43,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Version.h"
+#include "clang/CodeGen/BackendUtil.h"
 #include "clang/CodeGen/ConstantInitBuilder.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -587,6 +588,9 @@
     EmitModuleLinkOptions();
   }
 
+  // If there is device offloading code embed it in the host now.
+  EmbedObject(getModule(), CodeGenOpts, getDiags());
+
   // On ELF we pass the dependent library specifiers directly to the linker
   // without manipulating them. This is in contrast to other platforms where
   // they are mapped to a specific linker option by the compiler. This
Index: clang/lib/CodeGen/CodeGenAction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenAction.cpp
+++ clang/lib/CodeGen/CodeGenAction.cpp
@@ -1134,7 +1134,7 @@
     TheModule->setTargetTriple(TargetOpts.Triple);
   }
 
-  EmbedObject(TheModule.get(), CodeGenOpts, Diagnostics);
+  EmbedObject(*TheModule.get(), CodeGenOpts, Diagnostics);
   EmbedBitcode(TheModule.get(), CodeGenOpts, *MainFile);
 
   LLVMContext &Ctx = TheModule->getContext();
Index: clang/lib/CodeGen/BackendUtil.cpp
===================================================================
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -1753,7 +1753,7 @@
       CGOpts.CmdArgs);
 }
 
-void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
+void clang::EmbedObject(llvm::Module &M, const CodeGenOptions &CGOpts,
                         DiagnosticsEngine &Diags) {
   if (CGOpts.OffloadObjects.empty())
     return;
@@ -1771,12 +1771,12 @@
     if (std::error_code EC = ObjectOrErr.getError()) {
       auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
                                           "could not open '%0' for embedding");
-      Diags.Report(DiagID) << std::get<0>(FilenameAndSection);
+      Diags.Report(DiagID) << FilenameAndSection.first;
       return;
     }
 
     SmallString<128> SectionName(
-        {".llvm.offloading.", std::get<1>(FilenameAndSection)});
-    llvm::embedBufferInModule(*M, **ObjectOrErr, SectionName);
+        {".llvm.offloading.", FilenameAndSection.second});
+    llvm::embedBufferInModule(M, **ObjectOrErr, SectionName);
   }
 }
Index: clang/include/clang/CodeGen/BackendUtil.h
===================================================================
--- clang/include/clang/CodeGen/BackendUtil.h
+++ clang/include/clang/CodeGen/BackendUtil.h
@@ -45,7 +45,7 @@
   void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
                     llvm::MemoryBufferRef Buf);
 
-  void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
+  void EmbedObject(llvm::Module &M, const CodeGenOptions &CGOpts,
                    DiagnosticsEngine &Diags);
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to