Skip to content

Commit c94688b

Browse files
authored
Merge pull request #22819 from slavapestov/cherry-pick-5.1-branch
Cherry pick some fixes to qualification branch
2 parents bfd09ed + 8b01751 commit c94688b

21 files changed

+436
-148
lines changed

include/swift/Remote/MetadataReader.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class MetadataReader {
222222
StoredPointer TaggedPointerExtendedSlotShift;
223223
StoredPointer TaggedPointerExtendedSlotMask;
224224
StoredPointer TaggedPointerExtendedClasses;
225+
StoredPointer TaggedPointerObfuscator;
225226

226227
Demangle::NodeFactory Factory;
227228

@@ -744,7 +745,7 @@ class MetadataReader {
744745
if (getTaggedPointerEncoding() != TaggedPointerEncodingKind::Extended)
745746
return false;
746747

747-
return objectAddress & TaggedPointerMask;
748+
return (objectAddress ^ TaggedPointerObfuscator) & TaggedPointerMask;
748749
}
749750

750751
/// Read the isa pointer of an Object-C tagged pointer value.
@@ -761,8 +762,8 @@ class MetadataReader {
761762

762763
// Extended pointers have a tag of 0b111, using 8 additional bits
763764
// to specify the class.
764-
if (TaggedPointerExtendedMask != 0 &&
765-
((objectAddress & TaggedPointerExtendedMask)
765+
if (TaggedPointerExtendedMask != 0 &&
766+
(((objectAddress ^ TaggedPointerObfuscator) & TaggedPointerExtendedMask)
766767
== TaggedPointerExtendedMask)) {
767768
auto tag = ((objectAddress >> TaggedPointerExtendedSlotShift) &
768769
TaggedPointerExtendedSlotMask);
@@ -2469,6 +2470,9 @@ class MetadataReader {
24692470
TaggedPointerExtendedClasses =
24702471
TaggedPointerExtendedClassesAddr.getAddressData();
24712472

2473+
tryFindAndReadSymbol(TaggedPointerObfuscator,
2474+
"objc_debug_taggedpointer_obfuscator");
2475+
24722476
# undef tryFindSymbol
24732477
# undef tryReadSymbol
24742478
# undef tryFindAndReadSymbol

include/swift/SIL/SILBuilder.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,12 +2125,18 @@ class SILBuilder {
21252125
}
21262126

21272127
bool isLoadableOrOpaque(SILType Ty) {
2128-
if (!F) {
2129-
// We are inserting into the static initializer of a SILGlobalVariable.
2130-
// All types used there are loadable by definition.
2128+
auto &M = C.Module;
2129+
2130+
if (!SILModuleConventions(M).useLoweredAddresses())
21312131
return true;
2132-
}
2133-
return Ty.isLoadableOrOpaque(F);
2132+
2133+
auto expansion = ResilienceExpansion::Maximal;
2134+
// If there's no current SILFunction, we're inserting into a global
2135+
// variable initializer.
2136+
if (F)
2137+
expansion = F->getResilienceExpansion();
2138+
2139+
return M.getTypeLowering(Ty, expansion).isLoadable();
21342140
}
21352141

21362142
void appendOperandTypeName(SILType OpdTy, llvm::SmallString<16> &Name) {

include/swift/SIL/TypeLowering.h

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -306,23 +306,6 @@ class TypeLowering {
306306
return Properties.isResilient();
307307
}
308308

309-
/// Return the semantic type.
310-
///
311-
/// The semantic type is what a type pretends to be during
312-
/// type-checking: that is, the type that getTypeOfRValue would
313-
/// return on a variable of this type.
314-
SILType getSemanticType() const {
315-
// If you change this, change getSemanticTypeLowering() too.
316-
auto storageType = getLoweredType().getASTType();
317-
if (auto refType = dyn_cast<ReferenceStorageType>(storageType))
318-
return SILType::getPrimitiveType(refType.getReferentType(),
319-
SILValueCategory::Object);
320-
return getLoweredType();
321-
}
322-
323-
/// Return the lowering for the semantic type.
324-
inline const TypeLowering &getSemanticTypeLowering(TypeConverter &TC) const;
325-
326309
/// Produce an exact copy of the value in the given address as a
327310
/// scalar. The caller is responsible for destroying this value,
328311
/// e.g. by releasing it.
@@ -582,13 +565,6 @@ class TypeConverter {
582565

583566
llvm::BumpPtrAllocator IndependentBPA;
584567

585-
enum : unsigned {
586-
/// There is a unique entry with this uncurry level in the
587-
/// type-lowering map for every TLI we create. The map has the
588-
/// responsibility to call the destructor for these entries.
589-
UniqueLoweringEntry = ~0U
590-
};
591-
592568
struct CachingTypeKey {
593569
GenericSignature *Sig;
594570
AbstractionPattern::CachingKey OrigType;
@@ -700,8 +676,16 @@ class TypeConverter {
700676
#include "swift/SIL/BridgedTypes.def"
701677

702678
const TypeLowering &
703-
getTypeLoweringForLoweredType(TypeKey key, ResilienceExpansion forExpansion);
704-
const TypeLowering &getTypeLoweringForUncachedLoweredType(TypeKey key);
679+
getTypeLoweringForLoweredType(TypeKey key,
680+
ResilienceExpansion forExpansion);
681+
const TypeLowering &
682+
getTypeLoweringForUncachedLoweredType(TypeKey key,
683+
ResilienceExpansion forExpansion);
684+
685+
const TypeLowering &
686+
getTypeLoweringForExpansion(TypeKey key,
687+
ResilienceExpansion forExpansion,
688+
const TypeLowering *lowering);
705689

706690
public:
707691
SILModule &M;
@@ -763,15 +747,19 @@ class TypeConverter {
763747

764748
/// Lowers a Swift type to a SILType, and returns the SIL TypeLowering
765749
/// for that type.
766-
const TypeLowering &getTypeLowering(Type t) {
750+
const TypeLowering &
751+
getTypeLowering(Type t, ResilienceExpansion forExpansion =
752+
ResilienceExpansion::Minimal) {
767753
AbstractionPattern pattern(getCurGenericContext(), t->getCanonicalType());
768-
return getTypeLowering(pattern, t);
754+
return getTypeLowering(pattern, t, forExpansion);
769755
}
770756

771757
/// Lowers a Swift type to a SILType according to the abstraction
772758
/// patterns of the given original type.
773759
const TypeLowering &getTypeLowering(AbstractionPattern origType,
774-
Type substType);
760+
Type substType,
761+
ResilienceExpansion forExpansion =
762+
ResilienceExpansion::Minimal);
775763

776764
/// Returns the SIL TypeLowering for an already lowered SILType. If the
777765
/// SILType is an address, returns the TypeLowering for the pointed-to
@@ -782,16 +770,19 @@ class TypeConverter {
782770

783771
// Returns the lowered SIL type for a Swift type.
784772
SILType getLoweredType(Type t) {
785-
return getTypeLowering(t).getLoweredType();
773+
return getTypeLowering(t, ResilienceExpansion::Minimal).getLoweredType();
786774
}
787775

788776
// Returns the lowered SIL type for a Swift type.
789777
SILType getLoweredType(AbstractionPattern origType, Type substType) {
790-
return getTypeLowering(origType, substType).getLoweredType();
778+
return getTypeLowering(origType, substType, ResilienceExpansion::Minimal)
779+
.getLoweredType();
791780
}
792781

793-
SILType getLoweredLoadableType(Type t) {
794-
const TypeLowering &ti = getTypeLowering(t);
782+
SILType getLoweredLoadableType(Type t,
783+
ResilienceExpansion forExpansion =
784+
ResilienceExpansion::Minimal) {
785+
const TypeLowering &ti = getTypeLowering(t, forExpansion);
795786
assert(
796787
(ti.isLoadable() || !SILModuleConventions(M).useLoweredAddresses()) &&
797788
"unexpected address-only type");
@@ -1033,15 +1024,6 @@ class TypeConverter {
10331024
bool suppressOptional);
10341025
};
10351026

1036-
inline const TypeLowering &
1037-
TypeLowering::getSemanticTypeLowering(TypeConverter &TC) const {
1038-
// If you change this, change getSemanticType() too.
1039-
auto storageType = getLoweredType().getASTType();
1040-
if (auto refType = dyn_cast<ReferenceStorageType>(storageType))
1041-
return TC.getTypeLowering(refType.getReferentType());
1042-
return *this;
1043-
}
1044-
10451027
/// RAII interface to push a generic context.
10461028
class GenericContextScope {
10471029
TypeConverter &TC;

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,15 @@ bool isSimpleType(SILType SILTy, SILModule& Module);
535535
bool analyzeStaticInitializer(SILValue V,
536536
SmallVectorImpl<SILInstruction *> &Insns);
537537

538+
/// Returns true if the below operation will succeed.
539+
bool canReplaceLoadSequence(SILInstruction *I);
540+
538541
/// Replace load sequence which may contain
539542
/// a chain of struct_element_addr followed by a load.
540543
/// The sequence is traversed inside out, i.e.
541544
/// starting with the innermost struct_element_addr
542545
void replaceLoadSequence(SILInstruction *I,
543-
SILValue Value,
544-
SILBuilder &B);
546+
SILValue Value);
545547

546548

547549
/// Do we have enough information to determine all callees that could

lib/IRGen/GenProto.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -948,23 +948,35 @@ namespace {
948948

949949
/// Return true if the witness table requires runtime instantiation to
950950
/// handle resiliently-added requirements with default implementations.
951-
static bool isResilientConformance(const NormalProtocolConformance *conformance) {
951+
bool IRGenModule::isResilientConformance(
952+
const NormalProtocolConformance *conformance) {
952953
// If the protocol is not resilient, the conformance is not resilient
953954
// either.
954955
if (!conformance->getProtocol()->isResilient())
955956
return false;
956957

957-
// If the protocol is in the same module as the conformance, we're
958-
// not resilient.
959-
if (conformance->getDeclContext()->getParentModule()
960-
== conformance->getProtocol()->getParentModule())
958+
auto *conformanceModule = conformance->getDeclContext()->getParentModule();
959+
960+
// If the protocol and the conformance are both in the current module,
961+
// they're not resilient.
962+
if (conformanceModule == getSwiftModule() &&
963+
conformanceModule == conformance->getProtocol()->getParentModule())
964+
return false;
965+
966+
// If the protocol and the conformance are in the same module and the
967+
// conforming type is not generic, they're not resilient.
968+
//
969+
// This is an optimization -- a conformance of a non-generic type cannot
970+
// resiliently become dependent.
971+
if (!conformance->getDeclContext()->isGenericContext() &&
972+
conformanceModule == conformance->getProtocol()->getParentModule())
961973
return false;
962974

963975
// We have a resilient conformance.
964976
return true;
965977
}
966978

967-
static bool isResilientConformance(const RootProtocolConformance *root) {
979+
bool IRGenModule::isResilientConformance(const RootProtocolConformance *root) {
968980
if (auto normal = dyn_cast<NormalProtocolConformance>(root))
969981
return isResilientConformance(normal);
970982
// Self-conformances never require this.
@@ -997,6 +1009,7 @@ static bool hasDependentTypeWitness(
9971009
}
9981010

9991011
static bool isDependentConformance(
1012+
IRGenModule &IGM,
10001013
const RootProtocolConformance *rootConformance,
10011014
bool considerResilience,
10021015
llvm::SmallPtrSet<const NormalProtocolConformance *, 4> &visited){
@@ -1011,7 +1024,7 @@ static bool isDependentConformance(
10111024
return false;
10121025

10131026
// If the conformance is resilient, this is always true.
1014-
if (considerResilience && isResilientConformance(conformance))
1027+
if (considerResilience && IGM.isResilientConformance(conformance))
10151028
return true;
10161029

10171030
// Check whether any of the conformances are dependent.
@@ -1027,7 +1040,8 @@ static bool isDependentConformance(
10271040
auto assocConformance =
10281041
conformance->getAssociatedConformance(req.getFirstType(), assocProtocol);
10291042
if (assocConformance.isAbstract() ||
1030-
isDependentConformance(assocConformance.getConcrete()
1043+
isDependentConformance(IGM,
1044+
assocConformance.getConcrete()
10311045
->getRootConformance(),
10321046
considerResilience,
10331047
visited))
@@ -1045,10 +1059,12 @@ static bool isDependentConformance(
10451059

10461060
/// Is there anything about the given conformance that requires witness
10471061
/// tables to be dependently-generated?
1048-
static bool isDependentConformance(const RootProtocolConformance *conformance,
1049-
bool considerResilience) {
1062+
bool IRGenModule::isDependentConformance(
1063+
const RootProtocolConformance *conformance,
1064+
bool considerResilience) {
10501065
llvm::SmallPtrSet<const NormalProtocolConformance *, 4> visited;
1051-
return ::isDependentConformance(conformance, considerResilience, visited);
1066+
return ::isDependentConformance(*this, conformance, considerResilience,
1067+
visited);
10521068
}
10531069

10541070
static bool isSynthesizedNonUnique(const RootProtocolConformance *conformance) {
@@ -1286,7 +1302,7 @@ class AccessorConformanceInfo : public ConformanceInfo {
12861302
Conformance.getDeclContext())),
12871303
SILEntries(SILWT->getEntries()),
12881304
SILConditionalConformances(SILWT->getConditionalConformances()),
1289-
ResilientConformance(isResilientConformance(&Conformance)),
1305+
ResilientConformance(IGM.isResilientConformance(&Conformance)),
12901306
PI(IGM.getProtocolInfo(SILWT->getConformance()->getProtocol(),
12911307
(ResilientConformance
12921308
? ProtocolInfoKind::RequirementSignature
@@ -2086,7 +2102,7 @@ void IRGenerator::ensureRelativeSymbolCollocation(SILWitnessTable &wt) {
20862102

20872103
// Only resilient conformances use relative pointers for witness methods.
20882104
if (wt.isDeclaration() || isAvailableExternally(wt.getLinkage()) ||
2089-
!isResilientConformance(wt.getConformance()))
2105+
!CurrentIGM->isResilientConformance(wt.getConformance()))
20902106
return;
20912107

20922108
for (auto &entry : wt.getEntries()) {

lib/IRGen/IRGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,11 @@ class IRGenModule {
790790
ResilienceExpansion getResilienceExpansionForLayout(NominalTypeDecl *decl);
791791
ResilienceExpansion getResilienceExpansionForLayout(SILGlobalVariable *var);
792792

793+
bool isResilientConformance(const NormalProtocolConformance *conformance);
794+
bool isResilientConformance(const RootProtocolConformance *root);
795+
bool isDependentConformance(const RootProtocolConformance *conformance,
796+
bool considerResilience);
797+
793798
Alignment getCappedAlignment(Alignment alignment);
794799

795800
SpareBitVector getSpareBitsForType(llvm::Type *scalarTy, Size size);

lib/SIL/SILWitnessTable.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
//
1313
// This file defines the SILWitnessTable class, which is used to map a protocol
1414
// conformance for a type to its implementing SILFunctions. This information is
15-
// (FIXME will be) used by IRGen to create witness tables for protocol dispatch.
15+
// used by IRGen to create witness tables for protocol dispatch.
16+
//
1617
// It can also be used by generic specialization and existential
17-
// devirtualization passes to promote witness_method and protocol_method
18-
// instructions to static function_refs.
18+
// devirtualization passes to promote witness_method instructions to static
19+
// function_refs.
1920
//
2021
//===----------------------------------------------------------------------===//
2122

@@ -24,7 +25,6 @@
2425
#include "swift/AST/ASTMangler.h"
2526
#include "swift/AST/Module.h"
2627
#include "swift/AST/ProtocolConformance.h"
27-
#include "swift/ClangImporter/ClangModule.h"
2828
#include "swift/SIL/SILModule.h"
2929
#include "llvm/ADT/SmallString.h"
3030

@@ -172,11 +172,6 @@ bool SILWitnessTable::conformanceIsSerialized(
172172
if (normalConformance && normalConformance->isResilient())
173173
return false;
174174

175-
// Serialize witness tables for conformances synthesized by
176-
// the ClangImporter.
177-
if (isa<ClangModuleUnit>(conformance->getDeclContext()->getModuleScopeContext()))
178-
return true;
179-
180175
if (conformance->getProtocol()->getEffectiveAccess() < AccessLevel::Public)
181176
return false;
182177

0 commit comments

Comments
 (0)