32 Shape Inference
Shape inference derives facts about the dimensions, rank, element types, or encodings of shaped values. It is not one universal pass: different dialects supply different equations and runtime constraints, and some shapes remain dynamic by design.
32.1 Static Facts And Dynamic Values
A type such as tensor<4x?xf32> proves rank two and a first dimension of four, but not the second dimension. Operations may refine that information when their semantics determine output shape. A reshape may require a product equality; a matmul relates batch and contraction dimensions; a broadcast may select a larger extent.
Inference must distinguish facts proven at compile time from runtime checks. Turning a dynamic dimension into a static one without proof creates invalid IR or a miscompile. When a condition cannot be proven, retain a dynamic type and, where required, emit an assertion or runtime shape operation.
32.2 Shape Is A Cross-Operation Contract
An operation’s verifier checks local shape conditions. An inference or constraint-propagation pass uses those conditions across values and regions. When a rewrite changes rank, dimensions, or encodings, it must update result types and every user that depends on them. Reconstructing a tensor type from only element type and rank can silently discard encoding information.