Open
Description
Related to this comment.
Maybe related to #93332
This feature request targets the inline assembly macro asm!
and globally scope assembly global_asm!
to support direct string interpolation into the assembly template.
The semantic works very much like a format!
in a narrower sense, that only constant string is supported. The proposed macro word is interpolate $expr
where $expr
is a const-evaluatable expression that yields a &'static str
constant value.
An example of how it would work is as follows.
trait Helper {
const SRC: &'static str;
}
fn make_it_work<H: Helper>(h: &H, x: i64) {
asm!(
"mov {0}, {1}",
in(reg) x,
interpolate H::SRC
);
}
struct H;
impl Helper for H {
const SRC: &'static str = "MAGIC";
}
fn invoke() {
make_it_work(&H, 42);
}
The one and only instantiation of asm!
macro, when completely expanded through codegen, might have yield the following assembly line.
mov rcx, MAGIC
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
dingxiangfei2009 commentedon Oct 23, 2024
Requests from #132012 (comment) is fulfilled.
Bonus question I guess.
Do we want
in(..)
andout(..)
constraints to accept const values as well? I imagine that it would be helpful to additionally mark registers clobbered, for instance.dingxiangfei2009 commentedon Oct 23, 2024
@rustbot claim
Lokathor commentedon Oct 25, 2024
I'm unclear why this is
interpolate
instead of sticking withconst
. The value is still a constant input to the asm, as far as I can tell.thomcc commentedon Oct 25, 2024
It might be surprising to users that
const
on a string doesn't somehow give the assemnbly a refrerence to that constant string, rather than interpolating it directly.kulst commentedon Jan 26, 2025
One possible use-case for a feature like this might be implementing special operations for the NVPTX target:
In the assembly of this target the type of the operands is encoded in the instruction. NVPTX also has very special instructions that to my knowledge do not really have a complementary operation in the Rust language. Therefore to use such instructions from Rust inline assembly might be necessary:
One special instruction in NVPTX is to fetch from texture memory. To fetch from texture memory that stores f32 values an inline assembly instruction looks like this:
With the proposed solution this function could be made generic over the 32 Bit type it fetches and the 32 Bit type of the indexing variable x:
(It would be benefitial to be generic over the register size as well, but this is another topic I think. Another way to implement such special operations could be using LLVM intrinsics. However, I do not know if LLVM intrinsics do exist for all special NVPTX instructions and how stable these are.).