29 Legality
Legality is the explicit rule that determines whether IR is permitted at a particular conversion stage. It is not a statement that an operation is well-formed in general. An operation can be valid MLIR and still be illegal because a target stage requires it to be lowered away.
29.1 Legal, Illegal, And Dynamic
A conversion target marks operations or dialects legal, illegal, or dynamically legal. Dynamic legality depends on a predicate such as operand types, an attribute, or whether nested regions have already been converted. The target’s legality rules define the stopping condition for conversion.
For example, a pipeline may allow an arithmetic dialect while declaring tensor operations illegal after bufferization. It may allow a call only when its function type has been converted. The IR remains verified throughout, but the conversion succeeds only when every illegal construct has a legal replacement.
29.2 Legality Is Not Profitability
A legal operation need not be the best form for performance. A conversion target answers what the next stage can accept; a cost model answers which legal option is preferable. Mixing these decisions leads to conversion rules that sometimes fail based on optimization heuristics, making pipelines unreliable.
Dynamic legality should express a real boundary, not hide uncertainty. If a predicate cannot explain why an operation is legal, the conversion is probably missing an explicit representation or a later stage.