Skip to content
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

Support TLS access into dylibs on Windows #108089

Merged
merged 4 commits into from
Mar 29, 2023
Merged

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Feb 15, 2023

This allows access to #[thread_local] in upstream dylibs on Windows by introducing a MIR shim to return the address of the thread local. Accesses that go into an upstream dylib will call the MIR shim to get the address of it.

convert_tls_rvalues is introduced in rustc_codegen_ssa which rewrites MIR TLS accesses to dummy calls which are replaced with calls to the MIR shims when the dummy calls are lowered to backend calls.

A new dll_tls_export target option enables this behavior with a false value which is set for Windows platforms.

This fixes #84933.

@rustbot
Copy link
Collaborator

rustbot commented Feb 15, 2023

r? @Nilstrieb

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 15, 2023
@rustbot
Copy link
Collaborator

rustbot commented Feb 15, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

These commits modify compiler targets.
(See the Target Tier Policy.)

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look over it, looks great, thanks for finally solving this!

I am not familiar enough with this sort of code to approve it, so I'll hand it off to someone else.
r? @wesleywiser, I hope you can review this :D, assigning you directly since compiler includes plenty of people who wouldn't review this code either

ccing the people involved in #101368, @thomcc @eddyb @ChrisDenton

is_cleanup: false,
});

let ret_ty = Rvalue::ThreadLocalRef(def_id).ty(&IndexVec::new(), tcx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you factor out the "type of tls ref" logic into a helper function somewhere and use it here (and in the other places where you need it, including the impl of Rvalue::ty)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a tcx.thread_local_ptr_ty method for that. It's suspiciously similar to tcx.static_ptr_ty which normalizes and erases regions. It's possible thread_local_ptr_ty should call that.

compiler/rustc_mir_transform/src/shim.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/mir/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/mir/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/ty/instance.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/mir/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_monomorphize/src/partitioning/default.rs Outdated Show resolved Hide resolved
@rustbot rustbot assigned wesleywiser and unassigned Noratrieb Feb 15, 2023
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2023

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@Zoxc
Copy link
Contributor Author

Zoxc commented Feb 19, 2023

I've added untested cranelift support. I tried to test using rust.codegen-backends = ["cranelift"], but I ran into a std linking error:

libpanic_unwind-12135c8a369c0c33.rlib(panic_unwind-12135c8a369c0c33.panic_unwind.b580d029-cgu.1.rcgu.o) : error LNK2001: unresolved external symbol ☺??_7type_info@@6B@
F:\Rust\rust\build\x86_64-pc-windows-msvc\stage1-std\x86_64-pc-windows-msvc\release\deps\std-e639197673660265.dll : fatal error LNK1120: 1 unresolved externals

@bjorn3
Copy link
Member

bjorn3 commented Feb 19, 2023

On Windows panic_unwind imports \x01??_7type_info@@6B@. The \x01 here tells LLVM to not do any symbol mangling. cg_clif doesn't implement this yet: https://github.com/bjorn3/rustc_codegen_cranelift/issues/1332 It should work fine on Linux and macOS and it may also work if you remove panic-unwind from

let mut features = " panic-unwind".to_string();
, but I haven't tested that.

@bors
Copy link
Contributor

bors commented Feb 22, 2023

☔ The latest upstream changes (presumably #108340) made this pull request unmergeable. Please resolve the merge conflicts.

@wesleywiser
Copy link
Member

Thanks @Zoxc for fixing this! The implementation chosen looks good to me.

@bors r+

@bors
Copy link
Contributor

bors commented Mar 6, 2023

📌 Commit 6c9d750 has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 6, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 8, 2023
Support TLS access into dylibs on Windows

This allows access to `#[thread_local]`  in upstream dylibs on Windows by introducing a MIR shim to return the address of the thread local. Accesses that go into an upstream dylib will call the MIR shim to get the address of it.

`convert_tls_rvalues` is introduced in `rustc_codegen_ssa` which rewrites MIR TLS accesses to dummy calls which are replaced with calls to the MIR shims when the dummy calls are lowered to backend calls.

A new `dll_tls_export` target option enables this behavior with a `false` value which is set for Windows platforms.

This fixes rust-lang#84933.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 9, 2023
Support TLS access into dylibs on Windows

This allows access to `#[thread_local]`  in upstream dylibs on Windows by introducing a MIR shim to return the address of the thread local. Accesses that go into an upstream dylib will call the MIR shim to get the address of it.

`convert_tls_rvalues` is introduced in `rustc_codegen_ssa` which rewrites MIR TLS accesses to dummy calls which are replaced with calls to the MIR shims when the dummy calls are lowered to backend calls.

A new `dll_tls_export` target option enables this behavior with a `false` value which is set for Windows platforms.

This fixes rust-lang#84933.
@bjorn3
Copy link
Member

bjorn3 commented Mar 29, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Mar 29, 2023

📌 Commit 3019a34 has been approved by bjorn3

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 29, 2023
@bors
Copy link
Contributor

bors commented Mar 29, 2023

⌛ Testing commit 3019a34 with merge 55acf51dcaf91ffa64f7a2aea4fc0615720b28b9...

@bors
Copy link
Contributor

bors commented Mar 29, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 29, 2023
@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member

bjorn3 commented Mar 29, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Mar 29, 2023

📌 Commit d499bbb has been approved by bjorn3

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 29, 2023
@bors
Copy link
Contributor

bors commented Mar 29, 2023

⌛ Testing commit d499bbb with merge f98598c...

@bors
Copy link
Contributor

bors commented Mar 29, 2023

☀️ Test successful - checks-actions
Approved by: bjorn3
Pushing f98598c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 29, 2023
@bors bors merged commit f98598c into rust-lang:master Mar 29, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 29, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f98598c): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.5% [5.5%, 5.5%] 1
Improvements ✅
(primary)
-2.5% [-2.5%, -2.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.5% [-2.5%, -2.5%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

@Zoxc Zoxc deleted the windows-tls branch March 30, 2023 01:25
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 6, 2023
Workaround rust-lang#109797 on windows-gnu

The addition of `#[inline]` here in rust-lang#108089 caused an unrelated linking issue (rust-lang#109797). This PR removes this attribute again on Windows to avoid regressions.
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request Jun 1, 2023
Workaround #109797 on windows-gnu

The addition of `#[inline]` here in rust-lang/rust#108089 caused an unrelated linking issue (rust-lang/rust#109797). This PR removes this attribute again on Windows to avoid regressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support inlining cross-crate TLS access on MSVC
9 participants