-
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
base: gh/lanza/4/base
Are you sure you want to change the base?
Conversation
This commit implements basic thread-local storage support in ClangIR: 1. Added EmitThreadLocalVarDeclLValue() method to CIRGenCXXABI interface to handle thread-local variable access in an ABI-specific way. 2. Implemented EmitThreadLocalVarDeclLValue() in CIRGenItaniumCXXABI that: - Accesses thread-local variables directly using their GlobalOp - Relies on the 'tls_dyn' attribute on GlobalOp for TLS semantics - Handles both value and reference types correctly - Defers complex TLS wrapper function support to future work 3. Modified CIRGenExpr.cpp to call the ABI method instead of asserting when encountering TLS_Dynamic variables. 4. Added CIRGenModule::getAddrOfGlobalVar() helper for retrieving global variable addresses. 5. Added comprehensive test coverage in thread-local.cpp that verifies: - Basic thread_local variables with constant initialization - GNU-style __thread variables - Function-local thread_local static variables - Reading from and writing to thread_local variables - Extern thread_local variable declarations - Correct CIR generation with tls_dyn attribute - Proper LLVM IR lowering to llvm.threadlocal.address intrinsic The implementation takes a simplified approach for now, relying on the GlobalOp's thread_local attribute rather than generating wrapper functions. This is sufficient for variables with trivial initialization. Future work may add full wrapper function support for complex dynamic initialization. ghstack-source-id: 65ab4e1 Pull-Request: #1996
This commit implements basic thread-local storage support in ClangIR: 1. Added EmitThreadLocalVarDeclLValue() method to CIRGenCXXABI interface to handle thread-local variable access in an ABI-specific way. 2. Implemented EmitThreadLocalVarDeclLValue() in CIRGenItaniumCXXABI that: - Accesses thread-local variables directly using their GlobalOp - Relies on the 'tls_dyn' attribute on GlobalOp for TLS semantics - Handles both value and reference types correctly - Defers complex TLS wrapper function support to future work 3. Modified CIRGenExpr.cpp to call the ABI method instead of asserting when encountering TLS_Dynamic variables. 4. Added CIRGenModule::getAddrOfGlobalVar() helper for retrieving global variable addresses. 5. Added comprehensive test coverage in thread-local.cpp that verifies: - Basic thread_local variables with constant initialization - GNU-style __thread variables - Function-local thread_local static variables - Reading from and writing to thread_local variables - Extern thread_local variable declarations - Correct CIR generation with tls_dyn attribute - Proper LLVM IR lowering to llvm.threadlocal.address intrinsic The implementation takes a simplified approach for now, relying on the GlobalOp's thread_local attribute rather than generating wrapper functions. This is sufficient for variables with trivial initialization. Future work may add full wrapper function support for complex dynamic initialization. ghstack-source-id: 2a8fafc Pull-Request: #1996
This commit implements thread-local storage support in ClangIR: 1. Added EmitThreadLocalVarDeclLValue() method to CIRGenCXXABI interface to handle thread-local variable access in an ABI-specific way. 2. Implemented EmitThreadLocalVarDeclLValue() in CIRGenItaniumCXXABI that: - Accesses thread-local variables using cir.get_global thread_local - Relies on LLVM lowering to insert @llvm.threadlocal.address intrinsics - Handles both value and reference types correctly - Uses ClangIR's declarative approach instead of wrapper functions 3. Modified CIRGenExpr.cpp to call the ABI method for TLS_Dynamic variables. 4. Added CIRGenModule::getAddrOfGlobalVar() helper for retrieving global variable addresses. 5. Added comprehensive test coverage in thread-local.cpp that verifies: - Basic thread_local variables with constant initialization - GNU-style __thread variables - Function-local thread_local static variables - Reading from and writing to thread_local variables - Extern thread_local variable declarations - Correct CIR generation with tls_dyn attribute - Proper LLVM IR lowering to llvm.threadlocal.address intrinsic ClangIR's approach differs from traditional CodeGen: instead of creating wrapper functions (_ZTW*) in the IR, ClangIR marks GlobalOps with tls_dyn and relies on LLVM lowering to insert @llvm.threadlocal.address intrinsics. This achieves correct TLS semantics while maintaining ClangIR's higher-level representation. ghstack-source-id: d912c08 Pull-Request: #1996
This commit implements thread-local storage support in ClangIR: 1. Added EmitThreadLocalVarDeclLValue() method to CIRGenCXXABI interface to handle thread-local variable access in an ABI-specific way. 2. Implemented EmitThreadLocalVarDeclLValue() in CIRGenItaniumCXXABI that: - Accesses thread-local variables using cir.get_global thread_local - Relies on LLVM lowering to insert @llvm.threadlocal.address intrinsics - Handles both value and reference types correctly - Uses ClangIR's declarative approach instead of wrapper functions 3. Modified CIRGenExpr.cpp to call the ABI method for TLS_Dynamic variables. 4. Added CIRGenModule::getAddrOfGlobalVar() helper for retrieving global variable addresses. 5. Added comprehensive test coverage in thread-local.cpp that verifies: - Basic thread_local variables with constant initialization - GNU-style __thread variables - Function-local thread_local static variables - Reading from and writing to thread_local variables - Extern thread_local variable declarations - Correct CIR generation with tls_dyn attribute - Proper LLVM IR lowering to llvm.threadlocal.address intrinsic ClangIR's approach differs from traditional CodeGen: instead of creating wrapper functions (_ZTW*) in the IR, ClangIR marks GlobalOps with tls_dyn and relies on LLVM lowering to insert @llvm.threadlocal.address intrinsics. This achieves correct TLS semantics while maintaining ClangIR's higher-level representation. ghstack-source-id: 267f175 Pull-Request: #1996
bcardosolopes
left a comment
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.
Overall good, thanks! Some nits and ready to go
| llvm::DenseSet<clang::GlobalDecl> DiagnosedConflictingDefinitions; | ||
|
|
||
| /// thread_local variables defined or used in this TU. | ||
| std::vector<const clang::VarDecl *> CXXThreadLocals; |
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.
this should be lowercase too!
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.
this one needs changing too (even though a few others on this file haven't been migrated just yet)
This commit implements thread-local storage support in ClangIR: 1. Added EmitThreadLocalVarDeclLValue() method to CIRGenCXXABI interface to handle thread-local variable access in an ABI-specific way. 2. Implemented EmitThreadLocalVarDeclLValue() in CIRGenItaniumCXXABI that: - Accesses thread-local variables using cir.get_global thread_local - Relies on LLVM lowering to insert @llvm.threadlocal.address intrinsics - Handles both value and reference types correctly - Uses ClangIR's declarative approach instead of wrapper functions 3. Modified CIRGenExpr.cpp to call the ABI method for TLS_Dynamic variables. 4. Added CIRGenModule::getAddrOfGlobalVar() helper for retrieving global variable addresses. 5. Added comprehensive test coverage in thread-local.cpp that verifies: - Basic thread_local variables with constant initialization - GNU-style __thread variables - Function-local thread_local static variables - Reading from and writing to thread_local variables - Extern thread_local variable declarations - Correct CIR generation with tls_dyn attribute - Proper LLVM IR lowering to llvm.threadlocal.address intrinsic ClangIR's approach differs from traditional CodeGen: instead of creating wrapper functions (_ZTW*) in the IR, ClangIR marks GlobalOps with tls_dyn and relies on LLVM lowering to insert @llvm.threadlocal.address intrinsics. This achieves correct TLS semantics while maintaining ClangIR's higher-level representation. ghstack-source-id: 0a94a40 Pull-Request: #1996
Stack from ghstack (oldest at bottom):
This commit implements basic thread-local storage support in ClangIR:
Added EmitThreadLocalVarDeclLValue() method to CIRGenCXXABI interface
to handle thread-local variable access in an ABI-specific way.
Implemented EmitThreadLocalVarDeclLValue() in CIRGenItaniumCXXABI that:
Modified CIRGenExpr.cpp to call the ABI method instead of asserting
when encountering TLS_Dynamic variables.
Added CIRGenModule::getAddrOfGlobalVar() helper for retrieving
global variable addresses.
Added comprehensive test coverage in thread-local.cpp that verifies:
The implementation takes a simplified approach for now, relying on the
GlobalOp's thread_local attribute rather than generating wrapper functions.
This is sufficient for variables with trivial initialization. Future work
may add full wrapper function support for complex dynamic initialization.