<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/79714>79714</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang] [MinGW] Dllimported thread local variables not allowed when using emulated TLS
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mstorsjo
</td>
</tr>
</table>
<pre>
Clang uses Windows native TLS by default for MinGW targets, contrary to GCC, which uses emulated TLS. Native TLS has one big practical limitation; one cannot directly access such a TLS variable from another DLL - while this is allowed with emulated TLS.
When declaring a dllimported variable as thread local, Clang rightfully errors out when using native TLS. However, when using emulated TLS, such a construct could be allowed - GCC does allow and compile it:
```c
extern __declspec(dllimport) int __thread libdata;
int get(void) { return libdata; }
```
```console
$ x86_64-w64-mingw32-gcc tls-dllimport.c -S -o - -O2
get:
subq $40, %rsp
movq __imp___emutls_v.libdata(%rip), %rcx
call __emutls_get_address
movl (%rax), %eax
addq $40, %rsp
ret
$ clang -target x86_64-w64-mingw32 -c tls-dllimport.c -femulated-tls
tls-dllimport.c:1:43: error: 'libdata' cannot be thread local when declared 'dllimport'
1 | extern __declspec(dllimport) int __thread libdata;
| ^
1 error generated.
```
The error is entirely accurate when using native TLS, but with `-femulated-tls`, one could consider that it should be allowed - as GCC does allow it.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVcFu4zYQ_Rr6MpAhU5JlHXRI7GZ7SNvDLpCjQJFjiQuKdMmRnfx9QUm2k2zQHhoEtkTOPM57fjMUIejOItaseGTFYSVG6p2vh0DOh59u1Tr1Vu-NsB2MAQO8aKvcJYAVpM8IP56_Q_sGCo9iNARH5-EPbb-9AAnfIQXG9yCdJS_8G5CDb_t9XLr0WvYzIA6jEYQqQq3hzztsLwI4i9DqDk5eSNJSGDB60CRIO8uyx2lfCmsdgdIeJZk3EFJiCBBG2YOYkM7Ca9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