Skip to content

TempRValueOpt: don't move end_access instructions after a terminator instruction #59721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/SILOptimizer/Transforms/TempRValueElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ bool TempRValueOptPass::extendAccessScopes(
assert(endAccess->getBeginAccess()->getAccessKind() ==
SILAccessKind::Read &&
"a may-write end_access should not be in the copysrc lifetime");

// Don't move instructions beyond the block's terminator.
if (isa<TermInst>(lastUseInst))
return false;

endAccessToMove = endAccess;
}
} else if (endAccessToMove) {
Expand Down
32 changes: 31 additions & 1 deletion test/SILOptimizer/temp_rvalue_opt_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ struct GS<Base> {
var _value: Builtin.Int64
}

class Klass {}
class Klass {
@_hasStorage var i: Int
init()
}

struct Two {
var a: Klass
Expand Down Expand Up @@ -50,6 +53,7 @@ bb0(%0 : $*Klass, %1 : $*Klass):

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

sil_global @globalString : $String

Expand Down Expand Up @@ -976,6 +980,32 @@ bb0(%0 : $*Klass, %1 : @owned $Klass):
return %9999 : $()
}

// Just check that we don't crash here.
// Currently this pattern is not optimized, but we might in future.
sil [ossa] @dont_extend_access_scope_over_term_inst : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : @guaranteed $Klass):
%1 = ref_element_addr %0 : $Klass, #Klass.i
%2 = begin_access [read] [dynamic] %1 : $*Int
%3 = alloc_stack $Int
copy_addr %2 to [initialization] %3 : $*Int
end_access %2 : $*Int
%6 = function_ref @readonly_throwing_func : $@convention(thin) (@in_guaranteed Int) -> @error Error
try_apply %6(%3) : $@convention(thin) (@in_guaranteed Int) -> @error Error, normal bb1, error bb2
bb1(%8 : $()):
destroy_addr %3 : $*Int
dealloc_stack %3 : $*Int
br bb3
bb2(%12 : @owned $Error):
destroy_addr %3 : $*Int
dealloc_stack %3 : $*Int
destroy_value %12 : $Error
br bb3
bb3:
%17 = tuple ()
return %17 : $()
}


/////////////////
// Store Tests //
/////////////////
Expand Down