aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp')
-rw-r--r--llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
index 225b4fe95f67..37329b489555 100644
--- a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -158,7 +158,11 @@ void simplifyExternals(Module &M) {
Function *NewF =
Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
F.getAddressSpace(), "", &M);
- NewF->setVisibility(F.getVisibility());
+ NewF->copyAttributesFrom(&F);
+ // Only copy function attribtues.
+ NewF->setAttributes(
+ AttributeList::get(M.getContext(), AttributeList::FunctionIndex,
+ F.getAttributes().getFnAttributes()));
NewF->takeName(&F);
F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
F.eraseFromParent();
@@ -195,6 +199,26 @@ void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
forEachVirtualFunction(cast<Constant>(Op), Fn);
}
+// Clone any @llvm[.compiler].used over to the new module and append
+// values whose defs were cloned into that module.
+static void cloneUsedGlobalVariables(const Module &SrcM, Module &DestM,
+ bool CompilerUsed) {
+ SmallVector<GlobalValue *, 4> Used, NewUsed;
+ // First collect those in the llvm[.compiler].used set.
+ collectUsedGlobalVariables(SrcM, Used, CompilerUsed);
+ // Next build a set of the equivalent values defined in DestM.
+ for (auto *V : Used) {
+ auto *GV = DestM.getNamedValue(V->getName());
+ if (GV && !GV->isDeclaration())
+ NewUsed.push_back(GV);
+ }
+ // Finally, add them to a llvm[.compiler].used variable in DestM.
+ if (CompilerUsed)
+ appendToCompilerUsed(DestM, NewUsed);
+ else
+ appendToUsed(DestM, NewUsed);
+}
+
// If it's possible to split M into regular and thin LTO parts, do so and write
// a multi-module bitcode file with the two parts to OS. Otherwise, write only a
// regular LTO bitcode file to OS.
@@ -287,6 +311,11 @@ void splitAndWriteThinLTOBitcode(
StripDebugInfo(*MergedM);
MergedM->setModuleInlineAsm("");
+ // Clone any llvm.*used globals to ensure the included values are
+ // not deleted.
+ cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ false);
+ cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ true);
+
for (Function &F : *MergedM)
if (!F.isDeclaration()) {
// Reset the linkage of all functions eligible for virtual constant