[llvm] 067c035 - [GlobalOpt] Handle undef global_ctors gracefully

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 10 07:02:31 PST 2022


Author: Nikita Popov
Date: 2022-03-10T16:02:12+01:00
New Revision: 067c035012fc061ad6378458774ac2df117283c6

URL: https://github.com/llvm/llvm-project/commit/067c035012fc061ad6378458774ac2df117283c6
DIFF: https://github.com/llvm/llvm-project/commit/067c035012fc061ad6378458774ac2df117283c6.diff

LOG: [GlobalOpt] Handle undef global_ctors gracefully

If there are no ctors, then this can have an arbirary zero-sized
value. The current code checks for null, but it could also be
undef or poison.

Replacing the specific null check with a check for
non-ConstantArray.

Added: 
    llvm/test/Transforms/GlobalOpt/undef-ctor-dtor.ll

Modified: 
    llvm/lib/Transforms/Utils/CtorUtils.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/CtorUtils.cpp b/llvm/lib/Transforms/Utils/CtorUtils.cpp
index 069a86f6ab337..38bcce26a0635 100644
--- a/llvm/lib/Transforms/Utils/CtorUtils.cpp
+++ b/llvm/lib/Transforms/Utils/CtorUtils.cpp
@@ -63,8 +63,6 @@ static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemov
 /// Given a llvm.global_ctors list that we can understand,
 /// return a list of the functions and null terminator as a vector.
 static std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
-  if (GV->getInitializer()->isNullValue())
-    return std::vector<Function *>();
   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
   std::vector<Function *> Result;
   Result.reserve(CA->getNumOperands());
@@ -87,9 +85,11 @@ static GlobalVariable *findGlobalCtors(Module &M) {
   if (!GV->hasUniqueInitializer())
     return nullptr;
 
-  if (isa<ConstantAggregateZero>(GV->getInitializer()))
-    return GV;
-  ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
+  // If there are no ctors, then the initializer might be null/undef/poison.
+  // Ignore anything but an array.
+  ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
+  if (!CA)
+    return nullptr;
 
   for (auto &V : CA->operands()) {
     if (isa<ConstantAggregateZero>(V))

diff  --git a/llvm/test/Transforms/GlobalOpt/undef-ctor-dtor.ll b/llvm/test/Transforms/GlobalOpt/undef-ctor-dtor.ll
new file mode 100644
index 0000000000000..d77a5cdaafd65
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/undef-ctor-dtor.ll
@@ -0,0 +1,9 @@
+; RUN: opt -S -globalopt < %s | FileCheck %s
+
+; Gracefully handle undef global_ctors/global_dtors
+
+; CHECK: @llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] undef
+; CHECK: @llvm.global_dtors = appending global [0 x { i32, void ()*, i8* }] undef
+
+ at llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] undef
+ at llvm.global_dtors = appending global [0 x { i32, void ()*, i8* }] undef


        


More information about the llvm-commits mailing list