Skip to content

Don't duplicate basic blocks ending in dynamic_method_br #21212

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
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
3 changes: 3 additions & 0 deletions lib/SIL/LoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ bool SILLoop::canDuplicate(SILInstruction *I) const {
if (isa<BeginAccessInst>(I))
return false;

if (isa<DynamicMethodBranchInst>(I))
return false;

assert(I->isTriviallyDuplicatable() &&
"Code here must match isTriviallyDuplicatable in SILInstruction");
return true;
Expand Down
5 changes: 5 additions & 0 deletions lib/SIL/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,11 @@ bool SILInstruction::isTriviallyDuplicatable() const {
if (isa<BeginApplyInst>(this))
return false;

// dynamic_method_br is not duplicatable because IRGen does not support phi
// nodes of objc_method type.
if (isa<DynamicMethodBranchInst>(this))
return false;

// If you add more cases here, you should also update SILLoop:canDuplicate.

return true;
Expand Down
7 changes: 7 additions & 0 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4004,6 +4004,13 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
require(!(isa<MethodInst>(branchArg) &&
cast<MethodInst>(branchArg)->getMember().isForeign),
"branch argument cannot be a witness_method or an objc method_inst");
require(!(branchArg->getType().is<SILFunctionType>() &&
branchArg->getType()
.castTo<SILFunctionType>()
->getExtInfo()
.getRepresentation() ==
SILFunctionTypeRepresentation::ObjCMethod),
"branch argument cannot be a objective-c method");
return branchArg->getType() == bbArg->getType();
}

Expand Down
43 changes: 43 additions & 0 deletions test/SILOptimizer/simplify_cfg.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3147,3 +3147,46 @@ bb6:
%rv = tuple ()
return %rv : $()
}

class X {
@objc func f() { }
}

sil @external_g : $@convention(thin) () -> ()

// Don't tail duplicate dynamic_method_br. IRGen cannot handle phi nodes of
// objc_methods.
// CHECK-LABEL: sil @dont_tail_duplicate_dynamic_method_br
// CHECK: dynamic_method_br
// CHECK-NOT: dynamic_method_br
// CHECK: return
sil @dont_tail_duplicate_dynamic_method_br : $@convention(thin) (@owned Builtin.UnknownObject, Builtin.Int1) -> () {
bb0(%x : $Builtin.UnknownObject, %b : $Builtin.Int1):
cond_br %b, bb1, bb2

bb1:
%f = function_ref @external_f : $@convention(thin) () -> ()
apply %f() : $@convention(thin) () -> ()
strong_retain %x : $Builtin.UnknownObject
br bb3(%x : $Builtin.UnknownObject)

bb2:
%g = function_ref @external_g : $@convention(thin) () -> ()
apply %g() : $@convention(thin) () -> ()
strong_retain %x : $Builtin.UnknownObject
br bb3(%x : $Builtin.UnknownObject)

bb3(%y: $Builtin.UnknownObject):
strong_release %y : $Builtin.UnknownObject
dynamic_method_br %x : $Builtin.UnknownObject, #X.f!1.foreign, bb4, bb5

bb4(%m : $@convention(objc_method) (Builtin.UnknownObject) -> ()):
br bb6

bb5:
br bb6

bb6:
%r = tuple()
return %r : $()
}