Compiler
The native backend: LLVM
How the LLVM backend turns a program into a native executable, what it covers, and the runtime limits of natively built programs.
since 0.1.0-alpha.1linux
Description
The compiler produces a native executable with the LLVM backend. It starts from the same checked and
lowered program that the interpreters run (see lang.engines).
The LLVM backend
The LLVM backend is the primary native backend. It covers the whole language: classes, collections,
closures, exceptions, and the standard library's system services such as the event loop, timers,
async and await, files and threads. It is available when the compiler was built with LLVM; a
compiler built without it reports this build has no LLVM backend.
| Option | Result |
|---|---|
--emit-llvm |
Prints the program as LLVM IR text. |
--native-obj <out.o> |
Writes an object file directly, without calling any external tool. |
--build-native <out> |
Writes the object file, links it with the runtime library and the system C++ compiler, and leaves the executable <out>. |
--opt-level <0|2> |
2 (the default) optimises. 0 generates code faster and is easier to follow in a debugger. Any value other than 0 optimises. |
--build-native links against liblvrt.a, the runtime library, which is installed beside the
leviathan program; --runtime <path> names a different one. A shared library (--lib shared)
links liblvrt_pic.a instead, the same runtime compiled as position-independent code; the
liblvrt.a build is compiled for executables only, which makes it faster. The linker driver is the first of
clang++, g++ and cc found on the PATH. A file named lvrt.link beside the runtime library
lists any extra linker flags the runtime needs, such as the TLS libraries, and --build-native
reads it for you. If you link an object file from --native-obj yourself, you have to supply
those flags yourself.
A program for the native backend
use io::console;
bool isPrime(int n) {
if (n < 2) {
return false;
}
int d = 2;
while (d * d <= n) {
if (n % d == 0) {
return false;
}
d = d + 1;
}
return true;
}
int found = 0;
int last = 0;
for (int n in 1..50) {
if (isPrime(n)) {
found = found + 1;
last = n;
}
}
console.writeln("${found} primes up to 50, the largest is ${last}");
15 primes up to 50, the largest is 47
Saved as primes.lev:
leviathan --build-native primes primes.lev
./primes
leviathan --opt-level 0 --build-native primes_debug primes.lev
leviathan --native-obj primes.o primes.lev
Memory limits of a natively built program
The escaping heap, which holds objects, arrays, maps, closures and strings that live beyond the function that made them, lives in one window of address space per thread. Each thread reserves the window once, sized for the arena plus a heap ceiling of 16 GiB, and uses its memory in chunks: the first 256 MiB of heap is ready from the start, and each later chunk is at least as large as all the heap used so far. A program that fits in the first chunk never pays for the growth.
A limit on the process's address space, such as ulimit -v on Linux, counts the whole window of
every thread, used or not. When such a limit is set, the default ceiling follows it: the main thread
aims for a quarter of the limit, at least 256 MiB, and every other thread for a thirty-second of it,
at least 64 MiB; neither aims above 16 GiB. If the operating system refuses a reservation, the window is halved until it is
granted, down to 64 MiB of heap, and the ceiling is what was granted, so a thread that cannot get its
share runs with a smaller heap instead of stopping. On Windows the ceiling
does not depend on such a limit. The environment variable LANG_HEAP_MAX_MB changes the ceiling of
every thread, in whole mebibytes, when it is set for the running program:
- A positive value below 256 is raised to 256. The window can still end up smaller when the operating system refuses the reservation, as described above.
- A value that is not a whole positive number, such as
0,-1orabc, means "use the default 16 GiB". - Reaching the ceiling prints
lvrt: heap exhausted (ceiling N MiB), whereNis the ceiling in effect, and ends the program with exit status1. - If the operating system refuses memory below the ceiling, the program prints
lvrt: heap commit refused by the OS at N MiB committedand exits with status1. If it refuses even the smallest window, 64 MiB of heap, for a thread, the program prints a message that names the ceiling and suggests raising the address-space limit or loweringLANG_HEAP_MAX_MB, and exits with status1. - On WebAssembly there is no reservation: the heap grows the module's linear memory, and only while
nothing else has grown that memory since the heap last did. If something else has, the program
prints
lvrt: heap exhausted (wasm linear memory grown by another allocator); if the engine refuses to grow the memory, it printslvrt: wasm memory limit reached at N MiB. Either way it exits with status1. - A single object larger than 2 GiB, less a 16-byte header, can never be allocated however much
heap is left. The program prints
lvrt: object too largeand exits with status1. - The arena that holds short-lived values of a single function is a fixed 64 MiB per thread. Running out of it never stops the program: a value that no longer fits takes an ordinary heap block instead, and that block stays allocated until the program exits. Values created inside a loop whose register frees them do not use the arena at all.
LANG_HEAP_MAX_MB=512 ./primes
Rules
--build-nativeand--native-objneed a compiler built with the LLVM backend.--build-nativeneeds a C++ compiler on thePATHas the linker driver.--targetand--opt-levelapply to--native-objand--build-native.- A program that the backend cannot compile fails with a diagnostic naming the feature. It never compiles into a program that behaves differently.
LANG_HEAP_MAX_MBis read by the running program, not by the compiler.
Examples
Reading the generated code, for debugging or learning:
leviathan --emit-llvm primes.lev > primes.ll
Building natively and checking that the executable agrees with the bytecode interpreter:
leviathan --ir primes.lev > a.txt
leviathan --build-native primes_llvm primes.lev
./primes_llvm > b.txt
cmp a.txt b.txt
Notes
- The standard library ships as files that the compiler reads at start-up: the directory is found
as described in
lang.compiler-cli, with a copy built into the compiler as the last resort. The part of it that exposes the browser's DOM is only read when the target is WebAssembly.
See also
- The leviathan command line — Every option of the
leviathancompiler, grouped by what it does, with the exit statuses and how arguments reach your program. - The execution engines — The three ways the compiler runs a program, why they always agree, and the few places where one of them has to refuse a program.
- Cross-compilation: --target and per-target runtimes — Building executables for another operating system or processor, what the build needs for each target, and what a target can reject.
- The WebAssembly (browser) target — Building a program as a WebAssembly module for the browser, what it can use, what it cannot, and how it reaches the page.