Skip to content

Support using const pointers in asm const operand#138618

Open
nbdd0121 wants to merge 12 commits into
rust-lang:mainfrom
nbdd0121:asm_const_ptr
Open

Support using const pointers in asm const operand#138618
nbdd0121 wants to merge 12 commits into
rust-lang:mainfrom
nbdd0121:asm_const_ptr

Conversation

@nbdd0121

@nbdd0121 nbdd0121 commented Mar 17, 2025

Copy link
Copy Markdown
Member

View all comments

Implements RFC#3848 with tracking issue #128464

This adds support of const pointers for asm const in addition to plain integers.

The inline asm! support is implemented using i constraint, and the global_asm! and naked_asm! support is implemented by inserting symbol + offset and make symbol compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm.

The feature is also implemented for GCC backend but it's untested. Tested now.

@rustbot

This comment was marked as outdated.

@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. labels Mar 17, 2025
@fmease

fmease commented Mar 17, 2025

Copy link
Copy Markdown
Member

r? codegen

@rustbot rustbot assigned workingjubilee and unassigned fmease Mar 17, 2025
@rust-log-analyzer

This comment has been minimized.

@compiler-errors

Copy link
Copy Markdown
Contributor

r? compiler-errors

@rustbot

rustbot commented Apr 10, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Comment thread compiler/rustc_monomorphize/src/collector.rs
Comment thread compiler/rustc_codegen_ssa/src/traits/asm.rs Outdated
Comment thread compiler/rustc_codegen_ssa/src/traits/asm.rs Outdated
@bors

bors commented Apr 29, 2025

Copy link
Copy Markdown
Collaborator

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

@traviscross

Copy link
Copy Markdown
Contributor

@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. labels May 4, 2025
@rustbot

rustbot commented May 4, 2025

Copy link
Copy Markdown
Collaborator

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

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented May 13, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@bors

bors commented May 28, 2025

Copy link
Copy Markdown
Collaborator

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

Comment thread tests/ui/asm/const-refs-to-static.rs
Comment thread tests/assembly-llvm/asm/x86-types.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/asm.rs Outdated
@rustbot rustbot assigned bjorn3 and unassigned saethlin Apr 25, 2026
@rust-bors

This comment has been minimized.

Comment thread compiler/rustc_codegen_cranelift/src/global_asm.rs
// current codegen unit
tcx.symbol_name(instance)
}
GlobalAlloc::Static(def_id) => {

@bjorn3 bjorn3 Apr 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the difference between GlobalAsmOperandRef::Const with a static and GlobalAsmOperandRef::SymStatic?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See #138618 (comment)

#[thread_local] works with sym STATIC today, but it cannot be referenced in CTFE. I would actually be in favour of removing that feature.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would tend to agree with removing #[thread_local] support for sym STATIC. On some platforms (cough Windows cough) #[thread_local] accesses go through a function shim to ensure cross-dylib accesses work, so sym STATIC would probably already be broken for those platforms.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Rename GlobalAsmOperandRef::SymStatic to SymThreadLocalStatic

Comment thread compiler/rustc_codegen_cranelift/src/global_asm.rs Outdated
@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. labels Apr 30, 2026
@nbdd0121

nbdd0121 commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

I think the approach I use in rustc_codegen_gcc to avoid symbol name collision when a const is generated multiple times doesn't work if multiple CGUs decide to codegen the same symbol for a promoted constant.

The proper fix I think is to let backend mint new symbol names, rather than construct a fake Instance and use its symbol name like the current approach. This does mean that I need to create a new mechanism for backend to mint new global symbol names.

@rustbot

rustbot commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@rustbot

This comment has been minimized.

nbdd0121 added 12 commits June 16, 2026 13:02
This is currently a no-op, but will be useful when const in `global_asm!`
can be pointers.
Currently global_asm already have symbol names when using v0 scheme, this
makes them obtain symbols with legacy scheme too.
This gives the asm-const code the basic ability to deal wiht pointer and
provenances, which lays the ground work for asm_const_ptr.

Note that `SymStatic` is not fully removed, a specialized is kept and
renamed as `SymThreadLocalStatic`, for `#[thread_local]` statics where CTFE
does not support naming. The `#[thread_local]` is unstable feature and it's
not clear if we want to support this in `sym`, but removal of it should be
a separate PR.
With the previous commit, now we can see there are some code duplication
for the handling of `GlobalAlloc` inside backends. Do some clean up to
unify them.
CTFE pointers created via type ID, `without_provenance` or pointers to const
ZSTs can now be codegenned with all 3 backends. These pointers are generated
in the same way as integers.
The backend now fully supports codegen of const pointers, remove the
block inside typeck behind a new feature gate. Tests are also added.
@rustbot

rustbot commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main 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.

@nbdd0121

nbdd0121 commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

I've reworked how symbol name is generated and stop using the mangled name of the const, due to the outlined symbol name conflict issue above. This also resolves the concern #138618 (comment).

The new approach generates an unique symbol name prefix for each CGU, and then the codegen backends can use this symbol prefix to create helper symbols that they need internally. I think this can be useful longer term as well. The generated prefix is demangled to something like crate_name::{shim:cgu#1} for non-incremental build and crate_name::{shim:cgu#0}::<"incremental CGU name"> for incremental build.

EDIT: Due to two pushes the range-diff above is not the full picture, this is the full range diff.

@Darksonn

Copy link
Copy Markdown
Member

Is the cgu index still included in the symbol name when the number of cgus is 1?

@nbdd0121

Copy link
Copy Markdown
Member Author

It'll still demangle to things like crate_name::{shim:cgu#0} even with one CGU. However, v0 mangling will omit disambiguator completely if it is of value 0, so the resulting symbol name is just going to be something like _RNSCs<stable crate ID>_10crate_name3cgu

@nbdd0121

Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.