|
18 | 18 |
|
19 | 19 | #include "swift/Basic/Assertions.h"
|
20 | 20 | #include "swift/Basic/Defer.h"
|
| 21 | +#include "swift/SIL/BasicBlockBits.h" |
21 | 22 | #include "swift/SIL/BasicBlockUtils.h"
|
22 | 23 | #include "swift/SIL/OSSALifetimeCompletion.h"
|
23 | 24 | #include "swift/SIL/PrettyStackTrace.h"
|
|
26 | 27 | #include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
|
27 | 28 | #include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
|
28 | 29 | #include "swift/SILOptimizer/PassManager/Transforms.h"
|
| 30 | +#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h" |
29 | 31 | #include "swift/SILOptimizer/Utils/CanonicalizeInstruction.h"
|
30 | 32 | #include "swift/SILOptimizer/Utils/InstOptUtils.h"
|
| 33 | +#include "llvm/ADT/PostOrderIterator.h" |
31 | 34 |
|
32 | 35 | using namespace swift;
|
33 | 36 |
|
@@ -106,33 +109,163 @@ struct SILGenCleanup : SILModuleTransform {
|
106 | 109 | bool completeOSSALifetimes(SILFunction *function);
|
107 | 110 | template <typename Range>
|
108 | 111 | bool completeLifetimesInRange(Range const &range,
|
109 |
| - OSSALifetimeCompletion &completion); |
| 112 | + OSSALifetimeCompletion &completion, |
| 113 | + BasicBlockSet &completed); |
110 | 114 | };
|
111 | 115 |
|
| 116 | +/// Progress made so far on a walk beginning at an unreachable-terminated block. |
| 117 | +struct Walk { |
| 118 | + SILBasicBlock *current; |
| 119 | + SmallPtrSet<SILBasicBlock *, 16> seen; |
| 120 | +}; |
| 121 | + |
| 122 | +/// Populate `roots` with the last blocks that are discovered via backwards |
| 123 | +/// walks along any non-repeating paths starting at the ends in `walks`. |
| 124 | +void collectReachableRoots(SILFunction *function, SmallVectorImpl<Walk> &walks, |
| 125 | + StackList<SILBasicBlock *> &roots) { |
| 126 | + assert(!walks.empty()); |
| 127 | + assert(roots.empty()); |
| 128 | + |
| 129 | + // Always include the entry block as a root. Currently SILGen will emit |
| 130 | + // consumes in unreachable blocks of values defined in reachable blocks (e.g. |
| 131 | + // test/SILGen/unreachable_code.swift:testUnreachableCatchClause). |
| 132 | + // TODO: [fix_silgen_destroy_unreachable] Fix SILGen not to emit such |
| 133 | + // destroys and don't add the entry |
| 134 | + // block to roots here. |
| 135 | + roots.push_back(function->getEntryBlock()); |
| 136 | + |
| 137 | + StackList<SILBasicBlock *> backedges(function); |
| 138 | + |
| 139 | + BasicBlockSet visited(function); |
| 140 | + while (!walks.empty()) { |
| 141 | + auto walk = walks.pop_back_val(); |
| 142 | + |
| 143 | + SILBasicBlock *current = walk.current; |
| 144 | + |
| 145 | + while (auto *block = current) { |
| 146 | + current = nullptr; |
| 147 | + bool previouslyVisited = visited.insert(block); |
| 148 | + |
| 149 | + if (!walk.seen.insert(block).second) { |
| 150 | + // This block was seen on _this_ path. |
| 151 | + backedges.push_back(block); |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + if (!previouslyVisited) { |
| 156 | + // This block was seen on another path. Stop walking. |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + // No predecessors? Found a root. |
| 161 | + if (block->pred_empty()) { |
| 162 | + if (block == function->getEntryBlock()) { |
| 163 | + // TODO: [fix_silgen_destroy_unreachable] Remove this condition. |
| 164 | + continue; |
| 165 | + } |
| 166 | + roots.push_back(block); |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + // At least one predecessor. Continue walking this path from the first |
| 171 | + // predecessor and add all the others to "ends" for subsequent walking. |
| 172 | + |
| 173 | + for (auto pair : llvm::enumerate(block->getPredecessorBlocks())) { |
| 174 | + auto *predecessor = pair.value(); |
| 175 | + if (pair.index() == 0) { |
| 176 | + // Continue this walk from `predecessor`. |
| 177 | + current = predecessor; |
| 178 | + } else { |
| 179 | + // Clone this walk to another that continues from `predecessor`. |
| 180 | + walks.push_back({predecessor, walk.seen}); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // Only complete starting from backedges after completing starting from all |
| 187 | + // nodes without predecessors. This ensures that completion is done with |
| 188 | + // respect to the post-order based at a node without predecessors rather than |
| 189 | + // one based at a backedge when both a node without predecessor and a |
| 190 | + // backedge are discovered from the same end. |
| 191 | + for (auto *backedge : backedges) { |
| 192 | + roots.push_back(backedge); |
| 193 | + } |
| 194 | +} |
| 195 | + |
112 | 196 | bool SILGenCleanup::completeOSSALifetimes(SILFunction *function) {
|
113 | 197 | if (!getModule()->getOptions().OSSACompleteLifetimes)
|
114 | 198 | return false;
|
115 | 199 |
|
116 | 200 | LLVM_DEBUG(llvm::dbgs() << "Completing lifetimes in " << function->getName()
|
117 | 201 | << "\n");
|
118 | 202 |
|
119 |
| - bool changed = false; |
| 203 | + // First, collect all blocks terminated in unreachable. Enables bailing out |
| 204 | + // if there are none. |
| 205 | + SmallVector<Walk, 32> walks; |
| 206 | + function->visitUnreachableTerminatedBlocks([&walks](auto &block) { |
| 207 | + walks.push_back({&block, {}}); |
| 208 | + }); |
| 209 | + |
| 210 | + if (walks.empty()) { |
| 211 | + // There are no unreachable-terminated blocks, so there are no lifetimes to |
| 212 | + // complete. (SILGen may emit incomplete lifetimes, but not underconsumed |
| 213 | + // lifetimes.) |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + // Lifetimes must be completed in unreachable blocks that are reachable via |
| 218 | + // backwards walk from unreachable instructions. First, check whether there |
| 219 | + // are any unreachable blocks. |
| 220 | + ReachableBlocks reachableBlocks(function); |
| 221 | + reachableBlocks.compute(); |
| 222 | + StackList<SILBasicBlock *> roots(function); |
| 223 | + if (!reachableBlocks.hasUnreachableBlocks()) { |
| 224 | + // There are no blocks that are unreachable from the entry block. Thus, |
| 225 | + // every block will be completed when completing the post-order of the |
| 226 | + // entry block. |
| 227 | + roots.push_back(function->getEntryBlock()); |
| 228 | + } else { |
| 229 | + // There are unreachable blocks. Determine the roots that can be reached |
| 230 | + // when walking from the unreachable blocks. |
| 231 | + collectReachableRoots(function, walks, roots); |
| 232 | + } |
120 | 233 |
|
121 |
| - // Lifetimes must be completed inside out (bottom-up in the CFG). |
122 |
| - PostOrderFunctionInfo *postOrder = |
123 |
| - getAnalysis<PostOrderAnalysis>()->get(function); |
| 234 | + bool changed = false; |
124 | 235 | DeadEndBlocks *deb = getAnalysis<DeadEndBlocksAnalysis>()->get(function);
|
125 | 236 | OSSALifetimeCompletion completion(function, /*DomInfo*/ nullptr, *deb);
|
126 |
| - changed |= completeLifetimesInRange(postOrder->getPostOrder(), completion); |
| 237 | + BasicBlockSet completed(function); |
| 238 | + for (auto *root : roots) { |
| 239 | + if (root == function->getEntryBlock()) { |
| 240 | + assert(!completed.contains(root)); |
| 241 | + // When completing from the entry block, prefer the PostOrderAnalysis so |
| 242 | + // the result is cached. |
| 243 | + PostOrderFunctionInfo *postOrder = |
| 244 | + getAnalysis<PostOrderAnalysis>()->get(function); |
| 245 | + changed |= completeLifetimesInRange(postOrder->getPostOrder(), completion, |
| 246 | + completed); |
| 247 | + } |
| 248 | + if (completed.contains(root)) { |
| 249 | + // This block has already been completed in some other post-order |
| 250 | + // traversal. Thus the entire post-order rooted at it has already been |
| 251 | + // completed. |
| 252 | + continue; |
| 253 | + } |
| 254 | + changed |= completeLifetimesInRange( |
| 255 | + make_range(po_begin(root), po_end(root)), completion, completed); |
| 256 | + } |
127 | 257 | function->verifyOwnership(/*deadEndBlocks=*/nullptr);
|
128 | 258 | return changed;
|
129 | 259 | }
|
130 | 260 |
|
131 | 261 | template <typename Range>
|
132 |
| -bool SILGenCleanup::completeLifetimesInRange( |
133 |
| - Range const &range, OSSALifetimeCompletion &completion) { |
| 262 | +bool SILGenCleanup::completeLifetimesInRange(Range const &range, |
| 263 | + OSSALifetimeCompletion &completion, |
| 264 | + BasicBlockSet &completed) { |
134 | 265 | bool changed = false;
|
135 | 266 | for (auto *block : range) {
|
| 267 | + if (!completed.insert(block)) |
| 268 | + continue; |
136 | 269 | LLVM_DEBUG(llvm::dbgs()
|
137 | 270 | << "Completing lifetimes in bb" << block->getDebugID() << "\n");
|
138 | 271 | for (SILInstruction &inst : reverse(*block)) {
|
|
0 commit comments