Skip to content

Commit 5880196

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 5880196

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,50 @@ bb0(%0 : $*Array<MyKlass>, %1 : @owned $MyKlass):
286286
return %1 : $MyKlass
287287
}
288288

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

0 commit comments

Comments
 (0)