Skip to content

The ThreadLocal annotation isn't portable (but it could) #16176

@ysbaddaden

Description

@ysbaddaden

The @[ThreadLocal] annotation isn't portable to some targets, namely:

  • Android (API level < 29)
  • OpenBSD
  • Windows (MinGW)
  • some other targets that we don't support (e.g. OpenHarmony)

That makes using thread locals more complex than necessary (see #15616, #16029, #16168 and #16173) by requiring to check compilation flags, falling back to using the pthreads and Windows TLS or FLS APIs...

But LLVM supports emulated TLS (thread local storage) through the llvm::TargetOptions::EmulatedTLS setting for the target machine. Rust and Zig, for example, enable the setting on the LLVM target machine for the above targets.

The issue is that the LLVM doesn't expose the setting though the C API. It's only available to C++. Both Rust and Zig implement custom alternatives to LLVMCreateTargetMachine to support more than the very few settings exposed by LLVMTargetMachineOptions for example, so we'd either have to bring llvm_ext.cc back or have to patch LLVM to add the feature, or actually do both, so we'd be backward and forward compatible.

NOTE: the setting might be enabled by default for some targets. For example I tried the annotation on OpenBSD with LLVM 19, both natively in a VM and through cross compilation, and it just worked, but there are no guarantees (nor since which LLVM version did it happen, or maybe it's been patched in).

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions