Skip to content

Commit 5738e1f

Browse files
authored
Merge pull request #3978 from swiftix/swift-3.0-branch
[sil-cloner] Always call doPreProcess when visiting SILInstructions.
2 parents f395524 + 122ad91 commit 5738e1f

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class SILCloner : protected SILVisitor<ImplClass> {
6666
SILBuilder &getBuilder() { return Builder; }
6767

6868
protected:
69+
void beforeVisit(ValueBase *V) {
70+
if (auto I = dyn_cast<SILInstruction>(V)) {
71+
// Update the set of available opened archetypes with the opened
72+
// archetypes used by the current instruction.
73+
doPreProcess(I);
74+
}
75+
}
76+
6977
#define VALUE(CLASS, PARENT) \
7078
void visit##CLASS(CLASS *I) { \
7179
llvm_unreachable("SILCloner visiting non-instruction?"); \
@@ -380,9 +388,6 @@ SILCloner<ImplClass>::visitSILBasicBlock(SILBasicBlock* BB) {
380388
SILFunction &F = getBuilder().getFunction();
381389
// Iterate over and visit all instructions other than the terminator to clone.
382390
for (auto I = BB->begin(), E = --BB->end(); I != E; ++I) {
383-
// Update the set of available opened archetypes with the opened archetypes
384-
// used by the current instruction.
385-
doPreProcess(&*I);
386391
asImpl().visit(&*I);
387392
}
388393
// Iterate over successors to do the depth-first search.

include/swift/SIL/SILVisitor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ class SILVisitor {
3131
public:
3232
ImplClass &asImpl() { return static_cast<ImplClass &>(*this); }
3333

34+
// Peform any required pre-processing before visiting.
35+
// Sub-classes can override it to provide their custom
36+
// pre-processing steps.
37+
void beforeVisit(ValueBase *V) {
38+
}
39+
3440
ValueRetTy visit(ValueBase *V) {
41+
asImpl().beforeVisit(V);
42+
3543
switch (V->getKind()) {
3644
#define VALUE(CLASS, PARENT) \
3745
case ValueKind::CLASS: \

test/SILOptimizer/looprotate.sil

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ import Swift
2121
// CHECK: [[STRUCT:%.*]] = struct $Int32 ({{%.*}} : $Builtin.Int32)
2222
// CHECK: return [[STRUCT]] : $Int32
2323

24+
protocol P {
25+
func boo() -> Int64
26+
}
27+
2428
class Bar {
29+
func boo() -> Int64
2530
func foo()
2631
@objc func foo_objc()
2732
}
2833

2934
sil @_TFC4main3Bar3foofS0_FT_T_ : $@convention(method) (@guaranteed Bar) -> ()
35+
sil @_TFC4main3Bar3boofS0_FT_T_ : $@convention(method) (@guaranteed Bar) -> Int64
3036
sil @_TFC4main3Bar3foo_objcfS0_FT_T_ : $@convention(objc_method) (Bar) -> ()
3137

3238
sil_vtable Bar {
39+
#Bar.boo!1: _TFC4main3Bar3boofS0_FT_T_
3340
#Bar.foo!1: _TFC4main3Bar3foofS0_FT_T_
3441
#Bar.foo_objc!1: _TFC4main3Bar3foofS0_FT_T_
3542
}
@@ -104,6 +111,45 @@ bb3 (%23 : $Builtin.Int32) :
104111
return %24 : $Int32
105112
}
106113

114+
// The following function used to crash the compiler, because loop rotate
115+
// could not properly handle the reference from witness_method to the
116+
// archetype opened by open_existential_addr
117+
// CHECK-LABEL: sil @looprotate_with_opened_archetype
118+
// CHECK: return
119+
sil @looprotate_with_opened_archetype : $@convention(thin) (Int32, @in P) -> Int32 {
120+
bb0(%0 : $Int32, %25: $*P):
121+
%1 = struct_extract %0 : $Int32, #Int32._value
122+
%2 = integer_literal $Builtin.Int32, 0
123+
%30 = alloc_box $Bool
124+
%30a = project_box %30 : $@box Bool
125+
%40 = open_existential_addr %25 : $*P to $*@opened("C22498FA-CABF-11E5-B9A9-685B35C48C83") P
126+
br bb1(%1 : $Builtin.Int32, %2 : $Builtin.Int32, %25: $*P, %30 : $@box Bool, %30a : $*Bool)
127+
128+
bb1(%4 : $Builtin.Int32, %5 : $Builtin.Int32, %26: $*P, %31 : $@box Bool, %32 : $*Bool):
129+
%111 = witness_method $@opened("C22498FA-CABF-11E5-B9A9-685B35C48C83") P, #P.boo!1, %40 : $*@opened("C22498FA-CABF-11E5-B9A9-685B35C48C83") P : $@convention(witness_method) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64
130+
%122 = apply %111<@opened("C22498FA-CABF-11E5-B9A9-685B35C48C83") P>(%40) : $@convention(witness_method) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64
131+
%6 = struct $Int32 (%5 : $Builtin.Int32)
132+
%8 = builtin "cmp_eq_Word"(%5 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1
133+
cond_br %8, bb3, bb2
134+
135+
bb2:
136+
%10 = integer_literal $Builtin.Int32, 1
137+
%12 = integer_literal $Builtin.Int1, -1
138+
%13 = builtin "sadd_with_overflow_Word"(%5 : $Builtin.Int32, %10 : $Builtin.Int32, %12 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
139+
%14 = tuple_extract %13 : $(Builtin.Int32, Builtin.Int1), 0
140+
%15 = enum $Optional<Int32>, #Optional.some!enumelt.1, %6 : $Int32
141+
%16 = unchecked_enum_data %15 : $Optional<Int32>, #Optional.some!enumelt.1
142+
%17 = struct_extract %16 : $Int32, #Int32._value
143+
%19 = integer_literal $Builtin.Int1, -1
144+
%20 = builtin "sadd_with_overflow_Word"(%4 : $Builtin.Int32, %17 : $Builtin.Int32, %19 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
145+
%21 = tuple_extract %20 : $(Builtin.Int32, Builtin.Int1), 0
146+
br bb1(%21 : $Builtin.Int32, %14 : $Builtin.Int32, %26: $*P, %31 : $@box Bool, %32 : $*Bool)
147+
148+
bb3:
149+
%23 = struct $Int32 (%4 : $Builtin.Int32)
150+
return %23 : $Int32
151+
}
152+
107153
// Don't assert on this loop. bb2 is bb1 loop's new header after rotation but
108154
// also bb2 loop's header.
109155

0 commit comments

Comments
 (0)