Skip to content

Update embedded bitcode support to work with llvm r269706. #5202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,22 @@ static void embedBitcode(llvm::Module *M, const IRGenOptions &Opts)
if (Opts.EmbedMode == IRGenEmbedMode::None)
return;

// Save llvm.compiler.used and remove it.
SmallVector<llvm::Constant*, 2> UsedArray;
SmallSet<llvm::GlobalValue*, 4> UsedGlobals;
auto *UsedElementType =
llvm::Type::getInt8Ty(M->getContext())->getPointerTo(0);
llvm::GlobalVariable *Used =
collectUsedGlobalVariables(*M, UsedGlobals, true);
for (auto *GV : UsedGlobals) {
if (GV->getName() != "llvm.embedded.module" &&
GV->getName() != "llvm.cmdline")
UsedArray.push_back(
ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
}
if (Used)
Used->eraseFromParent();

// Embed the bitcode for the llvm module.
std::string Data;
llvm::raw_string_ostream OS(Data);
Expand All @@ -526,14 +542,15 @@ static void embedBitcode(llvm::Module *M, const IRGenOptions &Opts)
ArrayRef<uint8_t> ModuleData((uint8_t*)OS.str().data(), OS.str().size());
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,
llvm::GlobalValue::PrivateLinkage,
ModuleConstant);
UsedArray.push_back(
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
GV->setSection("__LLVM,__bitcode");
if (llvm::GlobalVariable *Old =
M->getGlobalVariable("llvm.embedded.module")) {
M->getGlobalVariable("llvm.embedded.module", true)) {
GV->takeName(Old);
Old->replaceAllUsesWith(GV);
delete Old;
Expand All @@ -547,16 +564,28 @@ static void embedBitcode(llvm::Module *M, const IRGenOptions &Opts)
llvm::Constant *CmdConstant =
llvm::ConstantDataArray::get(M->getContext(), CmdData);
GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
llvm::GlobalValue::AppendingLinkage,
llvm::GlobalValue::PrivateLinkage,
CmdConstant);
GV->setSection("__LLVM,__swift_cmdline");
if (llvm::GlobalVariable *Old = M->getGlobalVariable("llvm.cmdline")) {
UsedArray.push_back(
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
if (llvm::GlobalVariable *Old = M->getGlobalVariable("llvm.cmdline", true)) {
GV->takeName(Old);
Old->replaceAllUsesWith(GV);
delete Old;
} else {
GV->setName("llvm.cmdline");
}

if (UsedArray.empty())
return;

// Recreate llvm.compiler.used.
auto *ATy = llvm::ArrayType::get(UsedElementType, UsedArray.size());
auto *NewUsed = new GlobalVariable(
*M, ATy, false, llvm::GlobalValue::AppendingLinkage,
llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
NewUsed->setSection("llvm.metadata");
}

static void initLLVMModule(const IRGenModule &IGM) {
Expand Down
2 changes: 0 additions & 2 deletions test/Driver/multi-threaded.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// REQUIRES: rdar23493035

// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swiftc_driver -driver-print-jobs -module-name=ThisModule -wmo -num-threads 4 %S/Inputs/main.swift %s -emit-module -o test.swiftmodule | %FileCheck -check-prefix=MODULE %s
// RUN: echo "{\"%s\": {\"assembly\": \"/build/multi-threaded.s\"}, \"%S/Inputs/main.swift\": {\"assembly\": \"/build/main.s\"}}" > %t/ofms.json
Expand Down
1 change: 0 additions & 1 deletion test/Frontend/embed-bitcode.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
; REQUIRES: CPU=x86_64
; REQUIRES: rdar23493035
; RUN: llvm-as %s -o %t.bc
; RUN: %swift -target x86_64-apple-darwin10 -c -module-name someModule -embed-bitcode -disable-llvm-optzns -o %t2.o %t.bc -dump-clang-diagnostics 2> %t.diags.txt
; RUN: llvm-objdump -macho -section="__LLVM,__bitcode" %t2.o | %FileCheck %s
Expand Down
1 change: 0 additions & 1 deletion test/Frontend/embed-bitcode.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// REQUIRES: CPU=x86_64
// REQUIRES: rdar23493035
// RUN: %target-swift-frontend -c -module-name someModule -embed-bitcode-marker -o %t.o %s
// RUN: llvm-objdump -macho -section="__LLVM,__bitcode" %t.o | %FileCheck -check-prefix=MARKER %s
// RUN: llvm-objdump -macho -section="__LLVM,__swift_cmdline" %t.o | %FileCheck -check-prefix=MARKER-CMD %s
Expand Down