[PATCH] D125278: [GlobalOpt] Relax the check for ctor priorities

Alexander Shaposhnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 9 16:13:00 PDT 2022


alexander-shaposhnikov created this revision.
alexander-shaposhnikov added reviewers: rnk, compnerd.
alexander-shaposhnikov created this object with visibility "All Users".
Herald added a subscriber: hiraditya.
Herald added a project: All.
alexander-shaposhnikov requested review of this revision.
Herald added a project: LLVM.

This diff relaxes the check for the priorities of global constructors to enable
optimizeGlobalCtorsList to handle the case of different priorities.
This addresses the issue https://github.com/llvm/llvm-project/issues/55083 .
P.S. There are more opportunities to improve the existing schema/pipeline but those are unrelated to the current change.

Test plan:
1/ ninja check-all && ninja check-clang
2/ bootstrapped clang passes the tests


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125278

Files:
  llvm/lib/Transforms/Utils/CtorUtils.cpp
  llvm/test/Transforms/GlobalOpt/ctor-list-opt.ll


Index: llvm/test/Transforms/GlobalOpt/ctor-list-opt.ll
===================================================================
--- llvm/test/Transforms/GlobalOpt/ctor-list-opt.ll
+++ llvm/test/Transforms/GlobalOpt/ctor-list-opt.ll
@@ -1,7 +1,8 @@
 ; RUN: opt < %s -passes=globalopt -S | FileCheck %s
 ; CHECK-NOT: CTOR
 %ini = type { i32, void()*, i8* }
- at llvm.global_ctors = appending global [11 x %ini] [
+ at llvm.global_ctors = appending global [12 x %ini] [
+	%ini { i32 65534, void ()* @CTOR1, i8* null },
 	%ini { i32 65535, void ()* @CTOR1, i8* null },
 	%ini { i32 65535, void ()* @CTOR1, i8* null },
 	%ini { i32 65535, void ()* @CTOR2, i8* null },
Index: llvm/lib/Transforms/Utils/CtorUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/CtorUtils.cpp
+++ llvm/lib/Transforms/Utils/CtorUtils.cpp
@@ -91,6 +91,7 @@
   if (!CA)
     return nullptr;
 
+  SmallVector<uint64_t, 2> Priorities;
   for (auto &V : CA->operands()) {
     if (isa<ConstantAggregateZero>(V))
       continue;
@@ -105,9 +106,15 @@
 
     // Init priority must be standard.
     ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
-    if (CI->getZExtValue() != 65535)
-      return nullptr;
+    Priorities.push_back(CI->getZExtValue());
   }
+  // Protect against trying to statically evaluate ctors in the wrong order.
+  // Clang emits the elements of llvm.global_ctors in the correct order (see
+  // CodeGenModule::EmitCXXGlobalInitFunc in clang/lib/CodeGen/CGDeclCXX.cpp),
+  // but the documentation for llvm.global_ctors does not specify if this array
+  // is required to be sorted by priority or not.
+  if (!is_sorted(Priorities))
+    return nullptr;
 
   return GV;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125278.428230.patch
Type: text/x-patch
Size: 1717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220509/2672bea4/attachment.bin>


More information about the llvm-commits mailing list