Skip to content

Support using const pointers in asm const operand #138618

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

nbdd0121
Copy link
Contributor

@nbdd0121 nbdd0121 commented Mar 17, 2025

Implements #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.

@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 2025

r? @fmease

rustbot has assigned @fmease.
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 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
Copy link
Member

fmease commented Mar 17, 2025

r? codegen

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

This comment has been minimized.

@compiler-errors
Copy link
Member

r? compiler-errors

@rustbot
Copy link
Collaborator

rustbot commented Apr 10, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@@ -25,9 +26,12 @@ pub enum InlineAsmOperandRef<'tcx, B: BackendTypes + ?Sized> {
in_value: OperandRef<'tcx, B::Value>,
out_place: Option<PlaceRef<'tcx, B::Value>>,
},
Const {
Interpolate {
Copy link
Member

Choose a reason for hiding this comment

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

Please add more doc comment explaining the significance of the difference between Interpolate and Const for backends. It begs the question for why we ever turn const operands into strings, for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some explaination. Technically we should be able to use Const for integers, although I am a bit conservative here and don't want to change how existing things work.

This might also be useful to have in the future if we decided to add interpolate "some CTFE string to be interpolated" new type of operand.

@bors
Copy link
Collaborator

bors commented Apr 29, 2025

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

@traviscross
Copy link
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
Copy link
Collaborator

rustbot commented May 4, 2025

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

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented May 13, 2025

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@bors
Copy link
Collaborator

bors commented May 28, 2025

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

// and on x86 PIC symbol can't be constant.
// x86_64-LABEL: const_ptr:
// x86_64: #APP
// x86_64: mov al, byte ptr [{{.*}}anon{{.*}}]
Copy link
Member

Choose a reason for hiding this comment

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

// x86_64: mov al, byte ptr [{{.*}}anon{{.*}}]

What was the expansion you observed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mov al, byte ptr [.Lanon.2e8c0013b01edcd3779e8174c2338a00.0]

) {
let asm_arch = self.tcx.sess.asm_arch.unwrap();

// Default to Intel syntax on x86
let intel_syntax = matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
&& !options.contains(InlineAsmOptions::ATT_SYNTAX);

// Convert all operands to string interpolations
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be good to take this opportunity and move this into cg_ssa and and add some extra trait methods for the parts that are different. Similar to what I did in #142960

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GCC 15+ has ability for file-level assembly to reference symbols and constants, so I think codegen_llvm and codegen_gcc may eventually diverge and use different approaches.

@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jul 9, 2025
This is used for string interpolation currently, so rename it as so.
The name `Const` will be used to denote a general CTFE constant that
can either be integer or pointer.
@rustbot
Copy link
Collaborator

rustbot commented Jul 9, 2025

Some changes occurred in src/tools/cargo

cc @ehuss

@nbdd0121
Copy link
Contributor Author

nbdd0121 commented Jul 9, 2025

Oops, committed submodule changes by mistake

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

(remember to mark this as ready when it's ready 😃)

nbdd0121 added 5 commits July 9, 2025 23:58
This is intended for supporting passing arbitrary CTFE const into inline
assembly.
`global_asm!` themselves don't need symbol names; they currently only have
a symbol name during mono collecion to identify them to partitioning
algorithm.

However it will have shims generated under it which will need unique symbol
names. The name themselves ultimately doesn't matter, so they're generated
like other shim instances.
This is currently a no-op, but will be useful when const in `global_asm!`
can be pointers.
@nbdd0121
Copy link
Contributor Author

nbdd0121 commented Jul 9, 2025

I think this is ready from my side.

One thing that I am not completely certain about is on the aspect of symbol names. To be able to allow global asm to reference a promoted (unnamed) statics, I need give them names and the most sensible way to me is to use the symbol name of containing body as prefix. For naked_asm this is obvious, but for global_asm! this require me to give them unique names. I currently encode them like shims, I wonder that's a good-enough solution?

BTW, this also makes me wonder if we should just desugar global_asm as just naked functions with generated names. Given global_asm! have (fake) bodys now anyway, it feels like desugaring global_asm to naked_asm makes more sense then the other way around...

@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 Jul 9, 2025
@@ -867,6 +867,16 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
// are effectively living in their parent modules.
DefPathData::ForeignMod => return print_prefix(self),

// Global asm are handled similar to shims.
DefPathData::GlobalAsm => {
Copy link
Member

@compiler-errors compiler-errors Jul 9, 2025

Choose a reason for hiding this comment

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

Why not just give this a prefix? Then we'll encode it with that prefix + disambiguator + empty name. That is to say, it feels a bit obtuse to encode it like this, not that it's necessarily wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is that existing demangler should be able to demangle this without any changes. The symbol generated for global_asms should not be very meaningful, so similar to shims.

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.

None yet

10 participants