[llvm-bugs] [Bug 45351] New: function static variable instantiated at -O2 despite extern template

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Mar 29 13:50:28 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45351

            Bug ID: 45351
           Summary: function static variable instantiated at -O2 despite
                    extern template
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: sbence92 at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Created attachment 23291
  --> https://bugs.llvm.org/attachment.cgi?id=23291&action=edit
example

In the below example func is inlined and function_local_static becomes a
defined symbol when compiled with -O2 and above regardless of extern template
class declaration.

The problem is solved by adding -fno-inline .
Same can be observed with clang above 5.0 (works with 4.0.1), all gcc versions
(bugzilla ID 92359), icc, but not with msvc.

This is problematic when one tries to rely on the extern template mechanism in
shared libraries. The result is that function static variables get instantiated
more than 1 times.
Attached you find a minimal example showing exactly this.

minimal example that shows function_local_static being defined at -O2:

template <typename T>
struct S {
  int func() {
    static int function_local_static = 0;
    return ++function_local_static;
  }
};

extern template class S<int>;

int user()
{
    return S<int>().func();
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200329/fac9e418/attachment-0001.html>


More information about the llvm-bugs mailing list