Skip to content

Commit 9181ce6

Browse files
committed
[Windows] Put init_seg(compiler/lib) in llvm.global_ctors
Currently we treat initializers with init_seg(compiler/lib) as similar to any other init_seg, they simply have a global variable in the proper section (".CRT$XCC" for compiler/".CRT$XCL" for lib) and are added to llvm.used. However, this doesn't match with how LLVM sees normal (or init_seg(user)) initializers via llvm.global_ctors. This causes issues like incorrect init_seg(compiler) vs init_seg(user) ordering due to GlobalOpt evaluating constructors, and the ability to remove init_seg(compiler/lib) initializers at all. Currently we use 'A' for priorities less than 200. Use 200 for init_seg(compiler) (".CRT$XCC") and 400 for init_seg(lib) (".CRT$XCL"), which do not append the priority to the section name. Priorities between 200 and 400 use ".CRT$XCC${Priority}". This allows for some wiggle room for people/future extensions that want to add initializers between compiler and lib. Fixes #56922 Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D131910
1 parent ba1c396 commit 9181ce6

File tree

5 files changed

+73
-18
lines changed

5 files changed

+73
-18
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ relative ordering of values is important. For example:
127127
``Obj2`` will be initialized *before* ``Obj1`` despite the usual order of
128128
initialization being the opposite.
129129

130+
On Windows, ``init_seg(compiler)`` is represented with a priority of 200 and
131+
``init_seg(library)`` is represented with a priority of 400. ``init_seg(user)``
132+
uses the default 65535 priority.
133+
130134
This attribute is only supported for C++ and Objective-C++ and is ignored in
131135
other language modes. Currently, this attribute is not implemented on z/OS.
132136
}];

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,18 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
553553
CXXThreadLocalInits.push_back(Fn);
554554
CXXThreadLocalInitVars.push_back(D);
555555
} else if (PerformInit && ISA) {
556-
EmitPointerToInitFunc(D, Addr, Fn, ISA);
556+
// Contract with backend that "init_seg(compiler)" corresponds to priority
557+
// 200 and "init_seg(lib)" corresponds to priority 400.
558+
int Priority = -1;
559+
if (ISA->getSection() == ".CRT$XCC")
560+
Priority = 200;
561+
else if (ISA->getSection() == ".CRT$XCL")
562+
Priority = 400;
563+
564+
if (Priority != -1)
565+
AddGlobalCtor(Fn, Priority, COMDATKey);
566+
else
567+
EmitPointerToInitFunc(D, Addr, Fn, ISA);
557568
} else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
558569
OrderGlobalInitsOrStermFinalizers Key(IPA->getPriority(),
559570
PrioritizedCXXGlobalInits.size());

clang/test/CodeGenCXX/pragma-init_seg.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace simple_init {
1010
#pragma init_seg(compiler)
1111
int x = f();
1212
// CHECK: @"?x@simple_init@@3HA" = dso_local global i32 0, align 4
13-
// CHECK: @__cxx_init_fn_ptr = private constant ptr @"??__Ex@simple_init@@YAXXZ", section ".CRT$XCC"
13+
// No function pointer! This one goes on @llvm.global_ctors.
1414

1515
#pragma init_seg(lib)
1616
int y = f();
1717
// CHECK: @"?y@simple_init@@3HA" = dso_local global i32 0, align 4
18-
// CHECK: @__cxx_init_fn_ptr.1 = private constant ptr @"??__Ey@simple_init@@YAXXZ", section ".CRT$XCL"
18+
// No function pointer! This one goes on @llvm.global_ctors.
1919

2020
#pragma init_seg(user)
2121
int z = f();
@@ -29,44 +29,42 @@ namespace internal_init {
2929
namespace {
3030
int x = f();
3131
// CHECK: @"?x@?A0x{{[^@]*}}@internal_init@@3HA" = internal global i32 0, align 4
32-
// CHECK: @__cxx_init_fn_ptr.2 = private constant ptr @"??__Ex@?A0x{{[^@]*}}@internal_init@@YAXXZ", section ".asdf"
32+
// CHECK: @__cxx_init_fn_ptr = private constant ptr @"??__Ex@?A0x{{[^@]*}}@internal_init@@YAXXZ", section ".asdf"
3333
}
3434
}
3535

3636
namespace selectany_init {
3737
int __declspec(selectany) x = f();
3838
// CHECK: @"?x@selectany_init@@3HA" = weak_odr dso_local global i32 0, comdat, align 4
39-
// CHECK: @__cxx_init_fn_ptr.3 = private constant ptr @"??__Ex@selectany_init@@YAXXZ", section ".asdf", comdat($"?x@selectany_init@@3HA")
39+
// CHECK: @__cxx_init_fn_ptr.1 = private constant ptr @"??__Ex@selectany_init@@YAXXZ", section ".asdf", comdat($"?x@selectany_init@@3HA")
4040
}
4141

4242
namespace explicit_template_instantiation {
4343
template <typename T> struct A { static const int x; };
4444
template <typename T> const int A<T>::x = f();
4545
template struct A<int>;
4646
// CHECK: @"?x@?$A@H@explicit_template_instantiation@@2HB" = weak_odr dso_local global i32 0, comdat, align 4
47-
// 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")
47+
// 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")
4848
}
4949

5050
namespace implicit_template_instantiation {
5151
template <typename T> struct A { static const int x; };
5252
template <typename T> const int A<T>::x = f();
5353
int g() { return A<int>::x; }
5454
// CHECK: @"?x@?$A@H@implicit_template_instantiation@@2HB" = linkonce_odr dso_local global i32 0, comdat, align 4
55-
// 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")
55+
// 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")
5656
}
5757

5858
// ... and here's where we emitted user level ctors.
59-
// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }]
60-
// CHECK: [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_pragma_init_seg.cpp, ptr null }]
59+
// CHECK: @llvm.global_ctors = appending global [3 x { i32, ptr, ptr }]
60+
// 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 }]
6161

6262
// We have to mark everything used so we can survive globalopt, even through
6363
// LTO. There's no way LLVM could really understand if data in the .asdf
6464
// section is really used or dead.
6565
//
66-
// CHECK: @llvm.used = appending global [6 x ptr]
66+
// CHECK: @llvm.used = appending global [4 x ptr]
6767
// CHECK: [ptr @__cxx_init_fn_ptr,
6868
// CHECK: ptr @__cxx_init_fn_ptr.1,
6969
// CHECK: ptr @__cxx_init_fn_ptr.2,
70-
// CHECK: ptr @__cxx_init_fn_ptr.3,
71-
// CHECK: ptr @__cxx_init_fn_ptr.4,
72-
// CHECK: ptr @__cxx_init_fn_ptr.5], section "llvm.metadata"
70+
// CHECK: ptr @__cxx_init_fn_ptr.3], section "llvm.metadata"

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,11 +1889,24 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
18891889
// string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
18901890
// make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
18911891
// low priorities need to sort before 'L', since the CRT uses that
1892-
// internally, so we use ".CRT$XCA00001" for them.
1892+
// internally, so we use ".CRT$XCA00001" for them. We have a contract with
1893+
// the frontend that "init_seg(compiler)" corresponds to priority 200 and
1894+
// "init_seg(lib)" corresponds to priority 400, and those respectively use
1895+
// 'C' and 'L' without the priority suffix. Priorities between 200 and 400
1896+
// use 'C' with the priority as a suffix.
18931897
SmallString<24> Name;
1898+
char LastLetter = 'T';
1899+
bool AddPrioritySuffix = Priority != 200 && Priority != 400;
1900+
if (Priority < 200)
1901+
LastLetter = 'A';
1902+
else if (Priority < 400)
1903+
LastLetter = 'C';
1904+
else if (Priority == 400)
1905+
LastLetter = 'L';
18941906
raw_svector_ostream OS(Name);
1895-
OS << ".CRT$X" << (IsCtor ? "C" : "T") <<
1896-
(Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
1907+
OS << ".CRT$X" << (IsCtor ? "C" : "T") << LastLetter;
1908+
if (AddPrioritySuffix)
1909+
OS << format("%05u", Priority);
18971910
MCSectionCOFF *Sec = Ctx.getCOFFSection(
18981911
Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
18991912
SectionKind::getReadOnly());

llvm/test/CodeGen/X86/ctor-priority-coff.ll

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
; CHECK: .section .CRT$XCA00042,"dr"
77
; CHECK: .p2align 3
88
; CHECK: .quad f
9+
; CHECK: .section .CRT$XCC,"dr"
10+
; CHECK: .p2align 3
11+
; CHECK: .quad i
12+
; CHECK: .section .CRT$XCC00250,"dr"
13+
; CHECK: .p2align 3
14+
; CHECK: .quad k
15+
; CHECK: .section .CRT$XCL,"dr"
16+
; CHECK: .p2align 3
17+
; CHECK: .quad j
918
; CHECK: .section .CRT$XCT12345,"dr"
1019
; CHECK: .p2align 3
1120
; CHECK: .quad g
@@ -24,10 +33,13 @@ $h = comdat any
2433
@str1 = private dso_local unnamed_addr constant [6 x i8] c"first\00", align 1
2534
@str2 = private dso_local unnamed_addr constant [5 x i8] c"main\00", align 1
2635

27-
@llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [
36+
@llvm.global_ctors = appending global [6 x { i32, ptr, ptr }] [
2837
{ i32, ptr, ptr } { i32 12345, ptr @g, ptr null },
2938
{ i32, ptr, ptr } { i32 42, ptr @f, ptr null },
30-
{ i32, ptr, ptr } { i32 23456, ptr @init_h, ptr @h }
39+
{ i32, ptr, ptr } { i32 23456, ptr @init_h, ptr @h },
40+
{ i32, ptr, ptr } { i32 200, ptr @i, ptr null },
41+
{ i32, ptr, ptr } { i32 400, ptr @j, ptr null },
42+
{ i32, ptr, ptr } { i32 250, ptr @k, ptr null }
3143
]
3244

3345
declare dso_local i32 @puts(ptr nocapture readonly) local_unnamed_addr
@@ -50,6 +62,23 @@ entry:
5062
ret void
5163
}
5264

65+
define dso_local void @i() {
66+
entry:
67+
store i8 43, ptr @h
68+
ret void
69+
}
70+
71+
define dso_local void @j() {
72+
entry:
73+
store i8 44, ptr @h
74+
ret void
75+
}
76+
77+
define dso_local void @k() {
78+
entry:
79+
store i8 45, ptr @h
80+
ret void
81+
}
5382

5483
; Function Attrs: nounwind uwtable
5584
define dso_local i32 @main() local_unnamed_addr {

0 commit comments

Comments
 (0)