From 323015501834d58c75becc3b05c2bd0d587167ba Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sun, 29 Nov 2015 03:21:30 +0000 Subject: [PATCH] Fix a crash when writing merged bitcode. Playing with mutateType in here was making getValueType and getType incompatible. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254240 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 19 ++++++++++++++----- test/Linker/ctors.ll | 3 +++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index afb39a1ae6d..4b1b9d21e42 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -537,7 +537,7 @@ private: bool linkGlobalValueProto(GlobalValue *GV); bool linkModuleFlagsMetadata(); - void linkAppendingVarInit(const AppendingVarInfo &AVI); + void linkAppendingVarInit(AppendingVarInfo &AVI); void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src); bool linkFunctionBody(Function &Dst, Function &Src); @@ -1493,7 +1493,7 @@ static void getArrayElements(const Constant *C, Dest.push_back(C->getAggregateElement(i)); } -void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { +void ModuleLinker::linkAppendingVarInit(AppendingVarInfo &AVI) { // Merge the initializer. SmallVector DstElements; getArrayElements(AVI.DstInit, DstElements); @@ -1517,9 +1517,18 @@ void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { DstElements.push_back( MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer)); } - if (IsNewStructor) { + if (DstElements.size() != NewType->getNumElements()) { NewType = ArrayType::get(NewType->getElementType(), DstElements.size()); - AVI.NewGV->mutateType(PointerType::get(NewType, 0)); + GlobalVariable *Old = AVI.NewGV; + GlobalVariable *NG = new GlobalVariable( + *DstM, NewType, Old->isConstant(), Old->getLinkage(), /*init*/ nullptr, + /*name*/ "", Old, Old->getThreadLocalMode(), + Old->getType()->getAddressSpace()); + copyGVAttributes(NG, Old); + AVI.NewGV->replaceAllUsesWith( + ConstantExpr::getBitCast(NG, AVI.NewGV->getType())); + AVI.NewGV->eraseFromParent(); + AVI.NewGV = NG; } AVI.NewGV->setInitializer(ConstantArray::get(NewType, DstElements)); @@ -1909,7 +1918,7 @@ bool ModuleLinker::run() { if (linkGlobalValueProto(&GA)) return true; - for (const AppendingVarInfo &AppendingVar : AppendingVars) + for (AppendingVarInfo &AppendingVar : AppendingVars) linkAppendingVarInit(AppendingVar); for (const auto &Entry : DstM->getComdatSymbolTable()) { diff --git a/test/Linker/ctors.ll b/test/Linker/ctors.ll index 67bf4563718..208fe86402c 100644 --- a/test/Linker/ctors.ll +++ b/test/Linker/ctors.ll @@ -3,6 +3,9 @@ ; RUN: llvm-link %p/Inputs/ctors.ll %s -S -o - | \ ; RUN: FileCheck --check-prefix=ALL --check-prefix=CHECK2 %s +; Test the bitcode writer too. It used to crash. +; RUN: llvm-link %s %p/Inputs/ctors.ll -o t.bc + @v = weak global i8 0 ; CHECK1: @v = weak global i8 0 ; CHECK2: @v = weak global i8 1 -- 2.34.1