From b4307437e51d3a400de21de624a1610aee23346b Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 21 Mar 2023 16:16:52 +0100 Subject: [ModuleUtils] Handle globals_ctors/dtors with non-literal type (PR56809) If the global already exists, use its existing type, so we don't try to mix literal and non-literal structs among the elements. Fixes https://github.com/llvm/llvm-project/issues/56809. --- llvm/lib/Transforms/Utils/ModuleUtils.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'llvm/lib/Transforms/Utils/ModuleUtils.cpp') diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp index 6d17a466..adc2fc0 100644 --- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp +++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp @@ -31,11 +31,9 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F, // Get the current set of static global constructors and add the new ctor // to the list. SmallVector CurrentCtors; - StructType *EltTy = StructType::get( - IRB.getInt32Ty(), PointerType::get(FnTy, F->getAddressSpace()), - IRB.getInt8PtrTy()); - + StructType *EltTy; if (GlobalVariable *GVCtor = M.getNamedGlobal(ArrayName)) { + EltTy = cast(GVCtor->getValueType()->getArrayElementType()); if (Constant *Init = GVCtor->getInitializer()) { unsigned n = Init->getNumOperands(); CurrentCtors.reserve(n + 1); @@ -43,6 +41,10 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F, CurrentCtors.push_back(cast(Init->getOperand(i))); } GVCtor->eraseFromParent(); + } else { + EltTy = StructType::get( + IRB.getInt32Ty(), PointerType::get(FnTy, F->getAddressSpace()), + IRB.getInt8PtrTy()); } // Build a 3 field global_ctor entry. We don't take a comdat key. -- cgit v1.1