Bug 81880 - thread_local static member template initialisation fails
Summary: thread_local static member template initialisation fails
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: c++-thread_local
  Show dependency treegraph
 
Reported: 2017-08-17 12:44 UTC by maiphi.public
Modified: 2023-07-06 21:55 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 7.2.0
Last reconfirmed: 2021-08-22 00:00:00


Attachments
preprocessed source (113.39 KB, text/plain)
2017-08-17 12:44 UTC, maiphi.public
Details

Note You need to log in before you can comment on or make changes to this bug.
Description maiphi.public 2017-08-17 12:44:53 UTC
Created attachment 41997 [details]
preprocessed source

thread_local static member templates do not get initialised.

Example code:

#include <unordered_map>
#include <iostream>

class A {
public:
  template<typename T>
  thread_local static std::unordered_map<int,T> m;
};

template<typename T>
thread_local std::unordered_map<int,T> A::m{};

int main() {
  // A::m<int> = std::unordered_map<int,int>{}; // workaround
  std::cout << A::m<int>.bucket_count() << std::endl; // returns zero.
  A::m<int>.insert({1,2}); // causes SIGPFE (hash modulo bucket_count)
}

The unordered_map is not initialised and has a bucket count of zero. This leads to a zero division when the hash is taken modulo the bucket count. Without the thread_local or without the template it works fine. A workaround is to initialise the member manually in every thread which uses it (commented line).

OS: opensuse Leap 42.3, kernel 4.4.79-19-default x64
CPU: Intel Core i7-4790

$ g++ -o bug -v -save-temps -std=c++14 -Wall -Wextra -pedantic bug.cpp 
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-svn/configure --prefix=/path/to/gcc/svn --disable-multilib --enable-languages=c,c++,fortran
Thread model: posix
gcc version 8.0.0 20170817 (experimental) (GCC) 
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall' '-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/cc1plus -E -quiet -v -D_GNU_SOURCE bug.cpp -mtune=generic -march=x86-64 -std=c++14 -Wall -Wextra -Wpedantic -fpch-preprocess -o bug.ii
ignoring nonexistent directory "/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0/x86_64-pc-linux-gnu
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0/backward
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/include
 /usr/local/include
 /path/to/gcc/svn/include
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall' '-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/cc1plus -fpreprocessed bug.ii -quiet -dumpbase bug.cpp -mtune=generic -march=x86-64 -auxbase bug -Wall -Wextra -Wpedantic -std=c++14 -version -o bug.s
GNU C++14 (GCC) version 8.0.0 20170817 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 8.0.0 20170817 (experimental), GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++14 (GCC) version 8.0.0 20170817 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 8.0.0 20170817 (experimental), GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 7aa65dd64f7a61cf2867713db376431f
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall' '-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o bug.o bug.s
GNU assembler version 2.26.1 (x86_64-suse-linux) using BFD version (GNU Binutils; openSUSE Leap 42.3) 2.26.1
COMPILER_PATH=/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:./:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall' '-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/collect2 -plugin /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/liblto_plugin.so -plugin-opt=/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper -plugin-opt=-fresolution=bug.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o bug /usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/crtbegin.o -L/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0 -L/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L. -L/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../.. bug.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/crtend.o /usr/lib/../lib64/crtn.o
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall' '-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'

$ ./bug
0
Floating point exception (core dumped)

(also confirmed with gcc 7.1.1 and gcc 5.2.0)
Comment 1 Richard Biener 2017-08-17 14:26:08 UTC
works with clang++
Comment 2 Latimerius 2018-09-18 09:16:59 UTC
This bug still seems to exist in the current 9.0 HEAD.  Note also that another possible work-around might be to wrap the thread_local member in an accessor function, along the lines of

class A {
public:
  template<typename T>
  std::unordered_map<int,T> & get_m ()
  {
    thread_local static std::unordered_map<int,T> m;
    return m;
  }
};
Comment 3 Toby Brull 2020-08-30 17:24:56 UTC
I played around a bit with gcc, and it looks like the example can be made to work via the following diff:

---------------------------------------

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 639b00264d8..f6b10174f1b 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3898,6 +3898,8 @@ finish_id_expression_1 (tree id_expression,
          decl = finish_template_variable (decl);
          mark_used (decl);
          decl = convert_from_reference (decl);
+         if (tree wrap = maybe_get_tls_wrapper_call (decl))
+           decl = wrap;
        }
       else if (concept_check_p (decl))
        {

---------------------------------------

Not sure if this makes sense, though, in the greater scheme of things. So I'll just leave that here FYI.
Comment 4 Andrew Pinski 2021-08-22 22:32:52 UTC
Reduced testcase:
extern "C" void abort(void);
struct tt
{
  int *tt1 = new int{1};
  int bucket_count() const {return *tt1;}
};
struct A {
  template<typename T> thread_local static tt m;
};
template<typename T> thread_local tt A::m{};
int main() {
  if ( A::m<int>.bucket_count() != 1) abort();
  return 0;
}
Comment 5 Timothee Besset 2023-07-06 21:55:14 UTC
we are observing this with gcc 10.3.0