Open-Source Wikis

/

Zig

/

Systems

/

Compiler runtime

ziglang/zig

Compiler runtime

Active contributors: andrewrk, alexrp, jacobly

Purpose

lib/compiler_rt/ is a pure-Zig implementation of the helper routines that compilers (LLVM, the self-hosted backends, GCC) emit references to: software floating point, big-integer arithmetic, division/modulo on architectures without hardware support, atomic helpers, stack probes, exception personality routines, and so on. It is the equivalent of LLVM's compiler-rt/lib/builtins/ but written in Zig and shipped with every zig install.

Directory layout

  • lib/compiler_rt.zig — the root file (~11 KB). Re-exports everything and gates symbols on the active target.
  • lib/compiler_rt/ — one file per routine or group: divti3.zig, udivmodti4.zig, mulodi4.zig, floattidf.zig, addtf3.zig, truncsfhf2.zig, comparetf2.zig, clzdi2.zig, popcountdi2.zig, bswapsi2.zig, aulldiv.zig, aullrem.zig, plus arch-specific files and outline atomics generated by tools/gen_outline_atomics.zig.

What it covers

Categories visible from filenames:

Category Examples
Integer division/modulo divti3, udivmoddi4, divsi3, aulldiv, aullrem
128-bit integer arithmetic mulodi4, mulosi4, muloti4, addvti3, subvti3
Software floating point addtf3, subtf3, multf3, divtf3, comparetf3, truncsfhf2, extendhfsf2
Float ↔ int conversions floattidf, floattisf, fixunsdfti, fixunssfti
Bit utilities clzdi2, ctzdi2, popcountdi2, bswapsi2, parity
Atomics atomic_load_n, atomic_store_n, plus AArch64 outline atomics generated by tools/gen_outline_atomics.zig
Stack/safety stack_probe, unwind helpers, personality routines
OS/arch specifics Files prefixed with aulldiv (Windows ABI), aarch64_outline_atomics, etc.

When the LLVM backend or the self-hosted backends emit a reference to __divti3, __floattidf, etc., the linker resolves it from lib/compiler_rt/. The library is built as part of every Zig compilation when needed, with target-aware gating in lib/compiler_rt.zig. The names match LLVM's libcalls table so it is a drop-in replacement.

Tests

Behaviour tests live alongside the rest of the suite (test/behavior/floatop.zig etc.). Many of the routines also have inline test "..." blocks in their source files.

Integration points

  • LLVM backend (src/codegen/llvm.zig) — emits libcalls expecting these names.
  • Self-hosted backends (src/codegen/x86_64/, src/codegen/aarch64/, ...) — same.
  • C backend (src/codegen/c.zig) — emits portable C that links the same runtime.
  • zig cc — when compiling C, links compiler_rt instead of GCC's libgcc.a.

Entry points for modification

  • New libcall. Add the routine in lib/compiler_rt/<name>.zig and re-export from lib/compiler_rt.zig under the appropriate target gate.
  • AArch64 outline atomics. Edit tools/gen_outline_atomics.zig and regenerate.

Key source files

File Purpose
lib/compiler_rt.zig Root and re-exports.
lib/compiler_rt/ One file per routine.
tools/gen_outline_atomics.zig Generator for AArch64 outline atomics.
lib/ubsan_rt.zig UBSan runtime helpers.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Compiler runtime – Zig wiki | Factory