Skip to content

Make GNU __thread variable templates thread local [GH #536] - #538

Open
mborland wants to merge 1 commit into
edgcpp:mainfrom
mborland:fix/536
Open

mborland wants to merge 1 commit into
edgcpp:mainfrom
mborland:fix/536

Conversation

@mborland

@mborland mborland commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Currently, GNU __thread variable templates are given static storage duration instead of thread storage duration. This PR fixes this.

Closes: #536

Summary by CodeRabbit

  • Bug Fixes
    • GNU __thread variable-template instances now behave as thread-local variables, with separate storage and initialization for each thread.
    • Corrected C23 code generation for file-scope __thread variables with internal linkage.
    • Improved diagnostics for thread-local variable templates with dynamic initialization or addresses used in constant expressions.
  • Documentation
    • Added guidance and examples covering thread-local variable templates and related diagnostics.

@mborland
mborland requested a review from a team November 13, 2026 15:37
@mborland mborland added bug Something isn't working back end - c labels Nov 13, 2026
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The compiler now propagates GNU __thread to variable-template instances and checks their declaration modifiers. C23 generated-C handling recognizes these variables as thread-local. New regression tests cover thread isolation, specializations, dynamic initialization diagnostics, and constant-expression address use.

Changes: GNU thread-local variable templates

Layer / File(s) Summary
Template modifier propagation and checks
src/decls.c, src/decls.h, src/templates.c
The declaration-modifier check is available outside src/decls.c. Variable-template processing copies DM_THREAD to instances, checks initialization against declaration modifiers, and clears the modifier on a specialization’s first declaration.
C23 generated-C handling
src/c_gen_be.c
File-scope variable selection recognizes DM_THREAD when the relevant declaration-modifier and TLS-specifier options are enabled. It omits non-extern thread-local variables from the non-defining pass and includes them in the definition pass.
Regression tests and issue note
tests/tests/regressions/gh-536*.sft.cpp, tests/tests/regressions/.gh-536*.rto/*, src/pending_changes/gh-536-thread-variable-template.txt
Regression tests cover thread-local instance behavior, specializations, dynamic initialization diagnostics, and constant-expression address use. Test fixtures record GCC and Clang runs. The issue note describes the C23 code-generation case.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: daveedvdv-nvidia, wchilders-nvidia

Merge Risk: Minimal · up to 3fc37

The change makes GNU __thread variable-template instances thread-local, adds C23 generated-C handling for them, and adds regression tests for runtime and diagnostic behavior. No concrete merge-blocking defect was found in the reviewed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title says the change makes GNU __thread variable templates thread local, and [GH #536] names the linked issue. The changeset matches this. The templates.c change copies DM_THREAD onto variable-te…
Linked Issues check Passed [#536] Requirement: instances of GNU __thread variable templates must have thread storage duration. The reviewed summary shows that src/templates.c copies the DM_THREAD modifier, recorded during…
Out of Scope Changes check Passed All listed changes support the #536 fix. src/c_gen_be.c and src/templates.c implement the thread-local handling. src/decls.c and src/decls.h change check_variable_decl_modifiers from file-lo…
Full details: Title check

Explanation

The title says the change makes GNU __thread variable templates thread local, and [GH #536] names the linked issue. The changeset matches this. The templates.c change copies DM_THREAD onto variable-template instances, the C23 code generation in c_gen_be.c handles __thread file-scope variables, and the new gh-536 regression tests check distinct per-thread addresses. The title is specific and concise, so it passes.

Full details: Linked Issues check

Explanation

[#536] Requirement: instances of GNU __thread variable templates must have thread storage duration. The reviewed summary shows that src/templates.c copies the DM_THREAD modifier, recorded during declaration rescan, onto variable-template instances when thread-local storage specifiers are supported. src/c_gen_be.c adds is_thread_local_in_generated_c, which treats DM_THREAD as thread-local, so __thread declarations get the same file-scope handling as thread_local declarations. This addresses the C back-end cause named in the issue. tests/tests/regressions/gh-536.sft.cpp checks that each tested thread-local instance has a distinct address per thread, starts with its expected value, and keeps modifications local to the thread. The explicit tv<long> specialization is documented as shared, which matches normal explicit-specialization semantics. Automated test coverage exists. This assessment relies on the AI-generated summary and the listed changes; I did not inspect the diff hunks directly, so the exact code paths are not independently verified.

Full details: Out of Scope Changes check

Explanation

All listed changes support the #536 fix. src/c_gen_be.c and src/templates.c implement the thread-local handling. src/decls.c and src/decls.h change check_variable_decl_modifiers from file-local to external linkage, with a no-op macro when DECL_MODIFIERS_IN_USE is disabled. This exposes the helper needed by templates.c and is a supporting change. The added diagnostics for dynamic initialization of thread-local template variables and for taking an instance address in a constant expression are checked by the negative regression tests. These diagnostics follow from thread-local semantics that the issue's thread_local behavior already defines. The pending-changes note and the regression fixtures document and test the change. No unrelated change is identified in the summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between cd21a3e and 3fc3755.

Files selected for processing: 15

  • src/c_gen_be.c
  • src/decls.c
  • src/decls.h
  • src/pending_changes/gh-536-thread-variable-template.txt
  • src/templates.c
  • tests/tests/regressions/.gh-536-negative.rto/default.1.1.txt
  • tests/tests/regressions/.gh-536-negative.rto/default.2.1.txt
  • tests/tests/regressions/.gh-536.rto/default.1.1.txt
  • tests/tests/regressions/.gh-536.rto/default.1.2.txt
  • tests/tests/regressions/.gh-536.rto/default.2.1.txt
  • tests/tests/regressions/.gh-536.rto/default.2.2.txt
  • tests/tests/regressions/.gh-536.rto/default.3.1.txt
  • tests/tests/regressions/.gh-536.rto/default.3.2.txt
  • tests/tests/regressions/gh-536-negative.sft.cpp
  • tests/tests/regressions/gh-536.sft.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 3fc37557739f:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 3fc37557739f:

CI success.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

back end - c bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GNU __thread variable templates are not thread local

2 participants