diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp index d0fb71f341b7..e255113aab7e 100644 --- a/llvm/lib/IR/AutoUpgrade.cpp +++ b/llvm/lib/IR/AutoUpgrade.cpp @@ -191,16 +191,19 @@ static bool UpgradeGlobalStructors(GlobalVariable *GV) { StructType::get(GV->getContext(), Tys, /*isPacked=*/false); // Build new constants with a null third field filled in. - ConstantArray *OldInit = dyn_cast(GV->getInitializer()); - if (!OldInit) + Constant *OldInitC = GV->getInitializer(); + ConstantArray *OldInit = dyn_cast(OldInitC); + if (!OldInit && !isa(OldInitC)) return false; std::vector Initializers; - for (Use &U : OldInit->operands()) { - ConstantStruct *Init = cast(&U); - Constant *NewInit = + if (OldInit) { + for (Use &U : OldInit->operands()) { + ConstantStruct *Init = cast(&U); + Constant *NewInit = ConstantStruct::get(NewTy, Init->getOperand(0), Init->getOperand(1), Constant::getNullValue(VoidPtrTy), nullptr); - Initializers.push_back(NewInit); + Initializers.push_back(NewInit); + } } assert(Initializers.size() == ATy->getNumElements()); diff --git a/llvm/test/Bitcode/upgrade-global-ctors.ll b/llvm/test/Bitcode/upgrade-global-ctors.ll new file mode 100644 index 000000000000..bd253a81620f --- /dev/null +++ b/llvm/test/Bitcode/upgrade-global-ctors.ll @@ -0,0 +1,3 @@ +; RUN: llvm-dis < %s.bc| FileCheck %s + +; CHECK: @llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] zeroinitializer diff --git a/llvm/test/Bitcode/upgrade-global-ctors.ll.bc b/llvm/test/Bitcode/upgrade-global-ctors.ll.bc new file mode 100644 index 000000000000..927fd91867e9 Binary files /dev/null and b/llvm/test/Bitcode/upgrade-global-ctors.ll.bc differ