Issue 161100
Summary [clang] Inlined method creates separate thread_local variables in each C++ module
Labels clang
Assignees
Reporter smilediver
    The following program seems to instantiate separate `thread_local` variables for each C++ module. I'm not sure if this is a bug or expected behavior. If I remove `static` from `test()`, or remove either `inline` or `__attribute__((always_inline))` from `scope::scope()`, then only a single `thread_local` variable is instantiated.

```
clang --version
clang version 21.1.2
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
```

main.cppm
```cpp
module;

export module app;
import a;
import b;
import var;

extern "C++" int main() {
 scope s;
    a_test();
    b_test();
    return 0;
}
```

a.cppm
```cpp
module;

export module a;
import var;

export void a_test() {
    scope s;
}
```


b.cppm
```cpp
module;

export module b;
import var;

export void b_test() {
    scope s;
}
```


var.cppm
```cpp
module;

#include <cstdio>

export module var;


struct thread_var {
    thread_var() {
 printf("thread_var ctor\n");
    }
};


static thread_var* test() {
 printf("test\n");
    thread_local thread_var v;
    return &v;
}


export struct scope {
    inline __attribute__((always_inline)) scope() {
        printf("scope\n");
        test();
 }
};
```


CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 4.0)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

project(test)

add_executable(test)

target_sources(test
 PUBLIC FILE_SET CXX_MODULES FILES
        main.cppm
        a.cppm
 b.cppm
        var.cppm
)
```

Expected output:
```
scope
test
thread_var ctor
scope
test
scope
test
```

Actual output:
```
scope
test
thread_var ctor
scope
test
thread_var ctor
scope
test
thread_var ctor
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to