Skip to content

[k2] use initial-exec TLS model for performance critical thread locals#1629

Merged
apolyakov merged 2 commits into
masterfrom
apolyakov/k2-initial-exec-tls
May 21, 2026
Merged

[k2] use initial-exec TLS model for performance critical thread locals#1629
apolyakov merged 2 commits into
masterfrom
apolyakov/k2-initial-exec-tls

Conversation

@apolyakov
Copy link
Copy Markdown
Contributor

@apolyakov apolyakov commented May 18, 2026

The initial-exec TLS model provides faster access to thread-local variables by using a more efficient addressing mode that avoids dynamic TLS resolution at runtime. Since these state pointers are accessed frequently in hot paths, caching them in thread-local storage with initial-exec model improves performance.

The state pointers are now set once during lifecycle transitions (image/component/instance creation and initialization, and at poll entry) and then accessed directly from TLS rather than calling through the C API layer each time.

Note

The initial-exec TLS model is generally unsafe for dynamically loaded libraries (it requires TLS to be allocated at program startup). This is acceptable for K2 because the runtime is loaded in a controlled environment where this constraint is satisfied. In case of any problems we can try -mtls-dialect=gnu2 which is safe but a bit slower than initial-exec TLS model.

@apolyakov apolyakov force-pushed the apolyakov/k2-initial-exec-tls branch from 21cda52 to 88a5e3a Compare May 18, 2026 18:00
@apolyakov apolyakov force-pushed the apolyakov/k2-initial-exec-tls branch from 88a5e3a to 41b63e2 Compare May 18, 2026 18:10
@apolyakov apolyakov changed the title [k2] use -ftls-model=initial-exec [k2] use initial-exec TLS model for performance critical thread locals May 18, 2026
@apolyakov apolyakov marked this pull request as ready for review May 18, 2026 18:12
@apolyakov apolyakov self-assigned this May 18, 2026
@apolyakov apolyakov added optimization Memory comsumption / CPU speedup runtime Feature related to runtime k2 Affects compiler or runtime in K2 mode labels May 18, 2026
@apolyakov apolyakov added this to the next milestone May 18, 2026
Comment thread runtime-light/k2-platform/k2-api.h
Comment thread runtime-light/runtime-light.cpp
@apolyakov apolyakov merged commit 3aa0943 into master May 21, 2026
7 checks passed
@apolyakov apolyakov deleted the apolyakov/k2-initial-exec-tls branch May 21, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

k2 Affects compiler or runtime in K2 mode optimization Memory comsumption / CPU speedup runtime Feature related to runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants