Skip to content

Commit 33c9688

Browse files
committed
Add ossa unit tests for SimplifyCFG::simplifySwitchEnumUnreachableBlocks
1 parent caacb83 commit 33c9688

File tree

3 files changed

+174
-6
lines changed

3 files changed

+174
-6
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,10 +1715,6 @@ static bool isOnlyUnreachable(SILBasicBlock *BB) {
17151715
/// switch_enum where all but one block consists of just an
17161716
/// "unreachable" with an unchecked_enum_data and branch.
17171717
bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) {
1718-
if (!EnableOSSARewriteTerminator && Fn.hasOwnership()) {
1719-
if (!SEI->getOperand()->getType().isTrivial(Fn))
1720-
return false;
1721-
}
17221718
auto Count = SEI->getNumCases();
17231719

17241720
SILBasicBlock *Dest = nullptr;

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ struct SimplifyCFGSimplifySwitchEnumBlock : UnitTest {
295295
}
296296
};
297297

298+
struct SimplifyCFGSimplifySwitchEnumUnreachableBlocks : UnitTest {
299+
SimplifyCFGSimplifySwitchEnumUnreachableBlocks(UnitTestRunner *pass)
300+
: UnitTest(pass) {}
301+
void invoke(Arguments &arguments) override {
302+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
303+
passToRun->injectPassManager(getPass()->getPassManager());
304+
passToRun->injectFunction(getFunction());
305+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
306+
/*EnableJumpThread=*/false)
307+
.simplifySwitchEnumUnreachableBlocks(
308+
cast<SwitchEnumInst>(arguments.takeInstruction()));
309+
}
310+
};
311+
298312
// Arguments:
299313
// - string: list of characters, each of which specifies subsequent arguments
300314
// - A: (block) argument
@@ -415,9 +429,16 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
415429
ADD_UNIT_TEST_SUBCLASS("is-deinit-barrier", IsDeinitBarrierTest)
416430
ADD_UNIT_TEST_SUBCLASS("pruned-liveness-boundary-with-list-of-last-users-insertion-points", PrunedLivenessBoundaryWithListOfLastUsersInsertionPointsTest)
417431
ADD_UNIT_TEST_SUBCLASS("shrink-borrow-scope", ShrinkBorrowScopeTest)
418-
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-canonicalize-switch-enum", SimplifyCFGCanonicalizeSwitchEnum)
432+
433+
// SimplifyCFG unit tests
434+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-canonicalize-switch-enum",
435+
SimplifyCFGCanonicalizeSwitchEnum)
419436
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-simplify-switch-enum-block",
420437
SimplifyCFGSimplifySwitchEnumBlock)
438+
ADD_UNIT_TEST_SUBCLASS(
439+
"simplify-cfg-simplify-switch-enum-unreachable-blocks",
440+
SimplifyCFGSimplifySwitchEnumUnreachableBlocks)
441+
421442
ADD_UNIT_TEST_SUBCLASS("test-specification-parsing", TestSpecificationTest)
422443
ADD_UNIT_TEST_SUBCLASS("visit-adjacent-reborrows-of-phi", VisitAdjacentReborrowsOfPhiTest)
423444
/// [new_tests] Add the new mapping from string to subclass above this line.

test/SILOptimizer/simplify_cfg_ossa_switch_enum.sil

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ bb2:
211211
br bb3(%4 : $FakeOptional<Klass>)
212212

213213
bb3(%6 :@owned $FakeOptional<Klass>):
214-
test_specification "simplify-cfg-simplify-switch-enum-block @instruction[7]"
214+
test_specification "simplify-cfg-simplify-switch-enum-block @instruction[5]"
215215
switch_enum %6 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb4, case #FakeOptional.none!enumelt: bb5
216216

217217
bb4(%8 : @owned $Klass):
@@ -226,3 +226,154 @@ bb6:
226226
return %t : $()
227227
}
228228

229+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable1 :
230+
// CHECK-NOT: switch_enum
231+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable1'
232+
sil [ossa] @test_simplify_switch_enum_unreachable1 : $@convention(thin) (@owned Klass) -> () {
233+
bb0(%0 : @owned $Klass):
234+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[1]"
235+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
236+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
237+
238+
bb2:
239+
unreachable
240+
241+
bb1(%3 : @owned $Klass):
242+
destroy_value %3 : $Klass
243+
unreachable
244+
245+
bb3:
246+
%t = tuple ()
247+
return %t : $()
248+
}
249+
250+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable2 :
251+
// CHECK-NOT: switch_enum
252+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable2'
253+
sil [ossa] @test_simplify_switch_enum_unreachable2 : $@convention(thin) (@owned Klass) -> () {
254+
bb0(%0 : @owned $Klass):
255+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[1]"
256+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
257+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
258+
259+
bb2:
260+
unreachable
261+
262+
bb1(%3 : @owned $Klass):
263+
destroy_value %3 : $Klass
264+
br bb3
265+
266+
bb3:
267+
%t = tuple ()
268+
return %t : $()
269+
}
270+
271+
272+
// This case does not get optimized because the some block has instructions other than unreachable
273+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable3 :
274+
// CHECK: switch_enum
275+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable3'
276+
sil [ossa] @test_simplify_switch_enum_unreachable3 : $@convention(thin) (@owned Klass) -> () {
277+
bb0(%0 : @owned $Klass):
278+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[1]"
279+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
280+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
281+
282+
bb2:
283+
br bb3
284+
285+
bb1(%3 : @owned $Klass):
286+
destroy_value %3 : $Klass
287+
unreachable
288+
289+
bb3:
290+
%t = tuple ()
291+
return %t : $()
292+
}
293+
294+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable4 :
295+
// CHECK-NOT: switch_enum
296+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable4'
297+
sil [ossa] @test_simplify_switch_enum_unreachable4 : $@convention(thin) (@guaranteed Klass) -> () {
298+
bb0(%0 : @guaranteed $Klass):
299+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[1]"
300+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
301+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
302+
303+
bb2:
304+
unreachable
305+
306+
bb1(%3 : @guaranteed $Klass):
307+
unreachable
308+
309+
bb3:
310+
%t = tuple ()
311+
return %t : $()
312+
}
313+
314+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable5 :
315+
// CHECK-NOT: switch_enum
316+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable5'
317+
sil [ossa] @test_simplify_switch_enum_unreachable5 : $@convention(thin) (@owned Klass) -> () {
318+
bb0(%0 : @owned $Klass):
319+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[2]"
320+
%b = begin_borrow %0 : $Klass
321+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %b : $Klass
322+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
323+
324+
bb2:
325+
unreachable
326+
327+
bb1(%3 : @guaranteed $Klass):
328+
unreachable
329+
330+
bb3:
331+
end_borrow %b : $Klass
332+
destroy_value %0 : $Klass
333+
%t = tuple ()
334+
return %t : $()
335+
}
336+
337+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable6 :
338+
// CHECK-NOT: switch_enum
339+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable6'
340+
sil [ossa] @test_simplify_switch_enum_unreachable6 : $@convention(thin) (@guaranteed Klass) -> () {
341+
bb0(%0 : @guaranteed $Klass):
342+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[1]"
343+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
344+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
345+
346+
bb2:
347+
unreachable
348+
349+
bb1(%3 : @guaranteed $Klass):
350+
br bb3
351+
352+
bb3:
353+
%t = tuple ()
354+
return %t : $()
355+
}
356+
357+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_unreachable7 :
358+
// CHECK-NOT: switch_enum
359+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_unreachable7'
360+
sil [ossa] @test_simplify_switch_enum_unreachable7 : $@convention(thin) (@owned Klass) -> () {
361+
bb0(%0 : @owned $Klass):
362+
test_specification "simplify-cfg-simplify-switch-enum-unreachable-blocks @instruction[2]"
363+
%b = begin_borrow %0 : $Klass
364+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %b : $Klass
365+
switch_enum %1 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
366+
367+
bb2:
368+
unreachable
369+
370+
bb1(%3 : @guaranteed $Klass):
371+
br bb3
372+
373+
bb3:
374+
end_borrow %b : $Klass
375+
destroy_value %0 : $Klass
376+
%t = tuple ()
377+
return %t : $()
378+
}
379+

0 commit comments

Comments
 (0)