-
Notifications
You must be signed in to change notification settings - Fork 666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support MSA SIMD for MIPS #244
base: master
Are you sure you want to change the base?
Conversation
--enable-msa now works in single and double precision for MIPS. Tested on both 32-bit and 64-bit MIPS run in little-endian.
@i-evi How is that testable? Is there support for MSA in Qemu (+ linux, toolchains, ...) so that it could be tested there? Is there some MSA hardware available? |
@rdolbeau The MSA SIMD Instruction Set Architecture was introduced since MIPS Release 5. It provides support for 128-bit single- and double-precision floating-point vector arithmetic. We can see it on some MIPS processors, such as: Ingenic X2000 (32 bit MIPS R5 compatible) I have the above two computing platforms, and tested fftw optimized with MSA SIMD on it, they passed all test cases, and achieved a certain performance improvement. Debian supports mipsel and mips64el architectures, so testing on the above platforms can be easily done by native compilation and running. If it is difficult to obtain MIPS computer for testing, you can also use QEMU for testing.To cross-compile, on Debian, you can install the toolchain directly through apt: apt install gcc-mipsel-linux-gnu
apt install gcc-mips64el-linux-gnuabi64 Since MIPS Release 6, the MIPS Instruction Set Architecture has undergone major changes, and the toolchain has also changed. Installing the MIPS R6 toolchain on Debian is as follows: apt install gcc-mipsisa32r6el-linux-gnu
apt install gcc-mipsisa64r6el-linux-gnuabi64 Unfortunately, QEMU currently has limited support for MIPS MSA. If you are using Debian 11 (bullseye), it provides QEMU version 5.2.0, and only 64-bit MIPS R6 CPUs can execute MSA instructions correctly. So you need to set the parameter qemu-mips64el-static -cpu I6500 QEMU 7.2.0 supports MSA slightly better. Some MIPS R5 architecture CPUs can also execute MSA instructions correctly, such as I tested the fftw optimized with MSA through QEMU, and it works fine on 64-bit MIPS CPUs that support MSA. FFTW is an excellent project, it is widely used in digital signal processing related software. Although the development of MIPS over the years is not satisfactory, there are still a large number of MIPS chips in service or production. Thank you for your concern and hope fftw can support MIPS MSA. |
@i-evi Thanks for the details; for QEmu I was hoping for full system emulation - it's a lot easier to test when running natively. I did some stuff in MSA for crypto (e.g. https://github.com/jedisct1/supercop/tree/master/crypto_stream/chacha20/dolbeau/mipsel-msa) and at the time couldn't get QEmu+Linux to support MSA... i it's possible, would you have an example command line for QEmu (I have 7.2.0) that would enable installing e.g. Debian mips64el ? (any other distro would do if it's easier). Meanwhile I'd suggest updating the copyright in simd-msa.h; also if the code only support little-endian, it probably should fail explicitly in big-endian just in case. |
@rdolbeau Testing with QEMU is a little more complicated. A simple way is that we can use binfmt_misc + QEMU. Assuming you are using Debian (I recommend bookworm, the current sid, as it has the newer version 7.2.0 of QEMU), install the package qemu-user-static: apt install qemu-user-static The package manager configures binfmt support so that we can run binaries for any architecture supported by QEMU directly on the host. For example, we first cross-compile a simple program:
In this way, we get an executable
We already have binfmt_misc support, so we can run it directly:
This is equivalent to:
But when we compile dynamically linked programs, we may encounter errors like this:
The toolchain provides these dynamic libraries, which you may find in
First, copy all the library files provided by the toolchain to "./", then we chroot "./" as "/", so that we can run the dynamically linked executable "a.out". I used the above method to test fftw with QEMU. I configured
To use chroot, I renamed "bench" to "bench.mips64el", then created the file "bench" and made it executable:
Write the following script in the "bench" file we just created: #!/bin/bash
sudo QEMU_CPU=Loongson-3A4000 chroot ./ ./bench.mips64el $*
If you need a more sophisticated software testing environment, you can try using the full linux rootfs. On Debian, first install debootstrap: apt install debootstrap Debootstrap can help you install rootfs with the specified architecture, such as mips64el(don't forget to install qemu-user-static): debootstrap --arch=mips64el sid mips64el_root http://ftp.debian.org/debian After running the above command we can get a Debian rootfs. We first mount some filesystems to ensure that the host's resources can be used properly in the chroot environment(optional, but I suggest you do):
Then we run
You can use the native platform in the chroot environment. For example use
By using binfmt_misc + QEMU, and chroot into the rootfs of another architecture is the easiest way to set up a cross-architecture environment in my opinion. The chroot environment and the host environment use the same kernel. If you use qemu-system, you may also need to prepare a bootable kernel. |
@i-evi I'm of a different opinion on Qemu - I don't like modifying/cluttering my host for transient efforts, and I've found that full-system emulation is usually the easiest (and closest to real hardware configuration-wise). I do it routinely for Aarch64, PowerPC, RISC-V, SPARCv8 and 68k (and for those last two Qemu is even faster than vintage systems ;-) ), and it only requires a zvolume (ZFS) that can be easily snapshot'ed / backup'ed / migrated between hosts. I guess it all boils down to personal preferences :-) I'll have to see if I can coerce Qemu in doing what I want... |
@rdolbeau I agree with you. Using full system emulation on aarch64, riscv, etc. architectures is the convincing and easiest way. But for mips, the situation is a little different, QEMU's current support for mips is relatively poor. For 32-bit mips, run First download the installer image from https://ftp.debian.org/debian/dists/stable/main/installer-mips64el/current/images/, I take loongson-3 as an example. Create a virtual disk image qemu-system-mips64el \
-M loongson3-virt \
-m 2G \
-kernel vmlinuz-5.10.0-20-loongson-3 \
-initrd initrd.gz \
-append "console=ttyS0,115200 loglevel=4" \
-drive file=disk.img,format=raw,if=virtio \
-serial mon:stdio -nographic \
-device virtio-net-pci,netdev=net0 \
-netdev user,id=net0,net=10.0.2.0/24,host=10.0.2.1,restrict=false You will see the virtual machine boot, then go to the Debian installer and follow the normal installation steps to install Debian on your virtual disk. Follow the installer to complete the system installation. Since the installer has no way to install the bootloader, we exit QEMU after completing the installation, and then copy the newly installed initrd and kernel from the virtual disk image. Take my disk.img as an example (the virtual disk image I use is in raw format):
Then use the new initrd and kernel, and add the path to root in the kernel parameters (I installed on
You will be able to boot into Debian just installed on the virtual disk.Using the same approach, you can also try installing Debian mipsel on the I really don't recommend this though, because there are a lot of problems with SMP and memory support, you may only be able to use 1 CPU and no more than 2GB of memory. This is really annoying for slightly more complex software compilation and testing, because there are so few computing resources available. Also, what is known is that even QEMU version 7.2.0 has some issues with MSA support for 32-bit mips. Among the 64-bit mips CPUs, the ones that can correctly support MSA instructions are For the available platforms of Being able to do a full system emulation of mips using QEMU is certainly a good way. However, the current situation is that it seems difficult to test some advanced features such as MSA without spending a lot of time dealing with QEMU and kernel issues. As things stand, using qemu + chroot is a compromise. |
--enable-msa now works in single and double precision for MIPS.
Tested on both 32-bit and 64-bit MIPS run in little-endian.