[llvm] 95eb49a - [SCEV] Bail out on mixed int/pointer in SCEVWrapPredicate::implies.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 18 03:23:31 PST 2024


Author: Florian Hahn
Date: 2024-12-18T11:22:39Z
New Revision: 95eb49a0905568a13c840b7866ce5d9c47e022f0

URL: https://github.com/llvm/llvm-project/commit/95eb49a0905568a13c840b7866ce5d9c47e022f0
DIFF: https://github.com/llvm/llvm-project/commit/95eb49a0905568a13c840b7866ce5d9c47e022f0.diff

LOG: [SCEV] Bail out on mixed int/pointer in SCEVWrapPredicate::implies.

Fixes a crash when trying to extend the pointer start value to a narrow
integer type after b6c29fdffd65.

Added: 
    llvm/test/Analysis/LoopAccessAnalysis/nusw-predicates.ll

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index c820e8bf7266ad..d55d09020fc147 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -14978,6 +14978,11 @@ bool SCEVWrapPredicate::implies(const SCEVPredicate *N,
       Flags != SCEVWrapPredicate::IncrementNUSW)
     return false;
 
+  const SCEV *Start = AR->getStart();
+  const SCEV *OpStart = Op->AR->getStart();
+  if (Start->getType()->isPointerTy() != OpStart->getType()->isPointerTy())
+    return false;
+
   const SCEV *Step = AR->getStepRecurrence(SE);
   const SCEV *OpStep = Op->AR->getStepRecurrence(SE);
   if (!SE.isKnownPositive(Step) || !SE.isKnownPositive(OpStep))
@@ -14990,8 +14995,6 @@ bool SCEVWrapPredicate::implies(const SCEVPredicate *N,
   OpStep = SE.getNoopOrZeroExtend(OpStep, WiderTy);
 
   bool IsNUW = Flags == SCEVWrapPredicate::IncrementNUSW;
-  const SCEV *OpStart = Op->AR->getStart();
-  const SCEV *Start = AR->getStart();
   OpStart = IsNUW ? SE.getNoopOrZeroExtend(OpStart, WiderTy)
                   : SE.getNoopOrSignExtend(OpStart, WiderTy);
   Start = IsNUW ? SE.getNoopOrZeroExtend(Start, WiderTy)

diff  --git a/llvm/test/Analysis/LoopAccessAnalysis/nusw-predicates.ll b/llvm/test/Analysis/LoopAccessAnalysis/nusw-predicates.ll
new file mode 100644
index 00000000000000..5234d8f107271a
--- /dev/null
+++ b/llvm/test/Analysis/LoopAccessAnalysis/nusw-predicates.ll
@@ -0,0 +1,120 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes='print<access-info>' -disable-output %s 2>&1 | FileCheck %s
+
+target datalayout = "p:16:16"
+
+define void @int_and_pointer_predicate(ptr %v, i32 %N) {
+; CHECK-LABEL: 'int_and_pointer_predicate'
+; CHECK-NEXT:    loop:
+; CHECK-NEXT:      Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
+; CHECK-NEXT:  Unknown data dependence.
+; CHECK-NEXT:      Dependences:
+; CHECK-NEXT:        Unknown:
+; CHECK-NEXT:            store i16 0, ptr %gep.iv.i16, align 1 ->
+; CHECK-NEXT:            store i16 0, ptr %v, align 1
+; CHECK-EMPTY:
+; CHECK-NEXT:      Run-time memory checks:
+; CHECK-NEXT:      Grouped accesses:
+; CHECK-EMPTY:
+; CHECK-NEXT:      Non vectorizable stores to invariant address were not found in loop.
+; CHECK-NEXT:      SCEV assumptions:
+; CHECK-NEXT:      {0,+,1}<%loop> Added Flags: <nusw>
+; CHECK-NEXT:      {%v,+,4}<%loop> Added Flags: <nusw>
+; CHECK-EMPTY:
+; CHECK-NEXT:      Expressions re-written:
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %iv.i16 = trunc i64 %iv to i16
+  %gep.iv.i16 = getelementptr { i16, i16 }, ptr %v, i16 %iv.i16
+  store i16 0, ptr %gep.iv.i16, align 1
+  store i16 0, ptr %v, align 1
+  %iv.next = add i64 %iv, 1
+  %iv.i32 = trunc i64 %iv to i32
+  %.not = icmp ult i32 %N, %iv.i32
+  br i1 %.not, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+define void @int_and_multiple_pointer_predicates(ptr %v, ptr %w, i32 %N) {
+; CHECK-LABEL: 'int_and_multiple_pointer_predicates'
+; CHECK-NEXT:    loop:
+; CHECK-NEXT:      Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
+; CHECK-NEXT:  Unknown data dependence.
+; CHECK-NEXT:      Dependences:
+; CHECK-NEXT:        Unknown:
+; CHECK-NEXT:            store i16 0, ptr %gep.v, align 1 ->
+; CHECK-NEXT:            store i16 0, ptr %v, align 1
+; CHECK-EMPTY:
+; CHECK-NEXT:        Unknown:
+; CHECK-NEXT:            store i16 0, ptr %gep.w, align 1 ->
+; CHECK-NEXT:            store i16 0, ptr %w, align 1
+; CHECK-EMPTY:
+; CHECK-NEXT:      Run-time memory checks:
+; CHECK-NEXT:      Check 0:
+; CHECK-NEXT:        Comparing group ([[GRP1:0x[0-9a-f]+]]):
+; CHECK-NEXT:        ptr %v
+; CHECK-NEXT:        Against group ([[GRP2:0x[0-9a-f]+]]):
+; CHECK-NEXT:        ptr %w
+; CHECK-NEXT:      Check 1:
+; CHECK-NEXT:        Comparing group ([[GRP1]]):
+; CHECK-NEXT:        ptr %v
+; CHECK-NEXT:        Against group ([[GRP3:0x[0-9a-f]+]]):
+; CHECK-NEXT:          %gep.w = getelementptr { i16, i16 }, ptr %w, i16 %iv.i16
+; CHECK-NEXT:      Check 2:
+; CHECK-NEXT:        Comparing group ([[GRP4:0x[0-9a-f]+]]):
+; CHECK-NEXT:          %gep.v = getelementptr { i16, i16 }, ptr %v, i16 %iv.i16
+; CHECK-NEXT:        Against group ([[GRP2]]):
+; CHECK-NEXT:        ptr %w
+; CHECK-NEXT:      Check 3:
+; CHECK-NEXT:        Comparing group ([[GRP4]]):
+; CHECK-NEXT:          %gep.v = getelementptr { i16, i16 }, ptr %v, i16 %iv.i16
+; CHECK-NEXT:        Against group ([[GRP3]]):
+; CHECK-NEXT:          %gep.w = getelementptr { i16, i16 }, ptr %w, i16 %iv.i16
+; CHECK-NEXT:      Grouped accesses:
+; CHECK-NEXT:        Group [[GRP1]]:
+; CHECK-NEXT:          (Low: %v High: (2 + %v))
+; CHECK-NEXT:            Member: %v
+; CHECK-NEXT:        Group [[GRP4]]:
+; CHECK-NEXT:          (Low: %v High: (6 + (4 * (trunc i32 %N to i16)) + %v))
+; CHECK-NEXT:            Member: {%v,+,4}<%loop>
+; CHECK-NEXT:        Group [[GRP2]]:
+; CHECK-NEXT:          (Low: %w High: (2 + %w))
+; CHECK-NEXT:            Member: %w
+; CHECK-NEXT:        Group [[GRP3]]:
+; CHECK-NEXT:          (Low: %w High: (6 + (4 * (trunc i32 %N to i16)) + %w))
+; CHECK-NEXT:            Member: {%w,+,4}<%loop>
+; CHECK-EMPTY:
+; CHECK-NEXT:      Non vectorizable stores to invariant address were not found in loop.
+; CHECK-NEXT:      SCEV assumptions:
+; CHECK-NEXT:      {0,+,1}<%loop> Added Flags: <nusw>
+; CHECK-NEXT:      {%v,+,4}<%loop> Added Flags: <nusw>
+; CHECK-NEXT:      {%w,+,4}<%loop> Added Flags: <nusw>
+; CHECK-EMPTY:
+; CHECK-NEXT:      Expressions re-written:
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %iv.i16 = trunc i64 %iv to i16
+  %gep.v = getelementptr { i16, i16 }, ptr %v, i16 %iv.i16
+  store i16 0, ptr %gep.v, align 1
+  store i16 0, ptr %v, align 1
+  %gep.w = getelementptr { i16, i16 }, ptr %w, i16 %iv.i16
+  store i16 0, ptr %gep.w, align 1
+  store i16 0, ptr %w, align 1
+  %iv.next = add i64 %iv, 1
+  %iv.i32 = trunc i64 %iv to i32
+  %.not = icmp ult i32 %N, %iv.i32
+  br i1 %.not, label %exit, label %loop
+
+exit:
+  ret void
+}


        


More information about the llvm-commits mailing list