Skip to content

Commit bc4c49b

Browse files
committed
[CIR][CIRGen] Add initial __thread support
1 parent 9599823 commit bc4c49b

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,16 +609,33 @@ void CIRGenModule::replaceGlobal(mlir::cir::GlobalOp Old,
609609
Old.erase();
610610
}
611611

612+
mlir::cir::TLS_Model CIRGenModule::GetDefaultCIRTLSModel() const {
613+
switch (getCodeGenOpts().getDefaultTLSModel()) {
614+
case CodeGenOptions::GeneralDynamicTLSModel:
615+
return mlir::cir::TLS_Model::GeneralDynamic;
616+
case CodeGenOptions::LocalDynamicTLSModel:
617+
return mlir::cir::TLS_Model::LocalDynamic;
618+
case CodeGenOptions::InitialExecTLSModel:
619+
return mlir::cir::TLS_Model::InitialExec;
620+
case CodeGenOptions::LocalExecTLSModel:
621+
return mlir::cir::TLS_Model::LocalExec;
622+
}
623+
llvm_unreachable("Invalid TLS model!");
624+
}
625+
612626
void CIRGenModule::setTLSMode(mlir::Operation *Op, const VarDecl &D) const {
613627
assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
614-
llvm_unreachable("NYI");
628+
629+
auto TLM = GetDefaultCIRTLSModel();
615630

616631
// Override the TLS model if it is explicitly specified.
617632
if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
618633
llvm_unreachable("NYI");
619634
}
620635

621-
llvm_unreachable("NYI");
636+
auto global = dyn_cast<mlir::cir::GlobalOp>(Op);
637+
assert(global && "NYI for other operations");
638+
global.setTlsModel(TLM);
622639
}
623640

624641
/// If the specified mangled name is not in the module,
@@ -1115,9 +1132,10 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
11151132

11161133
// TODO(cir): setNonAliasAttributes(D, GV);
11171134

1118-
// TODO(cir): handle TLSKind if GV is not thread local
1119-
if (D->getTLSKind()) { // && !GV->isThreadLocal())
1120-
assert(0 && "not implemented");
1135+
if (D->getTLSKind() && !GV.getTlsModelAttr()) {
1136+
if (D->getTLSKind() == VarDecl::TLS_Dynamic)
1137+
llvm_unreachable("NYI");
1138+
setTLSMode(GV, *D);
11211139
}
11221140

11231141
// TODO(cir): maybeSetTrivialComdat(*D, *GV);

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ class CIRGenModule : public CIRGenTypeCache {
471471
/// variable declaration D.
472472
void setTLSMode(mlir::Operation *Op, const VarDecl &D) const;
473473

474+
/// Get TLS mode from CodeGenOptions.
475+
mlir::cir::TLS_Model GetDefaultCIRTLSModel() const;
476+
474477
/// Replace the present global `Old` with the given global `New`. Their symbol
475478
/// names must match; their types can be different. Usages of the old global
476479
/// will be automatically updated if their types mismatch.

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,9 +1727,12 @@ class CIRGlobalOpLowering
17271727
inline void setupRegionInitializedLLVMGlobalOp(
17281728
mlir::cir::GlobalOp op, mlir::ConversionPatternRewriter &rewriter) const {
17291729
const auto llvmType = getTypeConverter()->convertType(op.getSymType());
1730+
SmallVector<mlir::NamedAttribute> attributes;
17301731
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
17311732
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
1732-
op.getSymName(), nullptr);
1733+
op.getSymName(), nullptr, /*alignment*/ 0, /*addrSpace*/ 0,
1734+
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
1735+
/*comdat*/ mlir::SymbolRefAttr(), attributes);
17331736
newGlobalOp.getRegion().push_back(new mlir::Block());
17341737
rewriter.setInsertionPointToEnd(newGlobalOp.getInitializerBlock());
17351738
}
@@ -1757,7 +1760,7 @@ class CIRGlobalOpLowering
17571760
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
17581761
op, llvmType, isConst, linkage, symbol, mlir::Attribute(),
17591762
/*alignment*/ 0, /*addrSpace*/ 0,
1760-
/*dsoLocal*/ false, /*threadLocal*/ false,
1763+
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
17611764
/*comdat*/ mlir::SymbolRefAttr(), attributes);
17621765
return mlir::success();
17631766
}
@@ -1837,7 +1840,7 @@ class CIRGlobalOpLowering
18371840
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
18381841
op, llvmType, isConst, linkage, symbol, init.value(),
18391842
/*alignment*/ 0, /*addrSpace*/ 0,
1840-
/*dsoLocal*/ false, /*threadLocal*/ false,
1843+
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
18411844
/*comdat*/ mlir::SymbolRefAttr(), attributes);
18421845
return mlir::success();
18431846
}

clang/test/CIR/CodeGen/tls.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
6+
__thread int a;
7+
// CIR: cir.global external tls_dyn @a = #cir.int<0> : !s32i
8+
// LLVM: @a = thread_local global i32 0

0 commit comments

Comments
 (0)