Skip to content

Commit d010a09

Browse files
authored
Merge pull request #5418 from gottesmm/implement_verification_of_copyvalue_destroyvalue
2 parents 4637a53 + 1b1d90f commit d010a09

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

include/swift/SIL/InstructionUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class FunctionOwnershipEvaluator {
112112
F = NewF;
113113
HasOwnershipQualifiedInstruction = false;
114114
}
115-
bool evaluate(const SILInstruction &I);
115+
bool evaluate(SILInstruction *I);
116116
};
117117

118118
} // end namespace swift

lib/Parse/ParseSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3985,7 +3985,7 @@ bool SILParser::parseSILBasicBlock(SILBuilder &B) {
39853985
// Evaluate how the just parsed instruction effects this functions Ownership
39863986
// Qualification. For more details, see the comment on the
39873987
// FunctionOwnershipEvaluator class.
3988-
if (!OwnershipEvaluator.evaluate(*BB->rbegin()))
3988+
if (!OwnershipEvaluator.evaluate(&*BB->rbegin()))
39893989
return true;
39903990
} while (isStartOfSILInstruction());
39913991

lib/SIL/InstructionUtils.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/SIL/Projection.h"
1717
#include "swift/SIL/SILArgument.h"
1818
#include "swift/SIL/SILBasicBlock.h"
19+
#include "swift/SIL/SILVisitor.h"
1920

2021
using namespace swift;
2122

@@ -231,41 +232,49 @@ enum class OwnershipQualifiedKind {
231232
Unqualified,
232233
};
233234

234-
} // end anonymous namespace
235+
struct OwnershipQualifiedKindVisitor : SILInstructionVisitor<OwnershipQualifiedKindVisitor, OwnershipQualifiedKind> {
236+
237+
OwnershipQualifiedKind visitValueBase(ValueBase *V) {
238+
return OwnershipQualifiedKind::NotApplicable;
239+
}
235240

236-
static OwnershipQualifiedKind
237-
getOwnershipQualifiedKind(const SILInstruction &I) {
238-
switch (I.getKind()) {
239-
case ValueKind::LoadInst:
240-
if (cast<LoadInst>(I).getOwnershipQualifier() ==
241-
LoadOwnershipQualifier::Unqualified)
241+
#define QUALIFIED_INST(CLASS) \
242+
OwnershipQualifiedKind visit ## CLASS(CLASS *I) { \
243+
return OwnershipQualifiedKind::Qualified; \
244+
}
245+
QUALIFIED_INST(EndBorrowInst)
246+
QUALIFIED_INST(LoadBorrowInst)
247+
QUALIFIED_INST(CopyValueInst)
248+
QUALIFIED_INST(DestroyValueInst)
249+
#undef QUALIFIED_INST
250+
251+
OwnershipQualifiedKind visitLoadInst(LoadInst *LI) {
252+
if (LI->getOwnershipQualifier() == LoadOwnershipQualifier::Unqualified)
242253
return OwnershipQualifiedKind::Unqualified;
243254
return OwnershipQualifiedKind::Qualified;
244-
case ValueKind::StoreInst:
245-
if (cast<StoreInst>(I).getOwnershipQualifier() ==
246-
StoreOwnershipQualifier::Unqualified)
255+
}
256+
257+
OwnershipQualifiedKind visitStoreInst(StoreInst *SI) {
258+
if (SI->getOwnershipQualifier() == StoreOwnershipQualifier::Unqualified)
247259
return OwnershipQualifiedKind::Unqualified;
248260
return OwnershipQualifiedKind::Qualified;
249-
case ValueKind::LoadBorrowInst:
250-
case ValueKind::EndBorrowInst:
251-
return OwnershipQualifiedKind::Qualified;
252-
default:
253-
return OwnershipQualifiedKind::NotApplicable;
254261
}
255-
}
262+
};
263+
264+
} // end anonymous namespace
256265

257-
bool FunctionOwnershipEvaluator::evaluate(const SILInstruction &I) {
258-
assert(I.getFunction() == F.get() && "Can not evaluate function ownership "
259-
"implications of an instruction that "
260-
"does not belong to the instruction "
261-
"that we are evaluating");
266+
bool FunctionOwnershipEvaluator::evaluate(SILInstruction *I) {
267+
assert(I->getFunction() == F.get() && "Can not evaluate function ownership "
268+
"implications of an instruction that "
269+
"does not belong to the instruction "
270+
"that we are evaluating");
262271

263272
// If SIL ownership is not enabled in this module, just return true. There is
264273
// no further work to do here.
265-
if (!I.getModule().getOptions().EnableSILOwnership)
274+
if (!I->getModule().getOptions().EnableSILOwnership)
266275
return true;
267276

268-
switch (getOwnershipQualifiedKind(I)) {
277+
switch (OwnershipQualifiedKindVisitor().visit(I)) {
269278
case OwnershipQualifiedKind::Unqualified: {
270279
// If we already know that the function has unqualified ownership, just
271280
// return early.

lib/SIL/SILVerifier.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,22 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
13491349
"Source value should be an object value");
13501350
}
13511351

1352+
void checkCopyValueInst(CopyValueInst *I) {
1353+
require(I->getOperand()->getType().isObject(),
1354+
"Source value should be an object value");
1355+
requireTrueOrNone(F.hasQualifiedOwnership(),
1356+
"copy_value is only valid in functions with qualified "
1357+
"ownership");
1358+
}
1359+
1360+
void checkDestroyValueInst(DestroyValueInst *I) {
1361+
require(I->getOperand()->getType().isObject(),
1362+
"Source value should be an object value");
1363+
requireTrueOrNone(F.hasQualifiedOwnership(),
1364+
"destroy_value is only valid in functions with qualified "
1365+
"ownership");
1366+
}
1367+
13521368
void checkReleaseValueInst(ReleaseValueInst *I) {
13531369
require(I->getOperand()->getType().isObject(),
13541370
"Source value should be an object value");

lib/Serialization/DeserializeSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ bool SILDeserializer::readSILInstruction(
18921892

18931893
// Evaluate ResultVal's ownership. If we find that as a result of ResultVal,
18941894
// we are mixing qualified and unqualified ownership instructions, bail.
1895-
if (!OwnershipEvaluator.evaluate(*ResultVal))
1895+
if (!OwnershipEvaluator.evaluate(ResultVal))
18961896
return true;
18971897

18981898
if (ResultVal->hasValue()) {

0 commit comments

Comments
 (0)