[llvm] [LAA] Add stencil group merging to reduce runtime pointer checks (PR #187252)

Igor Kirillov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 04:17:10 PDT 2026


================
@@ -737,6 +762,366 @@ void RuntimePointerChecking::groupChecks(
   }
 }
 
+/// Result of decomposing a SCEV expression into stencil offset form:
+///   Offset = Constant + sum(Coefficients[stride] * stride)
+/// where each stride is a loop-invariant SCEV expression.
+struct StencilDecomposition {
+  int64_t Constant = 0;
+  /// Map from loop-invariant stride SCEV to its integer coefficient.
+  SmallMapVector<const SCEV *, int64_t, 4> Coefficients;
+};
+
+/// Try to decompose \p Expr into a stencil offset function of loop-invariant
+/// strides: C + a1*s1 + a2*s2 + ...
+/// Relies on SCEV's canonical form: AddExpr operands are flattened (N-ary),
+/// MulExpr has the constant operand first when present.
+/// Returns std::nullopt if the expression contains non-stencil terms or any
+/// SCEV constant doesn't fit in int64_t (we commit to the signed
+/// interpretation; values that need more than 64 significant bits are
+/// out of scope).
+static std::optional<StencilDecomposition>
+decomposeStencilOffset(const SCEV *Expr, ScalarEvolution &SE, const Loop &L) {
+  StencilDecomposition D;
+
+  // Collect top-level additive terms.
+  SmallVector<const SCEV *, 4> Terms;
+  if (auto *Add = dyn_cast<SCEVAddExpr>(Expr))
+    append_range(Terms, Add->operands());
+  else
+    Terms.push_back(Expr);
+
+  const SCEVConstant *C;
+  const SCEV *Stride;
+  for (const SCEV *Term : Terms) {
+    if (match(Term, m_SCEVConstant(C))) {
+      auto V = C->getAPInt().trySExtValue();
+      if (!V)
+        return std::nullopt;
+      D.Constant += *V;
+    } else if (match(Term, m_scev_Mul(m_SCEVConstant(C), m_SCEV(Stride)))) {
+      // Canonical 2-operand pattern (constant * loop-invariant).
+      if (!SE.isLoopInvariant(Stride, &L))
+        return std::nullopt;
+      auto V = C->getAPInt().trySExtValue();
+      if (!V)
+        return std::nullopt;
+      D.Coefficients[Stride] += *V;
+    } else if (SE.isLoopInvariant(Term, &L)) {
+      D.Coefficients[Term] += 1;
+    } else {
+      return std::nullopt;
+    }
+  }
+  return D;
+}
+
+void RuntimePointerChecking::mergeStencilGroups(PredicatedScalarEvolution &PSE,
----------------
igogo-x86 wrote:

What we need for the algorithm is all pointers to the same object. In LAA, these are represented as:

```
public:
  struct PointerInfo {
    ...
    unsigned DependencySetId;
    unsigned AliasSetId;
    ...
  };
```

with unique `DependencySetId `and `AliasSetId` values.

We need to find all `RuntimeCheckingPtrGroups` that have the same `DependencySetId` and `AliasSetId`. In my code, we find them directly by iterating over all `RuntimeCheckingPtrGroups`.

The alternative is to pass `MemoryDepChecker::DepCandidates &DepCands`, which holds equivalence sets where each set is exactly what we need: pointers sharing the same `DependencySetId` and `AliasSetId`. We could use it, but we would still have to traverse `RuntimePointerChecking::CheckingGroups` and build a map from pointers to groups and we already doing that. That looks more computationally expensive and less readable.

About replacing `groupChecks`: there are some similarities, and `groupChecks` is a bit overcomplicated. For example, it has a triple-nested loop and a Seen set, which I feel could be simplified. However, it is exact: stencil merging overestimates via a bounding box, whereas `groupChecks` merges pointers with a constant difference. It also has a compile-time budget restriction.

We also need some initial runtime-check count in order to decide whether we want to do stencil-group merging or not.

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


More information about the llvm-commits mailing list