Skip to content

Stabilize LoongArch64 SIMD Intrinsics - #2221

Merged
folkertdev merged 2 commits into
rust-lang:mainfrom
heiher:stabilize-loong64-simd
Oct 22, 2026
Merged

folkertdev merged 2 commits into
rust-lang:mainfrom
heiher:stabilize-loong64-simd

Conversation

@heiher

@heiher heiher commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Tracking issue: rust-lang/rust#162508

LoongArch64 SIMD intrinsics are already used by rustc itself and by a number of downstream projects. Experience from these real-world uses indicates that the API is ready for stabilization. Stabilizing the intrinsics would remove the nightly requirement for applications that currently depend on them.

API surface

Note for subtree update

Remove this line: https://github.com/rust-lang/rust/blob/1edd55dcfcd573872c727fa3e086369a71661ee0/compiler/rustc_span/src/lib.rs#L20

@rustbot

rustbot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @Amanieu, @adamgemmell, @davidtwco, @folkertdev, @sayantn
  • @Amanieu, @adamgemmell, @davidtwco, @folkertdev, @sayantn expanded to Amanieu, adamgemmell, davidtwco, folkertdev, sayantn
  • Random selection from Amanieu, adamgemmell, davidtwco, folkertdev, sayantn

@tgross35

tgross35 commented Sep 9, 2026

Copy link
Copy Markdown
Member

Note that this stabilizes more cases of rustc_legacy_const_generics, which was discussed at rust-lang/rust#149654. Cc @rust-lang/lang

Nominating since that's probably worth clarifying, and also this is a lot of surface area with potential sharp edges.

@rustbot label +I-libs-nominated

@rustbot

rustbot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Error: The feature relabel is not enabled in this repository.
To enable it add its section in the triagebot.toml in the root of the repository.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@heiher

heiher commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Note that this stabilizes more cases of rustc_legacy_const_generics, which was discussed at rust-lang/rust#149654.

Thanks for pointing this out. As far as I know, no projects currently depend on the LoongArch SIMD API signatures using the rustc_legacy_const_generics form, so there are no legacy compatibility concerns here. If adding new instances of rustc_legacy_const_generics is not desirable, we can simply remove it.

@heiher
heiher force-pushed the stabilize-loong64-simd branch from 5f1c38d to bc04e14 Compare September 9, 2026 13:11
@heiher

heiher commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

rustc_legacy_const_generics has been removed from LoongArch64 SIMD in v2.

@clarfonthey clarfonthey added the I-libs-nominated Nominated for discussion in a libs team meeting. label Sep 13, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

@clarfonthey has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This PR / issue is in pFCP or FCP with a disposition to merge it. labels Sep 13, 2026
@clarfonthey clarfonthey removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This PR / issue is in pFCP or FCP with a disposition to merge it. I-libs-nominated Nominated for discussion in a libs team meeting. labels Sep 13, 2026
@Amanieu

Amanieu commented Sep 13, 2026

Copy link
Copy Markdown
Member

It would be nice to have this fuzz tested against the C intrinsics using intrinsic-test before stabilizing.

@rfcbot concern fuzz testing

@Amanieu Amanieu added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This PR / issue is in pFCP or FCP with a disposition to merge it. labels Sep 13, 2026
@adamgemmell

Copy link
Copy Markdown
Contributor

There was a previous attempt at doing so at #1900, though it never fully passed CI and will need some rebasing.

@heiher

heiher commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

There was a previous attempt at doing so at #1900, though it never fully passed CI and will need some rebasing.

Thanks! There's a new impl in #2226 with all related CI checks passing.

@traviscross

traviscross commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

With these intrinsics, Miri reports UB in safe code:

#![feature(stdarch_loongarch)]
use std::arch::is_loongarch_feature_detected;
use std::arch::loongarch64::*;

#[target_feature(enable = "lsx")]
fn probe() -> i32 {
  let ones = lsx_vrepli_b::<1>();
  let zero = lsx_vrepli_b::<0>();
  let r = lsx_vdiv_b(ones, zero);
  lsx_vpickve2gr_b::<0>(r)
}

fn main() {
  if is_loongarch_feature_detected!("lsx") {
    // SAFETY: We just checked: the `lsx` feature is present.
    let lane0 = unsafe { probe() };
    println!("lane 0 = {lane0}");
  }
}

Godbolt link

From #2078, see:

cc @sayantn

@heiher

heiher commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

With these intrinsics, Miri reports UB in safe code:

Good catch!

I double-checked this with the LoongArch ISA developer. It differs from the previous comment in PR #2078. The ISA defines vdiv/vmod to return 0 if the divisor is 0. (This behavior will be documented in the SIMD reference)

So it seems we can't use portable simd_div/simd_rem here. I'll revert this part. Any other better approach? Thanks!

@Amanieu

Amanieu commented Sep 17, 2026

Copy link
Copy Markdown
Member

There are 2 options, depending on what the intrinsic behavior should be:

  1. Make it unsafe and declare that it is UB if any vector lane of the divisor is 0.
  2. Keep it safe and use LLVM target-specific intrinsics or inline asm to implement it.

@heiher

heiher commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

There are 2 options, depending on what the intrinsic behavior should be:

  1. Make it unsafe and declare that it is UB if any vector lane of the divisor is 0.
  2. Keep it safe and use LLVM target-specific intrinsics or inline asm to implement it.

Thanks! A new PR has been created based on option 2: #2229

heiher added a commit to heiher/stdarch that referenced this pull request Sep 18, 2026
Miri reports UB on division by zero with the LSX vdiv/vmod
intrinsics when implemented via the portable simd_div/simd_rem
helpers. [^1]

Per clarification from the LoongArch ISA developers, the hardware
defines that vdiv/vmod return 0 when the divisor is zero (this
will be documented in the SIMD reference). This differs from the
earlier assumption in PR rust-lang#2078, so the portable helpers are not
a correct match.

Switch back to the corresponding LLVM intrinsics so the Rust
wrappers preserve the defined ISA behavior.

[^1]: rust-lang#2221 (comment)
@rustbot

This comment has been minimized.

@heiher
heiher force-pushed the stabilize-loong64-simd branch from bc04e14 to 308a8ff Compare October 3, 2026 12:30
@rustbot

rustbot commented Oct 3, 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.

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Oct 8, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

folkertdev pushed a commit to folkertdev/rust that referenced this pull request Oct 12, 2026
Miri reports UB on division by zero with the LSX vdiv/vmod
intrinsics when implemented via the portable simd_div/simd_rem
helpers. [^1]

Per clarification from the LoongArch ISA developers, the hardware
defines that vdiv/vmod return 0 when the divisor is zero (this
will be documented in the SIMD reference). This differs from the
earlier assumption in PR rust-lang#2078, so the portable helpers are not
a correct match.

Switch back to the corresponding LLVM intrinsics so the Rust
wrappers preserve the defined ISA behavior.

[^1]: rust-lang/stdarch#2221 (comment)
@rust-rfcbot rust-rfcbot added finished-final-comment-period The final comment period is finished for this PR / issue. S-waiting-on-review to-announce and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Oct 18, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@heiher

heiher commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

a gentle ping.

@tgross35 tgross35 assigned tgross35 and unassigned folkertdev Oct 22, 2026

@tgross35 tgross35 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.

Stabilization LGTM so I'll approve this. However, there is no documentation anywhere. Would it be possible to send an update adding links and, if possible, descriptions? Similar to what other arches have.

View changes since this review

@tgross35
tgross35 added this pull request to the merge queue Oct 22, 2026
@tgross35
tgross35 removed this pull request from the merge queue due to a manual request Oct 22, 2026
@tgross35
tgross35 added this pull request to the merge queue Oct 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 22, 2026
@tgross35

Copy link
Copy Markdown
Member

Looks like stdarch needs a sync back

@folkertdev
folkertdev added this pull request to the merge queue Oct 22, 2026
Merged via the queue into rust-lang:main with commit 5154726 Oct 22, 2026
87 checks passed
@heiher
heiher deleted the stabilize-loong64-simd branch October 23, 2026 05:21
@heiher

heiher commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Stabilization LGTM so I'll approve this. However, there is no documentation anywhere. Would it be possible to send an update adding links and, if possible, descriptions? Similar to what other arches have.

Thanks! I'll add links to the official LoongArch reference docs once they become available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This PR / issue is in pFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / issue. S-waiting-on-review to-announce

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants