Skip to content

Fix a bug related to the opened archetypes tracking #4519

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
Aug 26, 2016
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: 13 additions & 0 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class SILBuilder {
void setInsertionPoint(SILBasicBlock *BB, SILBasicBlock::iterator InsertPt) {
this->BB = BB;
this->InsertPt = InsertPt;
if (InsertPt == BB->end())
return;
// Set the opened archetype context from the instruction.
this->getOpenedArchetypes().addOpenedArchetypeOperands(
InsertPt->getTypeDependentOperands());
}

/// setInsertionPoint - Set the insertion point to insert before the specified
Expand Down Expand Up @@ -177,6 +182,14 @@ class SILBuilder {
return InsertedInstrs;
}

//===--------------------------------------------------------------------===//
// Opened archetypes handling
//===--------------------------------------------------------------------===//
void addOpenedArchetypeOperands(SILInstruction *I) {
getOpenedArchetypes().addOpenedArchetypeOperands(
I->getTypeDependentOperands());
}

//===--------------------------------------------------------------------===//
// Type remapping
//===--------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ SILCombiner::createApplyWithConcreteType(FullApplySite AI,

FullApplySite NewAI;
Builder.setCurrentDebugScope(AI.getDebugScope());
Builder.addOpenedArchetypeOperands(AI.getInstruction());

if (auto *TAI = dyn_cast<TryApplyInst>(AI))
NewAI = Builder.createTryApply(AI.getLoc(), AI.getCallee(),
Expand Down Expand Up @@ -1156,6 +1157,7 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
// The type of the substitution is the source type of the thin to thick
// instruction.
SILType substTy = TTTFI->getOperand()->getType();
Builder.addOpenedArchetypeOperands(AI);
auto *NewAI = Builder.createApply(AI->getLoc(), TTTFI->getOperand(),
substTy, AI->getType(),
AI->getSubstitutions(), Arguments,
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ STATISTIC(NumTargetsPredicted, "Number of monomorphic functions predicted");
static FullApplySite CloneApply(FullApplySite AI, SILBuilder &Builder) {
// Clone the Apply.
Builder.setCurrentDebugScope(AI.getDebugScope());
Builder.addOpenedArchetypeOperands(AI.getInstruction());
auto Args = AI.getArguments();
SmallVector<SILValue, 8> Ret(Args.size());
for (unsigned i = 0, e = Args.size(); i != e; ++i)
Expand Down
64 changes: 64 additions & 0 deletions test/SILOptimizer/opened_archetype_operands_tracking.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -O | %FileCheck %s

// Check some corner cases related to tracking of opened archetypes.
// For example, the compiler used to crash compiling the "process" function (rdar://28024272)

sil_stage canonical

import Builtin
import Swift
import SwiftShims

public protocol P {
}

extension P {
func invokeClosure(_ closure: () throws -> ()) rethrows
}

public func process(s: P)

sil @invokeClosure : $@convention(method) <Self where Self : P> (@owned @callee_owned () -> @error Error, @in_guaranteed Self) -> @error Error {
bb0(%0 : $@callee_owned () -> @error Error, %1 : $*Self):
strong_release %0 : $@callee_owned () -> @error Error
%5 = tuple ()
return %5 : $()
}

sil @closure : $@convention(thin) () -> () {
bb0:
%0 = tuple ()
debug_value %0 : $()
%2 = tuple ()
return %2 : $()
}

// CHECK-LABEL: sil @process
// CHECK: bb0
// CHECK-NOT: try_apply
// CHECK-NOT: unreachable
// CHECK: apply
// CHECK-NOT: unreachable
// CHECK: return
sil @process : $@convention(thin) (@in P) -> () {
bb0(%0 : $*P):
%2 = open_existential_addr %0 : $*P to $*@opened("4C22C24E-6BAA-11E6-B904-B8E856428C60") P
%3 = function_ref @invokeClosure : $@convention(method) <τ_0_0 where τ_0_0 : P> (@owned @callee_owned () -> @error Error, @in_guaranteed τ_0_0) -> @error Error
// function_ref (process(s : P) -> ()).(closure #1)
%4 = function_ref @closure : $@convention(thin) () -> ()
%5 = thin_to_thick_function %4 : $@convention(thin) () -> () to $@callee_owned () -> ()
%6 = convert_function %5 : $@callee_owned () -> () to $@callee_owned () -> @error Error
try_apply %3<@opened("4C22C24E-6BAA-11E6-B904-B8E856428C60") P>(%6, %2) : $@convention(method) <τ_0_0 where τ_0_0 : P> (@owned @callee_owned () -> @error Error, @in_guaranteed τ_0_0) -> @error Error, normal bb1, error bb2

bb1(%8 : $()):
destroy_addr %0 : $*P
%10 = tuple ()
return %10 : $()

bb2(%12 : $Error):
unreachable
}

sil_default_witness_table P {
}