LLVM - Free Pascal wiki (original) (raw)

The current status of Low Level Virtual Machine (LLVM) support is ready for general testing.

Light bulb Note: The information in this section has been updated as of September 2023 and might be outdated by the time you read it

Progress

FPC with an LLVM code generator backend is available on the git main branch. It currently supports the following targets:

Usage

Install Clang

You can use a version included in an LLVM release available from the official LLVM site, or use a version that comes with Xcode (macOS) or your Linux distribution.

FPC can generate LLVM code that can be compiled with LLVM 7.0 until at least 17.0. Use FPC's -il command line parameter to list all supported LLVM and Xcode versions. These can be selected using the -Clv command line parameter.

Build FPC with LLVM support

Build FPC as usual, but add LLVM=1 to the make command line, and

Warning-icon.png

Warning: If you specify any custom parameters through OPT when building FPC, then also add those in OPTNEW and FPCMAKEOPT. The reason is that during the compilation of the packages directory only the contents of OPTNEW will be used (it's hard to change this because during cross-compiling there are special requirements to build the fpmake utility using the bootstrap compiler).

#include /etc/fpc.cfg #ifdef CPULLVM -FD/Users/Me/clang+llvm-8.0.0-x86_64-apple-darwin/bin -Clv8.0 #endif

Installing FPC with LLVM support

FPC built with LLVM support does not include the built-in code generator. Additionally, installing it will currently overwrite any FPC with the same version number that's installed in the same prefix (target directory). As the units generated by FPC with the LLVM backend are not compatible with those used by FPC with the built-in code generator, it's better to install such a version in a different prefix (target directory) for now. Use the make parameter INSTALL_PREFIX=/xxx/yyy to specify this prefix. As above, you can use custom block in your '$HOME/.fpc.cfg' to specify the alternative unit directories:

#ifdef CPULLVM -Fu/yourLLVMinstallPREFIX/lib/fpc/$fpcversion/units/$fpctarget/* -Fu/yourLLVMinstallPREFIX/lib/fpc/$fpcversion/units/$fpctarget/rtl #endif

Link-time optimisation means that potentially the entire program and all units that it uses are all optimised together.

To compile units with LTO support, or to compile a program or library with LTO, add -Clflto on the compiler command line. If you add this to OPT/OPTNEW when building FPC, all standard units and the compiler itself will also be built with LTO.

Notes:

Using Address Sanitizer (asan)

Address sanitizer is an LLVM code generation pass that instruments all memory accesses to provide (relatively) fast detection of reading uninitialised memory, buffer overruns, and accessing freed memory. It is similar to Valgrind, but faster, and also more accurate because LLVM knows exactly where each local and global variable starts and ends, so it can detect a pointer to one local/global variable crossing into the memory of another local/global variable.

To use it, compile all units and the main program/library with the -Clfsanitize=address option.

Notes:

Open tasks

Frequently Asked Questions

Will the FPC team, somewhere in the future, adopt the LLVM as the backend on all platforms?

No, for various reasons:

Is it at all likely that an LLVM compiler would produce significantly better/faster optimizations than FPC as it stand currently?

See Also