RISC-V support for LLVM projects (LLVM, Clang, ...)
Switch branches/tags
Nothing to show
Clone or download
Latest commit 3a81c43 Oct 18, 2018
Permalink
Failed to load latest commit information.
clang Rebase on 326957 and add further patches Mar 14, 2018
docs commit more docs Jan 15, 2018
scripts improve clang driver and update torture suite driver to take advantag… Nov 9, 2017
0001-Revert-upstreamed-RISC-V-changes.patch Rebase on 326957 and add further patches Mar 14, 2018
0002-RISCV-Recognise-riscv32-and-riscv64-in-triple-parsin.patch Rebase on 326957 and add further patches Mar 14, 2018
0003-RISCV-Add-RISC-V-ELF-defines.patch Rebase on 326957 and add further patches Mar 14, 2018
0004-RISCV-Add-stub-backend.patch Rebase on 326957 and add further patches Mar 14, 2018
0005-RISCV-Add-basic-RISCV-InstrFormats-InstrInfo-Registe.patch Rebase on 326957 and add further patches Mar 14, 2018
0006-RISCV-Add-bare-bones-RISC-V-MCTargetDesc.patch Rebase on 326957 and add further patches Mar 14, 2018
0007-RISCV-Add-basic-RISCVAsmParser.patch Rebase on 326957 and add further patches Mar 14, 2018
0008-RISCV-Add-RISCVInstPrinter-and-basic-MC-assembler-te.patch Rebase on 326957 and add further patches Mar 14, 2018
0009-RISCV-Add-support-for-all-RV32I-instructions.patch Rebase on 326957 and add further patches Mar 14, 2018
0010-RISCV-Add-support-for-disassembly.patch Rebase on 326957 and add further patches Mar 14, 2018
0011-RISCV-Add-common-fixups-and-relocations.patch Rebase on 326957 and add further patches Mar 14, 2018
0012-Add-RISC-V-support-to-update_llc_test_checks.py.patch Rebase on 326957 and add further patches Mar 14, 2018
0013-RISCV-Initial-codegen-support-for-ALU-operations.patch Rebase on 326957 and add further patches Mar 14, 2018
0014-RISCV-Codegen-support-for-materializing-constants.patch Rebase on 326957 and add further patches Mar 14, 2018
0015-RISCV-Codegen-support-for-memory-operations.patch Rebase on 326957 and add further patches Mar 14, 2018
0016-RISCV-Codegen-support-for-memory-operations-on-globa.patch Rebase on 326957 and add further patches Mar 14, 2018
0017-RISCV-Codegen-for-conditional-branches.patch Rebase on 326957 and add further patches Mar 14, 2018
0018-RISCV-Support-for-function-calls.patch Rebase on 326957 and add further patches Mar 14, 2018
0019-RISCV-Implement-lowering-of-ISD-SELECT.patch Rebase on 326957 and add further patches Mar 14, 2018
0020-RISCV-Support-and-tests-for-a-variety-of-additional-.patch Rebase on 326957 and add further patches Mar 14, 2018
0021-RISCV-Use-register-X0-ZERO-for-constant-0.patch Rebase on 326957 and add further patches Mar 14, 2018
0022-RISCV-Support-lowering-FrameIndex.patch Rebase on 326957 and add further patches Mar 14, 2018
0023-RISCV-Implement-prolog-and-epilog-insertion.patch Rebase on 326957 and add further patches Mar 14, 2018
0024-RISCV-Allow-lowering-of-dynamic_stackalloc-stacksave.patch Rebase on 326957 and add further patches Mar 14, 2018
0025-RISCV-Add-custom-CC_RISCV-calling-convention-and-imp.patch Rebase on 326957 and add further patches Mar 14, 2018
0026-RISCV-Support-for-varargs.patch Rebase on 326957 and add further patches Mar 14, 2018
0027-RISCV-Support-stack-frames-and-offsets-up-to-32-bits.patch Rebase on 326957 and add further patches Mar 14, 2018
0028-RISCV-Add-basic-support-for-inline-asm-constraints.patch Rebase on 326957 and add further patches Mar 14, 2018
0029-RISCV-Add-support-for-llvm.-frameaddress-returnaddre.patch Rebase on 326957 and add further patches Mar 14, 2018
0030-RISCV-Implement-branch-analysis.patch Rebase on 326957 and add further patches Mar 14, 2018
0031-RISCV-Implement-support-for-the-BranchRelaxation-pas.patch Rebase on 326957 and add further patches Mar 14, 2018
0032-RISCV-Reserve-an-emergency-spill-slot-for-the-regist.patch Rebase on 326957 and add further patches Mar 14, 2018
0033-RISCV-MC-layer-support-for-the-standard-RV32M-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0034-RISCV-MC-layer-support-for-the-standard-RV32A-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0035-RISCV-MC-layer-support-for-the-standard-RV32F-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0036-RISCV-MC-layer-support-for-the-standard-RV32D-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0037-RISCV-MC-layer-support-for-the-standard-RV64I-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0038-RISCV-MC-layer-support-for-the-standard-RV64M-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0039-RISCV-MC-layer-support-for-the-standard-RV64A-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0040-RISCV-MC-layer-support-for-the-standard-RV64F-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0041-RISCV-MC-layer-support-for-the-standard-RV64D-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0042-RISCV-Implement-frame-pointer-elimination.patch Rebase on 326957 and add further patches Mar 14, 2018
0043-RISCV-Codegen-support-for-the-standard-RV32M-instruc.patch Rebase on 326957 and add further patches Mar 14, 2018
0044-RISCV-Add-initial-RV64I-codegen-support.patch Rebase on 326957 and add further patches Mar 14, 2018
0045-LegalizeDAG-promote-frameaddr-returnaddr-arguments-t.patch Rebase on 326957 and add further patches Mar 14, 2018
0046-LegalizeDAG-promote-PREFETCH-operands-to-native-inte.patch Rebase on 326957 and add further patches Mar 14, 2018
0047-RISCV-Initial-support-for-emitting-call-frame-inform.patch Rebase on 326957 and add further patches Mar 14, 2018
0048-RISCV-Peephole-optimisation-for-load-store-of-global.patch Rebase on 326957 and add further patches Mar 14, 2018
0049-RISCV-Add-codegen-for-RV32F-arithmetic-and-conversio.patch Rebase on 326957 and add further patches Mar 14, 2018
0050-RISCV-Add-codegen-for-RV32F-floating-point-load-stor.patch Rebase on 326957 and add further patches Mar 14, 2018
0051-RISCV-Codegen-support-for-FPR32-stack-loads-stores.patch Rebase on 326957 and add further patches Mar 14, 2018
0052-RISCV-Codegen-support-for-floating-point-comparison-.patch Rebase on 326957 and add further patches Mar 14, 2018
0053-RISCV-WIP-Codegen-support-for-RV32F-fused-multiply-a.patch Rebase on 326957 and add further patches Mar 14, 2018
0054-RISCV-Add-minimum-necessary-for-RV32D-codegen-for-fa.patch Rebase on 326957 and add further patches Mar 14, 2018
0055-RISCV-Add-codegen-support-for-RV32D-floating-point-a.patch Rebase on 326957 and add further patches Mar 14, 2018
0056-RISCV-Codegen-support-for-RV32D-floating-point-conve.patch Rebase on 326957 and add further patches Mar 14, 2018
0057-RISCV-Codegen-support-for-RV32D-floating-point-compa.patch Rebase on 326957 and add further patches Mar 14, 2018
0058-RISCV-Codegen-support-for-RV32D-floating-point-load-.patch Rebase on 326957 and add further patches Mar 14, 2018
0059-RISCV-Add-xfailed-test-case-for-type-legalisation-fa.patch Rebase on 326957 and add further patches Mar 14, 2018
0060-Add-xfailed-RV32D-test-case-failing-with-multiple-vr.patch Rebase on 326957 and add further patches Mar 14, 2018
0061-RISCV-MC-layer-support-for-load-store-instructions-o.patch Rebase on 326957 and add further patches Mar 14, 2018
0062-RISCV-MC-layer-support-for-the-jump-branch-instructi.patch Rebase on 326957 and add further patches Mar 14, 2018
0063-RISCV-MC-layer-support-for-the-remaining-RVC-instruc.patch Rebase on 326957 and add further patches Mar 14, 2018
0064-RISCV-MC-layer-support-for-the-instructions-added-in.patch Rebase on 326957 and add further patches Mar 14, 2018
0065-RISCV-Implement-assembler-pseudo-instructions-for-RV.patch Rebase on 326957 and add further patches Mar 14, 2018
0066-RISCV-Implement-RISCVRegisterInfo-enableMultipleCopy.patch Rebase on 326957 and add further patches Mar 14, 2018
0067-RISCV-Enable-emission-of-aliased-instructions-by-def.patch Rebase on 326957 and add further patches Mar 14, 2018
0068-RISCV-Pass-MCSubtargetInfo-to-print-methods.patch Rebase on 326957 and add further patches Mar 14, 2018
0069-RISCV-Codegen-for-atomic-fences-loads-and-stores.patch Rebase on 326957 and add further patches Mar 14, 2018
0070-RISCV-Lowering-for-AMO-operations.patch Rebase on 326957 and add further patches Mar 14, 2018
0071-RISCV-Expose-options-to-enable-optimisation-of-compl.patch Rebase on 326957 and add further patches Mar 14, 2018
0072-RISCV-Implement-isLegalAddressingMode-for-RISC-V.patch Rebase on 326957 and add further patches Mar 14, 2018
0073-RISCV-Implement-isLegalAddImmediate.patch Rebase on 326957 and add further patches Mar 14, 2018
0074-RISCV-Implement-isLegalICmpImmediate.patch Rebase on 326957 and add further patches Mar 14, 2018
0075-RISCV-Implement-isTruncateFree.patch Rebase on 326957 and add further patches Mar 14, 2018
0076-RISCV-Implement-isZExtFree.patch Rebase on 326957 and add further patches Mar 14, 2018
0077-RISCV-Implement-computeKnownBitsForTargetNode-for-RI.patch Rebase on 326957 and add further patches Mar 14, 2018
0078-RISCV-Implement-isLoadFromStackSlot-and-isStoreToSta.patch Rebase on 326957 and add further patches Mar 14, 2018
0079-RISCV-Allow-RISCVAsmBackend-writeNopData-to-generate.patch Rebase on 326957 and add further patches Mar 14, 2018
0080-RISCV-Encode-RISCV-specific-ELF-e_flags-to-RISCV-Bin.patch Rebase on 326957 and add further patches Mar 14, 2018
0081-RISCV-Define-getSetCCResultType-for-setting-vector-s.patch Rebase on 326957 and add further patches Mar 14, 2018
0082-RISCV-Add-support-for-pcrel_lo.patch Rebase on 326957 and add further patches Mar 14, 2018
0083-RISCV-Add-ELFObjectFileBase-getRISCVFeatures-let-llv.patch Rebase on 326957 and add further patches Mar 14, 2018
0084-RISCV-Implement-MC-relaxations-for-compressed-instru.patch Rebase on 326957 and add further patches Mar 14, 2018
0085-RISCV-Set-AllowRegisterRenaming-1.patch Rebase on 326957 and add further patches Mar 14, 2018
README.mkd README fix Oct 17, 2018

README.mkd

RISC-V LLVM

About

This repository hosts a series of patches implementing a RISC-V backend for LLVM as well as initial tutorial material. For more information, see:

Our intent is that development and code review primarily occurs upstream on LLVM infrastructure. However, there is value is maintaining the RISC-V backend implementation as a set of clean patches. These will be continually rebased with the intention that they provide a useful reference for anybody starting a new LLVM backend or who wants to better understand backend implementation.

Mini FAQ

Should I be compiling my code with Clang and the RISC-V LLVM backend?

If you simply want to compile your C/C++ codebase for RISC-V then for the time being, just using GCC is a simpler starting point. Once RISC-V LLVM support is included in an official LLVM release in the future and is made slightly more user friendly, the answer to this question should change.

When should I use this repository and when should I use upstream LLVM/Clang

If you find the step-by-step patch history useful for educational purposes or want to check if there are any patches that I'm preparing for upstream but haven't submitted yet, then using this repository may be worthwhile. In all other cases, and especially if you want to contribute to the effort you are strongly advised to use the upstream LLVM repositories.

How can I build upstream LLVM+Clang and use it to cross-compile for a riscv32 target?

First you will need a built RISC-V gcc toolchain. The linker, compiled libraries, and libc header files will be used. You can build your own by following the instructions at the riscv-gnu-toolchain repo. It is sufficient to do the following:

git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
./configure --prefix=/your/gccinstallpath --with-arch=rv32imc --with-abi=ilp32
make -j9

You can then check out upstream LLVM and Clang and build it. llvm.org has useful documentation on building LLVM with CMake, but you should find enough to get started below. Note that you will need to apply a single out-of-tree patch for riscv32-unknown-elf target support in Clang until it is reviewed and committed.

git clone https://git.llvm.org/git/llvm.git
cd llvm/tools
git clone https://git.llvm.org/git/clang.git/
cd clang
wget https://reviews.llvm.org/D46822?download=true -O baremetal.patch
patch -p0 < baremetal.patch
cd ../../
mkdir build && cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE="Debug" \
  -DBUILD_SHARED_LIBS=True -DLLVM_USE_SPLIT_DWARF=True \
  -DLLVM_OPTIMIZED_TABLEGEN=True \
  -DLLVM_BUILD_TESTS=True \
  -DDEFAULT_SYSROOT="/path/to/riscv-gcc-install-path/riscv32-unknown-elf" \
  -DGCC_INSTALL_PREFIX="/path/to/riscv-gcc-install-path" \
  -DLLVM_DEFAULT_TARGET_TRIPLE="riscv32-unknown-elf" \
  -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="RISCV" ../
cmake --build .
./bin/clang /path/to/testfile.c

A few notes on the above CMake invocation and issues you might encounter:

  • It's possible to build LLVM with RISC-V support with a much simpler CMake invocation. Ultimately you need to ensure you set -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="RISCV"
  • This produces a debug build of LLVM and Clang which is significantly more useful for bug reporting if you encounter any issues. The downsides are that it will execute more slowly and compilation requires a large amount of disk space (~12G).
  • Building LLVM puts a heavy load on your linker. If you have lld installed, you may find significantly less memory is required to link if you set -DLLVM_ENABLE_LLD=True. Alternatively, ensure that /bin/ld is symlinked to ld.gold rather than ld.bfd.
  • It isn't necessary to set the default sysroot, gcc install prefix and default target triple when building clang, but doing so means you don't need to pass these arguments explicitly when invoking it. If you prefer, you can explicitly pass these flags when cross-compiling: -target riscv32-unknown-elf --sysroot=/path/to/riscv-gcc-install-path/riscv32-unknown-elf --gcc-toolchain=/path/to/riscv-gcc-install-path
  • You can also compile for riscv32-unknown-linux-elf in conjunction with a multilib toolchain as produced by the scripts in the riscv-gnu-toolchain repository. Note that only the ilp32 ABI is supported for the moment.

Status

As of May 2018, the vast majority of these patches are now upstream and most users wishing to experiment with support for RISC-V in LLVM projects will likely be best served by building directly from the upstream repositories. You may prefer to follow this repository if you want to study how the backend is put together.

Note that the patches in this repository are over-due for another rebase. I hope to address this soon.

The current patchset allows the entirety of the GCC torture suite to compile and run for {RV32I, RV32IM, RV32IFD}. Additionally, all torture suite tests compile and run for RV64I at O1, O2, O3, and Os. MC-layer support is present for RV32IMAFDC+RV64IMAFDC, and codegen support for RV32IMFDC and RV64I. See the lowRISC RISC-V LLVM status page for more details.

Future plans

As well as the obvious implementation tasks, it is also our intent to augment this patchset with copious documentation. The initial draft of this documentation is available in this repository.

Our aim is that RISC-V is the cleanest and most documented LLVM backend. As well as benefiting the wider LLVM community, this will make it substantially easier for research groups and hobbyists to explore hardware/software co-design with RISC-V.

Our lowRISC project is working to produce a completely open source System-on-Chip implementing the RISC-V instruction set architecture. One of the features we are pursuing is tagged memory. This repository will eventually host compiler support for that feature, although this may need time to mature before being ready to upstream and so may be developed in a separate branch.

RFCs

This work has so far produced a number of request for comment (RFC) threads on the LLVM mailing list:

License

All patches are under the LLVM license.

The docs/ directory is under the Creative Commons CC-BY-SA 4.0 license https://creativecommons.org/licenses/by-sa/4.0/.

How to build the patchset in this repository

There are two things to note:

  1. This repository currently serves a dual purpose - providing a clean and constantly rebased "perfect" history of the full RISC-V patchset, as well as being a way to grab all patches in one go, including those that haven't yet been committed upstream. As more and more patches are committed upstream, this repository will become mainly interesting as a reference on how to implement an LLVM backend, and aid in understanding the RISC-V backend. If you just want to test out the latest RISC-V LLVM support, chekcing out and building upstream LLVM is probably your best choice.
  2. This LLVM and Clang work is not quite at a stage where end-users are likely to find it useful. However, building and trying it out is a great first step towards contributing to the development effort.

You might build LLVM and Clang with these patches applied by doing something like the below. See the LLVM documentation for more general guidance. You will need ~7GiB space for a debug build of LLVM+Clang.

export REV=326957 # Check the most recent commit on this repo to ensure this is correct
svn co http://llvm.org/svn/llvm-project/llvm/trunk@$REV llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk@$REV clang
cd ..
for P in /path/to/riscv-llvm/*.patch; do patch -p1 < $P; done
for P in /patch/to/riscv-llvm/clang/*.patch; do patch -d tools/clang -p1 < $P; done
mkdir build
cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE="Debug" \
  -DBUILD_SHARED_LIBS=True -DLLVM_USE_SPLIT_DWARF=True \
  -DLLVM_OPTIMIZED_TABLEGEN=True \
  -DLLVM_BUILD_TESTS=True \
  -DDEFAULT_SYSROOT="/path/to/riscv-gcc-install-path/riscv32-unknown-elf" \
  -DGCC_INSTALL_PREFIX="/path/to/riscv-gcc-install-path" \
  -DLLVM_DEFAULT_TARGET_TRIPLE="riscv32-unknown-elf" \
  -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="RISCV" ../
cmake --build .

You may then want to run tests:

./bin/llvm-lit -s -i -v test/MC/RISCV       # RISC-V MC-layer tests
./bin/llvm-lit -s -i -v test/CodeGen/RISCV  # RISC-V CodeGen tests
./bin/llvm-lit -s -i -v test                # All tests

Credits

This work has been led by Alex Bradbury at lowRISC CIC, a not for profit company. If your employer would like to see my work on RISC-V LLVM to be sustained or to accelerate, contributing sponsorship and/or development time is the best way to do that. Please contact asb@lowrisc.org if you would like to discuss sponsorship, or have questions about code contributions that you can't discuss on the LLVM mailing list.

Thank you to everyone who has contributed review comments, suggestions, or code to this patchset and related support patches: Sameer Abu Asal, Chandler Carruth, Ahmed Charles, Shiva Chen, Chih-Mao Chen, Kito Cheng, David Chisnall, Simon Cook, David Craven, Hal Finkel, Eli Friedman, Ondrej Glasnak, Mandeep Singh Grang, Ben Horgan, David Kipping, James Y Knight, David Majnemer, Ed Maste, John McCall, Dylan McKay, Azharuddin Mohammed, Tim Northover, Krzysztof Parzyszek, Ana Pazos, Jordy Portman, Philip Reames, John Russo, Colin Schmidt, Ed Schouten, Pavel Šnobl, Ulrich Weigand, Mario Werner, Eugene Zalenko, Florian Zeitz, Leslie Zhai.