Skip to content

Commit c6a368d

Browse files
authored
Merge pull request swiftlang#34756 from gottesmm/pr-506a6eb10f358603d6870a894a758dddd7d4ab42
[ownership] Make SILUndef always have ValueOwnershipKind::None.
2 parents c2b3be4 + 9e0b1d1 commit c6a368d

File tree

7 files changed

+31
-32
lines changed

7 files changed

+31
-32
lines changed

include/swift/SIL/SILModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class SILModule {
238238
llvm::DenseMap<Identifier, BuiltinInfo> BuiltinIDCache;
239239

240240
/// This is the set of undef values we've created, for uniquing purposes.
241-
llvm::DenseMap<std::pair<SILType, unsigned>, SILUndef *> UndefValues;
241+
llvm::DenseMap<SILType, SILUndef *> UndefValues;
242242

243243
/// The stage of processing this module is at.
244244
SILStage Stage;

include/swift/SIL/SILUndef.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,23 @@ class SILInstruction;
2323
class SILModule;
2424

2525
class SILUndef : public ValueBase {
26-
ValueOwnershipKind ownershipKind;
27-
28-
SILUndef(SILType type, ValueOwnershipKind ownershipKind);
26+
SILUndef(SILType type);
2927

3028
public:
3129
void operator=(const SILArgument &) = delete;
3230
void operator delete(void *, size_t) = delete;
3331

34-
static SILUndef *get(SILType ty, SILModule &m, ValueOwnershipKind ownershipKind);
32+
static SILUndef *get(SILType ty, SILModule &m);
3533
static SILUndef *get(SILType ty, const SILFunction &f);
3634

3735
template <class OwnerTy>
3836
static SILUndef *getSentinelValue(SILType type, OwnerTy owner) {
3937
// Ownership kind isn't used here, the value just needs to have a unique
4038
// address.
41-
return new (*owner) SILUndef(type, OwnershipKind::None);
39+
return new (*owner) SILUndef(type);
4240
}
4341

44-
ValueOwnershipKind getOwnershipKind() const { return ownershipKind; }
42+
ValueOwnershipKind getOwnershipKind() const { return OwnershipKind::None; }
4543

4644
static bool classof(const SILArgument *) = delete;
4745
static bool classof(const SILInstruction *) = delete;

lib/SIL/IR/SILUndef.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,16 @@
1515

1616
using namespace swift;
1717

18-
static ValueOwnershipKind getOwnershipKindForUndef(SILType type, const SILFunction &f) {
19-
if (!f.hasOwnership())
20-
return OwnershipKind::None;
21-
if (type.isAddress() || type.isTrivial(f))
22-
return OwnershipKind::None;
23-
return OwnershipKind::Owned;
24-
}
25-
26-
SILUndef::SILUndef(SILType type, ValueOwnershipKind ownershipKind)
27-
: ValueBase(ValueKind::SILUndef, type, IsRepresentative::Yes),
28-
ownershipKind(ownershipKind) {}
18+
SILUndef::SILUndef(SILType type)
19+
: ValueBase(ValueKind::SILUndef, type, IsRepresentative::Yes) {}
2920

30-
SILUndef *SILUndef::get(SILType ty, SILModule &m, ValueOwnershipKind ownershipKind) {
31-
SILUndef *&entry = m.UndefValues[std::make_pair(ty, unsigned(ownershipKind))];
21+
SILUndef *SILUndef::get(SILType ty, SILModule &m) {
22+
SILUndef *&entry = m.UndefValues[ty];
3223
if (entry == nullptr)
33-
entry = new (m) SILUndef(ty, ownershipKind);
24+
entry = new (m) SILUndef(ty);
3425
return entry;
3526
}
3627

3728
SILUndef *SILUndef::get(SILType ty, const SILFunction &f) {
38-
auto ownershipKind = getOwnershipKindForUndef(ty, f);
39-
return SILUndef::get(ty, f.getModule(), ownershipKind);
29+
return SILUndef::get(ty, f.getModule());
4030
}

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,8 @@ ValueOwnershipKindClassifier::visitBuiltinInst(BuiltinInst *BI) {
585585
ValueOwnershipKind SILValue::getOwnershipKind() const {
586586
// If we do not have an undef, we should always be able to get to our function
587587
// here. If we do not have ownership enabled, just return none for everything
588-
// to short circuit ownership optimizations. If we have an undef we may still
589-
// get some results that are slightly wonky but hopefully when we lower
590-
// ownership we remove that.
588+
// to short circuit ownership optimizations. Since SILUndef in either case
589+
// will be ValueOwnershipKind::None, we will not get any wonky behavior here.
591590
//
592591
// We assume that any time we are in SILBuilder and call this without having a
593592
// value in a block yet, ossa is enabled.

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ bool SILParser::parseVerbatim(StringRef name) {
499499
SILParser::~SILParser() {
500500
for (auto &Entry : ForwardRefLocalValues) {
501501
if (ValueBase *dummyVal = LocalValues[Entry.first()]) {
502-
dummyVal->replaceAllUsesWith(
503-
SILUndef::get(dummyVal->getType(), SILMod, OwnershipKind::None));
502+
dummyVal->replaceAllUsesWith(SILUndef::get(dummyVal->getType(), SILMod));
504503
SILInstruction::destroy(cast<GlobalAddrInst>(dummyVal));
505504
SILMod.deallocateInst(cast<GlobalAddrInst>(dummyVal));
506505
}

lib/Serialization/DeserializeSIL.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ SILValue SILDeserializer::getLocalValue(ValueID Id,
277277
SILType Type) {
278278
// The first two IDs are special undefined values.
279279
if (Id == 0)
280-
return SILUndef::get(Type, SILMod, OwnershipKind::None);
281-
else if (Id == 1)
282-
return SILUndef::get(Type, SILMod, OwnershipKind::Owned);
280+
return SILUndef::get(Type, SILMod);
281+
assert(Id != 1 && "This used to be for SILUndef with OwnershipKind::Owned... "
282+
"but we don't support that anymore. Make sure no one "
283+
"changes that without updating this code if needed");
283284

284285
// Check to see if this is already defined.
285286
ValueBase *Entry = LocalValues.lookup(Id);

test/SIL/ownership-verifier/undef.sil

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ struct MyInt {
1212
var i: Builtin.Int32
1313
}
1414

15+
struct MyKlassWrapper {
16+
var i: Klass
17+
}
18+
1519
// Make sure that we handle undef in an appropriate way. Do to special handling
1620
// in SIL around undef, we test this using the ownership dumper for simplicity.
1721

@@ -31,14 +35,22 @@ struct MyInt {
3135
// CHECK: Visiting: {{.*}}%4 = struct $MyInt (undef : $Builtin.Int32)
3236
// CHECK-NEXT: Ownership Constraint:
3337
// CHECK-NEXT: Op #: 0
34-
// CHECK_NEXT: Constraint: <Constraint Kind:none LifetimeConstraint:NonLifetimeEnding>
38+
// CHECK-NEXT: Constraint: <Constraint Kind:none LifetimeConstraint:NonLifetimeEnding>
39+
// CHECK-LABEL: Visiting: {{%.*}} = struct $MyKlassWrapper (undef : $Klass)
40+
// CHECK-NEXT: Ownership Constraint:
41+
// CHECK-NEXT: Op #: 0
42+
// CHECK-NEXT: Constraint: <Constraint Kind:none LifetimeConstraint:NonLifetimeEnding>
43+
// CHECK-NEXT: Results Ownership Kinds:
44+
// CHECK-NEXT: Result: {{%.*}} = struct $MyKlassWrapper (undef : $Klass)
45+
// CHECK-NEXT: Kind: none
3546
sil [ossa] @undef_addresses_have_any_ownership : $@convention(thin) () -> () {
3647
bb0:
3748
%0 = mark_uninitialized [var] undef : $*Klass
3849
%1 = mark_uninitialized [var] undef : $Klass
3950
destroy_value %1 : $Klass
4051
%2 = mark_uninitialized [var] undef : $*Builtin.Int32
4152
%3 = struct $MyInt(undef : $Builtin.Int32)
53+
%4 = struct $MyKlassWrapper(undef : $Klass)
4254
%9999 = tuple()
4355
return %9999 : $()
4456
}

0 commit comments

Comments
 (0)