Adding/porting a backend/architecture to LLVM
After finally managing to port an architecture i thought i’d share the steps as there isnt a clear or simple guide on adding your own backend to LLVM. It should be noted that at this time of writing the LLVM documentation at llvm.org is outdated (also mentioned on their website).
Assuming you have your architecture code ready there are a few files needed for LLVM to detect, compile and use your backend. It is also recommended to look at other architectures for hints on the files/code required for your backend to be initialized by LLVM.
The initial important step is to place your backend folder in llvm/lib/Target. Make sure to have your TargetMachine.cpp and TargetMachine.h files correctly written otherwise clang will produce an error during compilation. Both files require code in the TargetInfo folder using TargetInfo.cpp for a simple definition of what your backend will be called and how it will be called.
- Non-code files required
In your backend folder, you will need makefiles and LLVMBuild.txt files in each folder in your backend. These files explain the catogary and helps configure to find your backend code. An easy way to do this would be to copy the files from other backends already available and rename the contents to match your backend.
- change certain LLVM configures
In order for LLVM to detect your backend for configuration and compilation in order to be used, you will need to modify:
-llvm/configure (look for places where the backends have been added by searching for existing architectures such as ‘ARM’ and add yours there the same way) The lines to add would be something like this:
[arch-name]*) llvm_cv_target_arch=”[arch-folder]” ;;
[arch-name]) TARGET_HAS_JIT=0;; (0 disables JIT, 1 enables JIT)
[arch-name]) TARGETS_TO_BUILD=”[arch-folder] $TARGETS_TO_BUILD” ;;
[arch-folder]) TARGETS_TO_BUILD=”[arch-folder] $TARGETS_TO_BUILD” ;;
– modify llvm/projects/sample/configure similarly to above
– add your backend path to llvm/lib/Target/LLVMBuild.txt
- LLVM code files to modify:
-llvm/lib/support/tripple.cpp (add your backend to sections that it supports below other backends, make sure to keep to the same syntax/format)
-llvm/include/llvm/ADT/Triple.h (add your backend to sections that lists other backends)
If your backend uses very different intrinsics, you can add the intrinsics to llvm/include where some architectures have their own versions there.
After performing the above changes, you can than run ./configure –enable-targets=”myarch”.
To install it in a different directory without super user privilages, run ./configure –prefix=”my directory” –enable-targets=”myarch”
using –enable-targets=”myarch” works more often than Target=”myarch” (happens for some backends such as microblaze. using –Target=”mblaze” will not work while –enable-targets=”mblaze” works).
you can compile certain backends by using –enable-targets=”arch1 arch2 arch3″
–enable-targets=all will include all detected backends.
Once it has been install you can check if your backend was detected and compiled by using llc –version.
Version of clang and LLVM used – 3.1