Skip to content

Conversation

Jules-Bertholet
Copy link
Contributor

@Jules-Bertholet Jules-Bertholet commented Sep 6, 2025

Tracking issue: #146177

thread_local! {
    #[rustc_align_static(64)]
    static SO_ALIGNED: u64 = const { 0 };
}

This increases the amount of recursion the macro performs (once per attribute in addition to the previous once per item), making it easier to hit the recursion limit. I’ve added workarounds to limit the impact in the case of long doc comments, but this still needs a crater run just in case.

r? libs

@rustbot label A-attributes A-macros A-thread-locals F-static_align T-libs

@rustbot
Copy link
Collaborator

rustbot commented Sep 6, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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 Sep 6, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 6, 2025

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

The Miri subtree was changed

cc @rust-lang/miri

@rustbot rustbot added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-thread-locals Area: Thread local storage (TLS) F-static_align #![feature(static_align)] S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. labels Sep 6, 2025
@rustbot rustbot assigned jhpratt and unassigned lcnr Sep 6, 2025
@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from 67db3cb to dd3475c Compare September 6, 2025 17:14
@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. and removed A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. labels Sep 6, 2025
@jdonszelmann jdonszelmann self-assigned this Sep 6, 2025
@jhpratt

This comment was marked as resolved.

@rustbot

This comment was marked as resolved.

@jhpratt jhpratt removed their assignment Sep 7, 2025
@jdonszelmann
Copy link
Contributor

@Jules-Bertholet would you mind rebasing? #146178 merged
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Sep 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 10, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from dd3475c to 9cdfc77 Compare September 10, 2025 19:03
@rustbot

This comment has been minimized.

([$($prev_align_attrs:tt)*] [$($prev_other_attrs:tt)*];
#[doc $($doc_rhs_1:tt)*] #[doc $($doc_rhs_2:tt)*] #[doc $($doc_rhs_3:tt)*] #[doc $($doc_rhs_4:tt)*]
#[doc $($doc_rhs_5:tt)*] #[doc $($doc_rhs_6:tt)*] #[doc $($doc_rhs_7:tt)*] #[doc $($doc_rhs_8:tt)*]
$($rest:tt)*) => (
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we could use a regular proc macro (IIRC, std can't use proc macros), we'd have to make it a part of rustc.

#![crate_type = "lib"]

thread_local! {
//~^ ERROR the `#[rustc_align_static]` attribute is an experimental feature
Copy link
Member

Choose a reason for hiding this comment

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

Are we (implicitly) stabilizing support for cfg_attr on the static in this PR? Can we test other forms of the attribute to verify we didn't allow other attributes (e.g., no_mangle or used(linker), etc.) and what the diagnostics are like?

Copy link
Contributor Author

@Jules-Bertholet Jules-Bertholet Sep 14, 2025

Choose a reason for hiding this comment

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

Are we (implicitly) stabilizing support for cfg_attr on the static in this PR?

It was already stable.

Can we test other forms of the attribute to verify we didn't allow other attributes (e.g., no_mangle or used(linker), etc.) and what the diagnostics are like?

I’ve added some tests for this. The error spans are not great, but that is a pre-existing issue (#146567), this PR doesn’t make it any worse than before.

Another pre-existing issue I just discovered is that unsafe(no_mangle) is erroneously accepted (but has no effect). I am looking into that now… (Edit: #146557)

@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from 475178f to b7fa550 Compare September 14, 2025 19:39
@Jules-Bertholet
Copy link
Contributor Author

I realized that I had neglected to support cfg_attr invocations where the number of attribute invocations in the right-hand side is not 1. The latest commit fixes this oversight, at the cost of making the macro even more complicated.

@jdonszelmann
Copy link
Contributor

This is a T-libs issue but to me this macro looks very scary and I'm not sure it needs to be a decl macro. Maybe a compiler provided macro like format_args!() (though it is obviously much more complex) or line!() would be a better fit? If you need help with that I've worked with that part of the compiler quite a bit. But again, I don't think this is something I can decide, I'm just not very happy with this declmacro

if self.state.get() == State::Alive {
self.value.get() as *const T
} else {
self.initialize(i, f)
Copy link
Member

Choose a reason for hiding this comment

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

This will hurt codegen for thread locals that otherwise had option optimization (e.g., if you store NonNull pointer today, this would optimize to a single load + test, rather than loading from two different memory locations).

I'm not sure how much that matters in practice, but it seems unfortunate to pay the cost for all users. Ideally, we'd only do so for the case where alignof(Option.Some.0) != alignof(Option), but I'm not sure if that's possible to check in macros / const-fn to optimize this.

Does this code get used only in the no_threads case? Or does it end up exported for the actually threaded cases too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only the no_threads case. I suppose we could have two impls depending on whether any rustc_align_static is present or not…

(@align) => (1),

// `rustc_align_static` attributes are present,
// translate them into a `const` block that computes the alignment
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this could be simplified by attaching the attributes to some static and then checking the alignment of that static (via align_of_val)? That way we presumably get most of the error checking from rustc and we don't nee to manually compute the alignment or deal with cfgs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We would have to translate rustc_align_static(…) into repr(align(…)), so just as much complexity as before.

We do rely on rustc for error checking, via static DUMMY below.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this was meant as a comment on the struct idea below? For this comment using a static shouldn't require any translation - presumably align_of_val(&DUMMY) gives us the alignment we need?

Copy link
Member

Choose a reason for hiding this comment

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

With regards to structs, I wonder if we should teach rustc to support rustc_align_static on fields/structs as a forever unstable extension just to simplify the impl here....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

presumably align_of_val(&DUMMY) gives us the alignment we need?

No, that would give the alignment of the type of DUMMY, which is 1.

// Note that to prevent an infinite loop we reset it back to null right
// before we return from the destructor ourselves.
abort_on_dtor_unwind(|| {
let ptr = unsafe { Box::from_raw(ptr as *mut Value<T>) };
Copy link
Member

Choose a reason for hiding this comment

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

Similarly to the above thought: is it plausible that for aligned thread locals we could generate a repr(C) struct that bumped the alignment of our stored value to what we need and then we'd alter our code to &struct.0 when actually returning pointers out to userspace? Presumably that would avoid the majority of the internal changes and avoid storing alignments into memory (wastefully).

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 realized that we can just use a const generic. Switched to that

@rust-log-analyzer

This comment has been minimized.

@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch 2 times, most recently from f12ddd7 to fca42ed Compare September 21, 2025 22:08
@rustbot

This comment has been minimized.

@Jules-Bertholet
Copy link
Contributor Author

Jules-Bertholet commented Sep 21, 2025

I have no idea why the Miri floating point test failed in #146281 (comment), or why fca42ed fixes it. @RalfJung do you know what is going on here?

@RalfJung
Copy link
Member

Well, it's a randomized test, so it always has a small chance of failing.

@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from fca42ed to 8d9ddfd Compare September 22, 2025 13:13
@bors
Copy link
Collaborator

bors commented Sep 24, 2025

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

@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from 8d9ddfd to ad8a3b4 Compare September 24, 2025 02:19
@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from ca8d8b3 to ff97f2a Compare September 24, 2025 21:45
@Jules-Bertholet Jules-Bertholet force-pushed the static-align-thread-local branch from ff97f2a to a4e87e9 Compare September 26, 2025 17:51
@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-thread-locals Area: Thread local storage (TLS) F-static_align #![feature(static_align)] S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

9 participants