Skip to content

Lesson 12.2 — Pipelines, plugins and extension points, and the phase-ordering problem

Techniques: pass plugins and extension points (LLVM's PassPlugin API, PassBuilder parsing and extension-point callbacks, the course registry); how -O1/-O2/-O3/-Os pipelines are assembled (buildPerModuleDefaultPipeline, simplification vs optimization pipelines, optsize/minsize); the phase-ordering problem (non-commuting passes, fixed-point pipelines, search) · Pebble implements: pebble-strength joins the course -O1 pipeline (E2) · Drill: none (see §9) · Prerequisites: Lesson 12.1 · Time: 3–4 hours

clang -O2 does not run "the optimizer"; it runs a pipeline: 119 top-level entries in LLVM 23, many of them adaptors holding dozens more passes, several passes appearing four or five times. Who decides that list, how can you add your own pass to it without recompiling LLVM, and why is there no single best order? This lesson answers the three questions: plugins and extension points, pipeline assembly, and phase ordering.

1. Problem and motivation

Pass plugins and extension points

A compiler ships a fixed set of passes, but users (researchers, language implementers, this course) need their own. Rebuilding LLVM for every experiment is not an option, so LLVM loads pass plugins: shared libraries exporting llvmGetPassPluginInfo, whose callback registers passes with a PassBuilder [LLVM-WNPM]. Registered passes are reachable by name in textual pipelines (opt -passes=pebble-strength). To join the default pipelines, a plugin registers callbacks at extension points: named holes in the pipeline templates (registerPeepholeEPCallback, registerOptimizerLastEPCallback, …) [LLVM-PB]. The legacy manager had the same idea (PassManagerBuilder::addExtension); GCC plugins insert passes relative to a named existing pass (register_callback(..., PLUGIN_PASS_MANAGER_SETUP, ...) with a register_pass_info) [GCC-Plugins].

Optimization pipelines

A pipeline for an optimization level is a design artifact: simplify first (inline, SROA, InstCombine, SimplifyCFG, GVN, loop canonicalization), then optimize for the machine (vectorize, unroll, lower). LLVM builds it in code, in PassBuilderPipelines.cpp [LLVM-PBP]; -print-pipeline-passes prints it as text you can re-run with -passes=. Size levels are no longer separate pipelines in LLVM 23: -Os/-Oz are the -O2 pipeline on functions carrying optsize/minsize attributes, which cost models consult.

Phase ordering

Passes do not commute: CSE before mem2reg sees loads and stores and finds nothing to merge; after mem2reg it removes a multiplication (§3). The phase-ordering problem asks for the sequence of passes that optimizes a metric for a given program. It has been attacked by exhaustive enumeration of distinct pipeline outcomes [KWTD06], genetic search [CSS99] and, in production compilers, by hand-tuned fixed pipelines that repeat cheap canonicalizing passes. Pebble meets it in Chapter 24, where you order your own passes.

2. Definitions and algorithms

Throughout the lesson, let \(\mathcal{P}\) be the set of programs (LLVM modules), \(\Sigma\) a finite set of passes, each a total function \(\mathcal{P} \to \mathcal{P}\) (a module pass, or a function, CGSCC or loop pass wrapped in its adaptors, Definition 12.1.9), and \(\mathrm{PM}\) the set of pass managers: finite sequences over \(\Sigma\), written \(\langle s_1, \dots, s_k \rangle\) or \(s_1 \cdots s_k\), with concatenation \(\cdot\) (Definition 12.1.1). Every numbered box restates the objects it uses.

Definition 12.2.1 (Pipeline text)

Let \(\mathrm{Names}\) be the set of pass names: non-empty runs of letters, digits and the characters -, _ and .. A pipeline text is a word of the grammar

\[ \begin{aligned} \mathit{pipeline} &\to \mathit{element} \;\{\; \texttt{,}\ \mathit{element} \;\} \\ \mathit{element} &\to \mathit{name} \;[\; \texttt{<}\ \mathit{params}\ \texttt{>} \;]\; [\; \texttt{(}\ \mathit{pipeline}\ \texttt{)} \;] \end{aligned} \]

where \(\mathit{name}\) derives exactly the elements of \(\mathrm{Names}\) and \(\mathit{params}\) derives any text with balanced < and >. An element with a parenthesized sub-pipeline is an adaptor element; every other element is a pass element.

In LLVM 23 the adaptor elements are function(...), cgscc(...), loop(...) and devirt<4>(...) (Definition 12.1.9 and 12.1.11) or a nested pass manager; the pass elements are passes, require<a>, invalidate<a>, and default<O2>-style aliases for whole pipelines (Definition 12.2.4).

Pass plugins and extension points

Definition 12.2.2 (Plugin, registration, extension point)

Let \(\mathrm{Names}\) be the set of pass names (Definition 12.2.1) and \(\mathrm{PM}\) the set of pass managers. A parsing callback is a partial function \(\kappa : \mathrm{Names} \times \mathrm{PM} \rightharpoonup \mathrm{PM}\) such that, whenever \(\kappa(x, \mathrm{pm})\) is defined for a name \(x\) and a pass manager \(\mathrm{pm}\), it equals \(\mathrm{pm} \cdot \langle s \rangle\) for one pass \(s\): the callback accepts \(x\) and appends its pass. A plugin is a shared object that, when loaded, registers a finite sequence of parsing callbacks \(\kappa_1, \dots, \kappa_r\) and of extension-point callbacks, in that order. Let \(\mathcal{E}\) be a finite set of extension points. A pipeline template over \(\mathcal{E}\) is a finite sequence \(T\) over \(\Sigma \cup \mathcal{E}\) in which each \(e \in \mathcal{E}\) occurs at most once. An extension-point callback for \(e \in \mathcal{E}\) is a function \(c : \mathrm{PM} \to \mathrm{PM}\) with \(c(\mathrm{pm}) = \mathrm{pm} \cdot \mathrm{pm}'\) for some \(\mathrm{pm}' \in \mathrm{PM}\) (it appends passes); let \(\mathcal{C}_e = \langle c_1, \dots, c_m \rangle\) be the callbacks registered for \(e\), in registration order. The instantiation of \(T\) is the pass manager \(T[e \mapsto c_m(\cdots c_1(\langle\rangle) \cdots) \text{ for every } e \in \mathcal{E}]\): each extension point is replaced by the passes its callbacks append to an empty pass manager.

In LLVM 23.1.2 a plugin exports PassPluginLibraryInfo llvmGetPassPluginInfo() with fields APIVersion (LLVM_PLUGIN_API_VERSION, 2 in LLVM 23), PluginName, PluginVersion and RegisterPassBuilderCallbacks(PassBuilder &) (llvm/include/llvm/Plugins/PassPlugin.h [LLVM-PassPlugin]); parsing callbacks are registered with registerPipelineParsingCallback, and extension-point callbacks with registerPipelineStartEPCallback, registerPeepholeEPCallback and the other register*EPCallback methods of PassBuilder.

Algorithm 12.2.3 (Resolving a pipeline element, PassBuilder::parsePassPipeline)

  • Input: a pipeline text \(t\) (Definition 12.2.1); a built-in pass registry \(\mathcal{R}\), a partial function from pass names and nesting levels to passes; the registered parsing callbacks \(\kappa_1, \dots, \kappa_r\) in registration order (Definition 12.2.2); the set \(\mathrm{Levels} = \{\text{module}, \text{cgscc}, \text{function}, \text{loop}\}\) of nesting levels, ordered module \(>\) cgscc \(>\) function \(>\) loop.
  • Output: a pass manager \(\mathrm{pm} \in \mathrm{PM}\), or an error "unknown pass name".
  • State: the pass manager \(\mathrm{pm}\) under construction at the current level; adding a pass \(s\) sets \(\mathrm{pm} \triangleq \mathrm{pm} \cdot \langle s \rangle\).
  • Precondition: \(t\) is a pipeline text (Definition 12.2.1).
  • Postcondition: each element of \(t\) became exactly one pass or adaptor of the level at which it appears.
  • Invariant: the nesting level of every element is determined by its enclosing adaptor elements; a pass element whose level is lower than the current one is wrapped in the chain of adaptors from the current level down to its own.
function Parse(t, level):
    elements ← ParseList(t)                  # recursive descent on Definition 12.2.1
    pm ← ⟨⟩                                   # a new pass manager at level
    for E in elements:
        if E is an adaptor element and E.name names an adaptor A:
            add A(Parse(E.sub, InnerLevel(A))) to pm
        else if (E.name, level) ∈ dom(ℛ):
            add ℛ(E.name, level)(E.params) to pm
        else if some κ_j (first in registration order) accepts (E.name, pm):
            pm ← κ_j(E.name, pm)              # κ_j appended its pass
        else if E.name is a pass of a lower level L' < level:
            add AdaptorChain(level, L')(Parse(E, L')) to pm
        else:
            error "unknown pass name 'E.name'"
    return pm

In LLVM 23.1.2 the registry \(\mathcal{R}\) is PassRegistry.def.

The course registry is a set of parsing callbacks

pebble/lib/Passes/Registry.cpp gives each PEBBLE_FUNCTION_PASS("pebble-strength", StrengthPass) one parsing callback that accepts the name (and pebble-strength<params>) and appends the pass. A skeleton build has no such callback, so Algorithm 12.2.3 reaches the last line: that is the "unknown pass name 'pebble-strength'" your tests show before you write the pass.

Optimization pipelines

Definition 12.2.4 (Pipeline template, optimization level)

An optimization level is an element \(\ell \in \{\mathrm{O0}, \mathrm{O1}, \mathrm{O2}, \mathrm{O3}\}\); the size variants are the levels \(\mathrm{O2}\) and \(\mathrm{O3}\) with size attributes on functions. A level template is a function \(T\) from optimization levels to pipeline templates (Definition 12.2.2); write \(T_\ell\) for \(T(\ell)\). The default pipeline for \(\ell\) is the instantiation of \(T_\ell\) with the registered extension-point callbacks. For \(\ell \neq \mathrm{O0}\), LLVM's level template has the shape

\[ T_\ell = \mathrm{prologue} \cdot \mathrm{PipelineStart} \cdot \mathrm{Simplify}_\ell \cdot \mathrm{Optimize}_\ell \cdot \mathrm{epilogue}, \]

where \(\mathrm{prologue}\) and \(\mathrm{epilogue}\) are fixed pass managers, \(\mathrm{PipelineStart}\) is an extension point, and \(\mathrm{Simplify}_\ell\) and \(\mathrm{Optimize}_\ell\) are pipeline templates, the simplification and optimization pipelines of \(\ell\), each containing further extension points.

In LLVM 23.1.2, \(\mathrm{Simplify}_\ell\) is buildModuleSimplificationPipeline (IPSCCP, globalopt, the CGSCC inliner pipeline with function simplification inside) and \(\mathrm{Optimize}_\ell\) is buildModuleOptimizationPipeline (loop rotation and unrolling, vectorizers, final cleanup); the whole is buildPerModuleDefaultPipeline [LLVM-PBP].

Algorithm 12.2.5 (Per-module default pipeline, buildPerModuleDefaultPipeline)

  • Input: an optimization level \(\ell \ne \mathrm{O0}\) (Definition 12.2.4); an LTO phase \(\mathrm{phase} \in \{\text{none}, \text{prelink}, \text{postlink}\}\); the registered extension-point callbacks \(\mathcal{C}_e\) for every extension point \(e\) (Definition 12.2.2).
  • Output: the default pipeline for \(\ell\): a module pass manager \(\mathrm{MPM} \in \mathrm{PM}\).
  • State: the module pass manager \(\mathrm{MPM}\) under construction, extended by concatenation; write \(\mathrm{Callbacks}(e, \ell)\) for the pass manager \(c_m(\cdots c_1(\langle\rangle)\cdots)\) built from \(\mathcal{C}_e = \langle c_1, \dots, c_m \rangle\).
  • Precondition: \(\ell \ne \mathrm{O0}\) (O0 has its own builder, which runs only required passes such as always-inline).
  • Postcondition: \(\mathrm{MPM}\) is the instantiation of \(T_\ell\) (Definition 12.2.4).
  • Invariant: every element added to \(\mathrm{MPM}\) is a pass or adaptor valid at module level.
function PerModuleDefault(ℓ, phase):
    MPM ← ⟨ memprof-remove-attributes (if no LTO), annotation2metadata, forceattrs ⟩
    MPM ← MPM · Callbacks(PipelineStart, ℓ)
    MPM ← MPM · ModuleSimplification(ℓ, phase)   # inferattrs, early function cleanup (EarlySimplification EP),
                                                 # ipsccp, globalopt, cgscc(devirt<4>(inline, function-attrs,
                                                 #   function(FunctionSimplification(ℓ)))), ...
    MPM ← MPM · ModuleOptimization(ℓ, phase)     # OptimizerEarly EP, loop rotate/unroll, vectorizers
                                                 # (VectorizerStart/End EPs), OptimizerLast EP, cleanup
    MPM ← MPM · ⟨ annotation-remarks ⟩ · (LTO pre-link additions)
    return MPM

function FunctionSimplification(ℓ):
    if ℓ = O1: return O1FunctionSimplification()    # no GVN, no jump threading, fewer loop passes
    return ⟨ sroa, early-cse<memssa>, speculative-execution, jump-threading, correlated-propagation,
             simplifycfg, instcombine, Peephole EP, ..., loop(licm, loop-rotate, simple-loop-unswitch, ...),
             ..., sroa, vector-combine, mldst-motion, gvn, sccp, bdce, instcombine, Peephole EP,
             jump-threading, correlated-propagation, adce, memcpyopt, dse, loop(licm), coro-elide,
             simplifycfg, instcombine, Peephole EP ⟩  # abridged; print it with -print-pipeline-passes

Phase ordering

Definition 12.2.6 (Phase-ordering problem)

Let \(\mathcal{P}\) be the set of programs, \(\Sigma\) a finite set of passes, each a total function \(\mathcal{P} \to \mathcal{P}\), and \(m : \mathcal{P} \to \mathbb{N}\) a cost. For a word \(w = s_1 \cdots s_\ell \in \Sigma^{*}\) and a program \(P \in \mathcal{P}\) write \(w(P) \triangleq s_\ell(\cdots s_1(P) \cdots)\), with \(\varepsilon(P) = P\) for the empty word. For an integer \(L \ge 0\) let \(\Sigma^{\le L}\) be the set of words of length at most \(L\). The phase-ordering problem for \(P\), \(m\) and the length bound \(L\) asks for a word \(w \in \Sigma^{\le L}\) that minimizes \(m(w(P))\). Two passes \(s, t \in \Sigma\) commute on \(P\) if \(t(s(P)) = s(t(P))\).

The cost \(m\) is, in practice, the instruction count, the code size, or the run time on a fixed input.

Algorithm 12.2.7 (Exhaustive phase-order search over distinct outcomes, after Kulkarni et al.)

  • Input: a program \(P \in \mathcal{P}\), a finite set \(\Sigma\) of passes, a cost \(m : \mathcal{P} \to \mathbb{N}\) and a length bound \(L \ge 0\) (Definition 12.2.6).
  • Output: a word \(w \in \Sigma^{\le L}\) minimizing \(m(w(P))\).
  • State: a partial map \(\mathrm{seen} : \mathcal{P} \rightharpoonup \Sigma^{*}\) from programs reached so far to a word producing them; the sequence \(\mathrm{frontier}\) of programs first reached in the previous round; the best program \(\mathrm{best} \in \mathrm{dom}(\mathrm{seen})\) so far.
  • Precondition: every pass is a deterministic function; programs can be compared for equality.
  • Postcondition: the returned \(w\) is optimal among \(\Sigma^{\le L}\) (Theorem 12.2.13).
  • Invariant: after round \(d \in \{0, \dots, L\}\), \(\mathrm{dom}(\mathrm{seen})\) is the set of programs \(v(P)\) with \(v \in \Sigma^{\le d}\), each mapped to a shortest word producing it, and \(\mathrm{frontier}\) holds the programs first reached in round \(d\).
function Search(P, Σ, m, L):
    seen ← { P ↦ ε }; frontier ← [P]; best ← P
    for d ← 1 to L:
        next ← []
        for Q in frontier:
            for s in Σ:
                R ← s(Q)
                if R ∉ dom(seen):              # a pass that changed nothing, or reached a known program, is pruned
                    seen[R] ← seen[Q] · s
                    next ← next · [R]
                    if m(R) < m(best): best ← R
        frontier ← next
    return seen[best]

Equality of programs is decided in practice by hashing the printed IR.

Definition 12.2.8 (Fixed-point pipeline)

Let \(\mathcal{P}\) be the set of programs and \(\Sigma\) a finite set of passes, each \(s \in \Sigma\) a total function \(\mathcal{P} \to \mathcal{P}\) together with a change flag \(\chi_s : \mathcal{P} \to \{\mathsf{true}, \mathsf{false}\}\) such that \(s(P) \ne P\) implies \(\chi_s(P) = \mathsf{true}\) for every \(P \in \mathcal{P}\) (a pass that changes its input reports a change; it may also report one without changing it). Let \(w = s_1 \cdots s_\ell \in \Sigma^{*}\) be a word. A round of \(w\) on a program \(P\) computes \(w(P)\) (Definition 12.2.6) and reports a change if \(\chi_{s_i}(P_{i-1}) = \mathsf{true}\) for some \(i \in \{1, \dots, \ell\}\), where \(P_0 = P\) and \(P_i = s_i(P_{i-1})\). The fixed-point pipeline of \(w\) on \(P\) runs rounds \(P^{(0)} = P\), \(P^{(j+1)} = w(P^{(j)})\) for \(j \ge 0\) as long as round \(j+1\) reports a change. It stabilizes on \(P\) if some round \(j + 1 \ge 1\) reports no change; then \(w^{\omega}(P) \triangleq P^{(j)}\) for the least such \(j\), and the pipeline ran \(j + 1\) rounds.

In LLVM 23 a pass reports a change by returning a report other than \(\mathrm{all}\) (Definition 12.1.3). GCC's pass_fre with may_iterate, LLVM's devirt<4> and InstCombine's internal worklist are bounded instances of fixed-point pipelines.

3. Worked example

The running example is the function of Chapter 12's pass exercises:

int f(int a, int b) {
  int x = a * b;
  int y = a * b;
  return (x + y) * 8;
}

compiled with clang -O0 -Xclang -disable-O0-optnone, so every local lives in an alloca. Take \(\Sigma = \{\texttt{mem2reg}, \texttt{early-cse}, \texttt{pebble-strength}\}\) and \(m\) = instructions in the function body. All \(3! = 6\) orders of the three passes, run with opt 23.1.2 and the course plugin:

order instructions why
mem2reg, early-cse, pebble-strength 4 CSE sees mul %a, %b twice on SSA values and merges them
mem2reg, pebble-strength, early-cse 4 same; strength reduction is independent of the other two
pebble-strength, mem2reg, early-cse 4 same
early-cse, mem2reg, pebble-strength 5 CSE ran on loads and stores: the two muls read different loads
early-cse, pebble-strength, mem2reg 5 same
pebble-strength, early-cse, mem2reg 5 same

So early-cse and mem2reg do not commute on \(P\) (Definition 12.2.6), while pebble-strength commutes with both: it only needs the constant operand, which is there before and after mem2reg. Algorithm 12.2.7 with \(L = 3\) finds a 4-instruction result after exploring the distinct programs; the ordering constraint "promote memory before CSE" is exactly what LLVM's simplification pipeline hard-codes (sroa precedes early-cse in FunctionSimplification).

Two passes that do not commute

Reproduce (clang 23.1.2, opt 23.1.2):

printf 'int f(int a, int b) {\n  int x = a * b;\n  int y = a * b;\n  return x + y;\n}\n' > po.c
clang-23 -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -S -emit-llvm po.c -o po.ll
for p in 'early-cse,mem2reg' 'mem2reg,early-cse'; do
  echo "== $p"; opt -passes="$p" -S po.ll | sed -n '/^define/,/^}/p'
done

Output:

== early-cse,mem2reg
define dso_local i32 @f(i32 noundef %a, i32 noundef %b) #0 {
entry:
  %mul = mul nsw i32 %a, %b
  %mul1 = mul nsw i32 %a, %b
  %add = add nsw i32 %mul, %mul1
  ret i32 %add
}
== mem2reg,early-cse
define dso_local i32 @f(i32 noundef %a, i32 noundef %b) #0 {
entry:
  %mul = mul nsw i32 %a, %b
  %add = add nsw i32 %mul, %mul
  ret i32 %add
}

What to notice: the same two passes, two results. Before mem2reg the multiplications read %0 = load %a.addr and %2 = load %a.addr — different SSA values, so EarlyCSE cannot prove them equal; after mem2reg both read %a directly (Proposition 12.2.10 below).

4. Invariants and correctness

Proposition 12.2.9 (Pipeline text is LL(1))

The grammar of Definition 12.2.1 is LL(1), and a pipeline text of \(n\) characters is recognized in \(O(n)\) time.

Proof

After a name, the next token decides the production: < starts params, ( starts a sub-pipeline, , or ) or the end closes the element — pairwise distinct, so the FIRST sets of the alternatives are disjoint, and name never derives the empty string. The params part is a balanced bracket word scanned with a counter. Each character is examined a constant number of times, which gives linear time. (Name resolution, Algorithm 12.2.3, then costs one lookup per element plus the callbacks tried.) ∎

Proposition 12.2.10 (Non-commutation)

Let \(\mathcal{P}\) be the set of LLVM modules and let \(\mathrm{cse}, \mathrm{m2r} : \mathcal{P} \to \mathcal{P}\) be the passes early-cse and mem2reg of LLVM 23.1.2. There is a program \(P \in \mathcal{P}\) with \(\mathrm{m2r}(\mathrm{cse}(P)) \ne \mathrm{cse}(\mathrm{m2r}(P))\): the two passes do not commute on \(P\) (Definition 12.2.6).

Proof

The program of the real-world box above is a witness: the two results differ in their instruction counts (4 and 3 non-label lines in the function body). ∎

Theorem 12.2.11 (Soundness composes)

Let \(\mathcal{P}\) be the set of programs and \(\Sigma\) a finite set of passes (Definition 12.2.6). A pass \(s \in \Sigma\) is sound if for every program \(P \in \mathcal{P}\), \(s(P)\) refines \(P\), written \(s(P) \sqsupseteq P\) (every behavior of \(s(P)\) is a behavior of \(P\); Definition 12.8.2). If every \(s \in \Sigma\) is sound, then for every word \(w \in \Sigma^{*}\) and every \(P\), \(w(P) \sqsupseteq P\); and for every \(w\) whose fixed-point pipeline stabilizes on \(P\) (Definition 12.2.8), \(w^{\omega}(P) \sqsupseteq P\).

Proof

Refinement \(\sqsupseteq\) is reflexive and transitive (Definition 12.8.2): if the behaviors of \(Q\) are included in those of \(P\) and the behaviors of \(R\) in those of \(Q\), then those of \(R\) are included in those of \(P\). By induction on \(\lvert w \rvert\): the empty word is the identity, and \(P \sqsupseteq P\) by reflexivity; for \(w = v s\) with \(s \in \Sigma\), \(w(P) = s(v(P))\) refines \(v(P)\) by soundness of \(s\), and \(v(P)\) refines \(P\) by the induction hypothesis; transitivity concludes. If the fixed-point pipeline stabilizes after \(j + 1\) rounds, then \(w^{\omega}(P) = w^{j}(P)\) is the result of the finite word \(w^{j} \in \Sigma^{*}\). ∎

Theorem 12.2.12 (Termination of fixed-point pipelines)

Let \(\Sigma\) be a finite set of passes with change flags (Definition 12.2.8) and \(w \in \Sigma^{*}\) a word. Suppose that the change flags are exact, \(\chi_s(P) = \mathsf{true}\) iff \(s(P) \ne P\) for every \(s \in \Sigma\) and \(P \in \mathcal{P}\), and that there is a measure \(\mu : \mathcal{P} \to \mathbb{N}\) such that \(s(P) \ne P\) implies \(\mu(s(P)) < \mu(P)\) for every \(s \in \Sigma\) and \(P \in \mathcal{P}\). Then the fixed-point pipeline of \(w\) stabilizes on every \(P\) after at most \(\mu(P) + 1\) rounds. Without such a measure it can run forever.

Proof

Every round that reports a change contains, by exactness of the flags, at least one pass application \(s(P_{i-1}) \ne P_{i-1}\), which strictly decreases \(\mu\); the other applications leave the program, hence \(\mu\), unchanged. So for every \(r \ge 0\), after \(r\) rounds that reported a change, the current program \(P'\) satisfies \(\mu(P') \le \mu(P) - r\), and since \(\mu \ge 0\) at most \(\mu(P)\) rounds report a change; the next round reports none and the pipeline stabilizes. For the second claim, let \(s\) rewrite every x * 2 into x + x and \(t\) rewrite every x + x into x * 2, and let \(P\) contain x * 2. In every round of \(w = s t\), \(s\) changes the program and \(t\) changes it back, so every round reports a change and the pipeline never stabilizes, although the program after each round is \(P\) again. This is why production pipelines insist on one canonical form per operation (InstCombine turns mul x, 2 into shl x, 1 and never the reverse, a change that lowers a suitable measure) and still bound their iterations (devirt<4>, instcombine<max-iterations=1>). ∎

Theorem 12.2.13 (Optimality of Algorithm 12.2.7)

Let \(P \in \mathcal{P}\) be a program, \(\Sigma\) a finite set of deterministic passes, \(m : \mathcal{P} \to \mathbb{N}\) a cost and \(L \ge 0\) a length bound (Definition 12.2.6). Algorithm 12.2.7 returns a word \(w \in \Sigma^{\le L}\) with \(m(w(P)) = \min_{v \in \Sigma^{\le L}} m(v(P))\).

Proof

We prove the invariant of Algorithm 12.2.7 by induction on the round \(d\): after round \(d\), \(\mathrm{dom}(\mathrm{seen})\) contains every program \(v(P)\) with \(\lvert v \rvert \le d\), and \(\mathrm{frontier}\) holds the programs first reached in round \(d\). For \(d = 0\) both hold by the initialization. Round \(d+1\) applies every \(s \in \Sigma\) to every program in \(\mathrm{frontier}\). A program reached at depth \(d+1\) is \(s(Q)\) for some \(Q = v(P)\) with \(\lvert v \rvert \le d\); if \(Q\) was first reached in round \(d\) it is in \(\mathrm{frontier}\) and \(s(Q)\) is generated now, and if \(Q\) was first reached in an earlier round \(d' < d\), \(s(Q)\) was already generated in round \(d' + 1 \le d\), because passes are deterministic functions and \(s(Q)\) depends only on \(Q\). So after round \(L\) every program \(v(P)\) with \(v \in \Sigma^{\le L}\) is in \(\mathrm{dom}(\mathrm{seen})\), \(\mathrm{best}\) minimizes \(m\) over all of them (it is updated at every insertion), and \(\mathrm{seen}[\mathrm{best}]\) is a word of length at most \(L\) producing it. ∎

The course -O1 pipeline is not LLVM's -O1

pebblec -O1 runs only the steps chapters register (PEBBLE_COURSE_PIPELINE_STEP); with the reference solution of this chapter that is function(pebble-strength). -O1 for clang/opt is LLVM's 100-entry default<O1>. Only while no step is registered (a skeleton build before your E2) does pebblec -O1 fall back to default<O1> (pebble/lib/Driver/CoursePipeline.cpp). So the moment your E2 registers its step, pebblec -O1 output becomes less optimized — no mem2reg, no InstCombine, only your strength reduction — and later chapters add their passes one by one. That is expected, not a bug; use pebblec -O2 (LLVM's default<O2>) when you want LLVM's optimizer. Chapter 24 is where you design the full course pipeline.

5. Complexity

Let \(n\) be the length of a pipeline text, \(\lvert \Sigma \rvert\) the number of passes, \(L\) the length bound, and \(D_d\) the number of distinct programs first reached at depth \(d\).

Technique Time (worst) Time (typical) Space Notes
Plugin loading and parsing (Alg. 12.2.3) \(O(n + e \cdot r)\) for \(e\) elements and \(r\) callbacks microseconds the pass objects callbacks are tried in order until one accepts
Default pipeline (Alg. 12.2.5) built once; running it is the cost of its passes O2 has 119 top-level entries (§7) — size levels reuse O2 with attributes
Phase-order search (Alg. 12.2.7) \(\lvert \Sigma \rvert \cdot \sum_{d < L} D_d\) pass runs, \(\le \sum_{d \le L} \lvert \Sigma \rvert^{d}\) far below the bound: many passes are no-ops on a given program, and many orders reach the same program (§3: 6 orders, 2 outcomes) all distinct programs Kulkarni et al. make exhaustive enumeration feasible for whole functions of a retargetable compiler by exactly this deduplication [KWTD06]

Justification. Proposition 12.2.9 gives the parse. For the search, each distinct program at depth \(d < L\) is expanded once with \(\lvert \Sigma \rvert\) passes; without deduplication, the number of words is \(\sum_{d \le L} \lvert \Sigma \rvert^{d} = \Theta(\lvert \Sigma \rvert^{L})\). Pathological family: if every pass changes every program into a new one (for instance, each \(s_i\) appends a distinct no-op instruction), no two words give the same program and \(D_d = \lvert \Sigma \rvert^{d}\): the search degenerates to full enumeration, \(\Theta(\lvert \Sigma \rvert^{L})\) pass runs. For LLVM's O2 with more than 60 distinct passes and \(L\) in the hundreds, exhaustive search is out of the question — hence hand-designed templates.

6. Variants and refinements

Pass plugins and extension points

  • Static linking of "plugins" (LLVM_${name}_LINK_INTO_TOOLS, and the course's pebble_passes linked into pebblec): the same registration code without dlopen [LLVM-WNPM]. Trade-off: rebuild to change.
  • -passes-ep-* options of opt insert textual pipelines at extension points without a plugin (§7). Trade-off: they are parsed before plugins are loaded, so they only accept built-in passes.
  • clang's -fpass-plugin= loads a plugin into the compiler driver's pipeline. Trade-off: only EP callbacks are reachable; there is no textual pipeline in clang.

Optimization pipelines

  • LTO pipelines (buildThinLTOPreLinkDefaultPipeline, buildLTODefaultPipeline): split the same work between compile and link time [LLVM-PBP]. Trade-off: summaries and a second pipeline.
  • Size attributes instead of Os pipelines (LLVM 23): one pipeline, cost models that read optsize. Trade-off: opt -passes='default<Os>' is an error (§7).
  • Profile-guided pipelines (PGO PGOOptions): the same template with instrumentation or profile-use passes inserted (Lesson 12.3).

Phase ordering

  • Genetic search over pass sequences [CSS99]: finds better sequences than the fixed one for code size. Trade-off: thousands of compilations per program.
  • Exhaustive distinct-instance search (Algorithm 12.2.7) [KWTD06]. Trade-off: exact but only for small \(\Sigma\) and small functions.
  • Fixed-point grouping and canonical forms (Theorem 12.2.12): repeat cheap passes, bound the repetitions. Trade-off: compile time, and some orders still lose.

7. In real compilers

Pass plugins and extension points

llvm/include/llvm/Plugins/PassPlugin.h — PassPluginLibraryInfo, LLVM_PLUGIN_API_VERSION [LLVM-PassPlugin]; llvm/include/llvm/Passes/PassBuilder.h — registerPipelineParsingCallback, registerPeepholeEPCallback, registerOptimizerLastEPCallback [LLVM-PB]; llvm/tools/opt/NewPMDriver.cpp — registerEPCallbacks is called before the plugins' registerPassBuilderCallbacks [LLVM-NewPMDriver]; the course registry pebble/lib/Passes/Registry.cpp (registerAll) and plugin entry pebble/lib/Passes/Plugin.cpp.

A plugin's registrations, and an extension point used four times

Reproduce (opt 23.1.2, the course plugin built by ./course test 12 --solution; $SOL is that build: SOL=$PWD/build/ci-solutions-linux from the repository root, ci-solutions-macos on macOS):

echo 'define void @f() { ret void }' > e.ll
opt -load-pass-plugin=$SOL/lib/PebblePasses.so -passes='print<pebble-passes>' \
    -disable-output e.ll 2>&1 | grep -E 'registry|ch12'
for o in O1 O2 O3; do
  echo "$o: $(opt -passes="default<$o>" -passes-ep-peephole='instnamer' -print-pipeline-passes \
               -disable-output e.ll | tr ',' '\n' | grep -c instnamer)"
done
opt -load-pass-plugin=$SOL/lib/PebblePasses.so -passes='default<O2>' \
    -passes-ep-peephole='pebble-strength' -print-pipeline-passes -disable-output e.ll 2>&1 | head -c 120; echo

Output:

Pebble pass registry:
  ch12 module pass pebble-bbcount
  ch12 function analysis pebble-stats
  ch12 function pass print<pebble-stats>
  ch12 function pass pebble-strength
O1: 4
O2: 4
O3: 4
Could not parse -passes-ep-peephole pipeline: unknown function pass 'pebble-strength' in pipeline 'pebble-strength'... I

What to notice: the plugin's four Chapter 12 registrations are parsing callbacks (Definition 12.2.2; other chapters' lines are filtered out). The Peephole extension point is invoked after four of the eight InstCombine runs of the O-pipelines (next box) — a callback there runs four times per function. opt's own -passes-ep-* flags are parsed in registerEPCallbacks before the plugin registers its names, so they cannot name plugin passes; a plugin that wants to join the O-pipelines must call registerPeepholeEPCallback itself.

Optimization pipelines

llvm/lib/Passes/PassBuilderPipelines.cpp — buildPerModuleDefaultPipeline, buildModuleSimplificationPipeline, buildFunctionSimplificationPipeline, buildO1FunctionSimplificationPipeline, buildModuleOptimizationPipeline, invokePeepholeEPCallbacks [LLVM-PBP]; llvm/lib/Passes/PassBuilder.cpp — the default<Os> rejection (LLVM 23.1.2). GCC: gcc/passes.def for the order and default_options_table in gcc/opts.cc for which -f flags each -O level enables (gcc-15) [GCC-Opts].

How big -O1, -O2 and -O3 are, and what -Os became

Reproduce (clang 23.1.2, opt 23.1.2):

echo 'define i32 @f(i32 %x) { ret i32 %x }' > t.ll
for o in O1 O2 O3; do
  opt -passes="default<$o>" -print-pipeline-passes -disable-output t.ll > pipe.$o.txt
  echo "$o: $(tr ',' '\n' < pipe.$o.txt | wc -l) elements"
done
norm() { tr ',' '\n' < "$1" | sed -E 's/<[^>]*>//g; s/^.*\(//; s/[()]//g' | sort -u; }
norm pipe.O1.txt > n1; norm pipe.O2.txt > n2; norm pipe.O3.txt > n3
echo "in O2, not O1: $(comm -13 n1 n2 | tr '\n' ' ')"
echo "in O3, not O2: $(comm -13 n2 n3 | tr '\n' ' ')"
opt -passes='default<Os>' -disable-output t.ll 2>&1
printf 'int f(int x){return x*8;}\n' > os.c
clang-23 -Os -S -emit-llvm -o - os.c | grep -o 'attributes #0 = { [a-z ]*optsize'

Output:

O1: 100 elements
O2: 119 elements
O3: 122 elements
in O2, not O1: aggressive-instcombine constraint-elimination correlated-propagation dfa-jump-threading dse extra-simple-loop-unswitch-passes gvn jump-table-to-switch jump-threading mldst-motion move-auto-init openmp-opt-cgscc slp-vectorizer speculative-execution 
in O3, not O2: argpromotion callsite-splitting chr 
LLVM ERROR: The optimization level "Os" is no longer supported. Use O2 in conjunction with the optsize attribute instead.
attributes #0 = { mustprogress nofree norecurse nosync nounwind optsize

What to notice: "elements" are comma-separated entries of the textual pipeline (nested ones included, so the count is a size measure, not a pass count). -O2 adds GVN, jump threading, DSE and the SLP vectorizer to -O1's set; -O3 adds only three passes (and more aggressive options such as unrolling thresholds, invisible here). -Os is -O2 plus the optsize attribute on every function (Definition 12.2.4).

Phase ordering

LLVM: the order is fixed in PassBuilderPipelines.cpp, with repetitions (eight InstCombine runs at -O2) and bounded fixed points (devirt<4>, instcombine<max-iterations=1>); GCC: gcc/passes.def repeats pass_ccp, pass_fre and pass_forwprop several times, with may_iterate parameters on FRE (gcc-15).

The same pass, many times, in LLVM's -O2

Reproduce (opt 23.1.2):

echo 'define i32 @f(i32 %x) { ret i32 %x }' > t.ll
opt -passes='default<O2>' -print-pipeline-passes -disable-output t.ll \
  | tr ',' '\n' | sed -E 's/<[^>]*>//g; s/^.*\(//; s/[()]//g' \
  | sort | uniq -c | sort -rn | head -6

Output:

      8 simplifycfg
      8 instcombine
      4 sroa
      4 licm
      3 require
      2 vector-combine

What to notice: a fixed template answers the phase-ordering problem by repeating the cheap, canonicalizing passes (SimplifyCFG, InstCombine) between the expensive ones, so that each expensive pass sees canonical input whichever pass ran before it — the practical form of Theorem 12.2.12.

8. Comparison

Technique Power / precision Speed Output / error quality Implementation effort Typical use
Pass plugins and extension points Any pass, at named points of the default pipelines or by name in textual pipelines dlopen + one callback per name; no LLVM rebuild "unknown pass name" if unregistered; print<pebble-passes> lists registrations Low: one registration line per pass (course registry) Research passes, out-of-tree tools (Polly, Enzyme, the course plugin)
Optimization pipelines (-O1/-O2/-O3/-Os) Hand-designed, balanced for code in general; not tuned per program O1: 100 elements; O2: 119; O3: 122 (§7) -print-pipeline-passes shows the exact pipeline, re-runnable with -passes= High, but done once by compiler developers Every clang -O<n> compilation
Phase ordering (search) Optimal for the searched program, metric and length bound (Theorem 12.2.13) Exponential in \(L\) in the worst case; pruned by distinct outcomes Per-program best sequence Medium (a driver around the compiler) Research, auto-tuning, embedded code size; informs fixed pipelines

Choose plugins and extension points when you add passes to LLVM without forking it — use EP callbacks to join the default pipelines, names to test in isolation. Choose the default pipelines when you compile code; change them only with measurements. Choose phase-order search when a hot function or a size budget justifies thousands of compilations, or to learn which orderings matter before fixing a pipeline.

9. Assessment

  • Quiz: pipe-ep-count (number), pipe-o2-not-o1 (set), pipe-os, plugin-ep-parse, phase-commute (number), phase-search-bound (number).
  • Drill: none — pipelines are read from -print-pipeline-passes, not computed by hand, and the phase-ordering search is exercised by the "Try it" below. The pass-manager drill pm-invalidation covers what a pipeline does to analyses.
  • Try it: re-run the §3 table with your own pebble-strength (build/<preset>/lib/PebblePasses.so) and add instcombine as a fourth pass: how many of the 24 orders reach the minimum?
  • Flashcards: tags plugins, pipelines, phase-ordering.
  • Exercise: E2 registers pebble-strength as the first course -O1 step (PEBBLE_COURSE_PIPELINE_STEP(1210, ...)).
  • Find where LLVM does it: in llvm/lib/Passes/PassBuilderPipelines.cpp, which function builds the function simplification pipeline used at -O1 only?

References

See the chapter references.