[llvm] [CodeGen] Sort .ctors in reverse on MinGW just like on other platforms (PR #68570)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 9 03:20:59 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-platform-windows

<details>
<summary>Changes</summary>

On MinGW targets, the .ctors section is always used (as opposed to on ELF platforms, where .init_section is the default but one can select using .ctors, with the -use-ctors option, or the UseInitArray field in TargetOptions).

Apply the reverse ordering regardless of whether the caller has set the UseInitArray flag for this target, as this target unconditionally uses the .ctors section anyway.

For the CodeGen/X86/constructor.ll testcase, note how this now produces the same output ordering as for ELF targets with -use-ctors.

This fixes https://github.com/llvm/llvm-project/issues/55938.

---
Full diff: https://github.com/llvm/llvm-project/pull/68570.diff


3 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+5-1) 
- (modified) llvm/test/CodeGen/X86/constructor.ll (+6-6) 
- (modified) llvm/test/MC/COFF/global_ctors_dtors.ll (+2-2) 


``````````diff
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 97d2fe3426406f3..5f4141e17541bf7 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2807,7 +2807,11 @@ void AsmPrinter::emitXXStructorList(const DataLayout &DL, const Constant *List,
 
   // Emit the structors in reverse order if we are using the .ctor/.dtor
   // initialization scheme.
-  if (!TM.Options.UseInitArray)
+  bool UseCtorSection = !TM.Options.UseInitArray;
+  // MinGW targets always use the .ctors section.
+  if (TM.getTargetTriple().isWindowsGNUEnvironment())
+    UseCtorSection = true;
+  if (UseCtorSection)
     std::reverse(Structors.begin(), Structors.end());
 
   const Align Align = DL.getPointerPrefAlignment();
diff --git a/llvm/test/CodeGen/X86/constructor.ll b/llvm/test/CodeGen/X86/constructor.ll
index 0fea69b5a7bcb52..4bc4e72b6472a68 100644
--- a/llvm/test/CodeGen/X86/constructor.ll
+++ b/llvm/test/CodeGen/X86/constructor.ll
@@ -77,14 +77,14 @@ entry:
 ; MCU-CTORS:         .section        .ctors,"aw", at progbits
 ; MCU-INIT-ARRAY:    .section        .init_array,"aw", at init_array
 
-; COFF-CTOR:		.section	.ctors.65520,"dw",associative,v
+; COFF-CTOR:		.section	.ctors,"dw"
 ; COFF-CTOR-NEXT:	.p2align	3
-; COFF-CTOR-NEXT:	.quad	g
+; COFF-CTOR-NEXT:	.quad	j
+; COFF-CTOR-NEXT:	.quad	i
+; COFF-CTOR-NEXT:	.quad	f
 ; COFF-CTOR-NEXT:	.section	.ctors.09980,"dw",associative,v
 ; COFF-CTOR-NEXT:	.p2align	3
 ; COFF-CTOR-NEXT:	.quad	h
-; COFF-CTOR-NEXT:	.section	.ctors,"dw"
+; COFF-CTOR-NEXT:	.section	.ctors.65520,"dw",associative,v
 ; COFF-CTOR-NEXT:	.p2align	3
-; COFF-CTOR-NEXT:	.quad	f
-; COFF-CTOR-NEXT:	.quad	i
-; COFF-CTOR-NEXT:	.quad	j
+; COFF-CTOR-NEXT:	.quad	g
diff --git a/llvm/test/MC/COFF/global_ctors_dtors.ll b/llvm/test/MC/COFF/global_ctors_dtors.ll
index 7df4d3e500b54c3..9b321a082a32e58 100644
--- a/llvm/test/MC/COFF/global_ctors_dtors.ll
+++ b/llvm/test/MC/COFF/global_ctors_dtors.ll
@@ -56,10 +56,10 @@ define i32 @main() nounwind {
 ; WIN32-NOT: c_global_ctor
 ; WIN32: .section .CRT$XTX,"dr"
 ; WIN32: a_global_dtor
-; MINGW32: .section .ctors,"dw"
-; MINGW32: a_global_ctor
 ; MINGW32: .section .ctors,"dw",associative,{{_?}}b
 ; MINGW32: b_global_ctor
 ; MINGW32-NOT: c_global_ctor
+; MINGW32: .section .ctors,"dw"
+; MINGW32: a_global_ctor
 ; MINGW32: .section .dtors,"dw"
 ; MINGW32: a_global_dtor

``````````

</details>


https://github.com/llvm/llvm-project/pull/68570


More information about the llvm-commits mailing list