From f9969a3d28e738e9427e371aac06d71269220123 Mon Sep 17 00:00:00 2001 From: Yuanfang Chen Date: Mon, 22 Aug 2022 15:59:51 -0700 Subject: [CodeGen] Sort llvm.global_ctors by lexing order before emission Fixes https://github.com/llvm/llvm-project/issues/55804 The lexing order is already bookkept in DelayedCXXInitPosition but we were not using it based on the wrong assumption that inline variable is unordered. This patch fixes it by ordering entries in llvm.global_ctors by orders in DelayedCXXInitPosition. for llvm.global_ctors entries without a lexing order, ordering them by the insertion order. (This *mostly* orders the template instantiation in https://reviews.llvm.org/D126341 intuitively, minus one tweak for which I'll submit a separate patch.) Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D127233 --- clang/lib/CodeGen/CodeGenModule.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.h') diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 14c791f..f57afdc 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -282,12 +282,15 @@ class CodeGenModule : public CodeGenTypeCache { public: struct Structor { - Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {} - Structor(int Priority, llvm::Constant *Initializer, + Structor() + : Priority(0), LexOrder(~0u), Initializer(nullptr), + AssociatedData(nullptr) {} + Structor(int Priority, unsigned LexOrder, llvm::Constant *Initializer, llvm::Constant *AssociatedData) - : Priority(Priority), Initializer(Initializer), + : Priority(Priority), LexOrder(LexOrder), Initializer(Initializer), AssociatedData(AssociatedData) {} int Priority; + unsigned LexOrder; llvm::Constant *Initializer; llvm::Constant *AssociatedData; }; @@ -1602,6 +1605,7 @@ private: // FIXME: Hardcoding priority here is gross. void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535, + unsigned LexOrder = ~0U, llvm::Constant *AssociatedData = nullptr); void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535, bool IsDtorAttrFunc = false); -- cgit v1.1