Skip to content

Commit ec0f472

Browse files
authored
Merge pull request #4519 from swiftix/master
Fix a bug related to the opened archetypes tracking
2 parents 4fa5182 + ef7f16c commit ec0f472

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ class SILBuilder {
135135
void setInsertionPoint(SILBasicBlock *BB, SILBasicBlock::iterator InsertPt) {
136136
this->BB = BB;
137137
this->InsertPt = InsertPt;
138+
if (InsertPt == BB->end())
139+
return;
140+
// Set the opened archetype context from the instruction.
141+
this->getOpenedArchetypes().addOpenedArchetypeOperands(
142+
InsertPt->getTypeDependentOperands());
138143
}
139144

140145
/// setInsertionPoint - Set the insertion point to insert before the specified
@@ -177,6 +182,14 @@ class SILBuilder {
177182
return InsertedInstrs;
178183
}
179184

185+
//===--------------------------------------------------------------------===//
186+
// Opened archetypes handling
187+
//===--------------------------------------------------------------------===//
188+
void addOpenedArchetypeOperands(SILInstruction *I) {
189+
getOpenedArchetypes().addOpenedArchetypeOperands(
190+
I->getTypeDependentOperands());
191+
}
192+
180193
//===--------------------------------------------------------------------===//
181194
// Type remapping
182195
//===--------------------------------------------------------------------===//

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ SILCombiner::createApplyWithConcreteType(FullApplySite AI,
721721

722722
FullApplySite NewAI;
723723
Builder.setCurrentDebugScope(AI.getDebugScope());
724+
Builder.addOpenedArchetypeOperands(AI.getInstruction());
724725

725726
if (auto *TAI = dyn_cast<TryApplyInst>(AI))
726727
NewAI = Builder.createTryApply(AI.getLoc(), AI.getCallee(),
@@ -1156,6 +1157,7 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
11561157
// The type of the substitution is the source type of the thin to thick
11571158
// instruction.
11581159
SILType substTy = TTTFI->getOperand()->getType();
1160+
Builder.addOpenedArchetypeOperands(AI);
11591161
auto *NewAI = Builder.createApply(AI->getLoc(), TTTFI->getOperand(),
11601162
substTy, AI->getType(),
11611163
AI->getSubstitutions(), Arguments,

lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ STATISTIC(NumTargetsPredicted, "Number of monomorphic functions predicted");
5050
static FullApplySite CloneApply(FullApplySite AI, SILBuilder &Builder) {
5151
// Clone the Apply.
5252
Builder.setCurrentDebugScope(AI.getDebugScope());
53+
Builder.addOpenedArchetypeOperands(AI.getInstruction());
5354
auto Args = AI.getArguments();
5455
SmallVector<SILValue, 8> Ret(Args.size());
5556
for (unsigned i = 0, e = Args.size(); i != e; ++i)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -O | %FileCheck %s
2+
3+
// Check some corner cases related to tracking of opened archetypes.
4+
// For example, the compiler used to crash compiling the "process" function (rdar://28024272)
5+
6+
sil_stage canonical
7+
8+
import Builtin
9+
import Swift
10+
import SwiftShims
11+
12+
public protocol P {
13+
}
14+
15+
extension P {
16+
func invokeClosure(_ closure: () throws -> ()) rethrows
17+
}
18+
19+
public func process(s: P)
20+
21+
sil @invokeClosure : $@convention(method) <Self where Self : P> (@owned @callee_owned () -> @error Error, @in_guaranteed Self) -> @error Error {
22+
bb0(%0 : $@callee_owned () -> @error Error, %1 : $*Self):
23+
strong_release %0 : $@callee_owned () -> @error Error
24+
%5 = tuple ()
25+
return %5 : $()
26+
}
27+
28+
sil @closure : $@convention(thin) () -> () {
29+
bb0:
30+
%0 = tuple ()
31+
debug_value %0 : $()
32+
%2 = tuple ()
33+
return %2 : $()
34+
}
35+
36+
// CHECK-LABEL: sil @process
37+
// CHECK: bb0
38+
// CHECK-NOT: try_apply
39+
// CHECK-NOT: unreachable
40+
// CHECK: apply
41+
// CHECK-NOT: unreachable
42+
// CHECK: return
43+
sil @process : $@convention(thin) (@in P) -> () {
44+
bb0(%0 : $*P):
45+
%2 = open_existential_addr %0 : $*P to $*@opened("4C22C24E-6BAA-11E6-B904-B8E856428C60") P
46+
%3 = function_ref @invokeClosure : $@convention(method) <τ_0_0 where τ_0_0 : P> (@owned @callee_owned () -> @error Error, @in_guaranteed τ_0_0) -> @error Error
47+
// function_ref (process(s : P) -> ()).(closure #1)
48+
%4 = function_ref @closure : $@convention(thin) () -> ()
49+
%5 = thin_to_thick_function %4 : $@convention(thin) () -> () to $@callee_owned () -> ()
50+
%6 = convert_function %5 : $@callee_owned () -> () to $@callee_owned () -> @error Error
51+
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
52+
53+
bb1(%8 : $()):
54+
destroy_addr %0 : $*P
55+
%10 = tuple ()
56+
return %10 : $()
57+
58+
bb2(%12 : $Error):
59+
unreachable
60+
}
61+
62+
sil_default_witness_table P {
63+
}
64+

0 commit comments

Comments
 (0)