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

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 15 11:12:12 PDT 2025


amehsan wrote:

> I guess SCEV fails to prove that the second store is executed unconditionally

Even for a much simpler IR we have the same problem:
```
; Function Attrs: nofree norecurse nosync nounwind memory(argmem: write) uwtable
define dso_local void @foo(ptr noundef writeonly captures(none) %A) local_unnamed_addr #0 {
entry:
  br label %for.body

for.cond.cleanup:                                 ; preds = %for.body
  ret void

for.body:                                         ; preds = %entry, %for.body
  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
  %conv = trunc i64 %indvars.iv to i8
  %arrayidx = getelementptr inbounds nuw [20 x i8], ptr %A, i64 %indvars.iv
  %arrayidx1 = getelementptr inbounds nuw i8, ptr %arrayidx, i64 15
  store i8 %conv, ptr %arrayidx1, align 1, !tbaa !6
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 10
  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body, !llvm.loop !9
}

```


Source code
```
void foo (char A[][20]) {
  for (int i = 0; i < 10; i++) {
          A[i][15] = i;
  }
}
```

Report:
```
Printing analysis 'Dependence Analysis' for function 'foo':
Monotonicity check:
  Inst:   store i8 %conv, ptr %arrayidx1, align 1, !tbaa !6
    Expr: {15,+,20}<%for.body>
    Monotonicity: Unknown
    Reason: {15,+,20}<%for.body>

Src:  store i8 %conv, ptr %arrayidx1, align 1, !tbaa !6 --> Dst:  store i8 %conv, ptr %arrayidx1, align 1, !tbaa !6
  da analyze - confused!
```

Command line:
```
opt  -disable-output "-passes=print<da>" -aa-pipeline=basic-aa   -da-enable-monotonicity-check -da-dump-monotonicity-report
```

I don't know how the IR of the testcase is generated  and why that one is OK. 

I suspect if we start solving this kind of problem in DA, we will eventually reinvent many SCEV wheels in DA. 

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


More information about the llvm-commits mailing list