Skip to content

Commit c65c540

Browse files
authored
Merge pull request #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

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType LoadTy,
537537

538538
// If we have an available value, then we want to extract the subelement from
539539
// the borrowed aggregate before each insertion point.
540-
SILSSAUpdater Updater;
540+
SILSSAUpdater Updater(B.getModule());
541541
Updater.Initialize(LoadTy);
542542
for (auto *I : Val.getInsertionPoints()) {
543543
// Use the scope and location of the store at the insertion point.

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ static bool removeAndReleaseArray(SingleValueInstruction *NewArrayValue,
595595
return false;
596596
}
597597
// For each store location, insert releases.
598-
SILSSAUpdater SSAUp;
598+
SILSSAUpdater SSAUp(ArrayDef->getModule());
599599
ValueLifetimeAnalysis::Frontier ArrayFrontier;
600600
if (!VLA.computeFrontier(ArrayFrontier,
601601
ValueLifetimeAnalysis::UsersMustPostDomDef,

lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ void BlockState::dump(RLEContext &Ctx) {
11911191
RLEContext::RLEContext(SILFunction *F, SILPassManager *PM, AliasAnalysis *AA,
11921192
TypeExpansionAnalysis *TE, PostOrderFunctionInfo *PO,
11931193
EpilogueARCFunctionInfo *EAFI, bool disableArrayLoads)
1194-
: Fn(F), PM(PM), AA(AA), TE(TE), PO(PO), EAFI(EAFI),
1194+
: Fn(F), PM(PM), AA(AA), TE(TE), Updater(F->getModule()), PO(PO), EAFI(EAFI),
11951195
ArrayType(disableArrayLoads ?
11961196
F->getModule().getASTContext().getArrayDecl() : nullptr)
11971197
#ifndef NDEBUG

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static bool isUsedOutsideOfBlock(SILValue V, SILBasicBlock *BB) {
207207
/// Helper function to perform SSA updates in case of jump threading.
208208
void swift::updateSSAAfterCloning(BasicBlockCloner &Cloner,
209209
SILBasicBlock *SrcBB, SILBasicBlock *DestBB) {
210-
SILSSAUpdater SSAUp;
210+
SILSSAUpdater SSAUp(SrcBB->getParent()->getModule());
211211
for (auto AvailValPair : Cloner.AvailVals) {
212212
ValueBase *Inst = AvailValPair.first;
213213
if (Inst->use_empty())

lib/SILOptimizer/Utils/SILSSAUpdater.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ void SILSSAUpdater::deallocateSentinel(SILUndef *D) {
3434
AlignedFree(D);
3535
}
3636

37-
SILSSAUpdater::SILSSAUpdater(SmallVectorImpl<SILPhiArgument *> *PHIs)
37+
SILSSAUpdater::SILSSAUpdater(SILModule &M, SmallVectorImpl<SILPhiArgument *> *PHIs)
3838
: AV(nullptr), PHISentinel(nullptr, deallocateSentinel),
39-
InsertedPHIs(PHIs) {}
39+
InsertedPHIs(PHIs), M(M) {}
4040

4141
SILSSAUpdater::~SILSSAUpdater() = default;
4242

4343
void SILSSAUpdater::Initialize(SILType Ty) {
4444
ValType = Ty;
4545

4646
PHISentinel = std::unique_ptr<SILUndef, void (*)(SILUndef *)>(
47-
SILUndef::getSentinelValue(Ty, this), SILSSAUpdater::deallocateSentinel);
47+
SILUndef::getSentinelValue(Ty, M, this), SILSSAUpdater::deallocateSentinel);
4848

4949
if (!AV)
5050
AV.reset(new AvailableValsTy());

test/SIL/ownership-verifier/use_verifier.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ sil @class_method_undef_test : $@convention(thin) () -> () {
10501050
bb0:
10511051
%0 = unchecked_ref_cast undef : $Builtin.NativeObject to $SuperKlass
10521052
%1 = class_method %0 : $SuperKlass, #SuperKlass.d!1 : (SuperKlass) -> () -> (), $@convention(method) (@guaranteed SuperKlass) -> ()
1053+
destroy_value %0 : $SuperKlass
10531054
%9999 = tuple()
10541055
return %9999 : $()
10551056
}
@@ -1058,6 +1059,7 @@ sil @forwarding_instruction_undef_test : $@convention(thin) () -> () {
10581059
bb0:
10591060
%0 = unchecked_ref_cast undef : $Builtin.NativeObject to $SuperKlass
10601061
%1 = unchecked_ref_cast %0 : $SuperKlass to $Builtin.NativeObject
1062+
destroy_value %1 : $Builtin.NativeObject
10611063
%9999 = tuple()
10621064
return %9999 : $()
10631065
}

0 commit comments

Comments
 (0)