Master the fundamental concepts of compiler optimization techniques through this focused micro-challenge.
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 worksProfile-Guided Optimization (PGO) runs a instrumented binary on representative input, records edge frequencies, then rebuilds with fprofile-use so the compiler lays out hot branches and inlines the right callees. Chrome, Firefox, and LLVM itself ship PGO-trained builds.
Phase 1: -fprofile-generate run workload. Phase 2: -fprofile-use recompile using .profraw merged to .profdata.
bashLoading…
Keep 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 generate and consume a profile to build a PGO-optimized binary. This exercise requires comparing branch layout or speed against a non-PGO build.
Profile-guided optimisation feeds real branch counts back into the compiler. One of its biggest wins is basic-block layout: put each block right after its most likely predecessor, so the hot path falls through instead of jumping, and push never-executed blocks (error handling) out of the way. Implement the classic Pettis. Hansen greedy chain-merging layout, and compare it against source order.
cLoading…
A -> B only if A is the tail of its chain, B is the head of its chain, and they are different chains. The chain of B is appended to the chain of A.block NAME: rejected (duplicate, size < 1, or more than 32 blocks), edge A B: rejected, edge A B: a block has at most two successors, and entry NAME: no such block.cLoading…
The percentage is rounded half up to 1 decimal with integer arithmetic.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover an if/else diamond with a biased branch, a different entry block, a chain that cannot grow because its tail already has a successor, ties between equally hot chains, a block with three successors, and unknown blocks.