Skip to content

Shim the LoongArch CRC intrinsics#5062

Merged
RalfJung merged 3 commits into
rust-lang:masterfrom
Gelbpunkt:la64-crc
May 27, 2026
Merged

Shim the LoongArch CRC intrinsics#5062
RalfJung merged 3 commits into
rust-lang:masterfrom
Gelbpunkt:la64-crc

Conversation

@Gelbpunkt
Copy link
Copy Markdown
Contributor

@Gelbpunkt Gelbpunkt commented May 24, 2026

I'm adding support for using these in zlib-rs, and @folkertdev suggested implementing them in Miri (trifectatechfoundation/zlib-rs#511 (comment)). The LoongArch CRC intrinsics are extremely similar to those found on AArch64, with the main difference that the argument order is swapped.

Here's the test in Miri and on actual hardware:

$ ./miri run tests/pass/shims/loongarch/intrinsics-loongarch64-crc.rs
$ cargo +miri build -Zroot-dir=/home/aelin/miri --manifest-path /home/aelin/miri/./Cargo.toml --all-targets
    Finished `dev` profile [optimized + debuginfo] target(s) in 0.14s
$ cargo +miri build -Zroot-dir=/home/aelin/miri --manifest-path /home/aelin/miri/cargo-miri/Cargo.toml --all-targets
    Finished `dev` profile [optimized + debuginfo] target(s) in 0.07s
$ cargo miri setup
$ /home/aelin/miri/target/debug/miri --edition 2021 --sysroot /home/aelin/.cache/miri tests/pass/shims/loongarch/intrinsics-loongarch64-crc.rs

$ rustc tests/pass/shims/loongarch/intrinsics-loongarch64-crc.rs -o /tmp/meow && /tmp/meow

$ uname -a
Linux seraphine 7.1.0-rc4-0-stable #1-Alpine SMP PREEMPT_DYNAMIC 2026-05-23 17:58:11 loongarch64 Linux
$ cat /proc/cpuinfo | grep Model -m 1
Model Name		: Loongson-3B6000

i.e. it behaves the same on real hardware and in Miri.

The implementation is based on the ACLE code with only very slight adjustments; the test is also basically the same one.

Helpful links for reviewing:

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 24, 2026

Thank you for contributing to Miri! A reviewer will take a look at your PR, typically within a week or two.
Please remember to not force-push to the PR branch except when you need to rebase due to a conflict or when the reviewer asks you for it.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label May 24, 2026
Comment thread src/shims/foreign_items.rs
Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Thanks a lot, just a few comments. :)

The code (and the test) are slightly ugly because the intrinsics take signed integers. I'd be very happy about suggestions to make it prettier.

What do you mean, you don't seem to be doing anything about the sign and just interpret the inputs as unsigned? (In the code, I mean. For the tests I guess it'd be up to stdarch to make the API less annoying...)

LLVM's intrinsic definitions: https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/IntrinsicsLoongArch.td#L78-L94 (note they all take i32 or i64 parameters)

LLVM doesn't distinguish signed and unsigned integers so that does not mean much

@rustbot author

View changes since this review

Comment thread src/shims/loongarch.rs Outdated
Comment thread src/shims/loongarch.rs
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels May 27, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 27, 2026

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

@Gelbpunkt
Copy link
Copy Markdown
Contributor Author

What do you mean, you don't seem to be doing anything about the sign and just interpret the inputs as unsigned? (In the code, I mean. For the tests I guess it'd be up to stdarch to make the API less annoying...)

Yeah, I did change that in a follow-up commit since because CI was complaining about i32 -> u32 casts. I think it's fine now anyways.

LLVM doesn't distinguish signed and unsigned integers so that does not mean much

TIL!

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels May 27, 2026
@RalfJung
Copy link
Copy Markdown
Member

Yeah, I did change that in a follow-up commit since because CI was complaining about i32 -> u32 casts. I think it's fine now anyways.

Okay, please update the PR description. :)

@Gelbpunkt
Copy link
Copy Markdown
Contributor Author

Okay, please update the PR description. :)

Done!

@RalfJung RalfJung enabled auto-merge May 27, 2026 19:04
@RalfJung RalfJung disabled auto-merge May 27, 2026 19:04
@RalfJung
Copy link
Copy Markdown
Member

Wait, we aren't even running these tests, are we? We'll have to add a loongarch target to CI. I'll take care of that.

@RalfJung RalfJung enabled auto-merge May 27, 2026 19:10
@RalfJung RalfJung added this pull request to the merge queue May 27, 2026
Merged via the queue into rust-lang:master with commit bba9f7e May 27, 2026
13 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Waiting for a review to complete label May 27, 2026
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.

5 participants