Linux RISCV64 Support

I have been working on patches for compiling Swift 5.6 for RISCV64 with Buildroot. First I compiled the patched compiler (swift, llvm-project and swift-core-tools-support) and then use Buildroot with patched compiler to cross compiler the Swift Stdlib. I have gotten to the point where I can successfully compile libdispatch, the Swift StdLib and runtime's .c, .cpp and .swift files for RISCV64, but the linking stage for the StdLib fails.

https://bugs.swift.org/browse/SR-16005

ld.lld: error: stdlib/public/core/LINUX/riscv64/Swift.o: cannot link object files with different floating-point ABI
ld.lld: error: Swift.o![|16x16](https://bugs.swift.org/images/icons/emoticons/sad.png).text+0x0): relocation R_RISCV_ALIGN requires unimplemented linker relaxation; recompile with -mno-relax

I have tried a lot of Swift compiler flags based on Every Option and Flag /swift (1.2) Accepts Ever · GitHub and passing them via -DSWIFT_STDLIB_EXTRA_SWIFT_COMPILE_FLAGS to cmake for cross compiling the StdLib but none seem to work.

8 Likes

I was able to build for RISCV64 with Soft Float enabled.

Swift patches

Buildroot

1 Like

https://github.com/apple/swift/pull/41858/

2 Likes

Amazing work!, @ColemanCDA. This has so much potential!

As a matter of fact, I just recently started to get into microcontrollers. In particular I'm supposed to write the firmware for an ESP32-C3 (RISCV32) based device and would love to use Swift rather than C and C++.

How similar are RISCV32 and RISCV64? What would it take to enhance your patch to also support RISCV32?

The issue is, RISCV32 is tipically not Linux capable and lacking MMU. The ISA itself is not an issue, it's the devices you can buy with that ISA not being designed for Linux. If you find a RISCV32 devices that can run Linux, let me know and I'll try to build Swift for it.

I see. Well, the devices I'm interested in are MCUs anyways. I guess what I'm really after is to expand Swift On Arduino to more platforms, i.e. ESP32-C3 (RISCV32).

Updated patches and PRs merged

PRs for Trunk:

Patches for Swift 5.7:

1 Like

Awesome!

Doug

1 Like

I've built a patched 5.7.1 compiler and have an example with Buildroot cross compiling the Swift StdLib. libswiftdispatch is currently failing to build.

"nezha" is the RISCV64 board