Skip to content

Commit ad3b1fe

Browse files
committed
[SCEV] Do not erase LoopUsers. PR53969
This patch fixes a logical error in how we work with `LoopUsers` map. It maps a loop onto a set of AddRecs that depend on it. The Addrecs are added to this map only once when they are created and put to the UniqueSCEVs` map. The only purpose of this map is to make sure that, whenever we forget a loop, all (directly or indirectly) dependent SCEVs get forgotten too. Current code erases SCEVs from dependent set of a given loop whenever we forget this loop. This is not a correct behavior due to the following scenario: 1. We have a loop `L` and an AddRec `AR` that depends on it; 2. We modify something in the loop, but don't destroy it. We still call forgetLoop on it; 3. `AR` is no longer dependent on `L` according to `LoopUsers`. It is erased from ValueExprMap` and `ExprValue map, but still exists in UniqueSCEVs; 4. We can later request the very same AddRec for the very same loop again, and get existing SCEV `AR`. 5. Now, `AR` exists and is used again, but its notion that it depends on `L` is lost; 6. Then we decide to delete `L`. `AR` will not be forgotten because we have lost it; 7. Just you wait when you run into a dangling pointer problem, or any other kind of problem because an active SCEV is now referecing a non-existent loop. The solution to this is to stop erasing values from `LoopUsers`. Yes, we will maybe forget something that is already not used, but it's cheap. This fixes a functional bug and potentially may have negative compile time impact on methods with huge or numerous loops. Differential Revision: https://reviews.llvm.org/D120303 Reviewed By: nikic
1 parent e7e17b3 commit ad3b1fe

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8068,7 +8068,6 @@ void ScalarEvolution::forgetLoop(const Loop *L) {
80688068
if (LoopUsersItr != LoopUsers.end()) {
80698069
ToForget.insert(ToForget.end(), LoopUsersItr->second.begin(),
80708070
LoopUsersItr->second.end());
8071-
LoopUsers.erase(LoopUsersItr);
80728071
}
80738072

80748073
// Drop information about expressions based on loop-header PHIs.

llvm/test/Transforms/LoopDeletion/pr53969.ll

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
12
; RUN: opt -passes="loop(indvars,loop-deletion)" -S < %s | FileCheck %s
2-
; XFAIL: *
3-
; REQUIRES: asserts
43

54
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
65
target triple = "x86_64-unknown-linux-gnu"
76

87
; Make sure we don't crash.
98
define void @test() {
10-
; CHECK-LABEL: test
9+
; CHECK-LABEL: @test(
10+
; CHECK-NEXT: bb:
11+
; CHECK-NEXT: br label [[BB1:%.*]]
12+
; CHECK: bb1:
13+
; CHECK-NEXT: [[TMP2:%.*]] = phi i32 [ 11, [[BB:%.*]] ]
14+
; CHECK-NEXT: [[TMP3:%.*]] = add nsw i32 112, -1
15+
; CHECK-NEXT: [[TMP4:%.*]] = add nuw nsw i32 [[TMP2]], 1
16+
; CHECK-NEXT: [[TMP5:%.*]] = mul i32 [[TMP3]], [[TMP3]]
17+
; CHECK-NEXT: [[TMP6:%.*]] = mul nsw i32 [[TMP2]], -6
18+
; CHECK-NEXT: [[TMP7:%.*]] = mul nsw i32 [[TMP6]], [[TMP5]]
19+
; CHECK-NEXT: [[TMP8:%.*]] = add nuw nsw i32 [[TMP7]], [[TMP2]]
20+
; CHECK-NEXT: [[TMP9:%.*]] = and i32 undef, 1
21+
; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i32 [[TMP9]], 0
22+
; CHECK-NEXT: br i1 [[TMP10]], label [[BB33_LOOPEXIT1:%.*]], label [[BB34_PREHEADER:%.*]]
23+
; CHECK: bb34.preheader:
24+
; CHECK-NEXT: br label [[BB34:%.*]]
25+
; CHECK: bb11:
26+
; CHECK-NEXT: [[TMP2_LCSSA12:%.*]] = phi i32 [ 11, [[BB34]] ]
27+
; CHECK-NEXT: br label [[BB33_LOOPEXIT:%.*]]
28+
; CHECK: bb12:
29+
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[TMP40:%.*]], 0
30+
; CHECK-NEXT: br label [[BB14:%.*]]
31+
; CHECK: bb14:
32+
; CHECK-NEXT: br i1 true, label [[BB32:%.*]], label [[BB22:%.*]]
33+
; CHECK: bb22:
34+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 4 to i32
35+
; CHECK-NEXT: [[TMP23:%.*]] = or i32 [[TMP1]], undef
36+
; CHECK-NEXT: [[TMP24:%.*]] = add i32 [[TMP23]], undef
37+
; CHECK-NEXT: br i1 false, label [[BB42:%.*]], label [[BB25:%.*]]
38+
; CHECK: bb25:
39+
; CHECK-NEXT: br label [[BB31:%.*]]
40+
; CHECK: bb31:
41+
; CHECK-NEXT: unreachable
42+
; CHECK: bb32:
43+
; CHECK-NEXT: ret void
44+
; CHECK: bb33.loopexit:
45+
; CHECK-NEXT: [[TMP2_LCSSA9:%.*]] = phi i32 [ [[TMP2_LCSSA12]], [[BB11:%.*]] ]
46+
; CHECK-NEXT: br label [[BB33:%.*]]
47+
; CHECK: bb33.loopexit1:
48+
; CHECK-NEXT: [[TMP2_LCSSA:%.*]] = phi i32 [ [[TMP2]], [[BB1]] ]
49+
; CHECK-NEXT: br label [[BB33]]
50+
; CHECK: bb33:
51+
; CHECK-NEXT: [[TMP210:%.*]] = phi i32 [ [[TMP2_LCSSA]], [[BB33_LOOPEXIT1]] ], [ [[TMP2_LCSSA9]], [[BB33_LOOPEXIT]] ]
52+
; CHECK-NEXT: call void @use(i32 [[TMP210]])
53+
; CHECK-NEXT: ret void
54+
; CHECK: bb34:
55+
; CHECK-NEXT: [[TMP36:%.*]] = xor i32 0, [[TMP8]]
56+
; CHECK-NEXT: [[TMP38:%.*]] = add i32 [[TMP36]], undef
57+
; CHECK-NEXT: [[TMP39:%.*]] = add i32 [[TMP38]], undef
58+
; CHECK-NEXT: [[TMP40]] = sext i32 [[TMP39]] to i64
59+
; CHECK-NEXT: br i1 false, label [[BB11]], label [[BB12:%.*]]
60+
; CHECK: bb42:
61+
; CHECK-NEXT: [[TMP24_LCSSA:%.*]] = phi i32 [ [[TMP24]], [[BB22]] ]
62+
; CHECK-NEXT: [[TMP18_LCSSA4:%.*]] = phi i64 [ [[TMP0]], [[BB22]] ]
63+
; CHECK-NEXT: store atomic i64 [[TMP18_LCSSA4]], i64 addrspace(1)* undef unordered, align 8
64+
; CHECK-NEXT: call void @use(i32 [[TMP24_LCSSA]])
65+
; CHECK-NEXT: ret void
66+
;
1167
bb:
1268
br label %bb1
1369

0 commit comments

Comments
 (0)