[PATCH] D127233: [CodeGen] Sort llvm.global_ctors by lexical order before emission

Yuanfang Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 16 10:00:30 PDT 2022


ychen added inline comments.


================
Comment at: clang/lib/CodeGen/CodeGenModule.cpp:544
   }
+  llvm::stable_sort(GlobalCtors, [](const Structor &L, const Structor &R) {
+    return L.LexOrder < R.LexOrder;
----------------
rnk wrote:
> Please move this sorting into EmitCtorList and apply it to destructors. I believe they are currently emitted in source order, and the loader executes them in reverse order, so we get the desired reverse source order cleanup behavior.
I looked into this. They are indeed "currently emitted in source order". However, if a dtor ever uses an entry in `llvm.global_dtors`, it must have bailed out of deferred emission here (https://github.com/llvm/llvm-project/blob/69c09d11f877a35655e285cda96ec0699e385fc9/clang/lib/CodeGen/CodeGenModule.cpp#L3256-L3263), which is to say, the corresponding ctor is also in lexing order. So the situation is either the ctor/dtor pair both use lexing order, or there is no dtor (like `inline int` with an initializer) and the ctor is deferred/reordered where this patch kicks in.



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127233/new/

https://reviews.llvm.org/D127233



More information about the cfe-commits mailing list