Swift on RISC-V

Swift on RISC-V

Hi everybody,

I would like announce Swift on RISC-V, a new project I am currently working on.

The RISC-V ecosystem is growing at a phenomenal rate and 2023 will see the introduction of many new exciting and powerful devices. From small soc boards up to large 64 / 128 core workstations and servers.

Primarily the focus on this project will be on building the swift toolchain for riscv64 devices.

Most of the core programming languages and development tools are already available for the riscv64 architecture and I think it is very important for the future of Swift to be available for riscv64 as well.

Project Goals

The main goals for the project are:-

  1. Setup and maintain a riscv64 CI build server for Swift development and testing.
  2. Provide feedback and #PR's to the Swift project for bugs and fixes required for RISC-V
  3. Port the swift toolchain to riscv64 on Debian / Ubuntu OS's
  4. Build and distribute Swift for riscv64 Debian and Ubuntu packages via the Swift Community Apt Repository.
  5. Build and distribute Swift riscv64 Libraries for hardware functionality such as GPIO on the VisionFive2 board.

Project Status June 2023

Help Needed

Help is needed in many areas and everybody is welcome to join the project.

You can use Github Discussions or the Swift Forums for any questions and support.

If someone can help getting the initial #PR's noticed and merged it would be very appreciated.

RISC-V International

For more information on RISC-V and the RISC-V community please go to
RISC-V International

38 Likes

this is is really cool!

i have a couple risc-v boards (visionfive2, hifive unmatched) so if there is any help needed with testing on real hardware etc i'd be willing to help out if possible ^_^

1 Like

@andrekandore, how does the performance of the visionfive2 compare with the hifive unmatched?

visionfive 2 seems a bit better than unmatched i must say

especially the graphical performance on unmatched was not great, even with a beefy gpu (games like tux racer or whatever was fine) ... but it was very hard to use for graphical development...

i suspect it has to do with what was explain in this thread: Memory access is too slow - #13 by davidlt - HiFive Unmatched - SiFive Forums

1 Like

Great, what is the current status of building the toolchain on one of those linux riscv64 boards? Can you run the Swift compiler validation suite on linux riscv64 yet? @ColemanCDA has been working on cross-compiling the Swift stdlib for riscv64, so that may be in good shape already.

You may want to set up a CI to cross-compile the Swift toolchain to linux riscv64, after which you could transfer it to the Visionfive2 board and run the Swift tests on it. That way, you don't have to build the Swift toolchain itself on the slower riscv64 board, but only use it for testing.

That is how I build the native Swift toolchain for Android that I distribute with the Termux app, I cross-compile that Swift toolchain package from linux x86_64 to Android AArch64, armv7, and x86_64.

I'd like to help out with this RISC-V port, but I have no idea how far along it is already. Also, will you be able to provide ssh access to a few contributors on this riscv64 board? That was a big problem with the prior armv7 CI, that such access was not available to debug build failures.

1 Like

@Finagolfin, providing ssh access to the VisionFive board at the moment is not practical as the OS and firmware are not stable and are being frequently updated.

The best option for development at the moment is to use docker on an arm64 or x86_64 machine and just use the riscv64 board for final verification of patches.

See here for more info Building a Developer Environment for Riscv64 With Docker.

Setting up a CI to cross-compile the Swift toolchain to linux riscv64 is a good idea and it would be great if somebody does this.
For myself one of the main purposes of this project is to assess to current state of the riscv64 ecosystem and its ability to build the swift toolchain natively.

The initial issues I have found have been posted here - swift-riscv64/issues

One of the first issues that will need to be fixed is implementing the swiftcall calling convention on riscv64. If you could help with this that would be great.

Why would that matter for ssh access, the board may need to be rebooted sometimes? Since we would only need to log in once in awhile, I don't think that would be a big deal.

Yep, that's best for most, but I've never used Docker and don't want to. I always use real hardware and never emulators, as there are inevitably differences. Anyway, I don't think I'll have much time for this port.

What is your assessment so far? Once you get the toolchain natively compiling, it should be pretty easy to cross-compile it after that, so I can help with that.

Thanks, will keep tabs on those.

No background with that, doubt I'd help much. Glad you set this repo and CI up, so the community can contribute in whatever areas they know best.

@Finagolfin the VisionFive board is not in a rack in a cloud server farm somewhere. It is on my desk at home next to my iMac. These are prototype dev boards and updating the firmware and OS system is a bit more involved than a reboot.

PR's #66530 and #66699 have been approved and merged :tada: :tada:
Big thank you to everyone who helped with these.

7 Likes

June 2024 - Special Announcement

Hi everyone,

I am excited to announce the first release from the Swift on RISC-V project.

Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: riscv64-unknown-linux-gnu

This a complete swift toolchain for installation and use on riscv64 hardware such as the visionfive2 board.

Please Note ~
This release is not production ready and may have unexpected bugs!

Known Issues ~

  • swift repl - not working

  • swift scripting - not working - e.g. swift hello.swift

  • macros are unavailable as the toolchain is build with bootstrapping off

  • swiftc requires extra flags -Xlinker -latomic - e.g. swiftc -Xlinker -latomic hello.swift

  • swift build requires extra flags -Xlinker -latomic - e.g. swift build -Xlinker -latomic

  • swift run requires extra flags -Xlinker -latomic - e.g. swift run -Xlinker -latomic

  • swift test requires extra flags -Xlinker -latomic - e.g. swift test -Xlinker -latomic

Installation ~

The release includes both .tar.gz and .deb package files.

The easiest way to install swift is to download the .deb package and use the system apt installer.

wget https://github.com/swift-riscv/swift-riscv64/releases/download/swift-5.10.1-RELEASE/swiftlang_5.10.1-1.0.0-debian-sid_riscv64.deb

sudo apt install ./swiftlang_5.10.1-1.0.0-debian-sid_riscv64.deb

Docker ~

If you don't have any riscv64 hardware but would still like to test out swift on riscv64 there is a docker image available.

docker pull swiftarm/swift:5.10.1-debian-unstable-riscv64
docker run --platform linux/riscv64 --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -it swiftarm/swift:5.10.1-debian-unstable-riscv64   /bin/bash

Project Update

  • All development has now moved to swift-5.10.1-RELEASE and the main-branch. Older swift versions have proved to be too difficult and not viable to continue.

  • Test release of swift-5.10.1-RELEASE for riscv64 is now available!

  • A new project, Patches to Upstream, has been set up to track upstream patches being submitted to swift and all the supporting projects.

  • New riscv64 hardware is coming soon! In the coming weeks we will be receiving a new Pioneer Box will hugely reduce build times and increase productivity.

Thats all for now, hope you enjoy using swift on riscv64.

Thanks.

One more thing ~

Included in the release there is a special riscv64 version of the Vapor Toolbox.

20 Likes

Exciting news!

The new Milk-V Pioneer riscv64 workstation has arrived!
Sophon SG2042 cpu - 64 cores at 2GHz
128GB RAM
1TB nvme ssd
Performance is looking very good in initial testing.

swift-5.10.1-RELEASE - V2 is now available.
This release fixes the -Xlinker -latomic requirement.
This also enables us to use this version to bootstrap future swift builds on riscv64.
Fully bootstrapped builds with macros support should be available soon.

The Swift Community Apt Repository has been upgraded to support the riscv64 architecture.
If you have a riscv64 device running Linux - Debian or Ubuntu, swift can now be installed using the apt package manager.

curl -s https://archive.swiftlang.xyz/install.sh | sudo bash
sudo apt install swiftlang
7 Likes

This is amazing! Excellent work, can’t wait to try it out in the future

Great to hear things are moving forward! Have you tried running the compiler validation test suite on RISC-V? It would be interesting to know how that does.

Up till now we have just been focussing on achieving a successful build so have been building with no tests activated.
This was mainly due to the very slow build times of 24+ hours on the VisionFive2 boards.
Now that we have the new Pioneer Workstation we can start running all the tests and posting the results.
I am expecting lots of interesting failures.

3 Likes

Very good to know. I shall cease trying to get it working on 5.7.3 immediately. When you first built 5.10.1 that was without a working swiftc compiler installed I assume? As that is the path that I will need to follow.

If you are still trying to get Swift into Debian and are not talking about RISC-V specifically, I suggest you try to get 5.8 or 5.9 in first, as both probably did not require a prebuilt Swift compiler to build the full Swift compiler quickly, ie with the bootstrapping mode. Once that's in, you could then use that prebuilt Debian compiler to build an updated Swift 5.10 or 6.0, which pretty much require such a prebuilt Swift compiler.

Alternately, you'd have to build the 5.10 compiler purely from C++ first, then rebuild the full Swift 5.10 compiler with that bootstrap C++-only Swift 5.10 compiler.

@SteveM
You will need to build a 5.10.1-release without bootstrapping first by adding these extra flags to the buildbot preset in the build-presets.ini file.

skip-early-swift-driver
skip-early-swiftsyntax
bootstrapping=off

You can then use this version to build a fully bootstrapped version.

This is all you need to do to build 5.10.1 versions for arm64 and x86_64.

Building for riscv64 is another story and there are too many patches needed at the moment to expect it to build using the official Debian build method you are working on.

As well as swift, there are also 18 sub projects that need patching. A lot of the patches are minor but some are major workarounds and are not suitable to be included in official releases.

There is also the issue of the gold linker not being available on riscv64 which requires the build to be modified to default to the lld linker.

@Finagolfin
I built 5.6.3 for Debian and got it into unstable and testing, but it started to FTBFS due to other changes such as with glibc. Right now 5.7.3 is in Debian unstable and will migrate to testing in a few days. Only x86_64 and arm64 for now.

When I was working on 5.6.3 I was under the impression that it was the last version of Swift that could be built with a C++ compiler, and that I would need to use Swift 5.6.3 to build 5.7.3 and that to build 5.8.1 and so on. But it seems that in reality we can still build with a C++ compiler, it just has missing features, but it can in turn be used to build itself again with all of the features. Maybe I should leapfrog to 5.9 or 5.10.

@futurejones
I don't mind having to make a large number of patches for RISC-V and have a compiler that is potentially buggy as long as only the RISC-V version is impacted. I will have to carefully plan out the 5.10.1 build for Debian as it would also need to build for x86_64 and arm64 at the same time with the same set of build files. Thank you for the guidance on how to pull it off. I might just give it a try on x86_64 and arm64 to see if I can't just skip all of the inbetween versions.

1 Like

OK, once that's in, can installation of that prebuilt 5.7.3 package be made a requirement to build 5.10.1? If so, you may be best off jumping to 5.10 that way.

That was 5.8.3, as 5.9 started requiring prebuilt Swift "host tools" on linux.

I don't think that has ever been the case: 5.10 through 6.1 are currently built on the official linux CI by the 5.8.1 compiler. It is possible that 5.7 is now too old to build 5.10, but you could easily check that with a local build first.

Yes, that is what I referred to earlier when I said, "Alternately, you'd have to build the 5.10 compiler purely from C++ first...", but as Neil points out, that is a more involved process.

Sure, I'm just suggesting that using a prebuilt 5.7 or 5.8 may be an easier way for you to make that jump to 5.10.

1 Like