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

USIZE_MARKER: make sure the function is monomorphized only once #123780

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions library/core/src/fmt/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,15 @@ extern "C" {
// first argument. The read_volatile here ensures that we can safely ready out a
// usize from the passed reference and that this address does not point at a
// non-usize taking function.
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |ptr, _| {
// SAFETY: ptr is a reference
let _v: usize = unsafe { crate::ptr::read_volatile(ptr) };
loop {}
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = {
// Make sure this function is only monomorphized once, and not considered cross-crate
// inlineable.
#[inline(never)]
Copy link
Member

Choose a reason for hiding this comment

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

#[inline(never)] is a suggestion to the optimizer, not a hard requirement. As such this change still doesn't guarantee that the function is only monomorphized once.

https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute

  • #[inline(never)] suggests that an inline expansion should never be performed.

Note: #[inline] in every form is a hint, with no requirements on the language to place a copy of the attributed function in the caller.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I don't think there is a way to actually do what USIZE_MARKER wants to do. But I don't think t-libs will let me remove this thing entirely, so using #[inline(never)] seems like an improvement...

fn usize_marker(ptr: &usize, _: &mut Formatter<'_>) -> Result {
// SAFETY: ptr is a reference
let _v: usize = unsafe { crate::ptr::read_volatile(ptr) };
loop {}
}

usize_marker
};
Loading