Skip to content

Commit 1876264

Browse files
committed
Fix ArrayElementPropagation when replacing array.get_element with a non control eq value
Ex: When original value is outside the loop, and array.get_element calls are within the loop
1 parent 07eb52a commit 1876264

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/SILOptimizer/Analysis/ArraySemantic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,7 @@ bool swift::ArraySemanticsCall::replaceByValue(SILValue V) {
743743
: SemanticsCall->getIterator();
744744
assert(InsertPt.has_value());
745745

746-
SILValue CopiedVal = SILBuilderWithScope(InsertPt.value())
747-
.emitCopyValueOperation(SemanticsCall->getLoc(), V);
746+
auto CopiedVal = makeCopiedValueAvailable(V, SemanticsCall->getParentBlock());
748747
SemanticsCall->replaceAllUsesWith(CopiedVal);
749748

750749
removeCall();

test/SILOptimizer/array_element_propagation_ossa_nontrivial.sil

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,51 @@ bb0(%0 : $*Array<MyKlass>, %1 : @owned $MyKlass):
286286
return %1 : $MyKlass
287287
}
288288

289+
// CHECK-LABEL: sil [ossa] propagate_inside_loop :
290+
// CHECK: bb1:
291+
// CHECK-NOT: apply
292+
// CHECK-LABEL: | end sil function 'propagate_inside_loop'
293+
294+
sil [ossa] @propagate_inside_loop : $@convention(thin) (@owned MyKlass) -> () {
295+
bb0(%arg0 : @owned $MyKlass):
296+
%0 = function_ref @swift_bufferAllocate : $@convention(thin) () -> @owned _ContiguousArrayStorage<MyKlass>
297+
%1 = integer_literal $Builtin.Word, 1
298+
%2 = struct $MyInt (%1 : $Builtin.Word)
299+
%3 = apply %0() : $@convention(thin) () -> @owned _ContiguousArrayStorage<MyKlass>
300+
%4 = metatype $@thin MyArray<MyKlass>.Type
301+
%5 = function_ref @adoptStorage : $@convention(thin) (@owned _ContiguousArrayStorage<MyKlass>, MyInt, @thin MyArray<MyKlass>.Type) -> @owned (MyArray<MyKlass>, UnsafeMutablePointer<MyKlass>)
302+
%6 = apply %5(%3, %2, %4) : $@convention(thin) (@owned _ContiguousArrayStorage<MyKlass>, MyInt, @thin MyArray<MyKlass>.Type) -> @owned (MyArray<MyKlass>, UnsafeMutablePointer<MyKlass>)
303+
(%7, %8a) = destructure_tuple %6 : $(MyArray<MyKlass>, UnsafeMutablePointer<MyKlass>)
304+
%8 = mark_dependence %8a : $UnsafeMutablePointer<MyKlass> on %7 : $MyArray<MyKlass>
305+
%9 = struct_extract %8 : $UnsafeMutablePointer<MyKlass>, #UnsafeMutablePointer._rawValue
306+
%10 = pointer_to_address %9 : $Builtin.RawPointer to [strict] $*MyKlass
307+
%11 = integer_literal $Builtin.Word, 0
308+
%11a = struct $MyInt (%11 : $Builtin.Word)
309+
store %arg0 to [init] %10 : $*MyKlass
310+
%26 = alloc_stack $MyKlass
311+
%27 = function_ref @hoistableIsNativeTypeChecked : $@convention(method) (@guaranteed MyArray<MyKlass>) -> MyBool
312+
%28 = apply %27(%7) : $@convention(method) (@guaranteed MyArray<MyKlass>) -> MyBool
313+
%29 = function_ref @checkSubscript : $@convention(method) (MyInt, MyBool, @guaranteed MyArray<MyKlass>) -> _MyDependenceToken
314+
%30 = apply %29(%11a, %28, %7) : $@convention(method) (MyInt, MyBool, @guaranteed MyArray<MyKlass>) -> _MyDependenceToken
315+
%31 = function_ref @getElement : $@convention(method) (MyInt, MyBool, _MyDependenceToken, @guaranteed MyArray<MyKlass>) -> @owned MyKlass
316+
br bb1
317+
318+
bb1:
319+
%32 = apply %31(%11a, %28, %30, %7) : $@convention(method) (MyInt, MyBool, _MyDependenceToken, @guaranteed MyArray<MyKlass>) -> @owned MyKlass
320+
store %32 to [init] %26 : $*MyKlass
321+
destroy_addr %26 : $*MyKlass
322+
cond_br undef, bb2, bb3
323+
324+
bb2:
325+
br bb1
326+
327+
bb3:
328+
br bb4
329+
330+
bb4:
331+
dealloc_stack %26 : $*MyKlass
332+
destroy_value %7 : $MyArray<MyKlass>
333+
%52 = tuple ()
334+
return %52 : $()
335+
}
336+

0 commit comments

Comments
 (0)