Skip to content

asm! support for the Xtensa architecture#147302

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
esp-rs:xtensa-asm
Jun 8, 2026
Merged

asm! support for the Xtensa architecture#147302
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
esp-rs:xtensa-asm

Conversation

@MabezDev

@MabezDev MabezDev commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

View all comments

This implements the asm! support for Xtensa. We've been using this code for a few years in our fork and it's been working well. I finally found some time to clean it up a bit and start the upstreaming process. This should be one of the final PRs for Xtensa support on the Rust side (minus bug fixes of course). After this, we're mostly just waiting on the LLVM upstreaming which is going well. This PR doesn't cover all possible asm options for Xtensa, but the base ISA plus a few extras that are used in Espressif chips.

r? Amanieu

@rustbot

rustbot commented Oct 3, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rustbot rustbot added 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. labels Oct 3, 2025
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 3, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_target/src/asm/xtensa.rs Outdated

@Amanieu Amanieu left a comment

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.

This is also missing an update to the unstable book entry for asm_experimental_arch.

View changes since this review

Comment thread compiler/rustc_target/src/asm/xtensa.rs Outdated
Comment thread compiler/rustc_target/src/asm/xtensa.rs Outdated
Comment thread compiler/rustc_target/src/asm/xtensa.rs
@bors

bors commented Nov 5, 2025

Copy link
Copy Markdown
Collaborator

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

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@MabezDev MabezDev force-pushed the xtensa-asm branch 2 times, most recently from ad6e15a to 1e1ed2e Compare November 14, 2025 13:12
@MabezDev

Copy link
Copy Markdown
Contributor Author

I've made most of the updates here (sorry for the delay!).

It turns out we'll need a patch that landed just after rust branched LLVM: https://github.com/rust-lang/llvm-project/tree/rustc/21.1-2025-08-01/llvm/lib/Target/Xtensa , otherwise the asm tests fail :(.

The good news is that we've since landed FP support in LLVM upstream too, so maybe the next time rust branches LLVM 21 (I guess 21.2?) we can rerun CI on this PR and it will be green.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_target/src/asm/xtensa.rs Outdated
Comment thread compiler/rustc_target/src/asm/xtensa.rs Outdated
Comment thread compiler/rustc_target/src/asm/xtensa.rs Outdated
@reddevilmidzy reddevilmidzy 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 Jan 4, 2026
@rust-bors

rust-bors Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

✌️ @MabezDev, you can now approve this pull request!

If @Amanieu told you to "r=me" after making some further change, then please make that change and post @bors r=Amanieu.

View changes since this delegation.

kassane pushed a commit to kassane/espressif-toolchains-research that referenced this pull request Jun 6, 2026
Deep-research synthesis from 5 parallel agents on inline-asm clobber
semantics, Zig std.lang.assembly location, and upstream Espressif
issue/PR freshness.

## docs/16 — new section "Cross-frontend inline-asm clobber matrix"

Documents the sharp difference between the 4 LLVM frontends on PIE
q-register clobbers (xtensa s3 EE.*):

  - C / esp-clang 21.1.3: NO q-reg support — `~{q0}` rejected at parse
    ("unknown register name 'q0'"). Only `"memory"` reaches LLVM.
  - Zig 0.17: First-class via `.{ .memory = true, .q0 = true, ... }` —
    lowers to `~{q0},~{q1},~{q2}` in IR. Definition lives in
    $ZIG/lib/std/lang/assembly.zig:852-938 (Xtensa branch); q0..q7 at
    lines 930-937. (std.builtin → std.lang rename in 0.17; one-release
    deprecation alias.)
  - LDC `__asm`: Raw LLVM constraint string passes through verbatim;
    no validation but full LLVM signal.
  - rustc 1.95-nightly: `out("q0")` rejected — esp-rs/rust#265 OPEN, PR
    #272 in draft (2026-06-05).

Plus the Zig RISC-V gap: Clobbers struct has v0..v31 (RVV) but NO q-regs
for Espressif ESPV vendor — only `.memory = true` is available for
esp32p4/s31 SIMD. Matches what experiments/simd/esp.zig already does.

Plus the new upstream milestone: rust-lang/rust#147302 ("asm! support
for Xtensa", MabezDev) MERGED 2026-06-05 — Xtensa asm! is no longer
fork-only.

## experiments/simd/ee.d — fix clobber list

The D source used `"r,r,r,~{memory}"` (matching the C version's ceiling)
but LDC can pass through `~{q0},~{q1},~{q2}`. Updated to
`"r,r,r,~{memory},~{q0},~{q1},~{q2}"` — codegen-affecting parity with
Zig. The 4 EE.* instructions still assemble identically; the register
allocator now knows to avoid reusing q0/q1/q2 across the block.

## docs/07 — outage notes + reframed #18916

  - kassane/esp-idf-dlang now returns 404 (deleted entirely — confirmed
    by 2 independent agents). No replacement mirror published. Existing
    installs work from cached /home/user/dl/ldc-esp.tar.xz (sha256
    c2cd9f5b...). scripts/setup.sh already handles the outage with a
    $LDC2_UPSTREAM fallback.
  - ziglang/zig#18916 reframed as resolved prior art (closed via
    PR #19018), not "another open ABI bug".

https://claude.ai/code/session_01DuR5N3YJNe42ry8vcxVxBk
kassane pushed a commit to kassane/espressif-toolchains-research that referenced this pull request Jun 6, 2026
Deep-research synthesis from 5 parallel agents on inline-asm clobber
semantics, Zig std.lang.assembly location, and upstream Espressif
issue/PR freshness.

## docs/16 — new section "Cross-frontend inline-asm clobber matrix"

Documents the sharp difference between the 4 LLVM frontends on PIE
q-register clobbers (xtensa s3 EE.*):

  - C / esp-clang 21.1.3: NO q-reg support — `~{q0}` rejected at parse
    ("unknown register name 'q0'"). Only `"memory"` reaches LLVM.
  - Zig 0.17: First-class via `.{ .memory = true, .q0 = true, ... }` —
    lowers to `~{q0},~{q1},~{q2}` in IR. Definition lives in
    $ZIG/lib/std/lang/assembly.zig:852-938 (Xtensa branch); q0..q7 at
    lines 930-937. (std.builtin → std.lang rename in 0.17; one-release
    deprecation alias.)
  - LDC `__asm`: Raw LLVM constraint string passes through verbatim;
    no validation but full LLVM signal.
  - rustc 1.95-nightly: `out("q0")` rejected — esp-rs/rust#265 OPEN, PR
    #272 in draft (2026-06-05).

Plus the Zig RISC-V gap: Clobbers struct has v0..v31 (RVV) but NO q-regs
for Espressif ESPV vendor — only `.memory = true` is available for
esp32p4/s31 SIMD. Matches what experiments/simd/esp.zig already does.

Plus the new upstream milestone: rust-lang/rust#147302 ("asm! support
for Xtensa", MabezDev) MERGED 2026-06-05 — Xtensa asm! is no longer
fork-only.

## experiments/simd/ee.d — fix clobber list

The D source used `"r,r,r,~{memory}"` (matching the C version's ceiling)
but LDC can pass through `~{q0},~{q1},~{q2}`. Updated to
`"r,r,r,~{memory},~{q0},~{q1},~{q2}"` — codegen-affecting parity with
Zig. The 4 EE.* instructions still assemble identically; the register
allocator now knows to avoid reusing q0/q1/q2 across the block.

## docs/07 — outage notes + reframed #18916

  - kassane/esp-idf-dlang now returns 404 (deleted entirely — confirmed
    by 2 independent agents). No replacement mirror published. Existing
    installs work from cached /home/user/dl/ldc-esp.tar.xz (sha256
    c2cd9f5b...). scripts/setup.sh already handles the outage with a
    $LDC2_UPSTREAM fallback.
  - ziglang/zig#18916 reframed as resolved prior art (closed via
    PR #19018), not "another open ABI bug".

https://claude.ai/code/session_01DuR5N3YJNe42ry8vcxVxBk
kassane pushed a commit to kassane/espressif-toolchains-research that referenced this pull request Jun 6, 2026
Ran /code-review across all 31 *.md files with 4 parallel finder angles
(outdated bug-close framing / content redundancy / cross-ref + link rot /
sweep). Applied 6 actionable findings:

1. docs/16:293 — "ESPV 2.2 spellings are not yet publicly documented"
   was stale. ESPV 2.2 .td files landed upstream in espressif/llvm-project
   commit 8108e99 (2026-04-23 on release/esp_22.x). Reframed: they ARE
   public upstream but not in bundled LLVM 22.1.4 — empirically the
   $ZIG/$LDC2 bundles still reject esp.vadd.s8 on -mcpu=esp32p4.

2. docs/16:95 — `#![feature(asm_experimental_arch)]` comment now notes
   rust-lang/rust#147302 ("asm! support for Xtensa", MabezDev) MERGED
   2026-06-05. Xtensa asm! is no longer fork-only; the feature flag
   stays per Rust's stability policy.

3. docs/07:175 — "kassane/esp-idf-dlang — was at" tagged with
   "(repo deleted 2026-06)" for immediate clarity.

4. docs/05:232 — clang code-size claim was "223 B" — typo, should be
   "219 B" (matches docs/06 table, docs/00, HANDOFF). The number
   appeared in the Zig 0.16 legacy-lane comparison.

5. docs/16:128 — Xtensa branch line numbers stale post-patch. The
   scripts/setup.sh auto-patch shifts the file from 3199 → 3230
   lines, moving .xtensa branch from line 852 → 883 and q0..q7
   from 930-937 → 961-968.

6. docs/16:157 — "Will reach end-users once a fresh $ZIG tarball
   ships" was stale: scripts/setup.sh ALREADY auto-patches the
   bundled assembly.zig on every setup run (PR #42 + PR #43).
   Reframed: "the auto-patch will become a no-op once a fresh
   $ZIG tarball ships with the kassane HEAD assembly.zig".

Plus a structural gap: CLAUDE.md repo map was missing 7 experiments
subdirs (addrspace, baremetal-mixin, compiler-parity, esp-rs-issues,
mangled-ffi, qemu-run, rust-zig) — all real experiments with their
own docs (docs/08, /11, /12, /13, /14, /15, /17, /18). Added.

Verified clean (no action needed per audit):
- No present-tense Zig stack-spill slips post-PR #40
- All cross-arch numerical claims (171/201/204/219/375/516 + legacy
  489/715) consistent across CLAUDE/HANDOFF/Research/docs/00/06/15
- Date consistency (2026-05-06 / 2026-05-29 / 2026-04-23 / 2026-05-30
  used uniformly where referenced)
- No TODO/FIXME/XXX/TBD markers in any .md
- LLVM-21/22 cluster framing consistent across CLAUDE/docs/04/06/23
- All section anchors valid (no docs/27, no broken §xref)

Larger-scope redundancy findings (esp32s31 narrative repeated in
CLAUDE+docs/09+docs/16+HANDOFF, LDC byval/sret narrative repeated in
docs/05+docs/19+docs/23) DEFERRED — they're load-bearing per-doc
context, not pure duplication; PR #31 already trimmed the worst of it
and further trimming risks losing per-doc reader paths.

https://claude.ai/code/session_01DuR5N3YJNe42ry8vcxVxBk
@folkertdev

Copy link
Copy Markdown
Contributor

the UI test still fails for me locally

--- a/tests/ui/feature-gates/feature-gate-xtensa-target-feature.stderr
+++ b/tests/ui/feature-gates/feature-gate-xtensa-target-feature.stderr
@@ -1,5 +1,5 @@
 error[E0658]: the target feature `bool` is currently unstable
-  --> $DIR/feature-gate-xtensa-target-feature.rs:11:18
+  --> $DIR/feature-gate-xtensa-target-feature.rs:12:18
    |
 LL | #[target_feature(enable = "bool")]
    |  

No idea why CI wouldn't catch that, let's try the job that failed earlier

@bors try x86_64-gnu-llvm-22-3

@rust-bors

rust-bors Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Unknown argument "x86_64-gnu-llvm-22-3". Did you mean to use @bors jobs=<jobs>|parent=<parent>? Run @bors help to see available commands.

@folkertdev

Copy link
Copy Markdown
Contributor

@bors try jobs=x86_64-gnu-llvm-22-3

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 6, 2026
asm! support for the Xtensa architecture


try-job: x86_64-gnu-llvm-22-3
@rust-bors

rust-bors Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 9326e98 failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

Co-authored-by: Taiki Endo <te316e89@gmail.com>
Co-authored-by: Kerry Jones <kerry@iodrive.co.za>
@folkertdev

Copy link
Copy Markdown
Contributor

@bors try jobs=x86_64-gnu-llvm-22-3

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 8, 2026
asm! support for the Xtensa architecture


try-job: x86_64-gnu-llvm-22-3
@MabezDev

MabezDev commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

I hope this should be good now. I've fixed the UI tests, and I've also simplified some of the fp asm tests to use explicit registers instead of the register class for now, as the f register constraint hasn't landed in LLVM yet (I've asked that this is upstreamed ASAP).

@rust-bors

rust-bors Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: caca363 (caca363d39ac9c45293af0af67b75cae19ae9dc0, parent: 06293ff2b120aecfc29f84b90a22a743a5b90fef)

@folkertdev

Copy link
Copy Markdown
Contributor

@bors r=Amanieu

@rust-bors

rust-bors Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 768fc10 has been approved by Amanieu

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 8, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 8, 2026
asm! support for the Xtensa architecture

This implements the asm! support for Xtensa. We've been using this code for a few years in [our fork](https://github.com/esp-rs/rust) and it's been working well. I finally found some time to clean it up a bit and start the upstreaming process. This should be one of the final PRs for Xtensa support on the Rust side (minus bug fixes of course). After this, we're mostly just waiting on the LLVM upstreaming which is going well. This PR doesn't cover all possible asm options for Xtensa, but the base ISA plus a few extras that are used in Espressif chips.

r? Amanieu
rust-bors Bot pushed a commit that referenced this pull request Jun 8, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #147302 (asm! support for the Xtensa architecture)
 - #148820 (Add very basic "comptime" fn implementation)
 - #157299 (Fix unstable diagnostics in tests)
 - #143511 (Improve TLS codegen by marking the panic/init path as cold)
 - #154608 (Add `_value` API for number literals in proc-macro)
 - #156762 (xfs support in `test_rename_directory_to_non_empty_directory`)
 - #157300 (Relax test requirements for consistency)
 - #157383 (tests: codegen-llvm: Ignore BPF targets in c-variadic-opt)
 - #157413 (fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option)
 - #157578 (Fix diagnostics for non-exhaustive destructuring assignments (#157553))
 - #157587 (explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls)
 - #157596 (test: remove ineffective link-extern-crate-with-drop-type test)
 - #157602 (rustdoc: Remove unnecessary fast path)
@rust-bors rust-bors Bot merged commit de70798 into rust-lang:main Jun 8, 2026
13 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 8, 2026
rust-timer added a commit that referenced this pull request Jun 8, 2026
Rollup merge of #147302 - esp-rs:xtensa-asm, r=Amanieu

asm! support for the Xtensa architecture

This implements the asm! support for Xtensa. We've been using this code for a few years in [our fork](https://github.com/esp-rs/rust) and it's been working well. I finally found some time to clean it up a bit and start the upstreaming process. This should be one of the final PRs for Xtensa support on the Rust side (minus bug fixes of course). After this, we're mostly just waiting on the LLVM upstreaming which is going well. This PR doesn't cover all possible asm options for Xtensa, but the base ISA plus a few extras that are used in Espressif chips.

r? Amanieu
@MabezDev MabezDev deleted the xtensa-asm branch June 9, 2026 09:27
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. O-xtensa S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

10 participants