diff --git a/content/changelog.mdx b/content/changelog.mdx index 57a51dd..1b731ac 100644 --- a/content/changelog.mdx +++ b/content/changelog.mdx @@ -11,6 +11,8 @@ releases can be downloaded from [GitHub](https://github.com/burnlang/burn/releas ## Unreleased +- Native code is much faster ([native executables](./internals/native.mdx#a-function)): the hottest locals in loops live in registers, parameters are no longer copied on every call, `push` stores in place, division by a constant uses a multiplication instead of the divide instruction, small constants are immediates, and small functions are inlined. A prime sieve runs 8× faster and pushing to an array 20× faster, on x86-64 and arm64. +- Builds are about twice as fast: large programs are assembled in parallel on every core, the generated assembly is a quarter smaller, and the runtime frees memory and collects cycles without allocating. The compiler builds itself in 6.3 s instead of 11.8 s, and the same runtime changes make every program that allocates a lot faster. - Local variables that only hold a parameter, another variable, or a field or element of one now borrow their value instead of counting a reference, when nothing can free it while they are used ([memory](./language/memory.mdx#what-the-compiler-does)). Loops over arrays of objects and strings and walks over linked structures no longer touch reference counts; the compiler's own code does 8.5% fewer of them and builds faster. - The compiler rejects memory hazards that used to compile, each with a suggested fix ([memory](./language/memory.mdx#what-the-compiler-rejects)): adding to or removing from an array or map inside a `for` loop over it, using an array, map or object while an `async fun` it was given to may still be changing it, and async functions that change globals or read global arrays, maps and objects. diff --git a/content/internals/native.mdx b/content/internals/native.mdx index 79e9213..e892808 100644 --- a/content/internals/native.mdx +++ b/content/internals/native.mdx @@ -8,7 +8,8 @@ order: 53 `burn build` (and `burnc`) turns a program into a standalone executable for the machine: x86-64 or 64-bit ARM, on Linux or macOS. It starts from the same IR as bvm, after the [ownership pass](./memory.mdx), and generates assembly -text that the system's C compiler assembles and links. +text that the system's C compiler assembles and links. A large program's assembly is cut into pieces of about 150000 +lines at function boundaries, which are assembled in parallel on every core and linked together. ```text IR ──▶ compiler/src/native/x86.bn or arm64.bn ──▶ app.s ──▶ cc ──▶ app @@ -34,11 +35,7 @@ bf_1: mov rbp, rsp sub rsp, 32 mov rax, qword ptr [rbp + 24] ; a - mov qword ptr [rbp - 8], rax - mov rax, qword ptr [rbp + 16] ; b - mov qword ptr [rbp - 16], rax - mov rax, qword ptr [rbp - 8] - mov rcx, qword ptr [rbp - 16] + mov rcx, qword ptr [rbp + 16] ; b add rax, rcx jo .L9 ; integer overflow? mov qword ptr [rbp - 24], rax @@ -57,8 +54,16 @@ bf_1: - **Functions** are labeled by number (`bf_1`); `--no-strip` keeps these labels in the symbol table. - **Arguments** are pushed on the stack by the caller, first argument first, so the last one is closest to the callee's frame; the caller removes them after the call. Results come back in `rax` (`x0` on ARM). -- **Locals** live in the stack frame at fixed offsets. Expressions are evaluated into `rax`/`x0`, with intermediate - values pushed on the stack. +- **Parameters** stay where the caller pushed them; the function reads and writes them there. +- **Locals** live in the stack frame at fixed offsets, except the hottest locals used in loops: up to four live in + `rbx`, `r13`, `r14` and `r15` (eight in `x19`–`x26` on ARM), which the function saves on entry and restores when + it returns. Expressions are evaluated into `rax`/`x0`, with intermediate values pushed on the stack; constants + that fit are used as immediates, and `x += 1` on a register local is a single instruction. +- **Small functions** whose body is one expression over their parameters (getters, wrappers, the runtime's memory + helpers) are inlined at their call sites before code generation (`lower/inline.bn`). +- **Division by a constant** uses shifts for powers of two and a multiplication by a precomputed magic number + otherwise, instead of the slow divide instruction. +- **`push`** stores the element in place while the array has room and only calls the runtime to grow it. - **Checks** are inline: overflow is a `jo` (on ARM, the flags after `adds`/`subs`, or a widening multiply), bounds checks compare with the length stored in the array, null checks test for zero. The error path is placed after the function and calls the runtime with the location id, so the fast path has no extra jumps. @@ -66,7 +71,7 @@ bf_1: convention, with the stack aligned to 16 bytes. - **Reference counting** is inlined: a retain or release is a null test, a static test, a test of the multi-threaded flag and an increment or decrement of the count at offset 12. Only freeing and the cycle check call - into the runtime ([memory management](./memory.mdx#retain-and-release-at-runtime)). + into the runtime, through one shared stub per program ([memory management](./memory.mdx#retain-and-release-at-runtime)). - **Fields and array elements** are read with one instruction after the checks, at the offsets described in [values in memory](./values.mdx). - **Interface calls** load the object's type id, index the dispatch table and call through it.