Skip to content

Commit ea32433

Browse files
authored
Merge pull request #59733 from eeckstein/fix-temprvalueopt-5.7
[5.7] TempRValueOpt: don't move `end_access` instructions after a terminator instruction.
2 parents 50d7916 + a68b39e commit ea32433

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ bool TempRValueOptPass::extendAccessScopes(
403403
assert(endAccess->getBeginAccess()->getAccessKind() ==
404404
SILAccessKind::Read &&
405405
"a may-write end_access should not be in the copysrc lifetime");
406+
407+
// Don't move instructions beyond the block's terminator.
408+
if (isa<TermInst>(lastUseInst))
409+
return false;
410+
406411
endAccessToMove = endAccess;
407412
}
408413
} else if (endAccessToMove) {

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)