|
20 | 20 | #include "swift/SIL/OwnershipUtils.h"
|
21 | 21 | #include "swift/SIL/SILBasicBlock.h"
|
22 | 22 | #include "swift/SIL/SILInstruction.h"
|
| 23 | +#include "swift/SIL/Test.h" |
23 | 24 | #include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
|
24 | 25 | #include "swift/SILOptimizer/Analysis/Reachability.h"
|
25 | 26 | #include "swift/SILOptimizer/Analysis/VisitBarrierAccessScopes.h"
|
| 27 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
26 | 28 | #include "swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h"
|
27 | 29 | #include "swift/SILOptimizer/Utils/InstOptUtils.h"
|
28 | 30 | #include "swift/SILOptimizer/Utils/InstructionDeleter.h"
|
@@ -490,3 +492,39 @@ bool swift::shrinkBorrowScope(
|
490 | 492 | calleeAnalysis);
|
491 | 493 | return ShrinkBorrowScope::run(context);
|
492 | 494 | }
|
| 495 | + |
| 496 | +namespace swift::test { |
| 497 | +// Arguments: |
| 498 | +// - BeginBorrowInst - the introducer for the scope to shrink |
| 499 | +// - bool - the expected return value of shrinkBorrowScope |
| 500 | +// - variadic list of values consisting of the copies expected to be rewritten |
| 501 | +// Dumps: |
| 502 | +// - DELETED: <<instruction deleted>> |
| 503 | +static FunctionTest ShrinkBorrowScopeTest( |
| 504 | + "shrink-borrow-scope", [](auto &function, auto &arguments, auto &test) { |
| 505 | + auto instruction = arguments.takeValue(); |
| 506 | + auto expected = arguments.takeBool(); |
| 507 | + auto *bbi = cast<BeginBorrowInst>(instruction); |
| 508 | + auto *analysis = test.template getAnalysis<BasicCalleeAnalysis>(); |
| 509 | + SmallVector<CopyValueInst *, 4> modifiedCopyValueInsts; |
| 510 | + InstructionDeleter deleter( |
| 511 | + InstModCallbacks().onDelete([&](auto *instruction) { |
| 512 | + llvm::errs() << "DELETED:\n"; |
| 513 | + instruction->dump(); |
| 514 | + })); |
| 515 | + auto shrunk = |
| 516 | + shrinkBorrowScope(*bbi, deleter, analysis, modifiedCopyValueInsts); |
| 517 | + unsigned index = 0; |
| 518 | + for (auto *cvi : modifiedCopyValueInsts) { |
| 519 | + auto expectedCopy = arguments.takeValue(); |
| 520 | + llvm::errs() << "rewritten copy " << index << ":\n"; |
| 521 | + llvm::errs() << "expected:\n"; |
| 522 | + expectedCopy->print(llvm::errs()); |
| 523 | + llvm::errs() << "got:\n"; |
| 524 | + cvi->dump(); |
| 525 | + assert(cvi == expectedCopy); |
| 526 | + ++index; |
| 527 | + } |
| 528 | + assert(expected == shrunk && "didn't shrink expectedly!?"); |
| 529 | + }); |
| 530 | +} // namespace swift::test |
0 commit comments