Skip to content

Commit 9650767

Browse files
authored
Merge pull request #5417 from gottesmm/ownership_model_eliminator_support_for_copyvalue_destroyvalue
2 parents de69652 + 813facf commit 9650767

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

lib/SILOptimizer/Transforms/OwnershipModelEliminator.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ struct OwnershipModelEliminatorVisitor
4141
SILBuilder &B;
4242

4343
OwnershipModelEliminatorVisitor(SILBuilder &B) : B(B) {}
44+
void beforeVisit(ValueBase *V) {
45+
auto *I = cast<SILInstruction>(V);
46+
B.setInsertionPoint(I);
47+
B.setCurrentDebugScope(I->getDebugScope());
48+
}
4449

4550
bool visitValueBase(ValueBase *V) { return false; }
4651
bool visitLoadInst(LoadInst *LI);
4752
bool visitStoreInst(StoreInst *SI);
53+
bool visitCopyValueInst(CopyValueInst *CVI);
54+
bool visitDestroyValueInst(DestroyValueInst *DVI);
4855
bool visitLoadBorrowInst(LoadBorrowInst *LBI);
4956
bool visitEndBorrowInst(EndBorrowInst *EBI) {
5057
EBI->eraseFromParent();
@@ -64,8 +71,6 @@ bool OwnershipModelEliminatorVisitor::visitLoadInst(LoadInst *LI) {
6471

6572
// Otherwise, we need to break down the load inst into its unqualified
6673
// components.
67-
B.setInsertionPoint(LI);
68-
B.setCurrentDebugScope(LI->getDebugScope());
6974
auto *UnqualifiedLoad = B.createLoad(LI->getLoc(), LI->getOperand());
7075

7176
// If we have a copy, insert a retain_value. All other copies do not require
@@ -91,9 +96,6 @@ bool OwnershipModelEliminatorVisitor::visitStoreInst(StoreInst *SI) {
9196
return false;
9297

9398
// Otherwise, we need to break down the store.
94-
B.setInsertionPoint(SI);
95-
B.setCurrentDebugScope(SI->getDebugScope());
96-
9799
if (Qualifier != StoreOwnershipQualifier::Assign) {
98100
// If the ownership qualifier is not an assign, we can just emit an
99101
// unqualified store.
@@ -118,8 +120,6 @@ bool OwnershipModelEliminatorVisitor::visitStoreInst(StoreInst *SI) {
118120
bool
119121
OwnershipModelEliminatorVisitor::visitLoadBorrowInst(LoadBorrowInst *LBI) {
120122
// Break down the load borrow into an unqualified load.
121-
B.setInsertionPoint(LBI);
122-
B.setCurrentDebugScope(LBI->getDebugScope());
123123
auto *UnqualifiedLoad = B.createLoad(LBI->getLoc(), LBI->getOperand());
124124

125125
// Then remove the qualified load and use the unqualified load as the def of
@@ -129,6 +129,19 @@ OwnershipModelEliminatorVisitor::visitLoadBorrowInst(LoadBorrowInst *LBI) {
129129
return true;
130130
}
131131

132+
bool OwnershipModelEliminatorVisitor::visitCopyValueInst(CopyValueInst *CVI) {
133+
B.createRetainValue(CVI->getLoc(), CVI->getOperand(), Atomicity::Atomic);
134+
CVI->replaceAllUsesWith(CVI->getOperand());
135+
CVI->eraseFromParent();
136+
return true;
137+
}
138+
139+
bool OwnershipModelEliminatorVisitor::visitDestroyValueInst(DestroyValueInst *DVI) {
140+
B.createReleaseValue(DVI->getLoc(), DVI->getOperand(), Atomicity::Atomic);
141+
DVI->eraseFromParent();
142+
return true;
143+
}
144+
132145
//===----------------------------------------------------------------------===//
133146
// Top Level Entry Point
134147
//===----------------------------------------------------------------------===//

test/SILOptimizer/ownership_model_eliminator.sil

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ bb0(%0 : $*Builtin.NativeObject):
6363
%3 = tuple()
6464
return %3 : $()
6565
}
66+
67+
// CHECK-LABEL: sil @copy_value_destroy_value : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
68+
// CHECK: bb0([[ARG1:%.*]] : $Builtin.NativeObject):
69+
// CHECK: retain_value [[ARG1]]
70+
// CHECK: release_value [[ARG1]]
71+
// CHECK: return [[ARG1]]
72+
sil @copy_value_destroy_value : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
73+
bb0(%0 : $Builtin.NativeObject):
74+
%1 = copy_value %0 : $Builtin.NativeObject
75+
destroy_value %0 : $Builtin.NativeObject
76+
return %1 : $Builtin.NativeObject
77+
}

0 commit comments

Comments
 (0)