Skip to content

Commit 0beffe7

Browse files
committed
[SILGen] In emitDynamicPackLoop, branch to the loop latch only if
there's a valid insertion point.
1 parent c8fe36d commit 0beffe7

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/SILGen/SILGenPack.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ void SILGenFunction::emitDynamicPackLoop(
810810
{
811811
FullExpr scope(Cleanups, CleanupLocation(loc));
812812
emitBody(curIndex, packExpansionIndex, packIndex);
813-
if (loopLatch) {
813+
if (loopLatch && B.hasValidInsertionPoint()) {
814814
B.createBranch(loc, loopLatch);
815815
}
816816
}

test/SILGen/pack_iteration.swift

+38
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ func iteratePatternMatch<each Element>(over element: repeat E<each Element>) {
149149
funcEnd()
150150
}
151151

152+
// CHECK-LABEL: sil hidden [ossa] @$s14pack_iteration19iterateTrivialBreak4overyxxQp_tRvzlF : $@convention(thin) <each Element> (@pack_guaranteed Pack{repeat each Element}) -> () {
153+
// CHECK: bb0([[PACK:%.*]] : $*Pack{repeat each Element}):
154+
// CHECK: [[IDX1:%.*]] = integer_literal $Builtin.Word, 0
155+
// CHECK: [[IDX2:%.*]] = integer_literal $Builtin.Word, 1
156+
// CHECK: [[PACK_LENGTH:%.*]] = pack_length $Pack{repeat each Element}
157+
// CHECK: br [[LOOP_DEST:bb[0-9]+]]([[IDX1]] : $Builtin.Word)
158+
//
159+
// CHECK: [[LOOP_DEST]]([[IDX3:%.*]] : $Builtin.Word):
160+
// CHECK: [[COND:%.*]] = builtin "cmp_eq_Word"([[IDX3]] : $Builtin.Word, [[PACK_LENGTH]] : $Builtin.Word) : $Builtin.Int1
161+
// CHECK: cond_br [[COND]], [[NONE_BB:bb[0-9]+]], [[SOME_BB:bb[0-9]+]]
162+
//
163+
// CHECK: [[SOME_BB]]:
164+
// CHECK: [[DYN_PACK_IDX:%.*]] = dynamic_pack_index [[IDX3]] of $Pack{repeat each Element}
165+
// CHECK: open_pack_element [[DYN_PACK_IDX]] of <each Element> at <Pack{repeat each Element}>, shape $each Element, uuid "[[UUID:.*]]"
166+
// CHECK: [[STACK:%.*]] = alloc_stack [lexical] $@pack_element("[[UUID]]") each Element, let, name "el"
167+
// CHECK: [[PACK_ELT_GET:%.*]] = pack_element_get [[DYN_PACK_IDX]] of [[PACK]] : $*Pack{repeat each Element} as $*@pack_element("[[UUID]]") each Element
168+
// CHECK: copy_addr [[PACK_ELT_GET]] to [init] [[STACK]] : $*@pack_element("[[UUID]]") each Element
169+
// CHECK: [[LOOP_END_FUNC:%.*]] = function_ref @loopBreakEnd : $@convention(thin) () -> ()
170+
// CHECK: apply [[LOOP_END_FUNC]]() : $@convention(thin) () -> ()
171+
// CHECK: destroy_addr [[STACK]] : $*@pack_element("[[UUID]]") each Element
172+
// CHECK: dealloc_stack [[STACK]] : $*@pack_element("[[UUID]]") each Element
173+
// CHECK: br [[FUNC_END_BB:bb[0-9]+]]
174+
//
175+
// CHECK: [[NONE_BB]]:
176+
// CHECK: br [[FUNC_END_BB]]
177+
//
178+
// CHECK: [[FUNC_END_BB]]
179+
// CHECK: [[FUNC_END_FUNC:%.*]] = function_ref @funcEnd : $@convention(thin) () -> ()
180+
// CHECK: apply [[FUNC_END_FUNC]]() : $@convention(thin) () -> ()
181+
// CHECK: } // end sil function '$s14pack_iteration19iterateTrivialBreak4overyxxQp_tRvzlF'
182+
func iterateTrivialBreak<each Element>(over element: repeat each Element) {
183+
for el in repeat each element {
184+
loopBreakEnd()
185+
break
186+
}
187+
funcEnd()
188+
}
189+
152190
// CHECK-LABEL: sil hidden [ossa] @$s14pack_iteration20iterateContinueBreak4overyxxQp_tRvzlF : $@convention(thin) <each Element> (@pack_guaranteed Pack{repeat each Element}) -> () {
153191
// CHECK: bb0([[PACK:%.*]] : $*Pack{repeat each Element}):
154192
// CHECK: [[IDX1:%.*]] = integer_literal $Builtin.Word, 0

0 commit comments

Comments
 (0)