|
13 | 13 | #include "swift/SILOptimizer/Utils/GenericCloner.h" |
14 | 14 |
|
15 | 15 | #include "swift/AST/Type.h" |
| 16 | +#include "swift/SIL/OwnershipUtils.h" |
16 | 17 | #include "swift/SIL/SILBasicBlock.h" |
17 | 18 | #include "swift/SIL/SILFunction.h" |
18 | 19 | #include "swift/SIL/SILInstruction.h" |
19 | 20 | #include "swift/SIL/SILModule.h" |
20 | 21 | #include "swift/SIL/SILValue.h" |
| 22 | +#include "swift/SIL/ScopedAddressUtils.h" |
21 | 23 | #include "swift/SILOptimizer/Utils/InstOptUtils.h" |
22 | 24 | #include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h" |
23 | 25 | #include "llvm/ADT/ArrayRef.h" |
@@ -61,7 +63,6 @@ void GenericCloner::populateCloned() { |
61 | 63 | assert(!ReturnValueAddr && "Stale cloner state."); |
62 | 64 |
|
63 | 65 | SILFunction *Cloned = getCloned(); |
64 | | - |
65 | 66 | // Create arguments for the entry block. |
66 | 67 | SILBasicBlock *OrigEntryBB = &*Original.begin(); |
67 | 68 | SILBasicBlock *ClonedEntryBB = Cloned->createBasicBlock(); |
@@ -142,14 +143,19 @@ void GenericCloner::populateCloned() { |
142 | 143 |
|
143 | 144 | // Store the new direct parameter to an alloc_stack. |
144 | 145 | createAllocStack(); |
145 | | - if (!NewArg->getArgumentConvention().isGuaranteedConvention()) { |
| 146 | + SILValue addr; |
| 147 | + if (NewArg->getArgumentConvention().isGuaranteedConvention() && |
| 148 | + NewArg->getFunction()->hasOwnership()) { |
| 149 | + auto *sbi = getBuilder().createStoreBorrow(Loc, NewArg, ASI); |
| 150 | + StoreBorrowsToCleanup.push_back(sbi); |
| 151 | + addr = sbi; |
| 152 | + } else { |
146 | 153 | getBuilder().emitStoreValueOperation(Loc, NewArg, ASI, |
147 | 154 | StoreOwnershipQualifier::Init); |
148 | | - } else { |
149 | | - getBuilder().emitStoreBorrowOperation(Loc, NewArg, ASI); |
| 155 | + addr = ASI; |
150 | 156 | } |
151 | 157 |
|
152 | | - entryArgs.push_back(ASI); |
| 158 | + entryArgs.push_back(addr); |
153 | 159 | return true; |
154 | 160 | } |
155 | 161 | } |
@@ -225,4 +231,25 @@ void GenericCloner::postFixUp(SILFunction *f) { |
225 | 231 | getBuilder().setInsertionPoint(applyBlock); |
226 | 232 | getBuilder().createUnreachable(apply->getLoc()); |
227 | 233 | } |
| 234 | + |
| 235 | + SmallVector<SILBasicBlock *, 4> discoveredBlocks; |
| 236 | + for (auto *sbi : StoreBorrowsToCleanup) { |
| 237 | + ScopedAddressValue scopedAddress(sbi); |
| 238 | + if (scopedAddress->getUses().empty()) { |
| 239 | + scopedAddress.createScopeEnd(sbi->getNextInstruction()->getIterator(), |
| 240 | + RegularLocation::getAutoGeneratedLocation()); |
| 241 | + continue; |
| 242 | + } |
| 243 | + discoveredBlocks.clear(); |
| 244 | + PrunedLiveness storeBorrowLiveness(&discoveredBlocks); |
| 245 | + bool success = scopedAddress.computeLiveness(storeBorrowLiveness); |
| 246 | + if (success) { |
| 247 | + scopedAddress.endScopeAtLivenessBoundary(&storeBorrowLiveness); |
| 248 | + continue; |
| 249 | + } |
| 250 | + for (auto *exit : FunctionExits) { |
| 251 | + scopedAddress.createScopeEnd(exit->getIterator(), |
| 252 | + RegularLocation::getAutoGeneratedLocation()); |
| 253 | + } |
| 254 | + } |
228 | 255 | } |
0 commit comments