Skip to content

Fix SILCombiner::propagateConcreteTypeOfInitExistential. #20010

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
Oct 24, 2018
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
13 changes: 10 additions & 3 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,8 @@ static bool canReplaceCopiedArg(FullApplySite Apply,
// If the witness method mutates Arg, we cannot replace Arg with
// the source of a copy. Otherwise the call would modify another value than
// the original argument.
if (Apply.getOrigCalleeType()->getParameters()[ArgIdx].isIndirectMutating())
auto origConv = Apply.getOrigCalleeConv();
if (origConv.getParamInfoForSILArg(ArgIdx).isIndirectMutating())
return false;

auto *DT = DA->get(Apply.getFunction());
Expand Down Expand Up @@ -838,9 +839,15 @@ SILInstruction *SILCombiner::createApplyWithConcreteType(
// Create the new set of arguments to apply including their substitutions.
SubstitutionMap NewCallSubs = Apply.getSubstitutionMap();
SmallVector<SILValue, 8> NewArgs;
unsigned NumApplyArgs = Apply.getNumArguments();
bool UpdatedArgs = false;
for (unsigned ArgIdx = 0; ArgIdx < NumApplyArgs; ArgIdx++) {
unsigned ArgIdx = 0;
// Push the indirect result arguments.
for (unsigned EndIdx = Apply.getSubstCalleeConv().getSILArgIndexOfFirstParam();
ArgIdx < EndIdx; ++ArgIdx) {
NewArgs.push_back(Apply.getArgument(ArgIdx));
}
// Transform the parameter arguments.
for (unsigned EndIdx = Apply.getNumArguments(); ArgIdx < EndIdx; ++ArgIdx) {
auto ArgIt = CEIs.find(ArgIdx);
if (ArgIt == CEIs.end()) {
// Use the old argument if it does not have a valid concrete existential.
Expand Down
39 changes: 39 additions & 0 deletions test/SILOptimizer/sil_combine_concrete_existential.sil
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,42 @@ bb0:
%v = tuple ()
return %v : $()
}

//===----------------------------------------------------------------------===//
// testWitnessCopiedSelfWithIndirectResult: Call to a witness method
// with an existential self that can be type-propagated. Exercise
// `SILCombiner::canReplaceArg` when `self` is not in argument position zero.
//
// rdar://45415719 Assertion failed: (Index < Length && "Invalid index!")
// ===----------------------------------------------------------------------===//

protocol AnyP {
func returnsSelf() -> Self
}

struct StructOfAnyP : AnyP {
func returnsSelf() -> StructOfAnyP
}

// CHECK-LABEL: sil @testWitnessCopiedSelfWithIndirectResult : $@convention(thin) () -> () {
// CHECK: [[IN:%[0-9]+]] = alloc_stack $StructOfAnyP
// CHECK: [[OUT:%[0-9]+]] = alloc_stack $StructOfAnyP
// CHECK: witness_method $StructOfAnyP, #AnyP.returnsSelf!1 : <Self where Self : AnyP> (Self) -> () -> @dynamic_self Self : $@convention(witness_method: AnyP) <τ_0_0 where τ_0_0 : AnyP> (@in_guaranteed τ_0_0) -> @out StructOfAnyP
// CHECK: apply %{{.*}}<StructOfAnyP>([[OUT]], [[IN]]) : $@convention(witness_method: AnyP) <τ_0_0 where τ_0_0 : AnyP> (@in_guaranteed τ_0_0) -> @out StructOfAnyP
// CHECK-LABEL: } // end sil function 'testWitnessCopiedSelfWithIndirectResult'
sil @testWitnessCopiedSelfWithIndirectResult : $() -> () {
bb0:
%a0 = alloc_stack $AnyP
%ie0 = init_existential_addr %a0 : $*AnyP, $StructOfAnyP
%a1 = alloc_stack $AnyP
copy_addr %a0 to [initialization] %a1 : $*AnyP
%o0 = open_existential_addr immutable_access %a1 : $*AnyP to $*@opened("7C4DAF8E-D722-11E8-920A-D0817AD9F6DD") AnyP
%a2 = alloc_stack $StructOfAnyP
%w0 = witness_method $@opened("7C4DAF8E-D722-11E8-920A-D0817AD9F6DD") AnyP, #AnyP.returnsSelf!1 : <Self where Self : AnyP> (Self) -> () -> @dynamic_self Self, %o0 : $*@opened("7C4DAF8E-D722-11E8-920A-D0817AD9F6DD") AnyP : $@convention(witness_method: AnyP) <τ_0_0 where τ_0_0 : AnyP> (@in_guaranteed τ_0_0) -> @out StructOfAnyP
%c0 = apply %w0<@opened("7C4DAF8E-D722-11E8-920A-D0817AD9F6DD") AnyP>(%a2, %o0) : $@convention(witness_method: AnyP) <τ_0_0 where τ_0_0 : AnyP> (@in_guaranteed τ_0_0) -> @out StructOfAnyP
dealloc_stack %a2 : $*StructOfAnyP
dealloc_stack %a1 : $*AnyP
dealloc_stack %a0 : $*AnyP
%v = tuple ()
return %v : $()
}