# Design: inline small leaf callees (/docs/experiments/tier0/design-inline)

## Problem [#problem]

Dev builds call every function, however small. In spectralnorm's inner loop, the call to a(i, j) has four integer operations, a conversion and a division. It costs the call itself. On x86-64 it also costs a stack slot for the float accumulator, because no XMM register is callee saved. The loop index and length take callee saved registers. spectralnorm dev runs 8.8x slower than -O 2.

## Change [#change]

In MirBuild, a direct call to a function of the same module is replaced by the callee's body. The callee must be a leaf that cannot leave by a panic:

- defined in the module (ModuleGen::funcs), not variadic, no no_inline attribute on the callee or the call (//go:noinline, defer thunks).
- one block that ends in llvm.return.
- at most 24 operations. None of them is a call, an invoke, inline assembly, an alloca, or an atomic or volatile memory access. None is an intrinsic outside the pure set of MIR (ctlz, cttz, ctpop, bswap, bitreverse, fshl, fshr, saturating float to int).
- no i128 and no aggregate above the MIR piece limit (so the caller cannot lose MIR because of the callee).

The builder maps the callee's block arguments to the call's operands (pieces for aggregates). It translates the callee's operations with its existing translation (op()). Then it binds the call's result to the returned value. Nothing else changes: no new blocks, no block splitting. CSE, constant folding and the allocator see the inlined operations as the caller's own.

## Properties [#properties]

- Semantics: the callee has no calls and one block. So it has no side effects beyond its own loads (and stores, if allowed later), and it cannot panic. Evaluating its operations in the caller's block in order is the same as the call.
- Tracebacks and pc tables: the inlined operations take the call site's location. A traceback can only start at a call or a fault. The callee makes no calls. A fault in a load of the callee would show the caller's line instead of the callee's. An example is a nil pointer that the front end did not check. The first version allows loads. Stores stay out.
- GC and stack maps: no safe point inside the inlined code (no calls).
- The callee is still compiled as its own function (other modules, function values).
- Compile time: one pass over the callee per call site, at most 24 operations. A per builder cache of the eligibility decision.

## Measurement plan [#measurement-plan]

- Static: on the tuios corpus, the number of inlined call sites, code bytes, and the change in MIR fallbacks (must stay 0 new).
- perfcmp dev, dev against MARMOT_TIER0_INLINE=1 with MIR on. Then all three modes before any flip (O 2 and release are unchanged code).
- Gates: a tier0 unit test (an inlined leaf with aggregates and a float result, and a no_inline callee that stays a call). Conform dev and gcstress with the switch, and fuzz 200.

## Later [#later]

Multi-block callees (with their panic blocks moved into the caller and an inline frame record for tracebacks), and callees with stores.