Skip to content

Commit eee8573

Browse files
committed
[SILOpt] Add tester for CanonicalizeOSSALifetime.
1 parent 82cd580 commit eee8573

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "swift/SIL/SILInstruction.h"
7373
#include "swift/SILOptimizer/PassManager/Passes.h"
7474
#include "swift/SILOptimizer/PassManager/Transforms.h"
75+
#include "swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h"
7576
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
7677
#include "swift/SILOptimizer/Utils/ParseTestSpecification.h"
7778
#include "llvm/ADT/StringRef.h"
@@ -175,6 +176,30 @@ struct TestSpecificationTest : UnitTest {
175176
}
176177
};
177178

179+
// Arguments:
180+
// - bool: pruneDebug
181+
// - bool: maximizeLifetimes
182+
// - SILValue: value to canonicalize
183+
// Dumps:
184+
// - function after value canonicalization
185+
struct CanonicalizeOSSALifetimeTest : UnitTest {
186+
CanonicalizeOSSALifetimeTest(UnitTestRunner *pass) : UnitTest(pass) {}
187+
void invoke(Arguments &arguments) override {
188+
auto *accessBlockAnalysis = getAnalysis<NonLocalAccessBlockAnalysis>();
189+
auto *dominanceAnalysis = getAnalysis<DominanceAnalysis>();
190+
DominanceInfo *domTree = dominanceAnalysis->get(getFunction());
191+
auto pruneDebug = arguments.takeBool();
192+
auto maximizeLifetimes = arguments.takeBool();
193+
InstructionDeleter deleter;
194+
CanonicalizeOSSALifetime canonicalizer(pruneDebug, maximizeLifetimes, accessBlockAnalysis,
195+
domTree, deleter);
196+
auto value = arguments.takeValue();
197+
canonicalizer.canonicalizeValueLifetime(value);
198+
getFunction()->dump();
199+
}
200+
};
201+
202+
/// [new_tests] Add the new UnitTest subclass below this line.
178203
/// [new_tests] Add the new UnitTest subclass above this line.
179204

180205
class UnitTestRunner : public SILFunctionTransform {
@@ -205,6 +230,8 @@ class UnitTestRunner : public SILFunctionTransform {
205230
}
206231

207232
ADD_UNIT_TEST_SUBCLASS("test-specification-parsing", TestSpecificationTest)
233+
ADD_UNIT_TEST_SUBCLASS("canonicalize-ossa-lifetime",
234+
CanonicalizeOSSALifetimeTest)
208235
/// [new_tests] Add the new mapping from string to subclass above this line.
209236

210237
#undef ADD_UNIT_TEST_SUBCLASS

test/SILOptimizer/unit_test.sil

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,26 @@ bb1:
8383
debug_value [trace] %three : $(_ : (), (_ : ()), ((), (_ : ())))
8484
return %zero : $()
8585
}
86+
87+
class C {}
88+
89+
sil [ossa] @getC : $@convention(thin) () -> @owned C
90+
sil [ossa] @borrowC : $@convention(thin) (@guaranteed C) -> ()
91+
sil [ossa] @takeC : $@convention(thin) (@owned C) -> ()
92+
93+
// CHECK-LABEL: begin running test 1 of 1 on fn: canonicalize-ossa-lifetime with: true, true, @trace
94+
// CHECK-LABEL: end running test 1 of 1 on fn: canonicalize-ossa-lifetime with: true, true, @trace
95+
sil [ossa] @fn : $@convention(thin) () -> () {
96+
entry:
97+
test_specification "canonicalize-ossa-lifetime true true @trace"
98+
%getC = function_ref @getC : $@convention(thin) () -> @owned C
99+
%c = apply %getC() : $@convention(thin) () -> @owned C
100+
debug_value [trace] %c : $C
101+
%borrowC = function_ref @borrowC : $@convention(thin) (@guaranteed C) -> ()
102+
apply %borrowC(%c) : $@convention(thin) (@guaranteed C) -> ()
103+
debug_value %c : $C
104+
destroy_value %c : $C
105+
%retval = tuple ()
106+
return %retval : $()
107+
}
108+

0 commit comments

Comments
 (0)