Master the fundamental concepts of compiler optimization techniques through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksPlacing cold error-handling functions in separate translation units or marking them __attribute__((cold)) keeps icache filled with the fast path. LLVM outlines cold blocks from hot functions during optimization.
hot encourages layout near callers; cold moves unlikely blocks away. -freorder-blocks-and-partition is a GCC knob for similar effect.
cLoading…
unlikely() on branches into cold helpersKeep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will mark functions hot/cold and inspect section placement or size changes. This exercise requires showing icache-friendly layout for the common case.
__attribute__((hot)) and __attribute__((cold)) do more than tune optimisation. GCC puts hot functions in .text.hot and cold ones in .text.unlikely, and the linker groups those sections. The code that actually runs therefore ends up packed together, touching fewer i-cache lines and fewer pages (iTLB entries). Model the linker's placement and measure the footprint of the executed code with and without the attributes.
cLoading…
.text in declaration order. With attributes, place .text.hot (hot functions), then .text (the rest), then .text.unlikely (cold functions), each in declaration order.[addr, addr + size).note: NAME is marked cold but ran N times and note: NAME is marked hot but never ran.func NAME: rejected (duplicate, size < 1, or more than 32), func NAME: unknown attribute X, and run: unknown function NAME.cLoading…
Each layout line lists every function by address.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover small pages, where the attributes save iTLB entries, a cold function that actually runs, a hot function that never runs, functions that straddle page boundaries, 128-byte lines, and invalid declarations.