r357610 - Bug-40323: MS ABI adding template static member in the linker directive section to make sure init function can be called before main.

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 11 14:04:28 PDT 2019


Hi Jennifer,

this caused https://bugs.llvm.org/show_bug.cgi?id=41471

Do you think this is easy to fix, or should we revert for now?

Thanks,
Nico

On Wed, Apr 3, 2019 at 1:20 PM Jennifer Yu via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> Author: jyu2
> Date: Wed Apr  3 10:21:40 2019
> New Revision: 357610
>
> URL: http://llvm.org/viewvc/llvm-project?rev=357610&view=rev
> Log:
> Bug-40323: MS ABI adding template static member in the linker directive
> section to make sure init function can be called before main.
>
> Added:
>     cfe/trunk/test/CodeGenCXX/microsoft-abi-template-static-init.cpp
> Modified:
>     cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
>
> Modified: cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=357610&r1=357609&r2=357610&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGDeclCXX.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGDeclCXX.cpp Wed Apr  3 10:21:40 2019
> @@ -481,6 +481,12 @@ CodeGenModule::EmitCXXGlobalVarDeclInitF
>      // minor startup time optimization.  In the MS C++ ABI, there are no
> guard
>      // variables, so this COMDAT key is required for correctness.
>      AddGlobalCtor(Fn, 65535, COMDATKey);
> +    if (getTarget().getCXXABI().isMicrosoft()) {
> +      // In The MS C++, MS add template static data member in the linker
> +      // drective.
> +      assert(COMDATKey);
> +      addUsedGlobal(COMDATKey);
> +    }
>    } else if (D->hasAttr<SelectAnyAttr>()) {
>      // SelectAny globals will be comdat-folded. Put the initializer into a
>      // COMDAT group associated with the global, so the initializers get
> folded
>
> Added: cfe/trunk/test/CodeGenCXX/microsoft-abi-template-static-init.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/microsoft-abi-template-static-init.cpp?rev=357610&view=auto
>
> ==============================================================================
> --- cfe/trunk/test/CodeGenCXX/microsoft-abi-template-static-init.cpp
> (added)
> +++ cfe/trunk/test/CodeGenCXX/microsoft-abi-template-static-init.cpp Wed
> Apr  3 10:21:40 2019
> @@ -0,0 +1,57 @@
> +// RUN: %clang_cc1 %s -triple=i686-pc-win32 -fms-extensions -emit-llvm -o
> - | FileCheck %s
> +// RUN: %clang_cc1 %s -triple=x86_64-pc-win32 -fms-extensions -emit-llvm
> -o - | FileCheck %s
> +// RUN: %clang_cc1 %s -triple=i686-pc-windows-msvc -fms-extensions
> -emit-llvm -o - | FileCheck %s
> +// RUN: %clang_cc1 %s -triple=x86_64-pc-windows-msvc -fms-extensions
> -emit-llvm -o - | FileCheck %s
> +
> +struct S {
> +  S();
> +  ~S();
> +};
> +
> +template <typename T> struct __declspec(dllexport) ExportedTemplate {
> +  static S s;
> +};
> +template <typename T> S ExportedTemplate<T>::s;
> +void useExportedTemplate(ExportedTemplate<int> x) {
> +  (void)x.s;
> +}
> +int f();
> +namespace selectany_init {
> +// MS don't put selectany static var in the linker directive, init routine
> +// f() is not getting called if x is not referenced.
> +int __declspec(selectany) x = f();
> +}
> +
> +namespace explicit_template_instantiation {
> +template <typename T> struct A { static  int x; };
> +template <typename T> int A<T>::x = f();
> +template struct A<int>;
> +}
> +
> +namespace implicit_template_instantiation {
> +template <typename T> struct A { static  int x; };
> +template <typename T>  int A<T>::x = f();
> +int g() { return A<int>::x; }
> +}
> +
> +
> +template <class T>
> +struct X_ {
> +  static T ioo;
> +  static T init();
> +};
> +template <class T> T X_<T>::ioo = X_<T>::init();
> +template struct X_<int>;
> +
> +template <class T>
> +struct X {
> +  static T ioo;
> +  static T init();
> +};
> +// template specialized static data don't need in llvm.used,
> +// the static init routine get call from _GLOBAL__sub_I_ routines.
> +template <> int X<int>::ioo = X<int>::init();
> +template struct X<int>;
> +// CHECK: @llvm.global_ctors = appending global [6 x { i32, void ()*, i8*
> }] [{ i32, void ()*, i8* } { i32 65535, void ()* @"??__Ex at selectany_init@@YAXXZ",
> i8* bitcast (i32* @"?x at selectany_init@@3HA" to i8*) }, { i32, void ()*,
> i8* } { i32 65535, void ()* @"??__E?x@?$A at H
> @explicit_template_instantiation@@2HA@@YAXXZ", i8* bitcast (i32* @"?x@
> ?$A at H@explicit_template_instantiation@@2HA" to i8*) }, { i32, void ()*,
> i8* } { i32 65535, void ()* @"??__E?ioo@?$X_ at H@@2HA@@YAXXZ", i8* bitcast
> (i32* @"?ioo@?$X_ at H@@2HA" to i8*) }, { i32, void ()*, i8* } { i32 65535,
> void ()* @"??__E?s@?$ExportedTemplate at H@@2US@@A@@YAXXZ", i8*
> getelementptr inbounds (%struct.S, %struct.S* @"?s@?$ExportedTemplate at H
> @@2US@@A", i32 0, i32 0) }, { i32, void ()*, i8* } { i32 65535, void ()*
> @"??__E?x@?$A at H@implicit_template_instantiation@@2HA@@YAXXZ", i8* bitcast
> (i32* @"?x@?$A at H@implicit_template_instantiation@@2HA" to i8*) }, { i32,
> void ()*, i8* } { i32 65535, void ()*
> @_GLOBAL__sub_I_microsoft_abi_template_static_init.cpp, i8* null }]
> +// CHECK: @llvm.used = appending global [4 x i8*] [i8* bitcast (i32* @"?x@
> ?$A at H@explicit_template_instantiation@@2HA" to i8*), i8* bitcast (i32*
> @"?ioo@?$X_ at H@@2HA" to i8*), i8* getelementptr inbounds (%struct.S,
> %struct.S* @"?s@?$ExportedTemplate at H@@2US@@A", i32 0, i32 0), i8* bitcast
> (i32* @"?x@?$A at H@implicit_template_instantiation@@2HA" to i8*)], section
> "llvm.metadata"
> +
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190411/4c3ee46d/attachment.html>


More information about the cfe-commits mailing list