30  Lowering

Lowering replaces a high-level representation with a lower-level one that makes different implementation details explicit. It is not synonymous with making IR uglier. A good lowering preserves exactly the information the next stage needs while deliberately discarding abstractions whose optimization opportunities have been exhausted.

30.1 Lowering Is Staged Representation Change

A typical path may move structured tensor computation into loops, vectors, memory accesses, control flow, and eventually a target dialect. Each transition changes the questions later passes can ask. Before lowering, a pass may know an operation is matrix multiplication; after lowering, it may only see loops and loads. That is why fusion, tiling, and shape reasoning usually happen before the structure is erased.

There is no universal order. A GPU compiler may lower directly from a tensor operation to an accelerator dialect; a CPU pipeline may preserve vectors longer. The correct path follows the target, runtime model, and transformations that still need high-level semantics.

30.2 Every Boundary Needs A Contract

A lowering pass must define source dialects, target dialects, type mappings, effects, and legalization rules. Tensor-to-memref conversion introduces mutable storage and therefore aliasing, allocation, and ownership questions. Structured control flow to CFG conversion changes nesting into branch edges and block arguments. These are semantic changes represented by explicit operations and conversion patterns, not formatting choices.

A pass is incomplete if it lowers producers but leaves users with obsolete types, or if it creates target operations without target data-layout and ABI information. Full conversion and verification make these omissions visible.

30.3 Debugging A Lowering Pipeline

Inspect IR between stages. When a conversion fails, identify the first illegal operation, then ask whether the missing piece is a rewrite pattern, a type conversion, a materialization, a dialect registration, or an incorrect legality rule. Do not jump directly to the final target: intermediate IR is where most representation mistakes become understandable.