Skip to content

Commit c791c4a

Browse files
committed
SIL: SILUndef must be aware of the resilience expansion
The ownership kind is Any for trivial types, or Owned otherwise, but whether a type is trivial or not will soon depend on the resilience expansion. This means that a SILModule now uniques two SILUndefs per type instead of one, and serialization uses two distinct sentinel IDs for this purpose as well. For now, the resilience expansion is not actually used here, so this change is NFC, other than changing the module format.
1 parent 18f2d92 commit c791c4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+129
-121
lines changed

include/swift/SIL/SILCloner.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ SILCloner<ImplClass>::getMappedValue(SILValue Value) {
526526
if (auto *U = dyn_cast<SILUndef>(Value)) {
527527
auto type = getOpType(U->getType());
528528
ValueBase *undef =
529-
(type == U->getType() ? U : SILUndef::get(type, Builder.getModule()));
529+
(type == U->getType() ? U : SILUndef::get(type, Builder.getFunction()));
530530
return SILValue(undef);
531531
}
532532

include/swift/SIL/SILModule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class SILModule {
221221
llvm::DenseMap<Identifier, BuiltinInfo> BuiltinIDCache;
222222

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

226226
/// The stage of processing this module is at.
227227
SILStage Stage;

include/swift/SIL/SILUndef.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ class SILModule;
2525
class SILUndef : public ValueBase {
2626
ValueOwnershipKind ownershipKind;
2727

28-
SILUndef(SILType type, SILModule &m);
28+
SILUndef(SILType type, ValueOwnershipKind ownershipKind);
2929

3030
public:
3131
void operator=(const SILArgument &) = delete;
3232
void operator delete(void *, size_t) SWIFT_DELETE_OPERATOR_DELETED;
3333

34-
static SILUndef *get(SILType ty, SILModule &m);
35-
static SILUndef *get(SILType ty, SILModule *m) { return get(ty, *m); }
34+
static SILUndef *get(SILType ty, SILModule &m, ValueOwnershipKind ownershipKind);
35+
static SILUndef *get(SILType ty, const SILFunction &f);
3636

3737
template <class OwnerTy>
38-
static SILUndef *getSentinelValue(SILType type, SILModule &m, OwnerTy owner) {
39-
return new (*owner) SILUndef(type, m);
38+
static SILUndef *getSentinelValue(SILType type, OwnerTy owner) {
39+
// Ownership kind isn't used here, the value just needs to have a unique
40+
// address.
41+
return new (*owner) SILUndef(type, ValueOwnershipKind::Any);
4042
}
4143

4244
ValueOwnershipKind getOwnershipKind() const { return ownershipKind; }

include/swift/SILOptimizer/Utils/SILSSAUpdater.h

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

54-
SILModule &M;
55-
5654
// Not copyable.
5755
void operator=(const SILSSAUpdater &) = delete;
5856
SILSSAUpdater(const SILSSAUpdater &) = delete;
5957

6058
public:
6159
explicit SILSSAUpdater(
62-
SILModule &M,
6360
SmallVectorImpl<SILPhiArgument *> *InsertedPHIs = nullptr);
6461
~SILSSAUpdater();
6562

include/swift/Serialization/ModuleFormat.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 476; // Last change: prebuilt module cache
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 477; // SILUndef serialized with ownership kind
5656

5757
using DeclIDField = BCFixed<31>;
5858

lib/IRGen/LoadableByAddress.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ void LoadableStorageAllocation::allocateLoadableStorage() {
13171317
SILArgument *LoadableStorageAllocation::replaceArgType(SILBuilder &argBuilder,
13181318
SILArgument *arg,
13191319
SILType newSILType) {
1320-
SILValue undef = SILUndef::get(newSILType, pass.F->getModule());
1320+
SILValue undef = SILUndef::get(newSILType, *pass.F);
13211321
SmallVector<Operand *, 8> useList(arg->use_begin(), arg->use_end());
13221322
for (auto *use : useList) {
13231323
use->set(undef);
@@ -1387,7 +1387,7 @@ void LoadableStorageAllocation::convertIndirectFunctionArgs() {
13871387

13881388
static void convertBBArgType(SILBuilder &argBuilder, SILType newSILType,
13891389
SILArgument *arg) {
1390-
SILValue undef = SILUndef::get(newSILType, argBuilder.getModule());
1390+
SILValue undef = SILUndef::get(newSILType, argBuilder.getFunction());
13911391
SmallVector<Operand *, 8> useList(arg->use_begin(), arg->use_end());
13921392
for (auto *use : useList) {
13931393
use->set(undef);

lib/ParseSIL/ParseSIL.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ bool SILParser::parseGlobalName(Identifier &Name) {
738738
SILValue SILParser::getLocalValue(UnresolvedValueName Name, SILType Type,
739739
SILLocation Loc, SILBuilder &B) {
740740
if (Name.isUndef())
741-
return SILUndef::get(Type, &SILMod);
741+
return SILUndef::get(Type, B.getFunction());
742742

743743
// Check to see if this is already defined.
744744
ValueBase *&Entry = LocalValues[Name.Name];

lib/SIL/Projection.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ ProjectionTree::computeExplodedArgumentValueInner(SILBuilder &Builder,
11531153
if (Iter != LeafValues.end())
11541154
return Iter->second;
11551155
// Return undef for dead node.
1156-
return SILUndef::get(Node->getType(), Mod);
1156+
return SILUndef::get(Node->getType(), Builder.getFunction());
11571157
}
11581158

11591159
// This is an aggregate node, construct its value from its children

lib/SIL/SILUndef.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@
1515

1616
using namespace swift;
1717

18-
static ValueOwnershipKind getOwnershipKindForUndef(SILType type, SILModule &m) {
19-
if (type.isTrivial(m))
18+
static ValueOwnershipKind getOwnershipKindForUndef(SILType type, const SILFunction &f) {
19+
if (type.isTrivial(f))
2020
return ValueOwnershipKind::Any;
2121
return ValueOwnershipKind::Owned;
2222
}
2323

24-
SILUndef::SILUndef(SILType type, SILModule &m)
24+
SILUndef::SILUndef(SILType type, ValueOwnershipKind ownershipKind)
2525
: ValueBase(ValueKind::SILUndef, type, IsRepresentative::Yes),
26-
ownershipKind(getOwnershipKindForUndef(type, m)) {}
26+
ownershipKind(ownershipKind) {}
2727

28-
SILUndef *SILUndef::get(SILType ty, SILModule &m) {
29-
// Unique these.
30-
SILUndef *&entry = m.UndefValues[ty];
28+
SILUndef *SILUndef::get(SILType ty, SILModule &m, ValueOwnershipKind ownershipKind) {
29+
SILUndef *&entry = m.UndefValues[std::make_pair(ty, unsigned(ownershipKind))];
3130
if (entry == nullptr)
32-
entry = new (m) SILUndef(ty, m);
31+
entry = new (m) SILUndef(ty, ownershipKind);
3332
return entry;
3433
}
34+
35+
SILUndef *SILUndef::get(SILType ty, const SILFunction &f) {
36+
auto ownershipKind = getOwnershipKindForUndef(ty, f);
37+
return SILUndef::get(ty, f.getModule(), ownershipKind);
38+
}

lib/SIL/SILValue.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ void ValueBase::replaceAllUsesWith(ValueBase *RHS) {
5353
}
5454

5555
void ValueBase::replaceAllUsesWithUndef() {
56-
SILModule *Mod = getModule();
57-
if (!Mod) {
56+
auto *F = getFunction();
57+
if (!F) {
5858
llvm_unreachable("replaceAllUsesWithUndef can only be used on ValueBase "
59-
"that have access to the parent module.");
59+
"that have access to the parent function.");
6060
}
6161
while (!use_empty()) {
6262
Operand *Op = *use_begin();
63-
Op->set(SILUndef::get(Op->get()->getType(), Mod));
63+
Op->set(SILUndef::get(Op->get()->getType(), *F));
6464
}
6565
}
6666

lib/SILGen/SILGenApply.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
14291429
// If the initializer is a C function imported as a member,
14301430
// there is no 'self' parameter. Mark it undef.
14311431
if (ctorRef->getDecl()->isImportAsMember()) {
1432-
self = SGF.emitUndef(expr, selfFormalType);
1432+
self = SGF.emitUndef(selfFormalType);
14331433
} else if (SGF.AllocatorMetatype) {
14341434
self = emitCorrespondingSelfValue(
14351435
ManagedValue::forUnmanaged(SGF.AllocatorMetatype), arg);

lib/SILGen/SILGenBridging.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &SGF,
681681

682682
assert(SGF.SGM.getASTContext().Diags.hadAnyError() &&
683683
"Bridging code should have complained");
684-
return SGF.emitUndef(loc, bridgedType);
684+
return SGF.emitUndef(bridgedType);
685685
}
686686

687687
// Bridge Error, or types that conform to it, to NSError.
@@ -754,7 +754,7 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &SGF,
754754
}
755755

756756
// Shouldn't get here unless the standard library is busted.
757-
return SGF.emitUndef(loc, loweredBridgedTy);
757+
return SGF.emitUndef(loweredBridgedTy);
758758
}
759759

760760
static ManagedValue emitNativeToCBridgedValue(SILGenFunction &SGF,
@@ -1070,7 +1070,7 @@ static ManagedValue emitCBridgedToNativeValue(SILGenFunction &SGF,
10701070

10711071
assert(SGF.SGM.getASTContext().Diags.hadAnyError() &&
10721072
"Bridging code should have complained");
1073-
return SGF.emitUndef(loc, nativeType);
1073+
return SGF.emitUndef(nativeType);
10741074
}
10751075

10761076
// id-to-Any bridging.
@@ -1138,7 +1138,7 @@ ManagedValue SILGenFunction::emitBridgedToNativeError(SILLocation loc,
11381138
auto nativeErrorTy = SILType::getExceptionType(getASTContext());
11391139

11401140
auto conformance = SGM.getNSErrorConformanceToError();
1141-
if (!conformance) return emitUndef(loc, nativeErrorTy);
1141+
if (!conformance) return emitUndef(nativeErrorTy);
11421142
ProtocolConformanceRef conformanceArray[] = {
11431143
ProtocolConformanceRef(conformance)
11441144
};

lib/SILGen/SILGenBuiltin.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ static ManagedValue emitCastToReferenceType(SILGenFunction &SGF,
299299
if (!argTy->mayHaveSuperclass() && !argTy->isClassExistentialType()) {
300300
SGF.SGM.diagnose(loc, diag::invalid_sil_builtin,
301301
"castToNativeObject source must be a class");
302-
SILValue undef = SILUndef::get(objPointerType, SGF.SGM.M);
303-
return ManagedValue::forUnmanaged(undef);
302+
return SGF.emitUndef(objPointerType);
304303
}
305304

306305
// Grab the argument.
@@ -362,8 +361,7 @@ static ManagedValue emitCastFromReferenceType(SILGenFunction &SGF,
362361
SGF.SGM.diagnose(loc, diag::invalid_sil_builtin,
363362
"castFromNativeObject dest must be an object type");
364363
// Recover by propagating an undef result.
365-
SILValue result = SILUndef::get(destType, SGF.SGM.M);
366-
return ManagedValue::forUnmanaged(result);
364+
return SGF.emitUndef(destType);
367365
}
368366

369367
return SGF.B.createUncheckedRefCast(loc, args[0], destType);
@@ -432,7 +430,7 @@ static ManagedValue emitBuiltinAddressOf(SILGenFunction &SGF,
432430
auto lv = SGF.emitLValue(inout->getSubExpr(), SGFAccessKind::ReadWrite);
433431
if (!lv.isPhysical() || !lv.isLoadingPure()) {
434432
SGF.SGM.diagnose(argument->getLoc(), diag::non_physical_addressof);
435-
return ManagedValue::forUnmanaged(SILUndef::get(rawPointerTy, &SGF.SGM.M));
433+
return SGF.emitUndef(rawPointerTy);
436434
}
437435

438436
auto addr = SGF.emitAddressOfLValue(argument, std::move(lv))
@@ -459,7 +457,7 @@ static ManagedValue emitBuiltinAddressOfBorrow(SILGenFunction &SGF,
459457
.getAsSingleValue(SGF, argument);
460458
if (!borrow.isPlusZero() || !borrow.getType().isAddress()) {
461459
SGF.SGM.diagnose(argument->getLoc(), diag::non_borrowed_indirect_addressof);
462-
return ManagedValue::forUnmanaged(SILUndef::get(rawPointerTy, &SGF.SGM.M));
460+
return SGF.emitUndef(rawPointerTy);
463461
}
464462

465463
addr = borrow.getValue();
@@ -788,8 +786,7 @@ static ManagedValue emitBuiltinCastToBridgeObject(SILGenFunction &SGF,
788786
!sourceType->isClassExistentialType()) {
789787
SGF.SGM.diagnose(loc, diag::invalid_sil_builtin,
790788
"castToBridgeObject source must be a class");
791-
SILValue undef = SILUndef::get(objPointerType, SGF.SGM.M);
792-
return ManagedValue::forUnmanaged(undef);
789+
return SGF.emitUndef(objPointerType);
793790
}
794791

795792
ManagedValue ref = args[0];
@@ -825,8 +822,7 @@ static ManagedValue emitBuiltinCastReferenceFromBridgeObject(
825822
SGF.SGM.diagnose(loc, diag::invalid_sil_builtin,
826823
"castReferenceFromBridgeObject dest must be an object type");
827824
// Recover by propagating an undef result.
828-
SILValue result = SILUndef::get(destType, SGF.SGM.M);
829-
return ManagedValue::forUnmanaged(result);
825+
return SGF.emitUndef(destType);
830826
}
831827

832828
return SGF.B.createBridgeObjectToRef(loc, args[0], destType);
@@ -873,8 +869,7 @@ static ManagedValue emitBuiltinValueToBridgeObject(SILGenFunction &SGF,
873869
SGF.SGM.diagnose(loc, diag::invalid_sil_builtin,
874870
"argument to builtin should be a builtin integer");
875871
SILType objPointerType = SILType::getBridgeObjectType(SGF.F.getASTContext());
876-
SILValue undef = SILUndef::get(objPointerType, SGF.SGM.M);
877-
return ManagedValue::forUnmanaged(undef);
872+
return SGF.emitUndef(objPointerType);
878873
}
879874

880875
SILValue result = SGF.B.createValueToBridgeObject(loc, args[0].getValue());

lib/SILGen/SILGenConvert.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ ManagedValue SILGenFunction::emitExistentialErasure(
644644
if (auto storedNSErrorConformance =
645645
SGM.getConformanceToBridgedStoredNSError(loc, concreteFormalType)) {
646646
auto nsErrorVar = SGM.getNSErrorRequirement(loc);
647-
if (!nsErrorVar) return emitUndef(loc, existentialTL.getLoweredType());
647+
if (!nsErrorVar) return emitUndef(existentialTL.getLoweredType());
648648

649649
SubstitutionMap nsErrorVarSubstitutions;
650650

@@ -686,7 +686,7 @@ ManagedValue SILGenFunction::emitExistentialErasure(
686686
// NSError from the value.
687687
auto getEmbeddedNSErrorFn = SGM.getGetErrorEmbeddedNSError(loc);
688688
if (!getEmbeddedNSErrorFn)
689-
return emitUndef(loc, existentialTL.getLoweredType());
689+
return emitUndef(existentialTL.getLoweredType());
690690

691691
auto getEmbeddedNSErrorSubstitutions =
692692
SubstitutionMap::getProtocolSubstitutions(ctx.getErrorDecl(),

lib/SILGen/SILGenExpr.cpp

+13-16
Original file line numberDiff line numberDiff line change
@@ -863,22 +863,19 @@ emitRValueForDecl(SILLocation loc, ConcreteDeclRef declRef, Type ncRefType,
863863
}
864864
CanType refType = ncRefType->getCanonicalType();
865865

866-
auto getUnmanagedRValue = [&](SILValue value) -> RValue {
867-
return RValue(*this, loc, refType, ManagedValue::forUnmanaged(value));
868-
};
869-
870866
// If this is a reference to a module, produce an undef value. The
871867
// module value should never actually be used.
872868
if (isa<ModuleDecl>(decl)) {
873-
return getUnmanagedRValue(
874-
SILUndef::get(getLoweredLoadableType(ncRefType), SGM.M));
869+
return emitUndefRValue(loc, refType);
875870
}
876871

877872
// If this is a reference to a type, produce a metatype.
878873
if (isa<TypeDecl>(decl)) {
879874
assert(refType->is<MetatypeType>() &&
880875
"type declref does not have metatype type?!");
881-
return getUnmanagedRValue(B.createMetatype(loc, getLoweredType(refType)));
876+
return RValue(*this, loc, refType,
877+
ManagedValue::forUnmanaged(
878+
B.createMetatype(loc, getLoweredType(refType))));
882879
}
883880

884881
// If this is a reference to a var, emit it as an l-value and then load.
@@ -1473,7 +1470,7 @@ static ManagedValue convertCFunctionSignature(SILGenFunction &SGF,
14731470
// just runs the risk of tripping up asserts in SILGenBridging.cpp
14741471
SGF.SGM.diagnose(e, diag::unsupported_c_function_pointer_conversion,
14751472
e->getSubExpr()->getType(), e->getType());
1476-
result = SGF.emitUndef(e, loweredDestTy);
1473+
result = SGF.emitUndef(loweredDestTy);
14771474
break;
14781475

14791476
case TypeConverter::ABIDifference::ThinToThick:
@@ -1657,13 +1654,13 @@ RValue RValueEmitter::visitFunctionConversionExpr(FunctionConversionExpr *e,
16571654
} else {
16581655
SGF.SGM.diagnose(e->getLoc(), diag::not_implemented,
16591656
"nontrivial thin function reference");
1660-
value = ManagedValue::forUnmanaged(SILUndef::get(expectedTy, SGF.SGM.M));
1657+
value = SGF.emitUndef(expectedTy);
16611658
}
16621659

16631660
if (value.getType() != expectedTy) {
16641661
SGF.SGM.diagnose(e->getLoc(), diag::not_implemented,
16651662
"nontrivial thin function reference");
1666-
value = ManagedValue::forUnmanaged(SILUndef::get(expectedTy, SGF.SGM.M));
1663+
value = SGF.emitUndef(expectedTy);
16671664
}
16681665
return RValue(SGF, e, value);
16691666
}
@@ -2524,7 +2521,7 @@ RValue RValueEmitter::visitObjCSelectorExpr(ObjCSelectorExpr *e, SGFContext C) {
25242521
}
25252522
if (!selectorMemberTy) {
25262523
SGF.SGM.diagnose(e, diag::objc_selector_malformed);
2527-
return RValue(SGF, e, SGF.emitUndef(e, loweredSelectorTy));
2524+
return RValue(SGF, e, SGF.emitUndef(loweredSelectorTy));
25282525
}
25292526

25302527
// Form the selector string.
@@ -5488,14 +5485,14 @@ ManagedValue SILGenFunction::emitRValueAsSingleValue(Expr *E, SGFContext C) {
54885485

54895486
RValue SILGenFunction::emitUndefRValue(SILLocation loc, Type type) {
54905487
return RValue(*this, loc, type->getCanonicalType(),
5491-
emitUndef(loc, getLoweredType(type)));
5488+
emitUndef(getLoweredType(type)));
54925489
}
54935490

5494-
ManagedValue SILGenFunction::emitUndef(SILLocation loc, Type type) {
5495-
return emitUndef(loc, getLoweredType(type));
5491+
ManagedValue SILGenFunction::emitUndef(Type type) {
5492+
return emitUndef(getLoweredType(type));
54965493
}
54975494

5498-
ManagedValue SILGenFunction::emitUndef(SILLocation loc, SILType type) {
5499-
SILValue undef = SILUndef::get(type, SGM.M);
5495+
ManagedValue SILGenFunction::emitUndef(SILType type) {
5496+
SILValue undef = SILUndef::get(type, F);
55005497
return ManagedValue::forUnmanaged(undef);
55015498
}

lib/SILGen/SILGenForeignError.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ emitBridgeErrorForForeignError(SILLocation loc,
185185
case ForeignErrorConvention::NilResult:
186186
return B.createOptionalNone(loc, bridgedResultType);
187187
case ForeignErrorConvention::NonNilError:
188-
return SILUndef::get(bridgedResultType, SGM.M);
188+
return SILUndef::get(bridgedResultType, F);
189189
}
190190
llvm_unreachable("bad foreign error convention kind");
191191
}

lib/SILGen/SILGenFunction.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
11201120
ManagedValue emitRValueAsSingleValue(Expr *E, SGFContext C = SGFContext());
11211121

11221122
/// Emit 'undef' in a particular formal type.
1123-
ManagedValue emitUndef(SILLocation loc, Type type);
1124-
ManagedValue emitUndef(SILLocation loc, SILType type);
1123+
ManagedValue emitUndef(Type type);
1124+
ManagedValue emitUndef(SILType type);
11251125
RValue emitUndefRValue(SILLocation loc, Type type);
11261126

11271127
std::pair<ManagedValue, SILValue>

0 commit comments

Comments
 (0)