[clang] [CIR] Add support for thread-local storage (TLS) (PR #168662)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 3 12:25:11 PST 2025
https://github.com/adams381 updated https://github.com/llvm/llvm-project/pull/168662
>From 36b3d82dee8fbd0adc37ce92b49fbf253d5fd477 Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Tue, 18 Nov 2025 20:06:19 -0800
Subject: [PATCH] [CIR] Add support for thread-local storage (TLS)
This commit adds full support for thread-local storage variables in ClangIR,
including code generation, lowering to LLVM IR, and comprehensive testing.
Changes include:
- Added CIR_TLSModel enum with 4 TLS models (GeneralDynamic, LocalDynamic,
InitialExec, LocalExec) to CIROps.td
- Extended GlobalOp with optional tls_model attribute
- Extended GetGlobalOp with thread_local unit attribute
- Added verification to ensure thread_local GetGlobalOp references globals
with tls_model set
- Implemented GetDefaultCIRTLSModel() and setTLSMode() in CIRGenModule
- Updated getAddrOfGlobalVar() to handle TLS access
- Removed MissingFeatures assertions for TLS operations
- Added lowering of GetGlobalOp with TLS to llvm.threadlocal.address intrinsic
- Added lowering of GlobalOp with tls_model to LLVM thread_local globals
- Added comprehensive test with CIR, LLVM, and OGCG checks
Known limitations (matching incubator):
- Static local TLS variables not yet implemented
- TLS_Dynamic with wrapper functions not yet implemented
This fixes issue #153270
---
.../CIR/Dialect/Builder/CIRBaseBuilder.h | 13 +++---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 17 ++++++-
clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 3 +-
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 7 +--
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 45 ++++++++++++++++---
clang/lib/CIR/CodeGen/CIRGenModule.h | 7 +++
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 5 ++-
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 12 ++---
clang/test/CIR/CodeGen/tls.c | 29 ++++++++++++
9 files changed, 116 insertions(+), 22 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/tls.c
diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 3288f5b12c77e..7bafa42df2739 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -300,14 +300,17 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return cir::GlobalViewAttr::get(type, symbol, indices);
}
- mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global) {
+ mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global,
+ bool threadLocal = false) {
assert(!cir::MissingFeatures::addressSpace());
- return cir::GetGlobalOp::create(
- *this, loc, getPointerTo(global.getSymType()), global.getSymName());
+ auto getGlobalOp = cir::GetGlobalOp::create(
+ *this, loc, getPointerTo(global.getSymType()), global.getSymNameAttr(),
+ threadLocal);
+ return getGlobalOp.getAddr();
}
- mlir::Value createGetGlobal(cir::GlobalOp global) {
- return createGetGlobal(global.getLoc(), global);
+ mlir::Value createGetGlobal(cir::GlobalOp global, bool threadLocal = false) {
+ return createGetGlobal(global.getLoc(), global, threadLocal);
}
/// Create a copy with inferred length.
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index dc56db1bbd4ea..37da115aad8a9 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -1882,6 +1882,13 @@ def CIR_GlobalLinkageKind : CIR_I32EnumAttr<
// properties of a global variable will be added over time as more of ClangIR
// is upstreamed.
+def CIR_TLSModel : CIR_I32EnumAttr<"TLS_Model", "TLS model", [
+ I32EnumAttrCase<"GeneralDynamic", 0, "tls_dyn">,
+ I32EnumAttrCase<"LocalDynamic", 1, "tls_local_dyn">,
+ I32EnumAttrCase<"InitialExec", 2, "tls_init_exec">,
+ I32EnumAttrCase<"LocalExec", 3, "tls_local_exec">
+]>;
+
def CIR_GlobalOp : CIR_Op<"global", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<CIRGlobalValueInterface>,
@@ -1910,6 +1917,7 @@ def CIR_GlobalOp : CIR_Op<"global", [
OptionalAttr<StrAttr>:$sym_visibility,
TypeAttr:$sym_type,
CIR_GlobalLinkageKind:$linkage,
+ OptionalAttr<CIR_TLSModel>:$tls_model,
OptionalAttr<AnyAttr>:$initial_value,
UnitAttr:$comdat,
UnitAttr:$constant,
@@ -1925,6 +1933,7 @@ def CIR_GlobalOp : CIR_Op<"global", [
(`constant` $constant^)?
$linkage
(`comdat` $comdat^)?
+ ($tls_model^)?
(`dso_local` $dso_local^)?
$sym_name
custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value,
@@ -1988,16 +1997,22 @@ def CIR_GetGlobalOp : CIR_Op<"get_global", [
undefined. The resulting type must always be a `!cir.ptr<...>` type with the
same address space as the global variable.
+ Addresses of thread local globals can only be retrieved if this operation
+ is marked `thread_local`, which indicates the address isn't constant.
+
Example:
```mlir
%x = cir.get_global @gv : !cir.ptr<i32>
+ ...
+ %y = cir.get_global thread_local @tls_gv : !cir.ptr<i32>
```
}];
- let arguments = (ins FlatSymbolRefAttr:$name);
+ let arguments = (ins FlatSymbolRefAttr:$name, UnitAttr:$tls);
let results = (outs Res<CIR_PointerType, "", []>:$addr);
let assemblyFormat = [{
+ (`thread_local` $tls^)?
$name `:` qualified(type($addr)) attr-dict
}];
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
index aeea0efeb77c3..27e9c7bc53fc4 100644
--- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
@@ -361,7 +361,8 @@ CIRGenModule::getOrCreateStaticVarDecl(const VarDecl &d,
if (supportsCOMDAT() && gv.isWeakForLinker())
gv.setComdat(true);
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
+ if (d.getTLSKind())
+ llvm_unreachable("TLS mode is NYI");
setGVProperties(gv, &d);
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 5ccb431e626ae..e2869bcba3b79 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -277,7 +277,6 @@ static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
QualType t = e->getType();
// If it's thread_local, emit a call to its wrapper function instead.
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
if (vd->getTLSKind() == VarDecl::TLS_Dynamic)
cgf.cgm.errorNYI(e->getSourceRange(),
"emitGlobalVarDeclLValue: thread_local variable");
@@ -312,7 +311,8 @@ static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
bool isVolatile, QualType ty,
bool isInit, bool isNontemporal) {
- assert(!cir::MissingFeatures::opLoadStoreThreadLocal());
+ // Traditional LLVM codegen handles thread local separately, CIR handles
+ // as part of getAddrOfGlobalVar (GetGlobalOp).
if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
// Boolean vectors use `iN` as storage type.
@@ -556,7 +556,8 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, LValue lvalue,
mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
QualType ty, SourceLocation loc,
LValueBaseInfo baseInfo) {
- assert(!cir::MissingFeatures::opLoadStoreThreadLocal());
+ // Traditional LLVM codegen handles thread local separately, CIR handles
+ // as part of getAddrOfGlobalVar (GetGlobalOp).
mlir::Type eltTy = addr.getElementType();
if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 9f9b2db4771df..8426d5dae110f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -679,8 +679,11 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
setLinkageForGV(gv, d);
- if (d->getTLSKind())
- errorNYI(d->getSourceRange(), "thread local global variable");
+ if (d->getTLSKind()) {
+ if (d->getTLSKind() == VarDecl::TLS_Dynamic)
+ errorNYI(d->getSourceRange(), "TLS dynamic");
+ setTLSMode(gv, *d);
+ }
setGVProperties(gv, d);
@@ -735,12 +738,13 @@ mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty,
if (!ty)
ty = getTypes().convertTypeForMem(astTy);
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
-
+ bool tlsAccess = d->getTLSKind() != VarDecl::TLS_None;
cir::GlobalOp g = getOrCreateCIRGlobal(d, ty, isForDefinition);
mlir::Type ptrTy = builder.getPointerTo(g.getSymType());
- return cir::GetGlobalOp::create(builder, getLoc(d->getSourceRange()), ptrTy,
- g.getSymName());
+ auto getGlobalOp = cir::GetGlobalOp::create(
+ builder, getLoc(d->getSourceRange()), ptrTy, g.getSymNameAttr(),
+ tlsAccess);
+ return getGlobalOp.getAddr();
}
cir::GlobalViewAttr CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *d) {
@@ -1898,6 +1902,35 @@ void CIRGenModule::setGVPropertiesAux(mlir::Operation *op,
assert(!cir::MissingFeatures::opGlobalPartition());
}
+cir::TLS_Model CIRGenModule::GetDefaultCIRTLSModel() const {
+ switch (getCodeGenOpts().getDefaultTLSModel()) {
+ case CodeGenOptions::GeneralDynamicTLSModel:
+ return cir::TLS_Model::GeneralDynamic;
+ case CodeGenOptions::LocalDynamicTLSModel:
+ return cir::TLS_Model::LocalDynamic;
+ case CodeGenOptions::InitialExecTLSModel:
+ return cir::TLS_Model::InitialExec;
+ case CodeGenOptions::LocalExecTLSModel:
+ return cir::TLS_Model::LocalExec;
+ }
+ llvm_unreachable("Invalid TLS model!");
+}
+
+void CIRGenModule::setTLSMode(mlir::Operation *op, const VarDecl &d) const {
+ assert(d.getTLSKind() && "setting TLS mode on non-TLS var!");
+
+ auto tlm = GetDefaultCIRTLSModel();
+
+ // Override the TLS model if it is explicitly specified.
+ if (d.getAttr<TLSModelAttr>()) {
+ llvm_unreachable("NYI");
+ }
+
+ auto global = dyn_cast<cir::GlobalOp>(op);
+ assert(global && "NYI for other operations");
+ global.setTlsModel(tlm);
+}
+
void CIRGenModule::setFunctionAttributes(GlobalDecl globalDecl,
cir::FuncOp func,
bool isIncompleteFunction,
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h
index 186913d1bac9d..44b810d1fffda 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.h
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -425,6 +425,13 @@ class CIRGenModule : public CIRGenTypeCache {
void setGVProperties(mlir::Operation *op, const NamedDecl *d) const;
void setGVPropertiesAux(mlir::Operation *op, const NamedDecl *d) const;
+ /// Set TLS mode for the given operation based on the given variable
+ /// declaration.
+ void setTLSMode(mlir::Operation *op, const VarDecl &d) const;
+
+ /// Get TLS mode from CodeGenOptions.
+ cir::TLS_Model GetDefaultCIRTLSModel() const;
+
/// Set function attributes for a function declaration.
void setFunctionAttributes(GlobalDecl gd, cir::FuncOp f,
bool isIncompleteFunction, bool isThunk);
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 7ba03ce40140c..8cb52336e35d2 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1527,7 +1527,10 @@ cir::GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
if (auto g = dyn_cast<GlobalOp>(op)) {
symTy = g.getSymType();
assert(!cir::MissingFeatures::addressSpace());
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
+ // Verify that for thread local global access, the global needs to
+ // be marked with tls bits.
+ if (getTls() && !g.getTlsModel())
+ return emitOpError("access to global not marked thread local");
} else if (auto f = dyn_cast<FuncOp>(op)) {
symTy = f.getFunctionType();
} else {
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index d94108294a9a3..fe57670a6408c 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -1911,7 +1911,11 @@ mlir::LogicalResult CIRToLLVMGetGlobalOpLowering::matchAndRewrite(
mlir::Operation *newop = mlir::LLVM::AddressOfOp::create(
rewriter, op.getLoc(), type, op.getName());
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
+ if (op.getTls()) {
+ // Handle access to TLS via intrinsic.
+ newop = mlir::LLVM::ThreadlocalAddressOp::create(rewriter, op.getLoc(),
+ type, newop->getResult(0));
+ }
rewriter.replaceOp(op, newop);
return mlir::success();
@@ -1933,8 +1937,7 @@ void CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp(
assert(!cir::MissingFeatures::addressSpace());
const unsigned addrSpace = 0;
const bool isDsoLocal = op.getDsoLocal();
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
- const bool isThreadLocal = false;
+ const bool isThreadLocal = (bool)op.getTlsModelAttr();
const uint64_t alignment = op.getAlignment().value_or(0);
const mlir::LLVM::Linkage linkage = convertLinkage(op.getLinkage());
const StringRef symbol = op.getSymName();
@@ -1993,8 +1996,7 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
assert(!cir::MissingFeatures::addressSpace());
const unsigned addrSpace = 0;
const bool isDsoLocal = op.getDsoLocal();
- assert(!cir::MissingFeatures::opGlobalThreadLocal());
- const bool isThreadLocal = false;
+ const bool isThreadLocal = (bool)op.getTlsModelAttr();
const uint64_t alignment = op.getAlignment().value_or(0);
const mlir::LLVM::Linkage linkage = convertLinkage(op.getLinkage());
const StringRef symbol = op.getSymName();
diff --git a/clang/test/CIR/CodeGen/tls.c b/clang/test/CIR/CodeGen/tls.c
new file mode 100644
index 0000000000000..983b31452b5c4
--- /dev/null
+++ b/clang/test/CIR/CodeGen/tls.c
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ogcg.ll
+// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ogcg.ll %s
+
+extern __thread int b;
+// CIR: cir.global "private" external tls_dyn @b : !s32i
+
+__thread int a;
+// CIR: cir.global external tls_dyn @a = #cir.int<0> : !s32i
+
+int c(void) { return *&b; }
+// CIR: cir.func dso_local @c() -> !s32i
+// CIR: %[[TLS_ADDR:.*]] = cir.get_global thread_local @b : !cir.ptr<!s32i>
+
+// LLVM: @b = external thread_local global i32
+// LLVM: @a = thread_local global i32 0
+
+// LLVM-LABEL: @c
+// LLVM: = call ptr @llvm.threadlocal.address.p0(ptr @b)
+
+// OGCG: @b = external thread_local{{.*}} global i32
+// OGCG: @a = thread_local{{.*}} global i32 0
+
+// OGCG-LABEL: define{{.*}} @c
+// OGCG: call{{.*}} ptr @llvm.threadlocal.address.p0(ptr{{.*}} @b)
+
More information about the cfe-commits
mailing list