[LAA] Fix type mismatch in getStartAndEndForAccess.#183116
[LAA] Fix type mismatch in getStartAndEndForAccess.#183116
Conversation
|
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-llvm-analysis Author: Kshitij Paranjape (kshitijvp) Changes
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/LoopAccessAnalysis.cpp#L253 Running This is caused by a type mismatch between Full diff: https://github.com/llvm/llvm-project/pull/183116.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 057636050db25..67530ac4ba31d 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -250,8 +250,13 @@ static bool evaluatePtrAddRecAtMaxBTCWillNotWrap(
return true;
});
if (DerefRK) {
- DerefBytesSCEV =
- SE.getUMaxExpr(DerefBytesSCEV, SE.getSCEV(DerefRK.IRArgValue));
+ const SCEV *DerefRKSCEV = SE.getSCEV(DerefRK.IRArgValue);
+ // Ensure both operands have the same type
+ Type *CommonTy =
+ SE.getWiderType(DerefBytesSCEV->getType(), DerefRKSCEV->getType());
+ DerefBytesSCEV = SE.getNoopOrAnyExtend(DerefBytesSCEV, CommonTy);
+ DerefRKSCEV = SE.getNoopOrAnyExtend(DerefRKSCEV, CommonTy);
+ DerefBytesSCEV = SE.getUMaxExpr(DerefBytesSCEV, DerefRKSCEV);
}
if (DerefBytesSCEV->isZero())
diff --git a/llvm/test/Analysis/LoopAccessAnalysis/type-mismatch-in-scalar-evolution.ll b/llvm/test/Analysis/LoopAccessAnalysis/type-mismatch-in-scalar-evolution.ll
new file mode 100644
index 0000000000000..0a28775ca290e
--- /dev/null
+++ b/llvm/test/Analysis/LoopAccessAnalysis/type-mismatch-in-scalar-evolution.ll
@@ -0,0 +1,34 @@
+; RUN: opt -S -p loop-vectorize -debug-only=loop-vectorize -force-vector-width=4 -disable-output 2>&1 < %s | FileCheck %s
+; REQUIRES: asserts
+
+; CHECK-LABEL: LV: Checking a loop in 'test_assumed_bounds_type_mismatch'
+; CHECK: LV: Not vectorizing: Cannot vectorize uncountable loop.
+
+define void @test_assumed_bounds_type_mismatch(ptr noalias %array, ptr readonly %pred, i32 %n) nosync nofree {
+entry:
+ %n_bytes = mul nuw nsw i32 %n, 2
+ call void @llvm.assume(i1 true) [ "dereferenceable"(ptr %pred, i32 %n_bytes) ]
+ %tc = sext i32 %n to i64
+ br label %for.body
+
+for.body:
+ %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.inc ]
+ %st.addr = getelementptr inbounds i16, ptr %array, i64 %iv
+ %data = load i16, ptr %st.addr, align 2
+ %inc = add nsw i16 %data, 1
+ store i16 %inc, ptr %st.addr, align 2
+ %ee.addr = getelementptr inbounds i16, ptr %pred, i64 %iv
+ %ee.val = load i16, ptr %ee.addr, align 2
+ %ee.cond = icmp sgt i16 %ee.val, 500
+ br i1 %ee.cond, label %exit, label %for.inc
+
+for.inc:
+ %iv.next = add nuw nsw i64 %iv, 1
+ %counted.cond = icmp eq i64 %iv.next, %tc
+ br i1 %counted.cond, label %exit, label %for.body
+
+exit:
+ ret void
+}
+
+declare void @llvm.assume(i1)
\ No newline at end of file
|
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
| @@ -0,0 +1,34 @@ | |||
| ; RUN: opt -S -p loop-vectorize -debug-only=loop-vectorize -force-vector-width=4 -disable-output 2>&1 < %s | FileCheck %s | |||
There was a problem hiding this comment.
you'd need to use -passes=print<aceess-info> for tests in this directory.
would be good to add to other existing early exit tests
There was a problem hiding this comment.
I have added to the existing one
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
|
ping |
| DerefBytesSCEV = SE.getNoopOrAnyExtend(DerefBytesSCEV, CommonTy); | ||
| DerefRKSCEV = SE.getNoopOrAnyExtend(DerefRKSCEV, CommonTy); |
There was a problem hiding this comment.
Why any extend? Both quantities should be positive, so better always use Zext?
| ret void | ||
| } | ||
|
|
||
| define void @test_assumed_bounds_type_mismatch(ptr noalias %array, ptr readonly %pred, i32 %n) nosync nofree { |
There was a problem hiding this comment.
is there a reason we need a loop with a store? If not, would be good to remove the store and add to llvm/test/Transforms/LoopVectorize/dereferenceable-info-from-assumption-variable-size.ll. It should get vectorized with the crash fixed
There was a problem hiding this comment.
yes the store is redundant in this case, needs to be removed.
There was a problem hiding this comment.
removing the store instruction is causing faulting load, instead of being vectorized.
llvm/test/Transforms/LoopVectorize/early_exit_store_legality.ll
Outdated
Show resolved
Hide resolved
The test will not be vectorized as it is potentially an early exit loop due to and removing the store instruction causes faulting load. |
It's fine as is. I also went ahead and clarified the PR title, hope that's OK with you |
|
Thanks, no issues with the PR title. |
sure |
|
I think you do not need to put complete backtrace in the commit message, we do not do it in practice, describing the issue and solution is enough. |
Sure, will change. |
SE.getUMaxExprcauses assertion failure due to type mismatch here:https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/LoopAccessAnalysis.cpp#L253
Running
opt -S -p loop-vectorize -debug-only=loop-vectorize llvm/test/Analysis/LoopAccessAnalysis/type-mismatch-in-scalar-evolution.llwithout the changes made in LoopAccessAnalysis.cpp causes assertion failure.This is caused by a type mismatch between
SE.getSCEV(DerefRK.IRArgValue)andDerefBytesSCEV.Fixing this by extending them to the wider type.