Skip to content

Commit 5a91d13

Browse files
committed
SIL: make verification for loadable types more accurate regarding resilience.
Allow creating instructions with types which are only loadable in resilient functions (but not in all functions).
1 parent 41e2e29 commit 5a91d13

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ class SILBuilder {
677677
!hasOwnership() && "Unqualified inst in qualified function");
678678
assert((Qualifier == LoadOwnershipQualifier::Unqualified) ||
679679
hasOwnership() && "Qualified inst in unqualified function");
680-
assert(LV->getType().isLoadableOrOpaque(getModule()));
680+
assert(isLoadableOrOpaque(LV->getType()));
681681
return insert(new (getModule())
682682
LoadInst(getSILDebugLocation(Loc), LV, Qualifier));
683683
}
@@ -696,13 +696,13 @@ class SILBuilder {
696696
/// non-address values.
697697
SILValue emitLoadValueOperation(SILLocation Loc, SILValue LV,
698698
LoadOwnershipQualifier Qualifier) {
699-
assert(LV->getType().isLoadableOrOpaque(getModule()));
699+
assert(isLoadableOrOpaque(LV->getType()));
700700
const auto &lowering = getTypeLowering(LV->getType());
701701
return lowering.emitLoad(*this, Loc, LV, Qualifier);
702702
}
703703

704704
LoadBorrowInst *createLoadBorrow(SILLocation Loc, SILValue LV) {
705-
assert(LV->getType().isLoadableOrOpaque(getModule()));
705+
assert(isLoadableOrOpaque(LV->getType()));
706706
return insert(new (getModule())
707707
LoadBorrowInst(getSILDebugLocation(Loc), LV));
708708
}
@@ -1069,7 +1069,7 @@ class SILBuilder {
10691069
}
10701070

10711071
DestroyValueInst *createDestroyValue(SILLocation Loc, SILValue operand) {
1072-
assert(operand->getType().isLoadableOrOpaque(getModule()));
1072+
assert(isLoadableOrOpaque(operand->getType()));
10731073
return insert(new (getModule())
10741074
DestroyValueInst(getSILDebugLocation(Loc), operand));
10751075
}
@@ -1100,7 +1100,7 @@ class SILBuilder {
11001100
RetainValueInst *createRetainValue(SILLocation Loc, SILValue operand,
11011101
Atomicity atomicity) {
11021102
assert(!hasOwnership());
1103-
assert(operand->getType().isLoadableOrOpaque(getModule()));
1103+
assert(isLoadableOrOpaque(operand->getType()));
11041104
return insert(new (getModule()) RetainValueInst(getSILDebugLocation(Loc),
11051105
operand, atomicity));
11061106
}
@@ -1115,7 +1115,7 @@ class SILBuilder {
11151115
ReleaseValueInst *createReleaseValue(SILLocation Loc, SILValue operand,
11161116
Atomicity atomicity) {
11171117
assert(!hasOwnership());
1118-
assert(operand->getType().isLoadableOrOpaque(getModule()));
1118+
assert(isLoadableOrOpaque(operand->getType()));
11191119
return insert(new (getModule()) ReleaseValueInst(getSILDebugLocation(Loc),
11201120
operand, atomicity));
11211121
}
@@ -1132,7 +1132,7 @@ class SILBuilder {
11321132
SILValue operand,
11331133
Atomicity atomicity) {
11341134
assert(hasOwnership());
1135-
assert(operand->getType().isLoadableOrOpaque(getModule()));
1135+
assert(isLoadableOrOpaque(operand->getType()));
11361136
return insert(new (getModule()) UnmanagedRetainValueInst(
11371137
getSILDebugLocation(Loc), operand, atomicity));
11381138
}
@@ -1141,7 +1141,7 @@ class SILBuilder {
11411141
SILValue operand,
11421142
Atomicity atomicity) {
11431143
assert(hasOwnership());
1144-
assert(operand->getType().isLoadableOrOpaque(getModule()));
1144+
assert(isLoadableOrOpaque(operand->getType()));
11451145
return insert(new (getModule()) UnmanagedReleaseValueInst(
11461146
getSILDebugLocation(Loc), operand, atomicity));
11471147
}
@@ -1177,14 +1177,14 @@ class SILBuilder {
11771177

11781178
StructInst *createStruct(SILLocation Loc, SILType Ty,
11791179
ArrayRef<SILValue> Elements) {
1180-
assert(Ty.isLoadableOrOpaque(getModule()));
1180+
assert(isLoadableOrOpaque(Ty));
11811181
return insert(StructInst::create(getSILDebugLocation(Loc), Ty, Elements,
11821182
getModule(), hasOwnership()));
11831183
}
11841184

11851185
TupleInst *createTuple(SILLocation Loc, SILType Ty,
11861186
ArrayRef<SILValue> Elements) {
1187-
assert(Ty.isLoadableOrOpaque(getModule()));
1187+
assert(isLoadableOrOpaque(Ty));
11881188
return insert(TupleInst::create(getSILDebugLocation(Loc), Ty, Elements,
11891189
getModule(), hasOwnership()));
11901190
}
@@ -1193,21 +1193,21 @@ class SILBuilder {
11931193

11941194
EnumInst *createEnum(SILLocation Loc, SILValue Operand,
11951195
EnumElementDecl *Element, SILType Ty) {
1196-
assert(Ty.isLoadableOrOpaque(getModule()));
1196+
assert(isLoadableOrOpaque(Ty));
11971197
return insert(new (getModule()) EnumInst(getSILDebugLocation(Loc),
11981198
Operand, Element, Ty));
11991199
}
12001200

12011201
/// Inject a loadable value into the corresponding optional type.
12021202
EnumInst *createOptionalSome(SILLocation Loc, SILValue operand, SILType ty) {
1203-
assert(ty.isLoadableOrOpaque(getModule()));
1203+
assert(isLoadableOrOpaque(ty));
12041204
auto someDecl = getModule().getASTContext().getOptionalSomeDecl();
12051205
return createEnum(Loc, operand, someDecl, ty);
12061206
}
12071207

12081208
/// Create the nil value of a loadable optional type.
12091209
EnumInst *createOptionalNone(SILLocation Loc, SILType ty) {
1210-
assert(ty.isLoadableOrOpaque(getModule()));
1210+
assert(isLoadableOrOpaque(ty));
12111211
auto noneDecl = getModule().getASTContext().getOptionalNoneDecl();
12121212
return createEnum(Loc, nullptr, noneDecl, ty);
12131213
}
@@ -1224,7 +1224,7 @@ class SILBuilder {
12241224
SILValue Operand,
12251225
EnumElementDecl *Element,
12261226
SILType Ty) {
1227-
assert(Ty.isLoadableOrOpaque(getModule()));
1227+
assert(isLoadableOrOpaque(Ty));
12281228
return insert(new (getModule()) UncheckedEnumDataInst(
12291229
getSILDebugLocation(Loc), Operand, Element, Ty));
12301230
}
@@ -1264,7 +1264,7 @@ class SILBuilder {
12641264
ArrayRef<std::pair<EnumElementDecl *, SILValue>> CaseValues,
12651265
Optional<ArrayRef<ProfileCounter>> CaseCounts = None,
12661266
ProfileCounter DefaultCount = ProfileCounter()) {
1267-
assert(Ty.isLoadableOrOpaque(getModule()));
1267+
assert(isLoadableOrOpaque(Ty));
12681268
return insert(SelectEnumInst::create(
12691269
getSILDebugLocation(Loc), Operand, Ty, DefaultValue, CaseValues,
12701270
getModule(), CaseCounts, DefaultCount, hasOwnership()));
@@ -2125,6 +2125,15 @@ class SILBuilder {
21252125
#endif
21262126
}
21272127

2128+
bool isLoadableOrOpaque(SILType Ty) {
2129+
if (!F) {
2130+
// We are inserting into the static initializer of a SILGlobalVariable.
2131+
// All types used there are loadable by definition.
2132+
return true;
2133+
}
2134+
return Ty.isLoadableOrOpaque(F);
2135+
}
2136+
21282137
void appendOperandTypeName(SILType OpdTy, llvm::SmallString<16> &Name) {
21292138
if (auto BuiltinIntTy =
21302139
dyn_cast<BuiltinIntegerType>(OpdTy.getASTType())) {

lib/SIL/SILBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ SILBuilder::createClassifyBridgeObject(SILLocation Loc, SILValue value) {
120120
// Create the appropriate cast instruction based on result type.
121121
SingleValueInstruction *
122122
SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
123-
assert(Ty.isLoadableOrOpaque(getModule()));
123+
assert(isLoadableOrOpaque(Ty));
124124
if (Ty.isTrivial(getModule()))
125125
return insert(UncheckedTrivialBitCastInst::create(
126126
getSILDebugLocation(Loc), Op, Ty, getFunction(), C.OpenedArchetypes));
@@ -552,7 +552,7 @@ void SILBuilder::emitShallowDestructureAddressOperation(
552552

553553
DebugValueInst *SILBuilder::createDebugValue(SILLocation Loc, SILValue src,
554554
SILDebugVariable Var) {
555-
assert(src->getType().isLoadableOrOpaque(getModule()));
555+
assert(isLoadableOrOpaque(src->getType()));
556556
// Debug location overrides cannot apply to debug value instructions.
557557
DebugLocOverrideRAII LocOverride{*this, None};
558558
return insert(

lib/SIL/SILVerifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
15341534
void checkLoadInst(LoadInst *LI) {
15351535
require(LI->getType().isObject(), "Result of load must be an object");
15361536
require(!fnConv.useLoweredAddresses()
1537-
|| LI->getType().isLoadable(LI->getModule()),
1537+
|| LI->getType().isLoadable(LI->getFunction()),
15381538
"Load must have a loadable type");
15391539
require(LI->getOperand()->getType().isAddress(),
15401540
"Load operand must be an address");
@@ -1573,7 +1573,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
15731573
"Inst with qualified ownership in a function that is not qualified");
15741574
require(LBI->getType().isObject(), "Result of load must be an object");
15751575
require(!fnConv.useLoweredAddresses()
1576-
|| LBI->getType().isLoadable(LBI->getModule()),
1576+
|| LBI->getType().isLoadable(LBI->getFunction()),
15771577
"Load must have a loadable type");
15781578
require(LBI->getOperand()->getType().isAddress(),
15791579
"Load operand must be an address");
@@ -1691,7 +1691,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16911691
require(SI->getSrc()->getType().isObject(),
16921692
"Can't store from an address source");
16931693
require(!fnConv.useLoweredAddresses()
1694-
|| SI->getSrc()->getType().isLoadable(SI->getModule()),
1694+
|| SI->getSrc()->getType().isLoadable(SI->getFunction()),
16951695
"Can't store a non loadable type");
16961696
require(SI->getDest()->getType().isAddress(),
16971697
"Must store to an address dest");

0 commit comments

Comments
 (0)