[PATCH] D17392: Embed bitcode in object file (clang cc1 part)
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Thu May 5 15:03:13 PDT 2016
rsmith added inline comments.
================
Comment at: lib/CodeGen/BackendUtil.cpp:769-770
@@ +768,4 @@
+void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
+ llvm::MemoryBufferRef Buf)
+{
+ if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
----------------
Ping.
================
Comment at: lib/CodeGen/BackendUtil.cpp:774-806
@@ +773,35 @@
+
+ // Embed the bitcode for the llvm module.
+ std::string Data;
+ ArrayRef<uint8_t> ModuleData;
+ Triple T(M->getTargetTriple());
+ if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
+ if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
+ (const unsigned char *)Buf.getBufferEnd())) {
+ // If the input is LLVM Assembly, bitcode is produced by serializing
+ // the module. Use-lists order need to be perserved in this case.
+ llvm::raw_string_ostream OS(Data);
+ llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
+ ModuleData =
+ ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
+ } else
+ // If the input is LLVM bitcode, write the input byte stream directly.
+ ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
+ Buf.getBufferSize());
+ }
+ llvm::Constant *ModuleConstant =
+ llvm::ConstantDataArray::get(M->getContext(), ModuleData);
+ // Use Appending linkage so it doesn't get optimized out.
+ llvm::GlobalVariable *GV = new llvm::GlobalVariable(
+ *M, ModuleConstant->getType(), true, llvm::GlobalValue::AppendingLinkage,
+ ModuleConstant);
+ GV->setSection(getSectionNameForBitcode(T));
+ if (llvm::GlobalVariable *Old =
+ M->getGlobalVariable("llvm.embedded.module")) {
+ assert(Old->use_empty() && "llvm.embedded.module must have no uses");
+ GV->takeName(Old);
+ Old->eraseFromParent();
+ } else {
+ GV->setName("llvm.embedded.module");
+ }
+
----------------
Can you skip all this if `Buf` is empty (in the common case where the input is not an IR file)?
================
Comment at: lib/Driver/Tools.cpp:3766-3771
@@ -3765,8 +3765,8 @@
// Add flags implied by -fembed-bitcode.
- CmdArgs.push_back("-fembed-bitcode");
+ Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
// Disable all llvm IR level optimizations.
CmdArgs.push_back("-disable-llvm-optzns");
}
if (C.getDriver().embedBitcodeMarkerOnly())
- CmdArgs.push_back("-fembed-bitcode-marker");
+ CmdArgs.push_back("-fembed-bitcode=marker");
----------------
This changes the semantics of `clang -fembed-bitcode -fembed-bitcode-marker` to embed only a marker and no bitcode. Is that OK?
================
Comment at: lib/Driver/Tools.cpp:5724-5728
@@ -5723,4 +5723,7 @@
// by the frontend.
- if (C.getDriver().isSaveTempsEnabled() && isa<CompileJobAction>(JA))
+ // When -fembed-bitcode is enabled, optimized bitcode is emitted because it
+ // has slightly different breakdown between stages.
+ if (C.getDriver().isSaveTempsEnabled() &&
+ !C.getDriver().embedBitcodeEnabled() && isa<CompileJobAction>(JA))
CmdArgs.push_back("-disable-llvm-passes");
----------------
Why is this necessary? `-fembed-bitcode` implies `-disable-llvm-optzns` anyway.
http://reviews.llvm.org/D17392
More information about the cfe-commits
mailing list