Chris Low-level systems developer
← Back to all posts

The Read the Compiler Is Not Allowed to Skip

ISA

No instruction-set claim is made. volatile is a contract with the C abstract machine: every access through a volatile lvalue is an observable side effect the compiler cannot optimize out. The lbu listings illustrate the mechanism on RISC-V, but the verdict follows from the type qualifier, and every paragraph holds with the CPU swapped.

MICROARCH

No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured anywhere in the argument; the hoist is a compiler decision read off the listings, before any hardware runs.

FIRMWARE

No boot code, machine-mode service, or privilege handoff takes part. The experiment is two freestanding functions compiled for a bare-metal target, and the argument never enters firmware.

KERNEL

No kernel is involved at any step. The driver is polled bare-metal code, and the comparison is made by reading compiler listings, never by running under an operating system.

ALGORITHM

The claim is a compiler contract about observable side effects, not a computation. Nothing is proved against an independent oracle, and no portable mechanism is under test.

The transmit path of my UART driver waits on one register: the line-status register at 0x10000005 on the QEMU virt board, bit 5, which the transmitter sets when its holding register is empty and ready for the next byte. The wait loop is the obvious three-liner: read the byte, test the bit, spin until it arrives. [1]

Drop the word volatile from the pointer type, compile at -O2, and the loop stops touching the hardware. I compiled both variants with the riscv64-unknown-elf 13.2.0 toolchain and read the difference straight off the two listings. Without volatile, GCC emits a single lbu before the loop and then a jump to itself: if bit 5 was clear on entry, the copy held in a5 can never change, because nothing in the loop writes to that address, so the compiler is entitled to treat the memory as unchanged. The wait becomes permanent. With volatile, the lbu moves inside the loop, one fresh read per iteration, until the transmitter sets the bit. [1]

The two listings, reduced to the loop itself:

# plain pointer, -O2: one read, then a jump to self
    lbu a5,5(a4)
.L3: j .L3
# volatile pointer: a fresh read every iteration
.L6: lbu a5,0(a4)
    andi a5,a5,32
    beq a5,zero,.L6
Blueprint-style diagram comparing the two compiled loops: the plain pointer reads the UART register once and spins on the cached byte, while the volatile pointer re-reads the register every iteration
Two loops, one word apart. The plain pointer's loop reads the register once and spins on the cached byte; the volatile loop re-reads 0x10000005 every iteration until bit 5 arrives.
Diagram: hand-built by Chris Dillinger for this post

volatile is a contract with the compiler, not the hardware. Every access through a volatile lvalue is an observable side effect the compiler must evaluate strictly, in order, which is exactly why volatile objects model memory-mapped I/O ports. [2] The rule at the bench: every memory-mapped register is volatile, or the optimized build turns a wait loop into a hang. The nasty part is where the bug hides. At -O0 the compiler hoists nothing, so the plain loop happens to work, and the failure appears only in the -O2 build you actually ship. One word in the type, at the declaration. That is all volatile buys you, and it is everything.

Sources

  1. 01
    uart.c: the polled UART driverChris Dillinger

    The transmit wait loop polls the line-status register through a volatile unsigned char pointer. The firsthand -O2 listings from riscv64-unknown-elf-gcc 13.2.0 show the load hoisted out of the loop without volatile and inside the loop with it; the -O0 listing shows the plain loop re-reading, which is where the bug hides.

  2. 02
    volatile type qualifiercppreference.com

    Every access through a volatile lvalue is an observable side effect evaluated strictly per the abstract machine, so the compiler cannot optimize it out; volatile objects model memory-mapped I/O ports.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app