aeubanks updated this revision to Diff 452798.
aeubanks added a comment.
Herald added a reviewer: aaron.ballman.

use 400 for lib, update documentation


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D131910

Files:
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/CGDeclCXX.cpp
  clang/test/CodeGenCXX/pragma-init_seg.cpp
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/test/CodeGen/X86/ctor-priority-coff.ll

Index: llvm/test/CodeGen/X86/ctor-priority-coff.ll
===================================================================
--- llvm/test/CodeGen/X86/ctor-priority-coff.ll
+++ llvm/test/CodeGen/X86/ctor-priority-coff.ll
@@ -6,6 +6,15 @@
 ; CHECK: .section        .CRT$XCA00042,"dr"
 ; CHECK: .p2align        3
 ; CHECK: .quad   f
+; CHECK: .section        .CRT$XCC,"dr"
+; CHECK: .p2align        3
+; CHECK: .quad   i
+; CHECK: .section        .CRT$XCC00250,"dr"
+; CHECK: .p2align        3
+; CHECK: .quad   k
+; CHECK: .section        .CRT$XCL,"dr"
+; CHECK: .p2align        3
+; CHECK: .quad   j
 ; CHECK: .section        .CRT$XCT12345,"dr"
 ; CHECK: .p2align        3
 ; CHECK: .quad   g
@@ -24,10 +33,13 @@
 @str1 = private dso_local unnamed_addr constant [6 x i8] c"first\00", align 1
 @str2 = private dso_local unnamed_addr constant [5 x i8] c"main\00", align 1
 
-@llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [
+@llvm.global_ctors = appending global [6 x { i32, ptr, ptr }] [
   { i32, ptr, ptr } { i32 12345, ptr @g, ptr null },
   { i32, ptr, ptr } { i32 42, ptr @f, ptr null },
-  { i32, ptr, ptr } { i32 23456, ptr @init_h, ptr @h }
+  { i32, ptr, ptr } { i32 23456, ptr @init_h, ptr @h },
+  { i32, ptr, ptr } { i32 200, ptr @i, ptr null },
+  { i32, ptr, ptr } { i32 400, ptr @j, ptr null },
+  { i32, ptr, ptr } { i32 250, ptr @k, ptr null }
 ]
 
 declare dso_local i32 @puts(ptr nocapture readonly) local_unnamed_addr
@@ -50,6 +62,23 @@
   ret void
 }
 
+define dso_local void @i() {
+entry:
+  store i8 43, ptr @h
+  ret void
+}
+
+define dso_local void @j() {
+entry:
+  store i8 44, ptr @h
+  ret void
+}
+
+define dso_local void @k() {
+entry:
+  store i8 45, ptr @h
+  ret void
+}
 
 ; Function Attrs: nounwind uwtable
 define dso_local i32 @main() local_unnamed_addr {
Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1889,11 +1889,24 @@
     // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
     // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
     // low priorities need to sort before 'L', since the CRT uses that
-    // internally, so we use ".CRT$XCA00001" for them.
+    // internally, so we use ".CRT$XCA00001" for them. We have a contract with
+    // the frontend that "init_seg(compiler)" corresponds to priority 200 and
+    // "init_seg(lib)" corresponds to priority 400, and those respectively use
+    // 'C' and 'L' without the priority suffix. Priorities between 200 and 400
+    // use 'C' with the priority as a suffix.
     SmallString<24> Name;
+    char LastLetter = 'T';
+    bool AddPrioritySuffix = Priority != 200 && Priority != 400;
+    if (Priority < 200)
+      LastLetter = 'A';
+    else if (Priority < 400)
+      LastLetter = 'C';
+    else if (Priority == 400)
+      LastLetter = 'L';
     raw_svector_ostream OS(Name);
-    OS << ".CRT$X" << (IsCtor ? "C" : "T") <<
-        (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
+    OS << ".CRT$X" << (IsCtor ? "C" : "T") << LastLetter;
+    if (AddPrioritySuffix)
+      OS << format("%05u", Priority);
     MCSectionCOFF *Sec = Ctx.getCOFFSection(
         Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
         SectionKind::getReadOnly());
Index: clang/test/CodeGenCXX/pragma-init_seg.cpp
===================================================================
--- clang/test/CodeGenCXX/pragma-init_seg.cpp
+++ clang/test/CodeGenCXX/pragma-init_seg.cpp
@@ -10,12 +10,12 @@
 #pragma init_seg(compiler)
 int x = f();
 // CHECK: @"?x@simple_init@@3HA" = dso_local global i32 0, align 4
-// CHECK: @__cxx_init_fn_ptr = private constant ptr @"??__Ex@simple_init@@YAXXZ", section ".CRT$XCC"
+// No function pointer!  This one goes on @llvm.global_ctors.
 
 #pragma init_seg(lib)
 int y = f();
 // CHECK: @"?y@simple_init@@3HA" = dso_local global i32 0, align 4
-// CHECK: @__cxx_init_fn_ptr.1 = private constant ptr @"??__Ey@simple_init@@YAXXZ", section ".CRT$XCL"
+// No function pointer!  This one goes on @llvm.global_ctors.
 
 #pragma init_seg(user)
 int z = f();
@@ -29,14 +29,14 @@
 namespace {
 int x = f();
 // CHECK: @"?x@?A0x{{[^@]*}}@internal_init@@3HA" = internal global i32 0, align 4
-// CHECK: @__cxx_init_fn_ptr.2 = private constant ptr @"??__Ex@?A0x{{[^@]*}}@internal_init@@YAXXZ", section ".asdf"
+// CHECK: @__cxx_init_fn_ptr = private constant ptr @"??__Ex@?A0x{{[^@]*}}@internal_init@@YAXXZ", section ".asdf"
 }
 }
 
 namespace selectany_init {
 int __declspec(selectany) x = f();
 // CHECK: @"?x@selectany_init@@3HA" = weak_odr dso_local global i32 0, comdat, align 4
-// CHECK: @__cxx_init_fn_ptr.3 = private constant ptr @"??__Ex@selectany_init@@YAXXZ", section ".asdf", comdat($"?x@selectany_init@@3HA")
+// CHECK: @__cxx_init_fn_ptr.1 = private constant ptr @"??__Ex@selectany_init@@YAXXZ", section ".asdf", comdat($"?x@selectany_init@@3HA")
 }
 
 namespace explicit_template_instantiation {
@@ -44,7 +44,7 @@
 template <typename T> const int A<T>::x = f();
 template struct A<int>;
 // CHECK: @"?x@?$A@H@explicit_template_instantiation@@2HB" = weak_odr dso_local global i32 0, comdat, align 4
-// CHECK: @__cxx_init_fn_ptr.4 = private constant ptr @"??__E?x@?$A@H@explicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A@H@explicit_template_instantiation@@2HB")
+// CHECK: @__cxx_init_fn_ptr.2 = private constant ptr @"??__E?x@?$A@H@explicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A@H@explicit_template_instantiation@@2HB")
 }
 
 namespace implicit_template_instantiation {
@@ -52,21 +52,19 @@
 template <typename T> const int A<T>::x = f();
 int g() { return A<int>::x; }
 // CHECK: @"?x@?$A@H@implicit_template_instantiation@@2HB" = linkonce_odr dso_local global i32 0, comdat, align 4
-// CHECK: @__cxx_init_fn_ptr.5 = private constant ptr @"??__E?x@?$A@H@implicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A@H@implicit_template_instantiation@@2HB")
+// CHECK: @__cxx_init_fn_ptr.3 = private constant ptr @"??__E?x@?$A@H@implicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A@H@implicit_template_instantiation@@2HB")
 }
 
 // ... and here's where we emitted user level ctors.
-// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }]
-// CHECK: [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_pragma_init_seg.cpp, ptr null }]
+// CHECK: @llvm.global_ctors = appending global [3 x { i32, ptr, ptr }]
+// CHECK: [{ i32, ptr, ptr } { i32 200, ptr @"??__Ex@simple_init@@YAXXZ", ptr @"?x@simple_init@@3HA" }, { i32, ptr, ptr } { i32 400, ptr @"??__Ey@simple_init@@YAXXZ", ptr @"?y@simple_init@@3HA" }, { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_pragma_init_seg.cpp, ptr null }]
 
 // We have to mark everything used so we can survive globalopt, even through
 // LTO.  There's no way LLVM could really understand if data in the .asdf
 // section is really used or dead.
 //
-// CHECK: @llvm.used = appending global [6 x ptr]
+// CHECK: @llvm.used = appending global [4 x ptr]
 // CHECK: [ptr @__cxx_init_fn_ptr,
 // CHECK: ptr @__cxx_init_fn_ptr.1,
 // CHECK: ptr @__cxx_init_fn_ptr.2,
-// CHECK: ptr @__cxx_init_fn_ptr.3,
-// CHECK: ptr @__cxx_init_fn_ptr.4,
-// CHECK: ptr @__cxx_init_fn_ptr.5], section "llvm.metadata"
+// CHECK: ptr @__cxx_init_fn_ptr.3], section "llvm.metadata"
Index: clang/lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- clang/lib/CodeGen/CGDeclCXX.cpp
+++ clang/lib/CodeGen/CGDeclCXX.cpp
@@ -553,7 +553,18 @@
     CXXThreadLocalInits.push_back(Fn);
     CXXThreadLocalInitVars.push_back(D);
   } else if (PerformInit && ISA) {
-    EmitPointerToInitFunc(D, Addr, Fn, ISA);
+    // Contract with backend that "init_seg(compiler)" corresponds to priority
+    // 200 and "init_seg(lib)" corresponds to priority 400.
+    int Priority = -1;
+    if (ISA->getSection() == ".CRT$XCC")
+      Priority = 200;
+    else if (ISA->getSection() == ".CRT$XCL")
+      Priority = 400;
+
+    if (Priority != -1)
+      AddGlobalCtor(Fn, Priority, COMDATKey);
+    else
+      EmitPointerToInitFunc(D, Addr, Fn, ISA);
   } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
     OrderGlobalInitsOrStermFinalizers Key(IPA->getPriority(),
                                           PrioritizedCXXGlobalInits.size());
Index: clang/include/clang/Basic/AttrDocs.td
===================================================================
--- clang/include/clang/Basic/AttrDocs.td
+++ clang/include/clang/Basic/AttrDocs.td
@@ -127,6 +127,10 @@
 ``Obj2`` will be initialized *before* ``Obj1`` despite the usual order of
 initialization being the opposite.
 
+On Windows, ``init_seg(compiler)`` is represented with a priority of 200 and
+``init_seg(library)`` is represented with a priority of 400. ``init_seg(user)``
+uses the default 65535 priority.
+
 This attribute is only supported for C++ and Objective-C++ and is ignored in
 other language modes. Currently, this attribute is not implemented on z/OS.
   }];
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to