[llvm] [DA] Add initial support for monotonicity check (PR #162280)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 15 08:46:15 PDT 2025


================
@@ -177,13 +189,189 @@ void DependenceAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.addRequiredTransitive<LoopInfoWrapperPass>();
 }
 
+namespace {
+
+/// The type of monotonicity of a SCEV. This property is defined with respect to
+/// the outermost loop that DA is analyzing.
+///
+/// This is designed to classify the behavior of AddRec expressions, and does
+/// not care about other SCEVs. For example, given the two loop-invariant values
+/// `A` and `B`, `A + B` is treated as Invariant even if the addition wraps.
+enum class SCEVMonotonicityType {
+  /// The expression is neither loop-invariant nor monotonic (or we fail to
+  /// prove it).
+  Unknown,
+
+  /// The expression is loop-invariant with respect to the outermost loop.
+  Invariant,
+
+  /// The expression is a (nested) affine AddRec and is monotonically increasing
+  /// or decreasing in a signed sense with respect to each loop. Monotonicity is
+  /// checked independently for each loop, and the expression is classified as
+  /// MultiSignedMonotonic if all AddRecs are nsw. For example, in the following
----------------
kasuga-fj wrote:

At first, I was thinking of handling various cases like as you mentioned, but now I think it's fine to focus on AddRecs for the time being. I don't have a strong preference, so changing the name seems reasonable to me (it's also a bit questionable that `Invariant` is included in the `SCEVMonotonicityType` in the first place, as you said in https://github.com/llvm/llvm-project/pull/162280#discussion_r2432566627).

I'm thinking of renaming it something like `MultivariateWrapType`. Since I'm not good at naming, I'm happy if you have a better idea.

As for the definition, the latter one seems to match my intention.

```
ExtremeValueList := {Expr(i_first, j_first), Expr(i_first, j_last), Expr(i_last, j_first), Expr(i_last, j_last)};
Range := [min(ExtremeValueList), max(ExtremeValueList)]
IsMonotonic(Expr) := Expr(i, j) is in Range for all i in [i_first, i_last] and j in [j_first, j_last]
```

(since I slacked off on studying, I don't really understand about topology...)

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


More information about the llvm-commits mailing list