Skip to content

Commit fed6145

Browse files
committed
[sil] Change all single value instructions with forwarding ownership to have static ownership.
Previously we would always calculate these instructions ownership dynamically when asked and rely on the ownership verifier to catch if we made any mistakes. Instead with this commit we move to a more static model where the ownership that these instructions can take are frozen on construction. This is a more static model that simplifies the ownership model. I also eliminated a few asserts that are enforced in other places that caused problems when parsing since we may not have a Function while Parsing (it was generally asserts if a type was trivial).
1 parent 703c571 commit fed6145

File tree

11 files changed

+370
-211
lines changed

11 files changed

+370
-211
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,19 @@ class SILBuilder {
121121
/// Reference to the provided SILBuilderContext.
122122
SILBuilderContext &C;
123123

124+
/// The SILFunction that we are currently inserting into if we have one.
125+
///
126+
/// If we are building into a block associated with a SILGlobalVariable this
127+
/// will be a nullptr.
128+
///
129+
/// TODO: This can be made cleaner by using a PointerUnion or the like so we
130+
/// can store the SILGlobalVariable here as well.
124131
SILFunction *F;
125132

133+
/// If the current block that we are inserting into must assume that
134+
/// the current context we are in has ownership.
135+
bool hasOwnership;
136+
126137
/// If this is non-null, the instruction is inserted in the specified
127138
/// basic block, at the specified InsertPt. If null, created instructions
128139
/// are not auto-inserted.
@@ -133,18 +144,20 @@ class SILBuilder {
133144

134145
public:
135146
explicit SILBuilder(SILFunction &F, bool isParsing = false)
136-
: TempContext(F.getModule()), C(TempContext), F(&F), BB(0) {
147+
: TempContext(F.getModule()), C(TempContext), F(&F),
148+
hasOwnership(F.hasQualifiedOwnership()), BB(0) {
137149
C.isParsing = isParsing;
138150
}
139151

140152
SILBuilder(SILFunction &F, SmallVectorImpl<SILInstruction *> *InsertedInstrs)
141153
: TempContext(F.getModule(), InsertedInstrs), C(TempContext), F(&F),
142-
BB(0) {}
154+
hasOwnership(F.hasQualifiedOwnership()), BB(0) {}
143155

144156
explicit SILBuilder(SILInstruction *I,
145157
SmallVectorImpl<SILInstruction *> *InsertedInstrs = 0)
146158
: TempContext(I->getFunction()->getModule(), InsertedInstrs),
147-
C(TempContext), F(I->getFunction()) {
159+
C(TempContext), F(I->getFunction()),
160+
hasOwnership(F->hasQualifiedOwnership()) {
148161
setInsertionPoint(I);
149162
}
150163

@@ -155,7 +168,8 @@ class SILBuilder {
155168
explicit SILBuilder(SILBasicBlock *BB,
156169
SmallVectorImpl<SILInstruction *> *InsertedInstrs = 0)
157170
: TempContext(BB->getParent()->getModule(), InsertedInstrs),
158-
C(TempContext), F(BB->getParent()) {
171+
C(TempContext), F(BB->getParent()),
172+
hasOwnership(F->hasQualifiedOwnership()) {
159173
setInsertionPoint(BB);
160174
}
161175

@@ -165,7 +179,8 @@ class SILBuilder {
165179
SILBuilder(SILBasicBlock *BB, SILBasicBlock::iterator InsertPt,
166180
SmallVectorImpl<SILInstruction *> *InsertedInstrs = 0)
167181
: TempContext(BB->getParent()->getModule(), InsertedInstrs),
168-
C(TempContext), F(BB->getParent()) {
182+
C(TempContext), F(BB->getParent()),
183+
hasOwnership(F->hasQualifiedOwnership()) {
169184
setInsertionPoint(BB, InsertPt);
170185
}
171186

@@ -174,7 +189,8 @@ class SILBuilder {
174189
///
175190
/// SILBuilderContext must outlive this SILBuilder instance.
176191
SILBuilder(SILInstruction *I, const SILDebugScope *DS, SILBuilderContext &C)
177-
: TempContext(C.getModule()), C(C), F(I->getFunction()) {
192+
: TempContext(C.getModule()), C(C), F(I->getFunction()),
193+
hasOwnership(F->hasQualifiedOwnership()) {
178194
assert(DS && "instruction has no debug scope");
179195
setCurrentDebugScope(DS);
180196
setInsertionPoint(I);
@@ -185,7 +201,8 @@ class SILBuilder {
185201
///
186202
/// SILBuilderContext must outlive this SILBuilder instance.
187203
SILBuilder(SILBasicBlock *BB, const SILDebugScope *DS, SILBuilderContext &C)
188-
: TempContext(C.getModule()), C(C), F(BB->getParent()) {
204+
: TempContext(C.getModule()), C(C), F(BB->getParent()),
205+
hasOwnership(F->hasQualifiedOwnership()) {
189206
assert(DS && "block has no debug scope");
190207
setCurrentDebugScope(DS);
191208
setInsertionPoint(BB);
@@ -243,6 +260,16 @@ class SILBuilder {
243260
return SILDebugLocation(overriddenLoc, Scope);
244261
}
245262

263+
/// Allow for users to override has ownership if necessary.
264+
///
265+
/// This is only used in the SILParser since it sets whether or not ownership
266+
/// is qualified after the SILBuilder is constructed due to the usage of
267+
/// AssumeUnqualifiedOwnershipWhenParsing.
268+
///
269+
/// TODO: Once we start printing [ossa] on SILFunctions to indicate ownership
270+
/// and get rid of this global option, this can go away.
271+
void setHasOwnership(bool newHasOwnership) { hasOwnership = newHasOwnership; }
272+
246273
//===--------------------------------------------------------------------===//
247274
// Insertion Point Management
248275
//===--------------------------------------------------------------------===//
@@ -1187,25 +1214,23 @@ class SILBuilder {
11871214
ObjectInst *createObject(SILLocation Loc, SILType Ty,
11881215
ArrayRef<SILValue> Elements,
11891216
unsigned NumBaseElements) {
1190-
return insert(
1191-
ObjectInst::create(getSILDebugLocation(Loc), Ty, Elements,
1192-
NumBaseElements, getModule()));
1217+
return insert(ObjectInst::create(getSILDebugLocation(Loc), Ty, Elements,
1218+
NumBaseElements, getModule(),
1219+
hasOwnership));
11931220
}
11941221

11951222
StructInst *createStruct(SILLocation Loc, SILType Ty,
11961223
ArrayRef<SILValue> Elements) {
11971224
assert(Ty.isLoadableOrOpaque(getModule()));
1198-
return insert(
1199-
StructInst::create(getSILDebugLocation(Loc), Ty, Elements,
1200-
getModule()));
1225+
return insert(StructInst::create(getSILDebugLocation(Loc), Ty, Elements,
1226+
getModule(), hasOwnership));
12011227
}
12021228

12031229
TupleInst *createTuple(SILLocation Loc, SILType Ty,
12041230
ArrayRef<SILValue> Elements) {
12051231
assert(Ty.isLoadableOrOpaque(getModule()));
1206-
return insert(
1207-
TupleInst::create(getSILDebugLocation(Loc), Ty, Elements,
1208-
getModule()));
1232+
return insert(TupleInst::create(getSILDebugLocation(Loc), Ty, Elements,
1233+
getModule(), hasOwnership));
12091234
}
12101235

12111236
TupleInst *createTuple(SILLocation loc, ArrayRef<SILValue> elts);
@@ -1286,7 +1311,7 @@ class SILBuilder {
12861311
assert(Ty.isLoadableOrOpaque(getModule()));
12871312
return insert(SelectEnumInst::create(
12881313
getSILDebugLocation(Loc), Operand, Ty, DefaultValue, CaseValues,
1289-
getFunction(), CaseCounts, DefaultCount));
1314+
getModule(), CaseCounts, DefaultCount, hasOwnership));
12901315
}
12911316

12921317
SelectEnumAddrInst *createSelectEnumAddr(
@@ -1296,15 +1321,15 @@ class SILBuilder {
12961321
ProfileCounter DefaultCount = ProfileCounter()) {
12971322
return insert(SelectEnumAddrInst::create(
12981323
getSILDebugLocation(Loc), Operand, Ty, DefaultValue, CaseValues,
1299-
getFunction(), CaseCounts, DefaultCount));
1324+
getModule(), CaseCounts, DefaultCount));
13001325
}
13011326

13021327
SelectValueInst *createSelectValue(
13031328
SILLocation Loc, SILValue Operand, SILType Ty, SILValue DefaultResult,
13041329
ArrayRef<std::pair<SILValue, SILValue>> CaseValuesAndResults) {
13051330
return insert(SelectValueInst::create(getSILDebugLocation(Loc), Operand, Ty,
13061331
DefaultResult, CaseValuesAndResults,
1307-
getFunction()));
1332+
getModule(), hasOwnership));
13081333
}
13091334

13101335
TupleExtractInst *createTupleExtract(SILLocation Loc, SILValue Operand,
@@ -1491,7 +1516,7 @@ class SILBuilder {
14911516
OpenExistentialRefInst *
14921517
createOpenExistentialRef(SILLocation Loc, SILValue Operand, SILType Ty) {
14931518
auto *I = insert(new (getModule()) OpenExistentialRefInst(
1494-
getSILDebugLocation(Loc), Operand, Ty));
1519+
getSILDebugLocation(Loc), Operand, Ty, hasOwnership));
14951520
if (C.OpenedArchetypesTracker)
14961521
C.OpenedArchetypesTracker->registerOpenedArchetypes(I);
14971522
return I;

0 commit comments

Comments
 (0)