1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -triple=i686-pc-win32 -fms-extensions -emit-llvm -o - | FileCheck %s
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc int f();
4*0a6a1f1dSLionel Sambuc
5*0a6a1f1dSLionel Sambuc // CHECK: $"\01?x@selectany_init@@3HA" = comdat any
6*0a6a1f1dSLionel Sambuc // CHECK: $"\01?x@?$A@H@explicit_template_instantiation@@2HB" = comdat any
7*0a6a1f1dSLionel Sambuc // CHECK: $"\01?x@?$A@H@implicit_template_instantiation@@2HB" = comdat any
8*0a6a1f1dSLionel Sambuc
9*0a6a1f1dSLionel Sambuc namespace simple_init {
10*0a6a1f1dSLionel Sambuc #pragma init_seg(compiler)
11*0a6a1f1dSLionel Sambuc int x = f();
12*0a6a1f1dSLionel Sambuc // CHECK: @"\01?x@simple_init@@3HA" = global i32 0, align 4
13*0a6a1f1dSLionel Sambuc // CHECK: @__cxx_init_fn_ptr = private constant void ()* @"\01??__Ex@simple_init@@YAXXZ", section ".CRT$XCC"
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #pragma init_seg(lib)
16*0a6a1f1dSLionel Sambuc int y = f();
17*0a6a1f1dSLionel Sambuc // CHECK: @"\01?y@simple_init@@3HA" = global i32 0, align 4
18*0a6a1f1dSLionel Sambuc // CHECK: @__cxx_init_fn_ptr1 = private constant void ()* @"\01??__Ey@simple_init@@YAXXZ", section ".CRT$XCL"
19*0a6a1f1dSLionel Sambuc
20*0a6a1f1dSLionel Sambuc #pragma init_seg(user)
21*0a6a1f1dSLionel Sambuc int z = f();
22*0a6a1f1dSLionel Sambuc // CHECK: @"\01?z@simple_init@@3HA" = global i32 0, align 4
23*0a6a1f1dSLionel Sambuc // No function pointer! This one goes on @llvm.global_ctors.
24*0a6a1f1dSLionel Sambuc }
25*0a6a1f1dSLionel Sambuc
26*0a6a1f1dSLionel Sambuc #pragma init_seg(".asdf")
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc namespace internal_init {
29*0a6a1f1dSLionel Sambuc namespace {
30*0a6a1f1dSLionel Sambuc int x = f();
31*0a6a1f1dSLionel Sambuc // CHECK: @"\01?x@?A@internal_init@@3HA" = internal global i32 0, align 4
32*0a6a1f1dSLionel Sambuc // CHECK: @__cxx_init_fn_ptr2 = private constant void ()* @"\01??__Ex@?A@internal_init@@YAXXZ", section ".asdf"
33*0a6a1f1dSLionel Sambuc }
34*0a6a1f1dSLionel Sambuc }
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc namespace selectany_init {
37*0a6a1f1dSLionel Sambuc int __declspec(selectany) x = f();
38*0a6a1f1dSLionel Sambuc // CHECK: @"\01?x@selectany_init@@3HA" = weak_odr global i32 0, comdat, align 4
39*0a6a1f1dSLionel Sambuc // CHECK: @__cxx_init_fn_ptr3 = private constant void ()* @"\01??__Ex@selectany_init@@YAXXZ", section ".asdf", comdat($"\01?x@selectany_init@@3HA")
40*0a6a1f1dSLionel Sambuc }
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc namespace explicit_template_instantiation {
43*0a6a1f1dSLionel Sambuc template <typename T> struct A { static const int x; };
44*0a6a1f1dSLionel Sambuc template <typename T> const int A<T>::x = f();
45*0a6a1f1dSLionel Sambuc template struct A<int>;
46*0a6a1f1dSLionel Sambuc // CHECK: @"\01?x@?$A@H@explicit_template_instantiation@@2HB" = weak_odr global i32 0, comdat, align 4
47*0a6a1f1dSLionel Sambuc // CHECK: @__cxx_init_fn_ptr4 = private constant void ()* @"\01??__Ex@?$A@H@explicit_template_instantiation@@2HB@YAXXZ", section ".asdf", comdat($"\01?x@?$A@H@explicit_template_instantiation@@2HB")
48*0a6a1f1dSLionel Sambuc }
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc namespace implicit_template_instantiation {
51*0a6a1f1dSLionel Sambuc template <typename T> struct A { static const int x; };
52*0a6a1f1dSLionel Sambuc template <typename T> const int A<T>::x = f();
g()53*0a6a1f1dSLionel Sambuc int g() { return A<int>::x; }
54*0a6a1f1dSLionel Sambuc // CHECK: @"\01?x@?$A@H@implicit_template_instantiation@@2HB" = linkonce_odr global i32 0, comdat, align 4
55*0a6a1f1dSLionel Sambuc // CHECK: @__cxx_init_fn_ptr5 = private constant void ()* @"\01??__Ex@?$A@H@implicit_template_instantiation@@2HB@YAXXZ", section ".asdf", comdat($"\01?x@?$A@H@implicit_template_instantiation@@2HB")
56*0a6a1f1dSLionel Sambuc }
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc // ... and here's where we emitted user level ctors.
59*0a6a1f1dSLionel Sambuc // CHECK: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }]
60*0a6a1f1dSLionel Sambuc // CHECK: [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_pragma_init_seg.cpp, i8* null }]
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc // We have to mark everything used so we can survive globalopt, even through
63*0a6a1f1dSLionel Sambuc // LTO. There's no way LLVM could really understand if data in the .asdf
64*0a6a1f1dSLionel Sambuc // section is really used or dead.
65*0a6a1f1dSLionel Sambuc //
66*0a6a1f1dSLionel Sambuc // CHECK: @llvm.used = appending global [6 x i8*]
67*0a6a1f1dSLionel Sambuc // CHECK: [i8* bitcast (void ()** @__cxx_init_fn_ptr to i8*),
68*0a6a1f1dSLionel Sambuc // CHECK: i8* bitcast (void ()** @__cxx_init_fn_ptr1 to i8*),
69*0a6a1f1dSLionel Sambuc // CHECK: i8* bitcast (void ()** @__cxx_init_fn_ptr2 to i8*),
70*0a6a1f1dSLionel Sambuc // CHECK: i8* bitcast (void ()** @__cxx_init_fn_ptr3 to i8*),
71*0a6a1f1dSLionel Sambuc // CHECK: i8* bitcast (void ()** @__cxx_init_fn_ptr4 to i8*),
72*0a6a1f1dSLionel Sambuc // CHECK: i8* bitcast (void ()** @__cxx_init_fn_ptr5 to i8*)], section "llvm.metadata"
73