jhuber6 updated this revision to Diff 403404. jhuber6 added a comment. Herald added a subscriber: mgorny.
Updating approach, use a vector of string pairs now. Multiple files are simply passed multiple times. Will add filename to the offloading section name laterf, as similar sections could be merged when performing a relocatable link. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D116542/new/ https://reviews.llvm.org/D116542 Files: clang/include/clang/Basic/CodeGenOptions.h clang/include/clang/CodeGen/BackendUtil.h clang/include/clang/Driver/Options.td clang/lib/CodeGen/BackendUtil.cpp clang/lib/CodeGen/CodeGenAction.cpp clang/test/Frontend/embed-object.ll llvm/include/llvm/Bitcode/BitcodeWriter.h llvm/lib/Bitcode/Writer/BitcodeWriter.cpp llvm/lib/Bitcode/Writer/CMakeLists.txt
Index: llvm/lib/Bitcode/Writer/CMakeLists.txt =================================================================== --- llvm/lib/Bitcode/Writer/CMakeLists.txt +++ llvm/lib/Bitcode/Writer/CMakeLists.txt @@ -11,6 +11,7 @@ Analysis Core MC + TransformUtils Object Support ) Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -69,6 +69,7 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/SHA1.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" #include <algorithm> #include <cassert> #include <cstddef> @@ -4972,3 +4973,18 @@ llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); NewUsed->setSection("llvm.metadata"); } + +void llvm::EmbedObjectInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, + StringRef SectionName) { + ArrayRef<char> ModuleData = ArrayRef<char>(Buf.getBufferStart(), Buf.getBufferSize()); + + // Embed the data in the + llvm::Constant *ModuleConstant = + llvm::ConstantDataArray::get(M.getContext(), ModuleData); + llvm::GlobalVariable *GV = new llvm::GlobalVariable( + M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, + ModuleConstant, "llvm.embedded.object"); + GV->setSection(SectionName); + + appendToCompilerUsed(M, GV); +} Index: llvm/include/llvm/Bitcode/BitcodeWriter.h =================================================================== --- llvm/include/llvm/Bitcode/BitcodeWriter.h +++ llvm/include/llvm/Bitcode/BitcodeWriter.h @@ -165,6 +165,11 @@ bool EmbedCmdline, const std::vector<uint8_t> &CmdArgs); + /// Embeds the memory buffer \p Buf into the module \p M as a global using the + /// section name \p SectionName. + void EmbedObjectInModule(Module &M, MemoryBufferRef Buf, + StringRef SectionName); + } // end namespace llvm #endif // LLVM_BITCODE_BITCODEWRITER_H Index: clang/test/Frontend/embed-object.ll =================================================================== --- /dev/null +++ clang/test/Frontend/embed-object.ll @@ -0,0 +1,13 @@ +; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm \ +; RUN: -fembed-offload-object=%S/Inputs/empty.h,section -x ir %s -o - \ +; RUN: | FileCheck %s -check-prefix=CHECK + +; CHECK: @llvm.embedded.object = private constant [0 x i8] zeroinitializer, section ".llvm.offloading.section" +; CHECK: @llvm.compiler.used = appending global [2 x i8*] [i8* @x, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @llvm.embedded.object, i32 0, i32 0)], section "llvm.metadata" + +@x = private constant i8 1 +@llvm.compiler.used = appending global [1 x i8*] [i8* @x], section "llvm.metadata" + +define i32 @foo() { + ret i32 0 +} Index: clang/lib/CodeGen/CodeGenAction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenAction.cpp +++ clang/lib/CodeGen/CodeGenAction.cpp @@ -1134,6 +1134,7 @@ TheModule->setTargetTriple(TargetOpts.Triple); } + EmbedBinary(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 @@ -1745,8 +1745,31 @@ llvm::MemoryBufferRef Buf) { if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) return; + llvm::EmbedBitcodeInModule( *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode, CGOpts.CmdArgs); } + +void clang::EmbedBinary(llvm::Module *M, const CodeGenOptions &CGOpts, + DiagnosticsEngine &Diags) { + if (CGOpts.OffloadObjects.empty()) + return; + + for (StringRef OffloadObject : CGOpts.OffloadObjects) { + auto FilenameAndSection = OffloadObject.split(','); + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr = + llvm::MemoryBuffer::getFileOrSTDIN(std::get<0>(FilenameAndSection)); + 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); + return; + } + + SmallString<128> SectionName( + {".llvm.offloading.", std::get<1>(FilenameAndSection)}); + llvm::EmbedObjectInModule(*M, **ObjectOrErr, SectionName); + } +} Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1150,6 +1150,10 @@ PosFlag<SetTrue, [CC1Option], "Enable support for the C++ Coroutines TS">, NegFlag<SetFalse>>; +def fembed_offload_binary_EQ : Joined<["-"], "fembed-offload-object=">, + Group<f_Group>, Flags<[NoXarchOption, CC1Option]>, + HelpText<"Embed Offloading device-side binary into host object file as a section.">, + MarshallingInfoStringVector<CodeGenOpts<"OffloadObjects">>; def fembed_bitcode_EQ : Joined<["-"], "fembed-bitcode=">, Group<f_Group>, Flags<[NoXarchOption, CC1Option, CC1AsOption]>, MetaVarName<"<option>">, HelpText<"Embed LLVM bitcode (option: off, all, bitcode, marker)">, Index: clang/include/clang/CodeGen/BackendUtil.h =================================================================== --- clang/include/clang/CodeGen/BackendUtil.h +++ clang/include/clang/CodeGen/BackendUtil.h @@ -44,6 +44,9 @@ void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf); + + void EmbedBinary(llvm::Module *M, const CodeGenOptions &CGOpts, + DiagnosticsEngine &Diags); } #endif Index: clang/include/clang/Basic/CodeGenOptions.h =================================================================== --- clang/include/clang/Basic/CodeGenOptions.h +++ clang/include/clang/Basic/CodeGenOptions.h @@ -276,6 +276,11 @@ /// CUDA runtime back-end for incorporating them into host-side object file. std::string CudaGpuBinaryFileName; + /// List of filenames and section name pairs passed in using the + /// -fembed-offload-object option to embed device-side offloading objects into + /// the host as a named section. Input passed in as '<filename>,<section>' + std::vector<std::string> OffloadObjects; + /// The name of the file to which the backend should save YAML optimization /// records. std::string OptRecordFile;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits