LLVM for Xtensa

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

LLVM for Xtensa

Postby ESP_igrr » Wed Feb 13, 2019 7:58 am

In some of the existing topics it has been already mentioned that Espressif is working on adding Xtensa support to LLVM. Today we are releasing the first work-in-progress version based on release/6.x branch of LLVM.

https://github.com/espressif/llvm-xtensa
https://github.com/espressif/clang-xtensa

The compiler is now in the stage of active development and testing. It was successfully tested using GCC torture testsuite and multiple ESP-IDF applications (although ESP-IDF versions available on Github do not support LLVM/Clang out of the box, yet).

Current release can generate Xtensa assembly code as output (not object files!), and has to be used together with GNU Binutils and GCC-built libraries (libgcc, libstdc++, newlib) to create object and binary files.

Current Xtensa architecture features implemented in compiler:

- support Xtensa Core Architecture instructions
- support Code Density option
- support Windowed Register option
- support Floating-Point Coprocessor option
- support Boolean option (only a subset of instructions)
- support Thread Pointer option
- support atomic operations

Current Xtensa target list:

- ESP32 target (used by default, can be selected with -target xtensa -mcpu=esp32 flags)
- ESP8266 target (can be selected with -target xtensa -mcpu=esp8266 flags)

Compiler optimization levels include O0/O1/O2/O3/Os options.

Installation

Since at the current release is not intended for developers who just want to build ESP-IDF apps, we do not provide binary releases of LLVM for Xtensa, yet. To compile LLVM from source, follow these steps:

Code: Select all

git clone https://github.com/espressif/llvm-xtensa.git
git clone https://github.com/espressif/clang-xtensa.git llvm-xtensa/tools/clang
mkdir llvm_build
cd llvm_build
cmake ../llvm-xtensa -DLLVM_TARGETS_TO_BUILD=Xtensa -DCMAKE_BUILD_TYPE=Release -G "Ninja"
cmake --build .
Running the compiler

Create a test.c source file and run:

Code: Select all

llvm_build/bin/clang -target xtensa -fomit-frame-pointer -S  test.c -o test.S
This should produce test.S, an assembly file produced from test.c. You can then use the existing xtensa-esp32-elf-as assembler to generate the object file.

Plans for next releases

Development of the compiler and its testing is constantly ongoing. In future releases, improved performance is planned, such as support for Zero Overhead Loop optimization, support for various architectures and targets.
Object code generation and standalone toolchain based on Clang/LLVM is also planned.

Summary features which will be implemented in next releases:

- rebasing on the upstream version of LLVM, following the new monorepo layout (https://github.com/llvm/llvm-project/).
- object code generation
- support for LX106 target (ESP8266)
- binary releases on Github
- support for using LLVM to build ESP-IDF apps
- improvements of generated code performance
- support for zero-overhead loop option
- MAC16 option
- PSRAM cache workaround for ESP32 chip

Issues and feedback

For general questions and feedback, please post in this forum thread. For specific bug reports (code generation, handling of some language features) please use https://github.com/espressif/llvm-xtensa/issues.

Updated 2019/03/26: support for both ESP32 and ESP8266 targets (-mcpu=esp32, -mcpu=esp8266)

p-rimes
Posts: 89
Joined: Thu Jun 08, 2017 6:20 pm

Re: LLVM for Xtensa

Postby p-rimes » Wed Feb 13, 2019 4:50 pm

Wow this is amazing news! Congratulations to the Espressif team!

The first question that came to my mind is, does this enable us (or get closer) to use the languages which target LLVM (e.g. Rust, Nim, Zig, etc)?

For Rust specifically, would Espressif also host a fork of "rust-lang/rust" and "rust-lang/libc", as we go through the following steps (this comment even has suggested PRs for mainline):
https://www.reddit.com/r/rust/comments/ ... w/d9gdjwf/

For C++ specifically, what C++ standard version is being targeted for current testing, and does it use libc++ or libstdc++? Can we use fancy flags like `-fcoroutines-ts`?

Thanks again for the amazing work.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: LLVM for Xtensa

Postby ESP_Angus » Wed Feb 13, 2019 11:56 pm

p-rimes wrote:
Wed Feb 13, 2019 4:50 pm
The first question that came to my mind is, does this enable us (or get closer) to use the languages which target LLVM (e.g. Rust, Nim, Zig, etc)?

For Rust specifically, would Espressif also host a fork of "rust-lang/rust" and "rust-lang/libc", as we go through the following steps (this comment even has suggested PRs for mainline):
https://www.reddit.com/r/rust/comments/ ... w/d9gdjwf/
There are a lot of people inside Espressif who are excited about the potential to leverage LLVM to see these languages run on ESP32.

Right now, it's unlikely we would host an official Espressif fork of a project like Rust. We don't have the spare in-house expertise for this at the moment, and having Espressif "maintain" any community effort would probably slow it down more than help it. Right now our focus in this area is on getting LLVM/clang fully stable, and making sure it meets the needs of those who want to use it.

In the future, though, anything is possible...

(I'll let someone who knows more about C++ answer the second question, but my guess is that at this stage all we have is GNU libstdc++ support.)

aykevl
Posts: 2
Joined: Fri Feb 15, 2019 10:58 am

Re: LLVM for Xtensa

Postby aykevl » Fri Feb 15, 2019 11:06 am

This is great news!
Are there plans to get this backend upstreamed after it has been rebased? I'm working on porting Go to microcontrollers (see tinygo.org) and I would really like to support this chip as well. Having it upstream would make it much easier to add support.

permal
Posts: 384
Joined: Sun May 14, 2017 5:36 pm

Re: LLVM for Xtensa

Postby permal » Sun Feb 17, 2019 12:13 pm

Espressif devs keeps on astonishing us all :)

ESP_AndreiS
Posts: 2
Joined: Wed Feb 13, 2019 10:02 am

Re: LLVM for Xtensa

Postby ESP_AndreiS » Fri Feb 22, 2019 11:51 am

aykevl wrote:
Fri Feb 15, 2019 11:06 am
This is great news!
Are there plans to get this backend upstreamed after it has been rebased? I'm working on porting Go to microcontrollers (see tinygo.org) and I would really like to support this chip as well. Having it upstream would make it much easier to add support.
Yes, we have plans to propose our LLVM Xtensa back-end to upstream, but it's a quite long process, because there is several stages to promote new back-end to the official mode and each stage may took monthes

aykevl
Posts: 2
Joined: Fri Feb 15, 2019 10:58 am

Re: LLVM for Xtensa

Postby aykevl » Mon Feb 25, 2019 12:18 pm

ESP_AndreiS wrote: Yes, we have plans to propose our LLVM Xtensa back-end to upstream, but it's a quite long process, because there is several stages to promote new back-end to the official mode and each stage may took monthes
Great! And yes I know the process can take quite some time. However, even an experimental backend upstream would make things much easier.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: LLVM for Xtensa

Postby ESP_igrr » Tue Mar 26, 2019 3:15 am

LLVM for Xtensa can now generate assembly for ESP8266, in addition to the ESP32. This can be controlled by -mcpu=esp32 or -mcpu=esp8266 flag.

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: LLVM for Xtensa

Postby Deouss » Fri Apr 12, 2019 3:54 pm

Have you tried it under Microsoft Visual studio 2017 ?

amazingfly
Posts: 1
Joined: Fri Apr 10, 2020 9:09 pm

Re: LLVM for Xtensa

Postby amazingfly » Fri Apr 10, 2020 9:15 pm

Hey guys, I'm super exited about this and TinyGo. Hows it going? Keep up the great work! I love my ESP32s!

Who is online

Users browsing this forum: Baidu [Spider] and 57 guests