Skip to content

Commit d10fdf7

Browse files
committed
[move-only] Do not treat accessing copyable fields of a moveonly address only type as consuming the address only type.
rdar://105106470
1 parent 20958c9 commit d10fdf7

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,19 @@ bool GatherUsesVisitor::visitUse(Operand *op) {
15111511
assert(op->getOperandNumber() == CopyAddrInst::Src &&
15121512
"Should have dest above in memInstMust{Rei,I}nitialize");
15131513

1514+
auto leafRange = TypeTreeLeafTypeRange::get(op->get(), getRootAddress());
1515+
if (!leafRange)
1516+
return false;
1517+
1518+
// If we have a non-move only type, just treat this as a liveness use.
1519+
if (!copyAddr->getSrc()->getType().isMoveOnly()) {
1520+
LLVM_DEBUG(llvm::dbgs()
1521+
<< "Found copy of copyable type. Treating as liveness use! "
1522+
<< *user);
1523+
useState.livenessUses.insert({user, *leafRange});
1524+
return true;
1525+
}
1526+
15141527
if (markedValue->getCheckKind() ==
15151528
MarkMustCheckInst::CheckKind::NoConsumeOrAssign) {
15161529
LLVM_DEBUG(llvm::dbgs()
@@ -1520,17 +1533,11 @@ bool GatherUsesVisitor::visitUse(Operand *op) {
15201533
return true;
15211534
}
15221535

1523-
auto leafRange = TypeTreeLeafTypeRange::get(op->get(), getRootAddress());
1524-
if (!leafRange)
1525-
return false;
1526-
15271536
// TODO: Add borrow checking here like below.
15281537

15291538
// TODO: Add destructure deinit checking here once address only checking is
15301539
// completely brought up.
15311540

1532-
// TODO: Add check here that we don't error on trivial/copyable types.
1533-
15341541
if (copyAddr->isTakeOfSrc()) {
15351542
LLVM_DEBUG(llvm::dbgs() << "Found take: " << *user);
15361543
useState.takeInsts.insert({user, *leafRange});

test/SILOptimizer/moveonly_addresschecker_diagnostics.swift

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,13 +2278,10 @@ public func addressOnlyGenericAssignToVar5Arg2<T>(_ x: borrowing AddressOnlyGene
22782278
// remove them when I fix it in the next commit.
22792279
public func addressOnlyGenericAccessAccessField<T>(_ x: borrowing AddressOnlyGeneric<T>) { // expected-error {{'x' has guaranteed ownership but was consumed}}
22802280
var x2 = x // expected-note {{consuming use here}}
2281-
// expected-error @-1 {{'x2' consumed by a use in a loop}}
2282-
// expected-error @-2 {{'x2' consumed more than once}}
22832281
x2 = AddressOnlyGeneric<T>()
2284-
borrowVal(x2.copyable) // expected-note {{consuming use here}}
2282+
borrowVal(x2.copyable)
22852283
for _ in 0..<1024 {
2286-
borrowVal(x2.copyable) // expected-note {{consuming use here}}
2287-
// expected-note @-1 {{consuming use here}}
2284+
borrowVal(x2.copyable)
22882285
}
22892286
}
22902287

@@ -2298,11 +2295,9 @@ public func addressOnlyGenericAccessAccessField2<T>(_ x: borrowing AddressOnlyGe
22982295
}
22992296

23002297
public func addressOnlyGenericAccessAccessFieldArg<T>(_ x2: inout AddressOnlyGeneric<T>) {
2301-
// expected-error @-1 {{'x2' consumed but not reinitialized before end of function}}
2302-
// expected-error @-2 {{'x2' consumed but not reinitialized before end of function}}
2303-
borrowVal(x2.copyable) // expected-note {{consuming use here}}
2298+
borrowVal(x2.copyable)
23042299
for _ in 0..<1024 {
2305-
borrowVal(x2.copyable) // expected-note {{consuming use here}}
2300+
borrowVal(x2.copyable)
23062301
}
23072302
}
23082303

@@ -2314,12 +2309,9 @@ public func addressOnlyGenericAccessAccessFieldArg2<T>(_ x2: inout AddressOnlyGe
23142309
}
23152310

23162311
public func addressOnlyGenericAccessAccessFieldArg3<T>(_ x2: consuming AddressOnlyGeneric<T>) {
2317-
// expected-error @-1 {{'x2' consumed by a use in a loop}}
2318-
// expected-error @-2 {{'x2' consumed more than once}}
2319-
borrowVal(x2.copyable) // expected-note {{consuming use here}}
2312+
borrowVal(x2.copyable)
23202313
for _ in 0..<1024 {
2321-
borrowVal(x2.copyable) // expected-note {{consuming use here}}
2322-
// expected-note @-1 {{consuming use here}}
2314+
borrowVal(x2.copyable)
23232315
}
23242316
}
23252317

@@ -2332,14 +2324,11 @@ public func addressOnlyGenericAccessAccessFieldArg4<T>(_ x2: consuming AddressOn
23322324

23332325
public func addressOnlyGenericAccessConsumeField<T>(_ x: borrowing AddressOnlyGeneric<T>) { // expected-error {{'x' has guaranteed ownership but was consumed}}
23342326
var x2 = x // expected-note {{consuming use here}}
2335-
// expected-error @-1 {{'x2' consumed by a use in a loop}}
2336-
// expected-error @-2 {{'x2' consumed more than once}}
23372327
x2 = AddressOnlyGeneric<T>()
23382328

2339-
consumeVal(x2.copyable) // expected-note {{consuming use here}}
2329+
consumeVal(x2.copyable)
23402330
for _ in 0..<1024 {
2341-
consumeVal(x2.copyable) // expected-note {{consuming use here}}
2342-
// expected-note @-1 {{consuming use here}}
2331+
consumeVal(x2.copyable)
23432332
}
23442333
}
23452334

@@ -2357,13 +2346,9 @@ public func addressOnlyGenericAccessConsumeField2<T>(_ x: borrowing AddressOnlyG
23572346
}
23582347

23592348
public func addressOnlyGenericAccessConsumeFieldArg<T>(_ x2: inout AddressOnlyGeneric<T>) {
2360-
// expected-error @-1 {{'x2' consumed but not reinitialized before end of function}}
2361-
// expected-error @-2 {{'x2' consumed but not reinitialized before end of function}}
2362-
2363-
consumeVal(x2.copyable) // expected-note {{consuming use here}}
2364-
2349+
consumeVal(x2.copyable)
23652350
for _ in 0..<1024 {
2366-
consumeVal(x2.copyable) // expected-note {{consuming use here}}
2351+
consumeVal(x2.copyable)
23672352
}
23682353
}
23692354

@@ -2378,14 +2363,10 @@ public func addressOnlyGenericAccessConsumeFieldArg2<T>(_ x2: inout AddressOnlyG
23782363
}
23792364

23802365
public func addressOnlyGenericAccessConsumeFieldArg3<T>(_ x2: consuming AddressOnlyGeneric<T>) {
2381-
// expected-error @-1 {{'x2' consumed by a use in a loop}}
2382-
// expected-error @-2 {{'x2' consumed more than once}}
2383-
2384-
consumeVal(x2.copyable) // expected-note {{consuming use here}}
2366+
consumeVal(x2.copyable)
23852367

23862368
for _ in 0..<1024 {
2387-
consumeVal(x2.copyable) // expected-note {{consuming use here}}
2388-
// expected-note @-1 {{consuming use here}}
2369+
consumeVal(x2.copyable)
23892370
}
23902371
}
23912372

0 commit comments

Comments
 (0)