Skip to content

Commit ad9fee2

Browse files
committed
[Test] Ensourced shrink-borrow-scope.
Moved the test next to the code it calls.
1 parent ee95732 commit ad9fee2

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

lib/SILOptimizer/UtilityPasses/TestRunner.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -326,34 +326,6 @@ static FunctionTest IsLexicalTest("is-lexical", [](auto &function,
326326
llvm::errs() << boolString << "\n";
327327
});
328328

329-
static FunctionTest ShrinkBorrowScopeTest(
330-
"shrink-borrow-scope", [](auto &function, auto &arguments, auto &test) {
331-
auto instruction = arguments.takeValue();
332-
auto expected = arguments.takeBool();
333-
auto *bbi = cast<BeginBorrowInst>(instruction);
334-
auto *analysis = test.template getAnalysis<BasicCalleeAnalysis>();
335-
SmallVector<CopyValueInst *, 4> modifiedCopyValueInsts;
336-
InstructionDeleter deleter(
337-
InstModCallbacks().onDelete([&](auto *instruction) {
338-
llvm::errs() << "DELETED:\n";
339-
instruction->dump();
340-
}));
341-
auto shrunk =
342-
shrinkBorrowScope(*bbi, deleter, analysis, modifiedCopyValueInsts);
343-
unsigned index = 0;
344-
for (auto *cvi : modifiedCopyValueInsts) {
345-
auto expectedCopy = arguments.takeValue();
346-
llvm::errs() << "rewritten copy " << index << ":\n";
347-
llvm::errs() << "expected:\n";
348-
expectedCopy->print(llvm::errs());
349-
llvm::errs() << "got:\n";
350-
cvi->dump();
351-
assert(cvi == expectedCopy);
352-
++index;
353-
}
354-
assert(expected == shrunk && "didn't shrink expectedly!?");
355-
});
356-
357329
// Arguments:
358330
// - SILValue: phi
359331
// Dumps:

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#include "swift/SIL/OwnershipUtils.h"
2121
#include "swift/SIL/SILBasicBlock.h"
2222
#include "swift/SIL/SILInstruction.h"
23+
#include "swift/SIL/Test.h"
2324
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
2425
#include "swift/SILOptimizer/Analysis/Reachability.h"
2526
#include "swift/SILOptimizer/Analysis/VisitBarrierAccessScopes.h"
27+
#include "swift/SILOptimizer/PassManager/Transforms.h"
2628
#include "swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h"
2729
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
2830
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
@@ -490,3 +492,39 @@ bool swift::shrinkBorrowScope(
490492
calleeAnalysis);
491493
return ShrinkBorrowScope::run(context);
492494
}
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

Comments
 (0)