[llvm] 551b177 - [OpenMP] Add a flag for embedding a file into the module
Joseph Huber via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 31 12:56:15 PST 2022
Author: Joseph Huber
Date: 2022-01-31T15:56:00-05:00
New Revision: 551b1774524428aae5692ed3d41f969288ecd5a2
URL: https://github.com/llvm/llvm-project/commit/551b1774524428aae5692ed3d41f969288ecd5a2
DIFF: https://github.com/llvm/llvm-project/commit/551b1774524428aae5692ed3d41f969288ecd5a2.diff
LOG: [OpenMP] Add a flag for embedding a file into the module
This patch adds support for a flag `-fembed-offload-binary` to embed a
file as an ELF section in the output by placing it in a global variable.
This can be used to bundle offloading files with the host binary so it
can be accessed by the linker. The section is named using the
`-fembed-offload-section` option.
Depends on D116541
Reviewed By: JonChesterfield
Differential Revision: https://reviews.llvm.org/D116542
Added:
clang/test/Frontend/embed-object.ll
Modified:
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
llvm/include/llvm/Bitcode/BitcodeWriter.h
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/lib/Bitcode/Writer/CMakeLists.txt
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h
index 5a5c2689c6899..128ca2f5df3c3 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -276,6 +276,11 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// 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;
diff --git a/clang/include/clang/CodeGen/BackendUtil.h b/clang/include/clang/CodeGen/BackendUtil.h
index 77d500079f012..d97af65a3d013 100644
--- a/clang/include/clang/CodeGen/BackendUtil.h
+++ b/clang/include/clang/CodeGen/BackendUtil.h
@@ -44,6 +44,9 @@ namespace clang {
void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
llvm::MemoryBufferRef Buf);
+
+ void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
+ DiagnosticsEngine &Diags);
}
#endif
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 9e8fdd2d68ca3..53e68ed2cef90 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1151,6 +1151,10 @@ defm coroutines_ts : BoolFOption<"coroutines-ts",
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)">,
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 9ae5c870afc81..956352dea146d 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1750,3 +1750,31 @@ void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
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);
+ }
+}
diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index b72b16cf2b5fe..c2c508dedb097 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -1134,6 +1134,7 @@ void CodeGenAction::ExecuteAction() {
TheModule->setTargetTriple(TargetOpts.Triple);
}
+ EmbedObject(TheModule.get(), CodeGenOpts, Diagnostics);
EmbedBitcode(TheModule.get(), CodeGenOpts, *MainFile);
LLVMContext &Ctx = TheModule->getContext();
diff --git a/clang/test/Frontend/embed-object.ll b/clang/test/Frontend/embed-object.ll
new file mode 100644
index 0000000000000..49b477d4a7be0
--- /dev/null
+++ b/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"
+
+ at x = private constant i8 1
+ at llvm.compiler.used = appending global [1 x i8*] [i8* @x], section "llvm.metadata"
+
+define i32 @foo() {
+ ret i32 0
+}
diff --git a/llvm/include/llvm/Bitcode/BitcodeWriter.h b/llvm/include/llvm/Bitcode/BitcodeWriter.h
index 7ad2d37a2a354..56e74cfb418da 100644
--- a/llvm/include/llvm/Bitcode/BitcodeWriter.h
+++ b/llvm/include/llvm/Bitcode/BitcodeWriter.h
@@ -165,6 +165,11 @@ class raw_ostream;
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
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index eb4e09ea3a265..fafccb4d42547 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/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 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
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);
+}
diff --git a/llvm/lib/Bitcode/Writer/CMakeLists.txt b/llvm/lib/Bitcode/Writer/CMakeLists.txt
index 36808c8ae28f7..dcdb73b854d4c 100644
--- a/llvm/lib/Bitcode/Writer/CMakeLists.txt
+++ b/llvm/lib/Bitcode/Writer/CMakeLists.txt
@@ -11,6 +11,7 @@ add_llvm_component_library(LLVMBitWriter
Analysis
Core
MC
+ TransformUtils
Object
Support
)
More information about the llvm-commits
mailing list