Skip to content

Commit 2ebdb7f

Browse files
authored
Merge pull request #6724 from gottesmm/silargument_ownershipkind
SILArguments now must always have ValueOwnershipKind
2 parents 3f7fa27 + ea1f804 commit 2ebdb7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+286
-156
lines changed

include/swift/SIL/SILArgument.h

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ class SILArgument : public ValueBase {
4040

4141
SILBasicBlock *ParentBB;
4242
const ValueDecl *Decl;
43+
ValueOwnershipKind OwnershipKind;
4344

4445
public:
46+
ValueOwnershipKind getOwnershipKind() const { return OwnershipKind; }
47+
void setOwnershipKind(ValueOwnershipKind NewKind) { OwnershipKind = NewKind; }
48+
4549
SILBasicBlock *getParent() { return ParentBB; }
4650
const SILBasicBlock *getParent() const { return ParentBB; }
4751

@@ -103,28 +107,26 @@ class SILArgument : public ValueBase {
103107

104108
protected:
105109
SILArgument(ValueKind SubClassKind, SILBasicBlock *ParentBB, SILType Ty,
110+
ValueOwnershipKind OwnershipKind,
106111
const ValueDecl *D = nullptr);
107112
SILArgument(ValueKind SubClassKind, SILBasicBlock *ParentBB,
108113
SILBasicBlock::arg_iterator Pos, SILType Ty,
114+
ValueOwnershipKind OwnershipKind,
109115
const ValueDecl *D = nullptr);
110116

111117
// A special constructor, only intended for use in
112-
// SILBasicBlock::replaceBBArg.
118+
// SILBasicBlock::replacePHIArg.
113119
explicit SILArgument(ValueKind SubClassKind, SILType Ty,
120+
ValueOwnershipKind OwnershipKind,
114121
const ValueDecl *D = nullptr)
115-
: ValueBase(SubClassKind, Ty), ParentBB(nullptr), Decl(D) {}
122+
: ValueBase(SubClassKind, Ty), ParentBB(nullptr), Decl(D), OwnershipKind(OwnershipKind) {}
116123
void setParent(SILBasicBlock *P) { ParentBB = P; }
117124

118125
friend SILBasicBlock;
119126
};
120127

121128
class SILPHIArgument : public SILArgument {
122-
ValueOwnershipKind Kind;
123-
124129
public:
125-
/// Return the static ownership kind associated with this SILPHIArgument.
126-
ValueOwnershipKind getOwnershipKind() const { return Kind; }
127-
128130
/// Returns the incoming SILValue from the \p BBIndex predecessor of this
129131
/// argument's parent BB. If the routine fails, it returns an empty SILValue.
130132
/// Note that for some predecessor terminators the incoming value is not
@@ -167,20 +169,19 @@ class SILPHIArgument : public SILArgument {
167169

168170
private:
169171
friend SILBasicBlock;
170-
SILPHIArgument(SILBasicBlock *ParentBB, SILType Ty, ValueOwnershipKind Kind,
172+
SILPHIArgument(SILBasicBlock *ParentBB, SILType Ty, ValueOwnershipKind OwnershipKind,
171173
const ValueDecl *D = nullptr)
172-
: SILArgument(ValueKind::SILPHIArgument, ParentBB, Ty, D), Kind(Kind) {}
174+
: SILArgument(ValueKind::SILPHIArgument, ParentBB, Ty, OwnershipKind, D) {}
173175
SILPHIArgument(SILBasicBlock *ParentBB, SILBasicBlock::arg_iterator Pos,
174-
SILType Ty, ValueOwnershipKind Kind,
176+
SILType Ty, ValueOwnershipKind OwnershipKind,
175177
const ValueDecl *D = nullptr)
176-
: SILArgument(ValueKind::SILPHIArgument, ParentBB, Pos, Ty, D),
177-
Kind(Kind) {}
178+
: SILArgument(ValueKind::SILPHIArgument, ParentBB, Pos, Ty, OwnershipKind, D) {}
178179

179180
// A special constructor, only intended for use in
180-
// SILBasicBlock::replaceBBArg.
181-
explicit SILPHIArgument(SILType Ty, ValueOwnershipKind Kind,
181+
// SILBasicBlock::replacePHIArg.
182+
explicit SILPHIArgument(SILType Ty, ValueOwnershipKind OwnershipKind,
182183
const ValueDecl *D = nullptr)
183-
: SILArgument(ValueKind::SILPHIArgument, Ty, D), Kind(Kind) {}
184+
: SILArgument(ValueKind::SILPHIArgument, Ty, OwnershipKind, D) {}
184185
};
185186

186187
class SILFunctionArgument : public SILArgument {
@@ -226,16 +227,12 @@ class SILFunctionArgument : public SILArgument {
226227
private:
227228
friend SILBasicBlock;
228229

229-
SILFunctionArgument(SILBasicBlock *ParentBB, SILType Ty,
230+
SILFunctionArgument(SILBasicBlock *ParentBB, SILType Ty, ValueOwnershipKind OwnershipKind,
230231
const ValueDecl *D = nullptr)
231-
: SILArgument(ValueKind::SILFunctionArgument, ParentBB, Ty, D) {}
232+
: SILArgument(ValueKind::SILFunctionArgument, ParentBB, Ty, OwnershipKind, D) {}
232233
SILFunctionArgument(SILBasicBlock *ParentBB, SILBasicBlock::arg_iterator Pos,
233-
SILType Ty, const ValueDecl *D = nullptr)
234-
: SILArgument(ValueKind::SILFunctionArgument, ParentBB, Pos, Ty, D) {}
235-
236-
// A special constructor, only intended for use in SILBasicBlock::replaceBBArg.
237-
explicit SILFunctionArgument(SILType Ty, const ValueDecl *D = nullptr)
238-
: SILArgument(ValueKind::SILFunctionArgument, Ty, D) {}
234+
SILType Ty, ValueOwnershipKind OwnershipKind, const ValueDecl *D = nullptr)
235+
: SILArgument(ValueKind::SILFunctionArgument, ParentBB, Pos, Ty, OwnershipKind, D) {}
239236
};
240237

241238
//===----------------------------------------------------------------------===//

include/swift/SIL/SILBasicBlock.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,38 +180,34 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
180180
const ValueDecl *D = nullptr);
181181

182182
SILFunctionArgument *insertFunctionArgument(unsigned Index, SILType Ty,
183+
ValueOwnershipKind OwnershipKind,
183184
const ValueDecl *D = nullptr) {
184185
arg_iterator Pos = ArgumentList.begin();
185186
std::advance(Pos, Index);
186-
return insertFunctionArgument(Pos, Ty, D);
187+
return insertFunctionArgument(Pos, Ty, OwnershipKind, D);
187188
}
188189

189190
/// Replace the \p{i}th BB arg with a new BBArg with SILType \p Ty and
190191
/// ValueDecl
191192
/// \p D.
192-
SILPHIArgument *
193-
replacePHIArgument(unsigned i, SILType Ty,
194-
ValueOwnershipKind Kind = ValueOwnershipKind::Any,
195-
const ValueDecl *D = nullptr);
193+
SILPHIArgument *replacePHIArgument(unsigned i, SILType Ty,
194+
ValueOwnershipKind Kind,
195+
const ValueDecl *D = nullptr);
196196

197197
/// Allocate a new argument of type \p Ty and append it to the argument
198198
/// list. Optionally you can pass in a value decl parameter.
199-
SILPHIArgument *
200-
createPHIArgument(SILType Ty,
201-
ValueOwnershipKind Kind = ValueOwnershipKind::Any,
202-
const ValueDecl *D = nullptr);
199+
SILPHIArgument *createPHIArgument(SILType Ty, ValueOwnershipKind Kind,
200+
const ValueDecl *D = nullptr);
203201

204202
/// Insert a new SILPHIArgument with type \p Ty and \p Decl at position \p
205203
/// Pos.
206-
SILPHIArgument *
207-
insertPHIArgument(arg_iterator Pos, SILType Ty,
208-
ValueOwnershipKind Kind = ValueOwnershipKind::Any,
209-
const ValueDecl *D = nullptr);
210-
211-
SILPHIArgument *
212-
insertPHIArgument(unsigned Index, SILType Ty,
213-
ValueOwnershipKind Kind = ValueOwnershipKind::Any,
214-
const ValueDecl *D = nullptr) {
204+
SILPHIArgument *insertPHIArgument(arg_iterator Pos, SILType Ty,
205+
ValueOwnershipKind Kind,
206+
const ValueDecl *D = nullptr);
207+
208+
SILPHIArgument *insertPHIArgument(unsigned Index, SILType Ty,
209+
ValueOwnershipKind Kind,
210+
const ValueDecl *D = nullptr) {
215211
arg_iterator Pos = ArgumentList.begin();
216212
std::advance(Pos, Index);
217213
return insertPHIArgument(Pos, Ty, Kind, D);
@@ -345,6 +341,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
345341
/// Insert a new SILFunctionArgument with type \p Ty and \p Decl at position
346342
/// \p Pos.
347343
SILFunctionArgument *insertFunctionArgument(arg_iterator Pos, SILType Ty,
344+
ValueOwnershipKind OwnershipKind,
348345
const ValueDecl *D = nullptr);
349346
};
350347

include/swift/SIL/SILType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ enum class ExistentialRepresentation {
6767
};
6868

6969
/// The value category.
70-
enum class SILValueCategory {
70+
enum class SILValueCategory : uint8_t {
7171
/// An object is a value of the type.
7272
Object,
7373

include/swift/SIL/SILValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct ValueOwnershipKind {
107107
} Value;
108108

109109
ValueOwnershipKind(innerty NewValue) : Value(NewValue) {}
110+
ValueOwnershipKind(unsigned NewValue) : Value(innerty(NewValue)) {}
110111
ValueOwnershipKind(SILModule &M, SILType Type,
111112
SILArgumentConvention Convention);
112113

include/swift/SILOptimizer/Utils/FunctionSignatureOptUtils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ struct ArgumentDescriptor {
125125
size_t explosionSize = ProjTree.liveLeafCount();
126126
return explosionSize >= 1 && explosionSize <= 3;
127127
}
128+
129+
llvm::Optional<ValueOwnershipKind>
130+
getTransformedOwnershipKind(SILType SubTy) {
131+
if (IsEntirelyDead)
132+
return None;
133+
if (SubTy.isTrivial(Arg->getModule()))
134+
return Optional<ValueOwnershipKind>(ValueOwnershipKind::Trivial);
135+
if (OwnedToGuaranteed)
136+
return Optional<ValueOwnershipKind>(ValueOwnershipKind::Guaranteed);
137+
return Arg->getOwnershipKind();
138+
}
128139
};
129140

130141
/// A structure that maintains all of the information about a specific

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 305; // Last change: linkage in SILVTable::Entry
57+
const uint16_t VERSION_MINOR = 306; // Last change: SILArgument+Ownership
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;

lib/Parse/ParseSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3963,7 +3963,7 @@ bool SILParser::parseSILBasicBlock(SILBuilder &B) {
39633963
if (IsEntry)
39643964
Arg = BB->createFunctionArgument(Ty);
39653965
else
3966-
Arg = BB->createPHIArgument(Ty);
3966+
Arg = BB->createPHIArgument(Ty, ValueOwnershipKind::Any);
39673967
setLocalValue(Arg, Name, NameLoc);
39683968
} while (P.consumeIf(tok::comma));
39693969

lib/SIL/DynamicCasts.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,9 @@ namespace {
929929
objectSource = Source(sourceAddr, sourceObjectType,
930930
CastConsumptionKind::TakeAlways);
931931
} else {
932-
SILValue sourceObjectValue =
933-
someBB->createPHIArgument(loweredSourceObjectType);
932+
// switch enum always start as @owned.
933+
SILValue sourceObjectValue = someBB->createPHIArgument(
934+
loweredSourceObjectType, ValueOwnershipKind::Owned);
934935
objectSource = Source(sourceObjectValue, sourceObjectType,
935936
source.Consumption);
936937
}
@@ -968,7 +969,8 @@ namespace {
968969
if (target.isAddress()) {
969970
return target.asAddressSource();
970971
} else {
971-
SILValue result = contBB->createPHIArgument(target.LoweredType);
972+
SILValue result = contBB->createPHIArgument(target.LoweredType,
973+
ValueOwnershipKind::Owned);
972974
return target.asScalarSource(result);
973975
}
974976
}
@@ -1214,7 +1216,8 @@ emitIndirectConditionalCastWithScalar(SILBuilder &B, ModuleDecl *M,
12141216
// Emit the success block.
12151217
B.setInsertionPoint(scalarSuccBB); {
12161218
auto &targetTL = B.getModule().Types.getTypeLowering(targetValueType);
1217-
SILValue succValue = scalarSuccBB->createPHIArgument(targetValueType);
1219+
SILValue succValue = scalarSuccBB->createPHIArgument(
1220+
targetValueType, ValueOwnershipKind::Owned);
12181221
if (!shouldTakeOnSuccess(consumption))
12191222
targetTL.emitCopyValue(B, loc, succValue);
12201223
targetTL.emitStoreOfCopy(B, loc, succValue, dest, IsInitialization);

lib/SIL/SILArgument.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ using namespace swift;
2424
//===----------------------------------------------------------------------===//
2525

2626
SILArgument::SILArgument(ValueKind ChildKind, SILBasicBlock *ParentBB,
27-
SILType Ty, const ValueDecl *D)
28-
: ValueBase(ChildKind, Ty), ParentBB(ParentBB), Decl(D) {
27+
SILType Ty, ValueOwnershipKind OwnershipKind,
28+
const ValueDecl *D)
29+
: ValueBase(ChildKind, Ty), ParentBB(ParentBB), Decl(D),
30+
OwnershipKind(OwnershipKind) {
2931
ParentBB->insertArgument(ParentBB->args_end(), this);
3032
}
3133

3234
SILArgument::SILArgument(ValueKind ChildKind, SILBasicBlock *ParentBB,
3335
SILBasicBlock::arg_iterator Pos, SILType Ty,
36+
ValueOwnershipKind OwnershipKind,
3437
const ValueDecl *D)
35-
: ValueBase(ChildKind, Ty), ParentBB(ParentBB), Decl(D) {
38+
: ValueBase(ChildKind, Ty), ParentBB(ParentBB), Decl(D), OwnershipKind(OwnershipKind) {
3639
// Function arguments need to have a decl.
3740
assert(
3841
!ParentBB->getParent()->isBare() &&

lib/SIL/SILBasicBlock.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ void SILBasicBlock::cloneArgumentList(SILBasicBlock *Other) {
107107
assert(Other->isEntry() == isEntry() &&
108108
"Expected to both blocks to be entries or not");
109109
if (isEntry()) {
110+
assert(args_empty() && "Expected to have no arguments");
110111
for (auto *FuncArg : Other->getFunctionArguments()) {
111-
createFunctionArgument(FuncArg->getType(), FuncArg->getDecl());
112+
createFunctionArgument(FuncArg->getType(),
113+
FuncArg->getDecl());
112114
}
113115
return;
114116
}
@@ -122,14 +124,20 @@ void SILBasicBlock::cloneArgumentList(SILBasicBlock *Other) {
122124
SILFunctionArgument *SILBasicBlock::createFunctionArgument(SILType Ty,
123125
const ValueDecl *D) {
124126
assert(isEntry() && "Function Arguments can only be in the entry block");
125-
return new (getModule()) SILFunctionArgument(this, Ty, D);
127+
SILFunction *Parent = getParent();
128+
auto OwnershipKind = ValueOwnershipKind(
129+
Parent->getModule(), Ty,
130+
Parent->getLoweredFunctionType()->getSILArgumentConvention(
131+
getNumArguments()));
132+
return new (getModule()) SILFunctionArgument(this, Ty, OwnershipKind, D);
126133
}
127134

128135
SILFunctionArgument *SILBasicBlock::insertFunctionArgument(arg_iterator Iter,
129136
SILType Ty,
137+
ValueOwnershipKind OwnershipKind,
130138
const ValueDecl *D) {
131139
assert(isEntry() && "Function Arguments can only be in the entry block");
132-
return new (getModule()) SILFunctionArgument(this, Iter, Ty, D);
140+
return new (getModule()) SILFunctionArgument(this, Iter, Ty, OwnershipKind, D);
133141
}
134142

135143
/// Replace the ith BB argument with a new one with type Ty (and optional
@@ -139,6 +147,8 @@ SILPHIArgument *SILBasicBlock::replacePHIArgument(unsigned i, SILType Ty,
139147
const ValueDecl *D) {
140148
assert(!isEntry() && "PHI Arguments can not be in the entry block");
141149
SILModule &M = getParent()->getModule();
150+
if (Ty.isTrivial(M))
151+
Kind = ValueOwnershipKind::Trivial;
142152

143153
assert(ArgumentList[i]->use_empty() && "Expected no uses of the old BB arg!");
144154

@@ -159,13 +169,21 @@ SILPHIArgument *SILBasicBlock::createPHIArgument(SILType Ty,
159169
ValueOwnershipKind Kind,
160170
const ValueDecl *D) {
161171
assert(!isEntry() && "PHI Arguments can not be in the entry block");
172+
assert(!getParent()->hasQualifiedOwnership() ||
173+
Kind != ValueOwnershipKind::Any);
174+
if (Ty.isTrivial(getModule()))
175+
Kind = ValueOwnershipKind::Trivial;
162176
return new (getModule()) SILPHIArgument(this, Ty, Kind, D);
163177
}
164178

165179
SILPHIArgument *SILBasicBlock::insertPHIArgument(arg_iterator Iter, SILType Ty,
166180
ValueOwnershipKind Kind,
167181
const ValueDecl *D) {
168182
assert(!isEntry() && "PHI Arguments can not be in the entry block");
183+
assert(!getParent()->hasQualifiedOwnership() ||
184+
Kind != ValueOwnershipKind::Any);
185+
if (Ty.isTrivial(getModule()))
186+
Kind = ValueOwnershipKind::Trivial;
169187
return new (getModule()) SILPHIArgument(this, Iter, Ty, Kind, D);
170188
}
171189

0 commit comments

Comments
 (0)