[llvm] 9181ce6 - [Windows] Put init_seg(compiler/lib) in llvm.global_ctors
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 16 08:25:45 PDT 2022
Author: Arthur Eubanks
Date: 2022-08-16T08:16:18-07:00
New Revision: 9181ce623fd8189252659da7c48de1982597b79c
URL: https://github.com/llvm/llvm-project/commit/9181ce623fd8189252659da7c48de1982597b79c
DIFF: https://github.com/llvm/llvm-project/commit/9181ce623fd8189252659da7c48de1982597b79c.diff
LOG: [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
Added:
Modified:
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
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 6afd3063524c9..32084bf512bc1 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -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.
}];
diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp
index 420141362f0e2..8ceb4863d6ead 100644
--- a/clang/lib/CodeGen/CGDeclCXX.cpp
+++ b/clang/lib/CodeGen/CGDeclCXX.cpp
@@ -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());
diff --git a/clang/test/CodeGenCXX/pragma-init_seg.cpp b/clang/test/CodeGenCXX/pragma-init_seg.cpp
index 187a06a0c0f0c..9ac8e33554cc4 100644
--- a/clang/test/CodeGenCXX/pragma-init_seg.cpp
+++ b/clang/test/CodeGenCXX/pragma-init_seg.cpp
@@ -10,12 +10,12 @@ namespace simple_init {
#pragma init_seg(compiler)
int x = f();
// CHECK: @"?x at simple_init@@3HA" = dso_local global i32 0, align 4
-// CHECK: @__cxx_init_fn_ptr = private constant ptr @"??__Ex at 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 at simple_init@@3HA" = dso_local global i32 0, align 4
-// CHECK: @__cxx_init_fn_ptr.1 = private constant ptr @"??__Ey at 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 at selectany_init@@3HA" = weak_odr dso_local global i32 0, comdat, align 4
-// CHECK: @__cxx_init_fn_ptr.3 = private constant ptr @"??__Ex at selectany_init@@YAXXZ", section ".asdf", comdat($"?x at selectany_init@@3HA")
+// CHECK: @__cxx_init_fn_ptr.1 = private constant ptr @"??__Ex at selectany_init@@YAXXZ", section ".asdf", comdat($"?x at 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 at 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 at H@explicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A at H@explicit_template_instantiation@@2HB")
+// CHECK: @__cxx_init_fn_ptr.2 = private constant ptr @"??__E?x@?$A at H@explicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A at 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 at 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 at H@implicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A at H@implicit_template_instantiation@@2HB")
+// CHECK: @__cxx_init_fn_ptr.3 = private constant ptr @"??__E?x@?$A at H@implicit_template_instantiation@@2HB@@YAXXZ", section ".asdf", comdat($"?x@?$A at 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 at simple_init@@YAXXZ", ptr @"?x at simple_init@@3HA" }, { i32, ptr, ptr } { i32 400, ptr @"??__Ey at simple_init@@YAXXZ", ptr @"?y at 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"
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 5859937632812..941e6a2c648ed 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -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());
diff --git a/llvm/test/CodeGen/X86/ctor-priority-coff.ll b/llvm/test/CodeGen/X86/ctor-priority-coff.ll
index 22014fe9a6ac8..2e4e7ca0c5dcf 100644
--- a/llvm/test/CodeGen/X86/ctor-priority-coff.ll
+++ b/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 @@ $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
- at llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [
+ at 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 {
More information about the llvm-commits
mailing list