Skip to content

ADR 0002: Debug, release and unchecked build modes

Status: Final Type: Architecture Created: 2026-07-26 Amended-By: 0022 (free-threading declaration), 0079 (the WebAssembly-text backend retired), 0053 (the list-loop elision that applies in both modes), 0055 (the loop shape leaves the byte-stability contract), 0062 (<<), 0075 (an executable's int grows past 64 bits in both modes) Related-ADRs: 0007 (assertions ride the exception machinery), 0020 (the other case release's latitude does not reach)

Context

The POST Python spec assumes a debug/release distinction: §4.2 requires bounds checks in debug and makes them configurable in release; §5.1 compiles assert to a no-op in release; §4.1.1 requires debug builds to diagnose integer overflow where practical and requires the compiler to document whether release wraps, traps, or assumes no overflow.

Decision

The answer to §4.1.1's documentation obligation: debug and release trap, an unchecked build wraps. Integer arithmetic on a 64-bit int is diagnosed in a debug and a release build, and the program aborts naming what overflowed. A release build differs from a debug build only by its assertions, as python -O differs from python. --release --unchecked drops the checks a run can only fail (the overflow diagnosis, the Array bounds check and the other stops listed below), so it continues where they would stop it, and its arithmetic wraps at the declared width, which is what the target does natively. The owner chose wrapping over the spec's other permitted answer for that build, assuming no overflow, so every backend wraps, the C backend included.

Release keeps the checks by the owner's decision (A194), taken on their measured cost: kept in a release build, they cost the thirteen benchmark programs 1.05× (C) to 1.14× (LLVM) their unchecked time, geometric mean, on Linux x86_64 (notes/measurements.md, 2026-11-12).

Two kinds of integer are outside that rule. In an executable, int grows past 64 bits as CPython's does, in both modes (0075, A189); the rule applies to the int of a library, an extension module, or an executable built with --no-grow-ints. The fixed-width types Int8 to Int64 and UInt8 to UInt64 are machine integers of their width and wrap in both modes. CPython does not implement these types, so it is not their oracle: under the interpreter an Int32-annotated value is a plain int and never wraps. Their differentials compare against CPython's value reduced to the width by postyp's casts.

What each mode does

Debug (default) Release (--release) Unchecked (--release --unchecked)
assert evaluated; a failure raises AssertionError, which a handler catches (0007) not lowered at all, as python -O does not lowered
Array subscript per-axis bounds check on the normalized index the same check no check
Integer + - * ** // %, unary -, abs on a 64-bit int overflow diagnosed overflow diagnosed wraps at the declared width
The same on a fixed-width type wraps at the declared width wraps at the declared width wraps at the declared width
Division by zero: /, //, %, divmod, 0.0 ** -1.0 raises ZeroDivisionError raises ZeroDivisionError raises ZeroDivisionError
Float ** too large for a double: 10.0 ** 400 raises OverflowError raises OverflowError raises OverflowError
Float ** with a complex CPython value: (-8.0) ** 0.5 reports and aborts reports and aborts reports and aborts
Integer << (ADR 0062) overflow diagnosed, a count past the width included overflow diagnosed wraps at the declared width
int.from_bytes past 64 bits, under the 64-bit int overflow diagnosed overflow diagnosed wraps
A range() step of zero known only at run time reports and aborts reports and aborts not checked: the loop runs forever or not at all
Narrowing cast (Int8(x)), int or float source the defined wrapping conversion, identical to postyp identical identical
int(x), round(x), math.floor/ceil/trunc of NaN or ±infinity raises CPython's ValueError or OverflowError (A191) the same the same
Fixed-width cast (Int64(x)) of NaN or ±infinity reports and aborts (postyp raises) reports and aborts returns 0, a documented value
A constant the compiler folds that a 64-bit int cannot hold compile error (A193) compile error (A193) compile error (A193)
C compiler flags -O2 -fPIC -fwrapv -O2 -fPIC -fwrapv -flto -O2 -fPIC -fwrapv -flto

Negative-index normalisation (a[-1] is the last element) is semantics, and every mode keeps it. CPython means the last element by it; every build agrees.

The checks live in the IR below the C optimiser, so the flag difference between modes has no semantic weight. -O0 in debug would buy nothing and risk backend divergence. Release's -flto is link-time inlining of the runtime translation units: it changes no floating-point operation and no output byte, only how many instructions an already-emitted call costs. Debug keeps plain flags, for fast links and clean debug info.

-fwrapv is in both modes because the C backend emits its arithmetic in signed C types, where an overflow is undefined behaviour the C compiler may exploit. With the flag, an unchecked build and a build without overflow checks wrap identically on all three backends.

The unchecked build's latitude is for overflow and bounds only

A quotient by zero has no wrapped value, and IEEE 754's infinity is not the value CPython computes. So a division, a remainder, divmod or a power whose zero case CPython answers with ZeroDivisionError raises it, with CPython's message, in both modes (A56), the argument ADR 0020 made for a negative integer exponent. A float power of finite operands whose result a double cannot hold raises CPython's OverflowError in both modes, with the text the platform's strerror gives. A negative float to a fractional power is a complex in CPython, which no Float64 holds, so it reports and aborts in both modes, as int ** negative does: CPython raises nothing a program could catch there. A divisor that folds to a non-zero constant, or an exponent that folds into [0, 1], has no check.

MIN // -1 and MIN % -1 have no machine answer either: C and LLVM leave them undefined and x86's idiv traps. Where the overflow is not checked (--unchecked, a fixed-width type, --no-overflow-checks) and the divisor may be -1, the lowering keeps -1 from the division: a // -1 is -a, which wraps to MIN, and a % -1 is 0. A divisor that folds to another constant keeps the one instruction.

The mode is a frontend parameter

It decides which IR the frontend emits. No backend sees it or branches on it. Two facts force this. QBE's optimisation passes match on the shape of the IR, so a check node a backend merely skipped would still break its unroll in release. Consumers that call a backend directly, never touching the build layer, must still get the mode they asked for. The consequence is the acceptance test: unchecked IR stays byte-stable. When build modes landed, release IR was byte-identical to the IR emitted before them, because release was the existing code path with a name. It changed only where a decision changed it on purpose: the loop record of 0055, which took the loop shape out of the contract, and the growing int of 0075. When release took the checks (A194), the contract moved with the code path to --unchecked, whose IR was checked byte-identical to the release IR before the change on the thirteen whole programs and the cross benchmark's kernels.

The default is debug

The shipped default is the one that cannot silently disagree with CPython. That is affordable because of the next decision.

Bounds checks are hoisted out of loops

A branch in a loop body destroys auto-vectorisation and kills QBE's unroll (measured at several-fold on the element-wise kernel). The kernels are written in one shape, for i in range(len(x)): out[i] = f(x[i]). There the loop already guarantees the index, so x[i] needs no check at all and out[i] needs one, hoisted into the preheader. A checked build then runs within a few percent of an unchecked one on those kernels, which makes debug-by-default, and since A194 a checked release, defensible. A structural test checks the shape of QBE's emitted IL, because a lowering change that silently switches a backend's optimiser off is visible nowhere else.

Overflow is diagnosed by default, with an opt-out

--no-overflow-checks keeps a checked build's bounds checks and assertions but drops the overflow diagnosis, for code that relies on modular arithmetic (hashes, checksums, PRNGs). The justification for the default: postyp's arithmetic does not wrap, so an overflowing program already disagreed with the interpreted oracle in every build, silently; debug merely reports a divergence that was there all along.

Only user-level arithmetic is checked. Compiler-synthesised arithmetic (loop counters, stride chains) stays unchecked, because QBE's strength reduction and unroll match on its exact shape and checking it would kill both passes in both modes. A range with a step other than 1 or -1 counts down the iterations CPython's length gives, computed once in unsigned arithmetic, so its counter needs no direction test inside the loop and cannot run past its stop near the largest integer.

The overflow check asks the backend whether the operation overflows (BinOp.ADD_OVF, SUB_OVF, MUL_OVF), and past that test computes the signed operation, which can no longer overflow. The C backend calls __builtin_*_overflow, the LLVM backend llvm.s*.with.overflow, and QBE computes a sign test, which a product skips when both operands fit in 32 bits. A check written in plain C over a signed product is one the compiler may fold away, since signed overflow is undefined behaviour; the builtin's answer is defined. Every check comes from one statement of where it is lowered, mode.checks_overflow, which the range proofs that remove the checks that cannot fire read too.

A failed check aborts

Uncatchable, by design: a failed bounds check or overflow diagnosis reports on stderr and aborts. A bounds-checked program stops with a message and cannot recover. So a checked run that finishes printed exactly what an unchecked build of the same program prints on the same input: every difference between them is of the kind "the checked build stops where the unchecked one continues". A finished checked run vouches for the unchecked build on that input, and on no other: on an input no checked run has seen, an unchecked build can compute a wrong value without a word. --unchecked asks its user to accept that, and release does not ask it (A194).

assert is the one check that is not of this kind. A failed assertion raises AssertionError, which a handler catches (0007), and release does not evaluate it. For assert, a debug build is python and a release build is python -O, which is the difference the language specifies.

Consequences

  • The project publishes two sets of performance numbers, release and --release --unchecked (benchmarks.wholeprog --unchecked, benchmarks cross --unchecked).
  • Wrapping in the C backend gave up speed that clang had drawn from undefined signed overflow, in index arithmetic it had treated as non-wrapping. Winning it back takes a proof that an operation cannot overflow, not an assumption (notes/measurements.md, 2026-11-01).
  • A function that may divide by zero is a raiser, so each call to it is followed by the pending-exception check (0007).
  • An out-of-range narrowing cast of a finite value is not diagnosed at runtime: postyp defines Int8(200) as -56 under the interpreter and compiled, the cast oracle pins the agreement, and a trapping cast would break the oracle while removing the only spelling for intended truncation (the spec's normative ask is a compile-time diagnostic, which exists). The one case with no wrapped value at all, a non-finite float source, traps in a checked build and returns a documented 0 in an unchecked one; the bare (int64_t)x conversion it replaced was undefined behaviour returning different bits every run.
  • The spec's word for bounds checks in release is "configurable": --unchecked is the configuration, and it goes with the overflow diagnosis, since both rest on the same vouching argument.
  • Single-writer assertions (§9.6's fourth row) need an ownership model that does not exist.
  • The 64-bit int this ADR's overflow rule governs is, since A189, the int of a library, an extension module and a --no-grow-ints executable; 0075 records the measurements that chose growth for an executable.

Alternatives considered

  • Mode as a backend/build-layer property: rejected: a backend that skips checks still keeps their nodes (breaking shape-matched passes), and consumers bypassing the build layer would silently get the wrong mode.
  • -O0 debug builds: rejected: the checks live in the IR, so -O0 buys no safety and risks divergence on a path that must stay bit-identical.
  • Trapping narrowing casts in debug: rejected: it would abort where the interpreted program returns a defined value, breaking the CPython oracle the test strategy rests on.
  • Assuming no overflow in release, the spec's third answer: rejected by the owner (2026-11-01). The C backend did this before -fwrapv, which made x + 1 > x at the largest int print True on the C backend and False on the other two.
  • Release without the checks, the decision until 2026-11-12: replaced by A194 once measured. It traded "never silently wrong" for 5 to 14% of speed on average, in the build users ship; --unchecked keeps the trade for the user who makes it.
  • Two switches, --no-bounds-checks with --no-overflow-checks: rejected (A194). Both checks rest on one argument, the vouching above, so one switch states it once; --no-overflow-checks stays for its own purpose, modular arithmetic, where wrapping is what the program means.
  • -O, -O1 … as aliases: rejected (A194). Every build is already -O2 for the C compiler, so a level would have nothing to vary, and -O3 reads as "the same program, faster" to a C user while here it would change what the program does.
  • An int that grows in debug only: see 0075's alternatives.