[Mlir-commits] [mlir] [mlir][Affine] Fix LICM incorrectly hoisting stores from zero-trip-count loops (PR #189165)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Fri Apr 3 01:16:31 PDT 2026


================
@@ -174,13 +174,22 @@ void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
   SmallVector<Operation *, 8> opsToMove;
   SmallPtrSet<Operation *, 8> opsWithUsers;
 
+  // If the trip count is statically known to be zero, do not hoist ops with
+  // side effects: the loop body never executes, so hoisting would cause them
+  // to run unconditionally, changing program semantics. Pure (side-effect-free
+  // and speculatable) ops are still eligible for hoisting.
+  std::optional<uint64_t> tripCount = getConstantTripCount(forOp);
+  bool zeroTripCount = tripCount.has_value() && *tripCount == 0;
----------------
ftynse wrote:

We have https://github.com/llvm/llvm-project/blob/c80443cd37b2e2788cba67ffa180a6331e5f0791/mlir/include/mlir/Interfaces/ControlFlowInterfaces.td#L337 that may be a bit more flexible to communicate "executes at least once" even when the upper bound is unknown, but it still won't interoperate with analyses.

https://github.com/llvm/llvm-project/pull/189165


More information about the Mlir-commits mailing list