-
Notifications
You must be signed in to change notification settings - Fork 182
[CIR] Add basic thread-local storage (TLS) support #1996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lanza
wants to merge
5
commits into
gh/lanza/4/base
Choose a base branch
from
gh/lanza/4/head
base: gh/lanza/4/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−8
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,6 +318,9 @@ class CIRGenItaniumCXXABI : public CIRGenCXXABI { | |
| return !isEmittedWithConstantInitializer(VD) || mayNeedDestruction(VD); | ||
| } | ||
|
|
||
| LValue EmitThreadLocalVarDeclLValue(CIRGenFunction &CGF, const VarDecl *VD, | ||
|
||
| QualType LValType) override; | ||
|
|
||
| bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override { | ||
| return true; | ||
| } | ||
|
|
@@ -2925,3 +2928,28 @@ Address CIRGenARMCXXABI::initializeArrayCookie(CIRGenFunction &cgf, | |
| return Address(dataPtr, cgf.getBuilder().getUIntNTy(8), | ||
| newPtr.getAlignment()); | ||
| } | ||
|
|
||
| LValue CIRGenItaniumCXXABI::EmitThreadLocalVarDeclLValue(CIRGenFunction &CGF, | ||
| const VarDecl *VD, | ||
| QualType LValType) { | ||
| // ClangIR's approach to thread-local variables differs from traditional | ||
| // CodeGen. Traditional CodeGen creates wrapper functions (e.g., _ZTW*) that | ||
| // handle dynamic initialization. ClangIR instead marks the GlobalOp with | ||
| // tls_dyn and relies on LLVM lowering to insert @llvm.threadlocal.address | ||
| // intrinsics, which achieves the same semantics without intermediate wrapper | ||
| // functions in CIR. | ||
|
|
||
| mlir::Value V = CGF.CGM.getAddrOfGlobalVar(VD); | ||
|
|
||
| auto RealVarTy = CGF.convertTypeForMem(VD->getType()); | ||
| CharUnits Alignment = CGF.getContext().getDeclAlign(VD); | ||
| Address Addr(V, RealVarTy, Alignment); | ||
|
|
||
| LValue LV; | ||
| if (VD->getType()->isReferenceType()) | ||
| LV = CGF.emitLoadOfReferenceLValue(Addr, CGF.getLoc(VD->getLocation()), | ||
| VD->getType(), AlignmentSource::Decl); | ||
| else | ||
| LV = CGF.makeAddrLValue(Addr, LValType, AlignmentSource::Decl); | ||
| return LV; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,9 @@ class CIRGenModule : public CIRGenTypeCache { | |
| /// for the same decl. | ||
| llvm::DenseSet<clang::GlobalDecl> DiagnosedConflictingDefinitions; | ||
|
|
||
| /// thread_local variables defined or used in this TU. | ||
| std::vector<const clang::VarDecl *> CXXThreadLocals; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be lowercase too! |
||
|
|
||
| /// ------- | ||
| /// Annotations | ||
| /// ------- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // RUN: %clang_cc1 -std=c++20 -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 -std=c++20 -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 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.og.ll | ||
| // RUN: FileCheck --check-prefix=OGCG --input-file=%t.og.ll %s | ||
|
|
||
| // Test basic thread_local variable with constant initialization | ||
| thread_local int tls_const = 42; | ||
| // CIR: cir.global{{.*}}tls_dyn{{.*}}@tls_const = #cir.int<42> : !s32i | ||
| // LLVM: @tls_const = thread_local global i32 42 | ||
| // OGCG: @tls_const = thread_local global i32 42 | ||
|
|
||
| // Test __thread (GNU-style) thread_local | ||
| __thread int tls_gnu_style = 10; | ||
| // CIR: cir.global{{.*}}tls_dyn{{.*}}@tls_gnu_style = #cir.int<10> : !s32i | ||
| // LLVM: @tls_gnu_style = thread_local global i32 10 | ||
| // OGCG: @tls_gnu_style = thread_local global i32 10 | ||
|
|
||
| // Test thread_local function-local static (constant init) | ||
| int get_tls_static() { | ||
| thread_local int tls_func_static = 100; | ||
| return ++tls_func_static; | ||
| } | ||
| // CIR-LABEL: cir.func{{.*}}@_Z14get_tls_staticv | ||
| // CIR: cir.get_global{{.*}}@_ZZ14get_tls_staticvE15tls_func_static | ||
| // LLVM-LABEL: @_Z14get_tls_staticv | ||
| // LLVM: load{{.*}}@_ZZ14get_tls_staticvE15tls_func_static | ||
| // OGCG-LABEL: @_Z14get_tls_staticv | ||
| // OGCG: @llvm.threadlocal.address.p0(ptr{{.*}}@_ZZ14get_tls_staticvE15tls_func_static) | ||
|
|
||
| // Test reading from thread_local variable | ||
| int read_tls() { | ||
| return tls_const; | ||
| } | ||
| // CIR-LABEL: cir.func{{.*}}@_Z8read_tlsv | ||
| // CIR: cir.get_global thread_local @tls_const | ||
| // LLVM-LABEL: @_Z8read_tlsv | ||
| // LLVM: @llvm.threadlocal.address.p0(ptr @tls_const) | ||
| // OGCG-LABEL: @_Z8read_tlsv | ||
| // OGCG: @llvm.threadlocal.address.p0(ptr{{.*}}@tls_const) | ||
|
|
||
| // Test writing to thread_local variable | ||
| void write_tls(int val) { | ||
| tls_const = val; | ||
| } | ||
| // CIR-LABEL: cir.func{{.*}}@_Z9write_tlsi | ||
| // CIR: cir.get_global thread_local @tls_const | ||
| // CIR: cir.store | ||
| // LLVM-LABEL: @_Z9write_tlsi | ||
| // LLVM: @llvm.threadlocal.address.p0(ptr @tls_const) | ||
| // OGCG-LABEL: @_Z9write_tlsi | ||
| // OGCG: @llvm.threadlocal.address.p0(ptr{{.*}}@tls_const) | ||
|
|
||
| // Test extern thread_local | ||
| extern thread_local int tls_extern; | ||
| int use_extern_tls() { | ||
| return tls_extern; | ||
| } | ||
| // CIR-LABEL: cir.func{{.*}}@_Z14use_extern_tlsv | ||
| // CIR: cir.get_global thread_local @tls_extern | ||
| // LLVM-LABEL: @_Z14use_extern_tlsv | ||
| // LLVM: @llvm.threadlocal.address.p0(ptr @tls_extern) | ||
| // OGCG-LABEL: @_Z14use_extern_tlsv | ||
| // OGCG: call ptr @_ZTW10tls_extern() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
camelBack here and everyelse it makes sense