This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG551b17745244: [OpenMP] Add a flag for embedding a file into
the module (authored by jhuber6).
Changed prior to commit:
https://reviews.llvm.org/D116542?vs=404506&id=404685#toc
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>
@@ -4973,3 +4974,19 @@
llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
NewUsed->setSection("llvm.metadata");
}
+
+void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
+ StringRef SectionName) {
+ ArrayRef<char> ModuleData =
+ ArrayRef<char>(Buf.getBufferStart(), Buf.getBufferSize());
+
+ // Embed the buffer into the module.
+ 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 EmbedBufferInModule(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,15 @@
+; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm \
+; RUN: -fembed-offload-object=%S/Inputs/empty.h,section1 \
+; RUN: -fembed-offload-object=%S/Inputs/empty.h,section2 -x ir %s -o - \
+; RUN: | FileCheck %s -check-prefix=CHECK
+
+; CHECK: @[[OBJECT1:.+]] = private constant [0 x i8] zeroinitializer, section ".llvm.offloading.section1"
+; CHECK: @[[OBJECT2:.+]] = private constant [0 x i8] zeroinitializer, section ".llvm.offloading.section2"
+; CHECK: @llvm.compiler.used = appending global [3 x i8*] [i8* @x, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @[[OBJECT1]], i32 0, i32 0), i8* getelementptr inbounds ([0 x i8], [0 x i8]* @[[OBJECT2]], i32 0, i32 0)], section "llvm.metadata"
+
+@x = private constant i8 1
[email protected] = 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);
}
+ 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
@@ -1750,3 +1750,31 @@
CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
CGOpts.CmdArgs);
}
+
+void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
+ DiagnosticsEngine &Diags) {
+ if (CGOpts.OffloadObjects.empty())
+ return;
+
+ for (StringRef OffloadObject : CGOpts.OffloadObjects) {
+ if (OffloadObject.count(',') != 1) {
+ Diags.Report(Diags.getCustomDiagID(
+ DiagnosticsEngine::Error, "Invalid string pair for embedding '%0'"))
+ << OffloadObject;
+ return;
+ }
+ 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::EmbedBufferInModule(*M, **ObjectOrErr, SectionName);
+ }
+}
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1151,6 +1151,10 @@
PosFlag<SetTrue, [CC1Option], "Enable support for the C++ Coroutines TS">,
NegFlag<SetFalse>>;
+def fembed_offload_object_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 EmbedObject(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
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits