[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
This commit is contained in:
Arthur Eubanks 2022-08-15 11:04:50 -07:00
parent ba1c396e09
commit 9181ce623f
5 changed files with 73 additions and 18 deletions

View File

@ -127,6 +127,10 @@ relative ordering of values is important. For example:
``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.
}];

View File

@ -553,7 +553,18 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
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());

View File

@ -10,12 +10,12 @@ namespace simple_init {
#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 internal_init {
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> struct A { static const int x; };
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> struct A { static const int x; };
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"

View File

@ -1889,11 +1889,24 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
// 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());

View File

@ -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 @@ $h = comdat any
@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 @@ entry:
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 {