Commit 4c7ef53
Automerge: [LifetimeSafety] Optimize loan propagation by separating persistent and block-local origins (#165789)
## Summary
Optimizes the lifetime analysis loan propagation by preventing block-local origins from participating in expensive join operations at block boundaries.
## Problem
The lifetime analysis currently performs join operations on all origins at every block boundary. However, many origins are block-local: they exist only to propagate loans between persistent origins across temporary declarations and expressions within a single block. Including them in joins is unnecessary and expensive.
## Solution
This PR classifies origins into two categories:
- **Persistent origins**: referenced in multiple basic blocks, must participate in joins
- **Block-local origins**: confined to a single block, can be discarded at block boundaries
### Implementation
1. **Pre-pass** (`computePersistentOrigins`): Analyzes all facts in the CFG to identify which origins appear in multiple blocks
2. **Split lattice**: Maintains two separate `OriginLoanMap`s:
- `PersistentOrigins`: participates in join operations
- `BlockLocalOrigins`: used during block execution, discarded at boundaries
3. **Optimized join**: Only merges persistent origins; block-local map is reset to empty
### Benefits
- Significantly reduces join complexity, especially in functions with many temporary locals
- Performance scales with the ratio of temporary to persistent origins
- Correctness preserved: block-local loan propagation still works within blocks
Fixes: llvm/llvm-project#165780
Fixes: llvm/llvm-project#164625. It brings down regression from **150% to 2%**.
---File tree
7 files changed
+132
-29
lines changed- clang
- include/clang/Analysis/Analyses/LifetimeSafety
- lib/Analysis/LifetimeSafety
- unittests/Analysis
7 files changed
+132
-29
lines changedLines changed: 4 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| 147 | + | |
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
152 | 153 | | |
153 | 154 | | |
154 | | - | |
| 155 | + | |
| 156 | + | |
155 | 157 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
| 158 | + | |
160 | 159 | | |
161 | 160 | | |
162 | 161 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
336 | | - | |
| 336 | + | |
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
12 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
13 | 76 | | |
| 77 | + | |
14 | 78 | | |
15 | 79 | | |
16 | 80 | | |
17 | 81 | | |
18 | 82 | | |
19 | | - | |
20 | | - | |
21 | 83 | | |
22 | 84 | | |
23 | | - | |
24 | | - | |
25 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
26 | 93 | | |
27 | 94 | | |
28 | 95 | | |
29 | | - | |
| 96 | + | |
| 97 | + | |
30 | 98 | | |
31 | 99 | | |
32 | 100 | | |
33 | 101 | | |
34 | 102 | | |
35 | | - | |
| 103 | + | |
| 104 | + | |
36 | 105 | | |
37 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
38 | 116 | | |
39 | 117 | | |
40 | 118 | | |
| |||
50 | 128 | | |
51 | 129 | | |
52 | 130 | | |
53 | | - | |
| 131 | + | |
| 132 | + | |
54 | 133 | | |
55 | 134 | | |
56 | 135 | | |
| |||
59 | 138 | | |
60 | 139 | | |
61 | 140 | | |
62 | | - | |
| 141 | + | |
63 | 142 | | |
64 | 143 | | |
65 | | - | |
| 144 | + | |
66 | 145 | | |
67 | 146 | | |
68 | 147 | | |
| |||
74 | 153 | | |
75 | 154 | | |
76 | 155 | | |
77 | | - | |
| 156 | + | |
78 | 157 | | |
79 | 158 | | |
80 | 159 | | |
81 | 160 | | |
82 | 161 | | |
83 | 162 | | |
84 | | - | |
85 | | - | |
86 | | - | |
| 163 | + | |
| 164 | + | |
87 | 165 | | |
88 | 166 | | |
89 | 167 | | |
| |||
98 | 176 | | |
99 | 177 | | |
100 | 178 | | |
101 | | - | |
| 179 | + | |
102 | 180 | | |
103 | 181 | | |
104 | 182 | | |
105 | 183 | | |
106 | 184 | | |
107 | 185 | | |
108 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
109 | 200 | | |
110 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
111 | 204 | | |
112 | 205 | | |
113 | 206 | | |
114 | 207 | | |
115 | 208 | | |
116 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
117 | 214 | | |
118 | 215 | | |
119 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
532 | | - | |
| 532 | + | |
| 533 | + | |
533 | 534 | | |
534 | 535 | | |
535 | 536 | | |
| |||
543 | 544 | | |
544 | 545 | | |
545 | 546 | | |
546 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
547 | 552 | | |
548 | 553 | | |
549 | 554 | | |
| |||
0 commit comments