Skip to content

Commit 8ac367d

Browse files
committed
TempRValueOpt: don't move end_access instructions after a terminator instruction.
When extending access scopes, prevent that we end up with an `end_access` after the block's terminator. Fixes a verifier crash. rdar://85020372
1 parent 528bd11 commit 8ac367d

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,13 @@ bool TempRValueOptPass::extendAccessScopes(
419419
return false;
420420
}
421421
}
422-
if (endAccessToMove)
422+
if (endAccessToMove) {
423+
// Don't move instructions beyond the block's terminator.
424+
if (isa<TermInst>(lastUseInst))
425+
return false;
426+
423427
endAccessToMove->moveAfter(lastUseInst);
428+
}
424429

425430
return true;
426431
}

test/SILOptimizer/temp_rvalue_opt_ossa.sil

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ struct GS<Base> {
1414
var _value: Builtin.Int64
1515
}
1616

17-
class Klass {}
17+
class Klass {
18+
@_hasStorage var i: Int
19+
init()
20+
}
1821

1922
struct Two {
2023
var a: Klass
@@ -50,6 +53,7 @@ bb0(%0 : $*Klass, %1 : $*Klass):
5053

5154
sil @throwing_function : $@convention(thin) (@in_guaranteed Klass) -> ((), @error Error)
5255
sil @use_gsbase_builtinnativeobject : $@convention(thin) (@guaranteed GS<Builtin.NativeObject>) -> ()
56+
sil [readonly] @readonly_throwing_func : $@convention(thin) (@in_guaranteed Int) -> @error Error
5357

5458
sil_global @globalString : $String
5559

@@ -976,6 +980,32 @@ bb0(%0 : $*Klass, %1 : @owned $Klass):
976980
return %9999 : $()
977981
}
978982

983+
// Just check that we don't crash here.
984+
// Currently this pattern is not optimized, but we might in future.
985+
sil [ossa] @dont_extend_access_scope_over_term_inst : $@convention(thin) (@guaranteed Klass) -> () {
986+
bb0(%0 : @guaranteed $Klass):
987+
%1 = ref_element_addr %0 : $Klass, #Klass.i
988+
%2 = begin_access [read] [dynamic] %1 : $*Int
989+
%3 = alloc_stack $Int
990+
copy_addr %2 to [initialization] %3 : $*Int
991+
end_access %2 : $*Int
992+
%6 = function_ref @readonly_throwing_func : $@convention(thin) (@in_guaranteed Int) -> @error Error
993+
try_apply %6(%3) : $@convention(thin) (@in_guaranteed Int) -> @error Error, normal bb1, error bb2
994+
bb1(%8 : $()):
995+
destroy_addr %3 : $*Int
996+
dealloc_stack %3 : $*Int
997+
br bb3
998+
bb2(%12 : @owned $Error):
999+
destroy_addr %3 : $*Int
1000+
dealloc_stack %3 : $*Int
1001+
destroy_value %12 : $Error
1002+
br bb3
1003+
bb3:
1004+
%17 = tuple ()
1005+
return %17 : $()
1006+
}
1007+
1008+
9791009
/////////////////
9801010
// Store Tests //
9811011
/////////////////

0 commit comments

Comments
 (0)