My gut feeling is that we don’t do loop rotation as a canonical pass, so there is no guarantee that the loop must be run when it comes to LICM. Am I understanding it correctly?
This depends on the specific loop operation but is true for something like scf.for
and for the “before”-region of a scf.while
.
In some scenarios you could deduce from the bounds of a scf.for
whether it is excecuted at least once in which case speculatability would not be required to hoist an operation, only that it has no side-effects.
Also it seems that LICM is pretty strong about not moving memory ops out of the loop, i.e,
isMemoryEffectFree
is required, while LLVM is relatively weak on that.
Correct. Moving side-effecting operations out of loops is sadly non-trivial as this requires analysis to ensure that the loop does not contain other side-effecting operations that affect the operation that is to be hoisted. LLVM can analyze this with MemorySSA but in general requires more infrastructure such as alias-analysis and more.
Is it also related to speculation?
Not quite. Speculatability is mostly an indicator for whether an operation has wholly defined behaviour or not which is somewhat orthoganal to side-effecting operations, but in practice often appear together. Both need to be accoutned for when determining whether code motion is legal. See the original discussion that lead to the introduction of speculatability here: [RFC] Mark tensor.dim and memref.dim as side effecting