Skip to content

Commit c65c540

Browse files
authored
Merge pull request swiftlang#20819 from gottesmm/pr-f46c6ad2fee1ea70f7b25a8f6a89ab7af28c1404
[ownership] Ban ValueOwnershipKind::Any in preparation for eliminated ValueOwnershipKind::Trivial
2 parents d930cbd + 6766c1f commit c65c540

File tree

16 files changed

+70
-44
lines changed

16 files changed

+70
-44
lines changed

include/swift/SIL/SILUndef.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,29 @@
1717
#include "swift/SIL/SILValue.h"
1818

1919
namespace swift {
20+
2021
class SILArgument;
2122
class SILInstruction;
2223
class SILModule;
2324

2425
class SILUndef : public ValueBase {
25-
void operator=(const SILArgument &) = delete;
26-
27-
void operator delete(void *Ptr, size_t) SWIFT_DELETE_OPERATOR_DELETED;
26+
ValueOwnershipKind ownershipKind;
2827

29-
SILUndef(SILType Ty)
30-
: ValueBase(ValueKind::SILUndef, Ty, IsRepresentative::Yes) {}
28+
SILUndef(SILType type, SILModule &m);
3129

3230
public:
31+
void operator=(const SILArgument &) = delete;
32+
void operator delete(void *, size_t) SWIFT_DELETE_OPERATOR_DELETED;
33+
34+
static SILUndef *get(SILType ty, SILModule &m);
35+
static SILUndef *get(SILType ty, SILModule *m) { return get(ty, *m); }
3336

34-
static SILUndef *get(SILType Ty, SILModule *M);
35-
static SILUndef *get(SILType Ty, SILModule &M) { return get(Ty, &M); }
37+
template <class OwnerTy>
38+
static SILUndef *getSentinelValue(SILType type, SILModule &m, OwnerTy owner) {
39+
return new (*owner) SILUndef(type, m);
40+
}
3641

37-
template<class OwnerTy>
38-
static SILUndef *getSentinelValue(SILType Ty, OwnerTy Owner) { return new (*Owner) SILUndef(Ty); }
42+
ValueOwnershipKind getOwnershipKind() const { return ownershipKind; }
3943

4044
static bool classof(const SILArgument *) = delete;
4145
static bool classof(const SILInstruction *) = delete;

include/swift/SILOptimizer/Utils/SILSSAUpdater.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ class SILSSAUpdater {
5151
// If not null updated with inserted 'phi' nodes (SILArgument).
5252
SmallVectorImpl<SILPhiArgument *> *InsertedPHIs;
5353

54+
SILModule &M;
55+
5456
// Not copyable.
5557
void operator=(const SILSSAUpdater &) = delete;
5658
SILSSAUpdater(const SILSSAUpdater &) = delete;
5759

5860
public:
5961
explicit SILSSAUpdater(
62+
SILModule &M,
6063
SmallVectorImpl<SILPhiArgument *> *InsertedPHIs = nullptr);
6164
~SILSSAUpdater();
6265

lib/SIL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ add_swift_host_library(swiftSIL STATIC
3737
SILProfiler.cpp
3838
SILSuccessor.cpp
3939
SILType.cpp
40+
SILUndef.cpp
4041
SILValue.cpp
4142
SILVerifier.cpp
4243
SILOwnershipVerifier.cpp

lib/SIL/SIL.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@
3030

3131
using namespace swift;
3232

33-
SILUndef *SILUndef::get(SILType Ty, SILModule *M) {
34-
// Unique these.
35-
SILUndef *&Entry = M->UndefValues[Ty];
36-
if (Entry == nullptr)
37-
Entry = new (*M) SILUndef(Ty);
38-
return Entry;
39-
}
40-
4133
FormalLinkage swift::getDeclLinkage(const ValueDecl *D) {
4234
const DeclContext *fileContext = D->getDeclContext()->getModuleScopeContext();
4335

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,6 @@ void SILInstruction::verifyOperandOwnership() const {
669669
continue;
670670
SILValue opValue = op.get();
671671

672-
// Skip any SILUndef that we see.
673-
if (isa<SILUndef>(opValue))
674-
continue;
675672
auto operandOwnershipKindMap = op.getOwnershipKindMap();
676673
auto valueOwnershipKind = opValue.getOwnershipKind();
677674
if (operandOwnershipKindMap.canAcceptKind(valueOwnershipKind))
@@ -702,11 +699,6 @@ void SILValue::verifyOwnership(SILModule &mod,
702699
if (DisableOwnershipVerification)
703700
return;
704701

705-
// If we are SILUndef, just bail. SILUndef can pair with anything. Any uses of
706-
// the SILUndef will make sure that the matching checks out.
707-
if (isa<SILUndef>(*this))
708-
return;
709-
710702
// Since we do not have SILUndef, we now know that getFunction() should return
711703
// a real function. Assert in case this assumption is no longer true.
712704
SILFunction *f = (*this)->getFunction();
@@ -717,6 +709,8 @@ void SILValue::verifyOwnership(SILModule &mod,
717709
if (!f->hasQualifiedOwnership() || !f->shouldVerifyOwnership())
718710
return;
719711

712+
assert(getOwnershipKind() != ValueOwnershipKind::Any &&
713+
"No values should have any ownership anymore");
720714
ErrorBehaviorKind errorBehavior;
721715
if (IsSILOwnershipVerifierTestingEnabled) {
722716
errorBehavior = ErrorBehaviorKind::PrintMessageAndReturnFalse;
@@ -743,11 +737,6 @@ bool OwnershipChecker::checkValue(SILValue value) {
743737
lifetimeEndingUsers.clear();
744738
liveBlocks.clear();
745739

746-
// If we are SILUndef, just bail. SILUndef can pair with anything. Any uses of
747-
// the SILUndef will make sure that the matching checks out.
748-
if (isa<SILUndef>(value))
749-
return false;
750-
751740
// Since we do not have SILUndef, we now know that getFunction() should return
752741
// a real function. Assert in case this assumption is no longer true.
753742
SILFunction *f = value->getFunction();

lib/SIL/SILUndef.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- SILUndef.cpp -----------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/SIL/SILUndef.h"
14+
#include "swift/SIL/SILModule.h"
15+
16+
using namespace swift;
17+
18+
static ValueOwnershipKind getOwnershipKindForUndef(SILType type, SILModule &m) {
19+
if (type.isTrivial(m))
20+
return ValueOwnershipKind::Trivial;
21+
return ValueOwnershipKind::Owned;
22+
}
23+
24+
SILUndef::SILUndef(SILType type, SILModule &m)
25+
: ValueBase(ValueKind::SILUndef, type, IsRepresentative::Yes),
26+
ownershipKind(getOwnershipKindForUndef(type, m)) {}
27+
28+
SILUndef *SILUndef::get(SILType ty, SILModule &m) {
29+
// Unique these.
30+
SILUndef *&entry = m.UndefValues[ty];
31+
if (entry == nullptr)
32+
entry = new (m) SILUndef(ty, m);
33+
return entry;
34+
}

lib/SIL/ValueOwnership.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ ValueOwnershipKindClassifier::visitUncheckedOwnershipConversionInst(
280280
return I->getConversionOwnershipKind();
281281
}
282282

283-
ValueOwnershipKind ValueOwnershipKindClassifier::visitSILUndef(SILUndef *Arg) {
284-
return ValueOwnershipKind::Any;
283+
ValueOwnershipKind ValueOwnershipKindClassifier::visitSILUndef(SILUndef *arg) {
284+
return arg->getOwnershipKind();
285285
}
286286

287287
ValueOwnershipKind

lib/SILOptimizer/LoopTransforms/COWArrayOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ class RegionCloner : public SILCloner<RegionCloner> {
16191619
}
16201620

16211621
void updateSSAForm() {
1622-
SILSSAUpdater SSAUp;
1622+
SILSSAUpdater SSAUp(StartBB->getParent()->getModule());
16231623
for (auto *origBB : originalPreorderBlocks()) {
16241624
// Update outside used phi values.
16251625
for (auto *arg : origBB->getArguments())

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ rewriteNewLoopEntryCheckBlock(SILBasicBlock *Header,
174174
SILBasicBlock *EntryCheckBlock,
175175
const llvm::DenseMap<ValueBase *, SILValue> &ValueMap) {
176176
SmallVector<SILPhiArgument *, 4> InsertedPHIs;
177-
SILSSAUpdater Updater(&InsertedPHIs);
177+
SILSSAUpdater Updater(Header->getParent()->getModule(), &InsertedPHIs);
178178

179179
// Fix PHIs (incoming arguments).
180180
for (auto *Arg : Header->getArguments())

lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ void LoopCloner::collectLoopLiveOutValues(
305305
}
306306

307307
static void
308-
updateSSA(SILLoop *Loop,
308+
updateSSA(SILModule &M, SILLoop *Loop,
309309
DenseMap<SILValue, SmallVector<SILValue, 8>> &LoopLiveOutValues) {
310-
SILSSAUpdater SSAUp;
310+
SILSSAUpdater SSAUp(M);
311311
for (auto &MapEntry : LoopLiveOutValues) {
312312
// Collect out of loop uses of this value.
313313
auto OrigValue = MapEntry.first;
@@ -335,6 +335,7 @@ static bool tryToUnrollLoop(SILLoop *Loop) {
335335
auto *Preheader = Loop->getLoopPreheader();
336336
if (!Preheader)
337337
return false;
338+
SILModule &M = Preheader->getParent()->getModule();
338339

339340
auto *Latch = Loop->getLoopLatch();
340341
if (!Latch)
@@ -409,7 +410,7 @@ static bool tryToUnrollLoop(SILLoop *Loop) {
409410
}
410411

411412
// Fixup SSA form for loop values used outside the loop.
412-
updateSSA(Loop, LoopLiveOutValues);
413+
updateSSA(M, Loop, LoopLiveOutValues);
413414
return true;
414415
}
415416

0 commit comments

Comments
 (0)