Skip to content

Commit 515dc39

Browse files
authored
Fix ownership SROA (#34691)
Stores to new allocations should have the same ownership qualifier as the original store
1 parent 1105531 commit 515dc39

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/SILOptimizer/Transforms/SILSROA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void SROAMemoryUseAnalyzer::chopUpAlloca(std::vector<AllocStackInst *> &Worklist
248248
for (unsigned eltNo : indices(NewAllocations)) {
249249
builder.emitStoreValueOperation(SI->getLoc(), destructured[eltNo],
250250
NewAllocations[eltNo],
251-
StoreOwnershipQualifier::Init);
251+
SI->getOwnershipQualifier());
252252
}
253253
SI->eraseFromParent();
254254
}

test/SILOptimizer/sroa_ossa.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,33 @@ bb0(%0 : @owned $NotTrivial):
496496
%6 = tuple ()
497497
return %6 : $()
498498
}
499+
500+
// CHECK-LABEL: sil [ossa] @struct_with_obj_fields_multiple_stores :
501+
// CHECK: bb0([[ARG_0:%[0-9]+]] : @owned $NotTrivial, [[ARG_1:%[0-9]+]] : @owned $NotTrivial):
502+
// CHECK: [[VAR_1:%[0-9]+]] = alloc_stack $Obj
503+
// CHECK: [[VAR_2:%[0-9]+]] = alloc_stack $Obj
504+
// CHECK: ([[VAR_4:%[0-9]+]], [[VAR_6:%[0-9]+]]) = destructure_struct [[ARG_0]]
505+
// CHECK: store [[VAR_4]] to [init] [[VAR_1]] : $*Obj
506+
// CHECK: store [[VAR_6]] to [init] [[VAR_2]] : $*Obj
507+
// CHECK: ([[VAR_4a:%[0-9]+]], [[VAR_6a:%[0-9]+]]) = destructure_struct [[ARG_1]]
508+
// Make sure we use [assign] here
509+
// CHECK: store [[VAR_4a]] to [assign] [[VAR_1]] : $*Obj
510+
// CHECK: store [[VAR_6a]] to [assign] [[VAR_2]] : $*Obj
511+
// CHECK-LABEL: } // end sil function 'struct_with_obj_fields_multiple_stores'
512+
sil [ossa] @struct_with_obj_fields_multiple_stores : $@convention(thin) (@owned NotTrivial, @owned NotTrivial) -> () {
513+
bb0(%0 : @owned $NotTrivial, %1 : @owned $NotTrivial):
514+
%stk = alloc_stack $NotTrivial
515+
store %0 to [init] %stk : $*NotTrivial
516+
%3 = struct_element_addr %stk : $*NotTrivial, #NotTrivial.y
517+
%4 = load [copy] %3 : $*Obj
518+
%2 = function_ref @use_obj : $@convention(thin) (@owned Obj) -> ()
519+
apply %2(%4) : $@convention(thin) (@owned Obj) -> ()
520+
store %1 to [assign] %stk : $*NotTrivial
521+
%4a = load [copy] %3 : $*Obj
522+
apply %2(%4a) : $@convention(thin) (@owned Obj) -> ()
523+
%5 = load [take] %stk : $*NotTrivial
524+
destroy_value %5 : $NotTrivial
525+
dealloc_stack %stk : $*NotTrivial
526+
%6 = tuple ()
527+
return %6 : $()
528+
}

0 commit comments

Comments
 (0)