Skip to content

Commit 89a447a

Browse files
committed
[ownership] When splitting destructures, simplify the instruction if possible.
I noticed that this is needed at -Onone to maintain the same codegen output.
1 parent cec2b98 commit 89a447a

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

lib/SILOptimizer/Transforms/OwnershipModelEliminator.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,19 @@ static void splitDestructure(SILBuilder &B, SILInstruction *I, SILValue Op) {
275275
SingleValueInstruction *ProjInst =
276276
Proj.createObjectProjection(B, Loc, Op).get();
277277

278+
// If we can simplify, do so.
279+
if (SILValue NewV = simplifyInstruction(ProjInst)) {
280+
Result->replaceAllUsesWith(NewV);
281+
ProjInst->eraseFromParent();
282+
continue;
283+
}
284+
278285
Result->replaceAllUsesWith(ProjInst);
279286
}
280287

281-
I->eraseFromParent();
288+
// We may have exposed trivially dead instructions due to
289+
// simplifyInstruction... delete I and any such instructions!
290+
recursivelyDeleteTriviallyDeadInstructions(I, true);
282291
}
283292

284293
bool OwnershipModelEliminatorVisitor::visitDestructureStructInst(

test/SILOptimizer/ownership_model_eliminator.sil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,14 @@ bb0(%0 : @owned $(Builtin.NativeObject, Builtin.Int32), %1 : @owned $TestArray2)
318318
%7 = tuple (%2 : $Builtin.NativeObject, %4 : $TestArrayStorage, %6 : $TestArrayStorage)
319319
return %7 : $(Builtin.NativeObject, TestArrayStorage, TestArrayStorage)
320320
}
321+
322+
// CHECK-LABEL: sil [canonical] @test_simplify_instruction : $@convention(thin) (@owned Builtin.NativeObject, Builtin.Int32) -> @owned Builtin.NativeObject {
323+
// CHECK: bb0([[ARG:%.*]] : $Builtin.NativeObject,
324+
// CHECK-NEXT: return [[ARG]]
325+
// CHECK: } // end sil function 'test_simplify_instruction'
326+
sil [canonical] [ossa] @test_simplify_instruction : $@convention(thin) (@owned Builtin.NativeObject, Builtin.Int32) -> @owned Builtin.NativeObject {
327+
bb0(%0 : @owned $Builtin.NativeObject, %1 : $Builtin.Int32):
328+
%2 = tuple(%0 : $Builtin.NativeObject, %1 : $Builtin.Int32)
329+
(%3, %4) = destructure_tuple %2 : $(Builtin.NativeObject, Builtin.Int32)
330+
return %3 : $Builtin.NativeObject
331+
}

test/Serialization/transparent-std.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ func test_foo(x: Builtin.Int1, y: Builtin.Int1) -> Builtin.Int1 {
1616
}
1717

1818
// SIL-LABEL: sil public_external [transparent] [serialized] [canonical] @$s19def_transparent_std12assign_tuple1x1yyBi64__Bot_BptF : $@convention(thin) (Builtin.Int64, @guaranteed Builtin.NativeObject, Builtin.RawPointer) -> () {
19-
// SIL: = tuple (%0 : $Builtin.Int64, %1 : $Builtin.NativeObject)
20-
// SIL: retain_value
21-
// SIL: = tuple_extract
22-
// SIL: = tuple_extract
23-
// SIL: = pointer_to_address
24-
// SIL: = tuple
25-
// SIL: = load
19+
// SIL: bb0([[ARG0:%.*]] : ${{.*}}, [[ARG1:%.*]] : ${{.*}}, [[ARG2:%.*]] :
20+
// SIL: [[TUP:%.*]] = tuple ([[ARG0]] : $Builtin.Int64, [[ARG1]] : $Builtin.NativeObject)
21+
// SIL: retain_value [[TUP]]
22+
// SIL: [[CASTED_PTR:%.*]] = pointer_to_address [[ARG2]]
23+
// SIL: [[CASTED_PTR_0:%.*]] = tuple_element_addr [[CASTED_PTR]] : $*(Builtin.Int64, Builtin.NativeObject), 0
24+
// SIL: store [[ARG0]] to [[CASTED_PTR_0]]
25+
// SIL: [[CASTED_PTR_1:%.*]] = tuple_element_addr [[CASTED_PTR]] : $*(Builtin.Int64, Builtin.NativeObject), 1
26+
// SIL: [[OLD_VALUE:%.*]] = load [[CASTED_PTR_1]]
27+
// SIL: store [[ARG1]] to [[CASTED_PTR_1]]
28+
// SIL: strong_release [[OLD_VALUE]]
29+
// SIL: } // end sil function '$s19def_transparent_std12assign_tuple1x1yyBi64__Bot_BptF'
2630
func test_tuple(x: (Builtin.Int64, Builtin.NativeObject),
2731
y: Builtin.RawPointer) {
2832
assign_tuple(x: x, y: y)

0 commit comments

Comments
 (0)