Skip to content

Commit 8339a7b

Browse files
mmereckiigcbot
authored andcommitted
[LLVM] Fix stack overflow in SCEV
Break recursion in getSCEVIter.
1 parent b0ed3e0 commit 8339a7b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2023 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
/*========================== begin_copyright_notice ============================
10+
11+
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
12+
See https://llvm.org/LICENSE.txt for license information.
13+
SPDX-License-Identifier: Apache-2.0 with LLVM-exception
14+
15+
============================= end_copyright_notice ===========================*/
16+
17+
# TODO: Once upstreamed, update with LLORG revision & adjust per community review
18+
19+
diff -Naur "a/llvm/include/llvm/Analysis/ScalarEvolution.h" "b/llvm/include/llvm/Analysis/ScalarEvolution.h"
20+
--- "a/llvm/include/llvm/Analysis/ScalarEvolution.h" 2023-09-27 19:27:08.174510600 +0200
21+
+++ "b/llvm/include/llvm/Analysis/ScalarEvolution.h" 2023-09-27 18:41:11.301171800 +0200
22+
@@ -1292,6 +1292,9 @@
23+
/// Mark predicate values currently being processed by isImpliedCond.
24+
SmallPtrSet<const Value *, 6> PendingLoopPredicates;
25+
26+
+ /// Set of Phis processed in createSCEVIter.
27+
+ SmallPtrSet<const PHINode*, 6> PendingPhiSCEVIter;
28+
+
29+
/// Mark SCEVUnknown Phis currently being processed by getRangeRef.
30+
SmallPtrSet<const PHINode *, 6> PendingPhiRanges;
31+
32+
diff -Naur "a/llvm/lib/Analysis/ScalarEvolution.cpp" "b/llvm/lib/Analysis/ScalarEvolution.cpp"
33+
--- "a/llvm/lib/Analysis/ScalarEvolution.cpp" 2023-09-27 19:27:08.189098000 +0200
34+
+++ "b/llvm/lib/Analysis/ScalarEvolution.cpp" 2023-09-27 19:05:40.601713600 +0200
35+
@@ -7076,6 +7076,13 @@
36+
37+
Stack.emplace_back(V, true);
38+
Stack.emplace_back(V, false);
39+
+
40+
+ if (isa<PHINode>(V) &&
41+
+ !PendingPhiSCEVIter.insert(cast<PHINode>(V)).second) {
42+
+ // cycled back to createSCEVIter while processing this Phi, bail
43+
+ return getUnknown(V);
44+
+ }
45+
+
46+
while (!Stack.empty()) {
47+
auto E = Stack.pop_back_val();
48+
Value *CurV = E.getPointer();
49+
@@ -7088,6 +7095,12 @@
50+
// If all operands have been visited already, create the SCEV.
51+
if (E.getInt()) {
52+
CreatedSCEV = createSCEV(CurV);
53+
+ if (isa<PHINode>(CurV))
54+
+ PendingPhiSCEVIter.erase(cast<PHINode>(CurV));
55+
+ } else if (isa<PHINode>(CurV) &&
56+
+ PendingPhiSCEVIter.count(cast<PHINode>(CurV)) > 0) {
57+
+ // cycled Phi, try to get its SCEV
58+
+ CreatedSCEV = createSCEV(CurV);
59+
} else {
60+
// Otherwise get the operands we need to create SCEV's for before creating
61+
// the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
62+
@@ -7101,8 +7114,11 @@
63+
// Queue CurV for SCEV creation, followed by its's operands which need to
64+
// be constructed first.
65+
Stack.emplace_back(CurV, true);
66+
- for (Value *Op : Ops)
67+
+ if (isa<PHINode>(CurV))
68+
+ PendingPhiSCEVIter.insert(cast<PHINode>(CurV));
69+
+ for (Value *Op : Ops) {
70+
Stack.emplace_back(Op, false);
71+
+ }
72+
}
73+
}
74+
75+
@@ -7225,7 +7241,8 @@
76+
return getUnknown(V);
77+
78+
case Instruction::PHI:
79+
- // Keep constructing SCEVs' for phis recursively for now.
80+
+ for (auto& Op : cast<PHINode>(U)->operands())
81+
+ Ops.push_back(Op);
82+
return nullptr;
83+
84+
case Instruction::Select: {

0 commit comments

Comments
 (0)