Skip to content

[clang] Inlined method creates separate thread_local variables in each C++ module #161100

@smilediver

Description

@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

module;

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

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

a.cppm

module;

export module a;
import var;

export void a_test() {
    scope s;
}

b.cppm

module;

export module b;
import var;

export void b_test() {
    scope s;
}

var.cppm

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_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

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:modulesC++20 modules and Clang Header Modules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions