Skip to content

Conversation

a4lg
Copy link
Contributor

@a4lg a4lg commented Sep 13, 2025

This commit performs various improvements (better register allocation, less register clobbering on the worst case and better readability) of RISC-V inline assembly use cases.

Note that it does not change the p module (which defines the "P" extension draft instructions but very likely to change).

  1. Use lateout as possible.
    Unlike out(reg) and in(reg) pair, lateout(reg) and in(reg) can share the same register because they state that the late-output register is written after all the reads are performed. It can improve register allocation.
  2. Add preserves_flags option as possible.
    While RISC-V doesn't have regular condition codes, RISC-V inline assembly in the Rust language assumes that some registers (mainly vector state registers) may be overwritten by default. By adding preserves_flags to the intrinsics corresponding instructions without overwriting them, it can minimize register clobbering on the worst case.
  3. Use trailing semicolon.
    As asm! declares an action and it doesn't return a value by itself, it would be better to have trailing semicolon to denote that an asm! call is effectively a statement.
  4. Make most of asm! calls multi-lined.
    rustfmt makes some simple (yet long) asm! calls multi-lined but it does not perform formatting of complex asm! calls with inputs and/or outputs. To keep consistency, it makes most of the asm! calls multi-lined.

@rustbot
Copy link
Collaborator

rustbot commented Sep 13, 2025

r? @folkertdev

rustbot has assigned @folkertdev.
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

@a4lg a4lg force-pushed the riscv-inline-asm-general-improvements branch from 0c6897b to ea93173 Compare September 13, 2025 08:24
@a4lg
Copy link
Contributor Author

a4lg commented Sep 13, 2025

r? @sayantn

Fixed the style issues (resulted in a Check Style CI failure) and performed similar changes (not automatically performed by rustfmt) for consistency. As a result, this PR makes most of the asm! calls multi-lined.

@rustbot rustbot assigned sayantn and unassigned folkertdev Sep 13, 2025
@a4lg a4lg force-pushed the riscv-inline-asm-general-improvements branch 5 times, most recently from 080594d to ffd627b Compare September 14, 2025 01:32
This commit performs various improvements (better register allocation,
less register clobbering on the worst case and better readability) of
RISC-V inline assembly use cases.

Note that it does not change the `p` module (which defines the "P"
extension draft instructions but very likely to change).

1.  Use `lateout` as possible.
    Unlike `out(reg)` and `in(reg)` pair, `lateout(reg)` and `in(reg)`
    can share the same register because they state that the late-output
    register is written after all the reads are performed.
    It can improve register allocation.
2.  Add `preserves_flags` option as possible.
    While RISC-V doesn't have _regular_ condition codes, RISC-V inline
    assembly in the Rust language assumes that some registers
    (mainly vector state registers) may be overwritten by default.
    By adding `preserves_flags` to the intrinsics corresponding
    instructions without overwriting them, it can minimize register
    clobbering on the worst case.
3.  Use trailing semicolon.
    As `asm!` declares an action and it doesn't return a value by
    itself, it would be better to have trailing semicolon to denote that
    an `asm!` call is effectively a statement.
4.  Make most of `asm!` calls multi-lined.
    `rustfmt` makes some simple (yet long) `asm!` calls multi-lined but
    it does not perform formatting of complex `asm!` calls with inputs
    and/or outputs.  To keep consistency, it makes most of the `asm!`
    calls multi-lined.
@a4lg a4lg force-pushed the riscv-inline-asm-general-improvements branch from ffd627b to cca602d Compare September 14, 2025 05:08
@sayantn sayantn added this pull request to the merge queue Sep 15, 2025
Merged via the queue into rust-lang:master with commit 9f12c1a Sep 15, 2025
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants