[llvm] [DA] Fix the check between Subscript and Size after delinearization (PR #151326)
Ryotaro Kasuga via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 4 03:32:24 PDT 2025
================
@@ -594,14 +594,15 @@ for.end12: ; preds = %for.inc10, %entry
}
+; FIXME? It seems that we cannot prove that %N is non-negative...
define void @nonnegative(ptr nocapture %A, i32 %N) {
; CHECK-LABEL: 'nonnegative'
; CHECK-NEXT: Src: store i32 1, ptr %arrayidx, align 4 --> Dst: store i32 1, ptr %arrayidx, align 4
-; CHECK-NEXT: da analyze - none!
+; CHECK-NEXT: da analyze - output [* *]!
; CHECK-NEXT: Src: store i32 1, ptr %arrayidx, align 4 --> Dst: store i32 2, ptr %arrayidx, align 4
-; CHECK-NEXT: da analyze - consistent output [0 0|<]!
+; CHECK-NEXT: da analyze - output [* *|<]!
; CHECK-NEXT: Src: store i32 2, ptr %arrayidx, align 4 --> Dst: store i32 2, ptr %arrayidx, align 4
-; CHECK-NEXT: da analyze - none!
+; CHECK-NEXT: da analyze - output [* *]!
----------------
kasuga-fj wrote:
After giving it some thought, I feel like the original result is correct. Giving a negative value (i.e., a value whose most significant bit is 1) to `%N` seems likely to make the GEP `poison`, based on [the same logic in LAA](https://github.com/llvm/llvm-project/blob/440a4de55265c67cf41a77e40f96d7c18ed7590a/llvm/lib/Analysis/LoopAccessAnalysis.cpp#L991-L998). Storing to `poison` occurs UB, so we don't have to consider such cases?
I've been thinking about it, and I now believe we should interpret all values as signed numbers in DA. We must also ensure that all subscripts do not wrap around in the signed sense. Especially, an offset will be [sign-extended if the type is smaller than pointer index type](https://llvm.org/docs/LangRef.html#id240).
Consider the case where the offset is of type `i8`, the pointer index type is larger one, and we have an AddRec like `{126,+,1}<%loop>`. The AddRec will be evaluated as follows at each iteration: `0b01111110`, `0b01111111`, `0b10000000`, and so on. The distance between the first two values is 1, but the distance between the next two values will be larger than 1 since `0b10000000` will be extended in signed sense.
So, what do you think? Or, do you know what other similar analysis passes (like LAA) do?
https://github.com/llvm/llvm-project/pull/151326
More information about the llvm-commits
mailing list