Avent of TinyGo: //go:inline

How TinyGo's optimisation levels affect inlining and when the //go:inline hint makes a difference.


Inlining is a key optimisation because it allows a compiler to see beyond the function as written. This extra visibility allows the compiler to then prove parameters are constant, return values do not escape their caller, the outcome of conditions becomes known, the bounds of array accesses become provable, and many more.

Like its bigger sibling, TinyGo supports automatic inlining because LLVM provides strong support for it. This post explores how the various optimisation flags tinygo offers, and the two inlining pragmas, impact the inlining decisions LLVM makes.

//go:inline vs //go:noinline

Before I discuss the details of optimisation flags, I need to set the stage. Big Go, and thus TinyGo, support the former's //go:noinline pragma. As its name suggests, when attached to a function, the compiler will not inline that function, period. This is mostly used in testing or debugging, but is occasionally useful in convincing the compiler to not hoist common, small, cold helpers.

In 2020, as a byproduct of removing the //go:volatile pragma and migrating to a more sustainable way represent memory mapped hardware registers, //go:inline was added to TinyGo as a way to hint to the compiler that it should inline the register access methods. It’s important to note that //go:inline is not the antonym of //go:noinline. The latter is a directive, do not perform the inlining operation. The former is merely a suggestion, a hint, it is expressed as a request, not an order.

The reason for this extends way back into programming lore. A please inline pragma exists in many languages, and have always left their users coveting a strong form. The reason there cannot be a must inline flag boils down to the fact that inlining is not simply text replacement. An inlining algorithm has to take into account the vagaries of the language, its alias semantics, stack unwinding, etc. //go:noinline is straightforward, simply don’t run the inline algorithm, but //go:inline or any inlining pragma is subject to the limits of what can be expressed in the underlying compiler and the language.

-opt

In addition to the hints discussed above, TinyGo supports five levels of optimisation, z, s, 0, 1, and 2. True to its name, these optimisation levels are arranged, not in order of slowest to fastest, but in order of smallest to largest program size (although 0 is the outlier in being neither small, or fast). As inlining small helpers into their caller to avoid the overhead of function dispatch often increases the size of a binary, just as loop unrolling, magic multiplication, and other tricks that trade space for speed.

As of TinyGo 0.45, for all targets the default optimisation level is -opt=z: smallest. For a project which targets small microcontrollers this is a reasonable a default. In addition to their paltry storage and parsimonious RAM, almost all TinyGo bare metal targets are simple in-order CPUs. This means no L1 cache, no branch prediction, no speculative execution, as all these cost die space and power, two more things on the list the small microcontrollers lack.

The five optimisation level TinyGo offers are mapped into the controls that LLVM offers. -opt=1/2 are straightforward as they map closely to clang's -O flag, however z and s are a pastiche of budgets, settings, and function attributes to guide LLVM to favour smaller binaries. In the context of inlining, the two function attributes are inlinehint and minsize. inlinehint is attached to any //go:inline function. minsize is emitted on all functions at -opt=z to instruct the compiler to prefer smallest size, similar to clang / gcc's -Os. Crucially, minsize overrides inlinehint.

As with all configuration parameters, newer ones are added over time, and the meaning of older ones changes as their impact is tested in the real world. With that said, as of writing, for TinyGo 0.45 with LLVM 22, the impact of //go:inline is as follows:

-opt Threshold without hint With inlinehint Effect of //go:inline
z 5 Ignored (minsize) None
s 50 325 Large: any callee costing 50 to 325
0 Inliner doesn't run — None
1 225 325 Only callees costing 225 to 325
2 225 325 Only callees costing 225 to 325

Astute readers will observe that //go:inline's biggest impact is at -opt=s, where it made programs larger.

Take away

The quiet part said out loud: unless your program needs a boost to the inlining budget at -opt=1/2, //go:inline is probably not the right thing to use. //go:inline on small functions is pointless because small functions inline at all levels except -opt=0, because nothing inlines at -opt=0 by policy. At -opt=1/2 the inlining budget is already aggressive so a hint is rarely needed.