Skip to content

Commit 8aaa7b4

Browse files
committed
SILOptimizer: Pipe through TypeExpansionContext
1 parent 33f4f57 commit 8aaa7b4

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

+817
-823
lines changed

include/swift/SIL/TypeSubstCloner.h

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
9090
// Use the new set of substitutions to compute the new
9191
// substituted callee type.
9292
RecursiveSubstCalleeSILType = LoweredFnTy->substGenericArgs(
93-
AI.getModule(), RecursiveSubs);
93+
AI.getModule(), RecursiveSubs,
94+
Builder.getTypeExpansionContext());
9495
}
9596

9697
// The specialized recursive function may have different calling
@@ -109,7 +110,8 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
109110

110111
assert(Subs.empty() ||
111112
SubstCalleeSILType ==
112-
Callee->getType().substGenericArgs(AI.getModule(), Subs));
113+
Callee->getType().substGenericArgs(
114+
AI.getModule(), Subs, Builder.getTypeExpansionContext()));
113115
}
114116

115117
ArrayRef<SILValue> getArguments() const {
@@ -168,17 +170,40 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
168170
SILType &Sty = TypeCache[Ty];
169171
if (!Sty) {
170172
Sty = Ty.subst(Original.getModule(), SubsMap);
173+
if (!Sty.getASTType()->hasOpaqueArchetype() ||
174+
!getBuilder()
175+
.getTypeExpansionContext()
176+
.shouldLookThroughOpaqueTypeArchetypes())
177+
return Sty;
178+
// Remap types containing opaque result types in the current context.
179+
Sty = getBuilder().getTypeLowering(Sty).getLoweredType().getCategoryType(
180+
Sty.getCategory());
171181
}
172182
return Sty;
173183
}
174184

175185
CanType remapASTType(CanType ty) {
176-
return ty.subst(SubsMap)->getCanonicalType();
186+
auto substTy = ty.subst(SubsMap)->getCanonicalType();
187+
if (!substTy->hasOpaqueArchetype() ||
188+
!getBuilder().getTypeExpansionContext()
189+
.shouldLookThroughOpaqueTypeArchetypes())
190+
return substTy;
191+
// Remap types containing opaque result types in the current context.
192+
return getBuilder().getModule().Types.getLoweredRValueType(
193+
TypeExpansionContext(getBuilder().getFunction()), substTy);
177194
}
178195

179-
ProtocolConformanceRef remapConformance(Type type,
196+
ProtocolConformanceRef remapConformance(Type ty,
180197
ProtocolConformanceRef conf) {
181-
return conf.subst(type, SubsMap);
198+
auto conformance = conf.subst(ty, SubsMap);
199+
auto substTy = ty.subst(SubsMap)->getCanonicalType();
200+
auto context = getBuilder().getTypeExpansionContext();
201+
if (substTy->hasOpaqueArchetype() &&
202+
context.shouldLookThroughOpaqueTypeArchetypes()) {
203+
conformance =
204+
substOpaqueTypesWithUnderlyingTypes(conformance, substTy, context);
205+
}
206+
return conformance;
182207
}
183208

184209
SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {

include/swift/SILOptimizer/Analysis/AccessSummaryAnalysis.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class AccessSummaryAnalysis : public BottomUpIPAnalysis {
6262

6363
/// Returns a description of the summary. For debugging and testing
6464
/// purposes.
65-
std::string getDescription(SILType BaseType, SILModule &M) const;
65+
std::string getDescription(SILType BaseType, SILModule &M,
66+
TypeExpansionContext context) const;
6667
};
6768

6869
typedef llvm::SmallDenseMap<const IndexTrieNode *, SubAccessSummary, 8>
@@ -85,7 +86,8 @@ class AccessSummaryAnalysis : public BottomUpIPAnalysis {
8586

8687
/// Returns a description of the summary. For debugging and testing
8788
/// purposes.
88-
std::string getDescription(SILType BaseType, SILModule &M) const;
89+
std::string getDescription(SILType BaseType, SILModule &M,
90+
TypeExpansionContext context) const;
8991

9092
/// Returns the accesses that the function performs to subpaths of the
9193
/// argument.
@@ -208,7 +210,8 @@ class AccessSummaryAnalysis : public BottomUpIPAnalysis {
208210
/// The base type must be the type of the root of the path.
209211
static std::string getSubPathDescription(SILType BaseType,
210212
const IndexTrieNode *SubPath,
211-
SILModule &M);
213+
SILModule &M,
214+
TypeExpansionContext context);
212215

213216
/// Performs a lexicographic comparison of two subpaths, first by path length
214217
/// and then by index of the last path component. Returns true when lhs

include/swift/SILOptimizer/Analysis/TypeExpansionAnalysis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class TypeExpansionAnalysis : public SILAnalysis {
3232
}
3333

3434
/// Return ProjectionPath to every leaf or intermediate node of the given type.
35-
const ProjectionPathList &getTypeExpansion(SILType B, SILModule *Mod);
35+
const ProjectionPathList &getTypeExpansion(SILType B, SILModule *Mod,
36+
TypeExpansionContext context);
3637

3738
/// Invalidate all information in this analysis.
3839
virtual void invalidate() override {

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ PASS(NoReturnFolding, "noreturn-folding",
230230
"Prune Control Flow at No-Return Calls Using SIL unreachable")
231231
PASS(ObjectOutliner, "object-outliner",
232232
"Outlining of Global Objects")
233-
PASS(OpaqueArchetypeSpecializer, "opaque-archetype-specializer",
234-
"Opaque archetype specializer")
235233
PASS(Outliner, "outliner",
236234
"Function Outlining Optimization")
237235
PASS(OwnershipModelEliminator, "ownership-model-eliminator",

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ class ReabstractionInfo {
100100
// Reference to the original generic non-specialized callee function.
101101
SILFunction *Callee;
102102

103+
// The module the specialization is created in.
104+
ModuleDecl *TargetModule = nullptr;
105+
106+
bool isWholeModule = false;
107+
103108
// The apply site which invokes the generic function.
104109
ApplySite Apply;
105110

@@ -140,24 +145,27 @@ class ReabstractionInfo {
140145
/// substitutions \p ParamSubs.
141146
/// If specialization is not possible getSpecializedType() will return an
142147
/// invalid type.
143-
ReabstractionInfo(ApplySite Apply, SILFunction *Callee,
148+
ReabstractionInfo(ModuleDecl *targetModule,
149+
bool isModuleWholeModule,
150+
ApplySite Apply, SILFunction *Callee,
144151
SubstitutionMap ParamSubs,
145152
IsSerialized_t Serialized,
146153
bool ConvertIndirectToDirect = true,
147154
OptRemark::Emitter *ORE = nullptr);
148155

149156
/// Constructs the ReabstractionInfo for generic function \p Callee with
150157
/// a specialization signature.
151-
ReabstractionInfo(SILFunction *Callee, GenericSignature SpecializedSig);
158+
ReabstractionInfo(ModuleDecl *targetModule, bool isModuleWholeModule,
159+
SILFunction *Callee, GenericSignature SpecializedSig);
152160

153161
IsSerialized_t isSerialized() const {
154162
return Serialized;
155163
}
156164

157-
ResilienceExpansion getResilienceExpansion() const {
158-
return (Serialized
159-
? ResilienceExpansion::Minimal
160-
: ResilienceExpansion::Maximal);
165+
TypeExpansionContext getResilienceExpansion() const {
166+
auto resilience = (Serialized ? ResilienceExpansion::Minimal
167+
: ResilienceExpansion::Maximal);
168+
return TypeExpansionContext(resilience, TargetModule, isWholeModule);
161169
}
162170

163171
/// Returns true if the \p ParamIdx'th (non-result) formal parameter is

include/swift/SILOptimizer/Utils/LoadStoreOptUtils.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ class LSBase {
169169
}
170170

171171
/// Print the LSBase.
172-
virtual void print(llvm::raw_ostream &os, SILModule *Mod) {
172+
virtual void print(llvm::raw_ostream &os, SILModule *Mod,
173+
TypeExpansionContext context) {
173174
os << Base;
174-
Path.getValue().print(os, *Mod);
175+
Path.getValue().print(os, *Mod, context);
175176
}
176177

177-
virtual void dump(SILModule *Mod) { print(llvm::dbgs(), Mod); }
178+
virtual void dump(SILModule *Mod, TypeExpansionContext context) {
179+
print(llvm::dbgs(), Mod, context);
180+
}
178181
};
179182

180183
static inline llvm::hash_code hash_value(const LSBase &S) {
@@ -257,16 +260,18 @@ class LSValue : public LSBase {
257260
return Path.getValue().createExtract(Base, Inst, true);
258261
}
259262

260-
void print(llvm::raw_ostream &os, SILModule *Mod) {
263+
void print(llvm::raw_ostream &os, SILModule *Mod,
264+
TypeExpansionContext context) {
261265
if (CoveringValue) {
262266
os << "Covering Value";
263267
return;
264268
}
265-
LSBase::print(os, Mod);
269+
LSBase::print(os, Mod, context);
266270
}
267271

268272
/// Expand this SILValue to all individual fields it contains.
269-
static void expand(SILValue Base, SILModule *Mod, LSValueList &Vals,
273+
static void expand(SILValue Base, SILModule *Mod,
274+
TypeExpansionContext context, LSValueList &Vals,
270275
TypeExpansionAnalysis *TE);
271276

272277
/// Given a memory location and a map between the expansions of the location
@@ -329,13 +334,14 @@ class LSLocation : public LSBase {
329334
}
330335

331336
/// Returns the type of the object the LSLocation represents.
332-
SILType getType(SILModule *M) {
333-
return Path.getValue().getMostDerivedType(*M);
337+
SILType getType(SILModule *M, TypeExpansionContext context) {
338+
return Path.getValue().getMostDerivedType(*M, context);
334339
}
335340

336341
/// Get the first level locations based on this location's first level
337342
/// projection.
338-
void getNextLevelLSLocations(LSLocationList &Locs, SILModule *Mod);
343+
void getNextLevelLSLocations(LSLocationList &Locs, SILModule *Mod,
344+
TypeExpansionContext context);
339345

340346
/// Check whether the 2 LSLocations may alias each other or not.
341347
bool isMayAliasLSLocation(const LSLocation &RHS, AliasAnalysis *AA);
@@ -351,15 +357,18 @@ class LSLocation : public LSBase {
351357
/// In SIL, we can have a store to an aggregate and loads from its individual
352358
/// fields. Therefore, we expand all the operations on aggregates onto
353359
/// individual fields and process them separately.
354-
static void expand(LSLocation Base, SILModule *Mod, LSLocationList &Locs,
360+
static void expand(LSLocation Base, SILModule *Mod,
361+
TypeExpansionContext context, LSLocationList &Locs,
355362
TypeExpansionAnalysis *TE);
356363

357364
/// Given a set of locations derived from the same base, try to merge/reduce
358365
/// them into smallest number of LSLocations possible.
359-
static void reduce(LSLocation Base, SILModule *Mod, LSLocationList &Locs);
366+
static void reduce(LSLocation Base, SILModule *Mod,
367+
TypeExpansionContext context, LSLocationList &Locs);
360368

361369
/// Enumerate the given Mem LSLocation.
362-
static void enumerateLSLocation(SILModule *M, SILValue Mem,
370+
static void enumerateLSLocation(TypeExpansionContext context, SILModule *M,
371+
SILValue Mem,
363372
std::vector<LSLocation> &LSLocationVault,
364373
LSLocationIndexMap &LocToBit,
365374
LSLocationBaseMap &BaseToLoc,

lib/SILOptimizer/Analysis/AccessSummaryAnalysis.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,13 @@ void AccessSummaryAnalysis::recompute(FunctionInfo *initial) {
380380
} while (needAnotherIteration);
381381
}
382382

383-
std::string
384-
AccessSummaryAnalysis::SubAccessSummary::getDescription(SILType BaseType,
385-
SILModule &M) const {
383+
std::string AccessSummaryAnalysis::SubAccessSummary::getDescription(
384+
SILType BaseType, SILModule &M, TypeExpansionContext context) const {
386385
std::string sbuf;
387386
llvm::raw_string_ostream os(sbuf);
388387

389-
os << AccessSummaryAnalysis::getSubPathDescription(BaseType, SubPath, M);
388+
os << AccessSummaryAnalysis::getSubPathDescription(BaseType, SubPath, M,
389+
context);
390390

391391
if (!SubPath->isRoot())
392392
os << " ";
@@ -409,9 +409,8 @@ void AccessSummaryAnalysis::ArgumentSummary::getSortedSubAccesses(
409409
assert(storage.size() == SubAccesses.size());
410410
}
411411

412-
std::string
413-
AccessSummaryAnalysis::ArgumentSummary::getDescription(SILType BaseType,
414-
SILModule &M) const {
412+
std::string AccessSummaryAnalysis::ArgumentSummary::getDescription(
413+
SILType BaseType, SILModule &M, TypeExpansionContext context) const {
415414
std::string sbuf;
416415
llvm::raw_string_ostream os(sbuf);
417416
os << "[";
@@ -425,7 +424,7 @@ AccessSummaryAnalysis::ArgumentSummary::getDescription(SILType BaseType,
425424
if (index > 0) {
426425
os << ", ";
427426
}
428-
os << subAccess.getDescription(BaseType, M);
427+
os << subAccess.getDescription(BaseType, M, context);
429428
++index;
430429
}
431430
os << "]";
@@ -551,7 +550,8 @@ AccessSummaryAnalysis::findSubPathAccessed(BeginAccessInst *BAI) {
551550
/// that stored-property relaxation supports: struct stored properties
552551
/// and tuple elements.
553552
std::string AccessSummaryAnalysis::getSubPathDescription(
554-
SILType baseType, const IndexTrieNode *subPath, SILModule &M) {
553+
SILType baseType, const IndexTrieNode *subPath, SILModule &M,
554+
TypeExpansionContext context) {
555555
// Walk the trie to the root to collect the sequence (in reverse order).
556556
llvm::SmallVector<unsigned, 4> reversedIndices;
557557
const IndexTrieNode *I = subPath;
@@ -570,7 +570,7 @@ std::string AccessSummaryAnalysis::getSubPathDescription(
570570
if (StructDecl *D = containingType.getStructOrBoundGenericStruct()) {
571571
VarDecl *var = D->getStoredProperties()[index];
572572
os << var->getBaseName();
573-
containingType = containingType.getFieldType(var, M);
573+
containingType = containingType.getFieldType(var, M, context);
574574
continue;
575575
}
576576

@@ -635,7 +635,8 @@ void AccessSummaryAnalysis::FunctionSummary::print(raw_ostream &os,
635635
}
636636
SILArgument *arg = fn->getArgument(i);
637637
SILModule &m = fn->getModule();
638-
os << getAccessForArgument(i).getDescription(arg->getType(), m);
638+
os << getAccessForArgument(i).getDescription(arg->getType(), m,
639+
TypeExpansionContext(*fn));
639640
}
640641

641642
os << ")";

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ static bool typedAccessTBAAMayAlias(SILType LTy, SILType RTy,
475475

476476
// If one type is an aggregate and it contains the other type then the record
477477
// reference may alias the aggregate reference.
478-
if (LTy.aggregateContainsRecord(RTy, Mod) ||
479-
RTy.aggregateContainsRecord(LTy, Mod))
478+
if (LTy.aggregateContainsRecord(RTy, Mod, F.getTypeExpansionContext()) ||
479+
RTy.aggregateContainsRecord(LTy, Mod, F.getTypeExpansionContext()))
480480
return true;
481481

482482
// FIXME: All the code following could be made significantly more aggressive

lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ static bool findRecursiveRefType(SILType Ty, const SILFunction &F,
7070

7171
if (auto *Str = Ty.getStructOrBoundGenericStruct()) {
7272
for (auto *Field : Str->getStoredProperties()) {
73-
if (findRecursiveRefType(Ty.getFieldType(Field, Mod), F, mustBeRef))
73+
if (findRecursiveRefType(
74+
Ty.getFieldType(Field, Mod, F.getTypeExpansionContext()), F,
75+
mustBeRef))
7476
return true;
7577
}
7678
return false;
@@ -84,9 +86,10 @@ static bool findRecursiveRefType(SILType Ty, const SILFunction &F,
8486
}
8587
if (auto En = Ty.getEnumOrBoundGenericEnum()) {
8688
for (auto *ElemDecl : En->getAllElements()) {
87-
if (ElemDecl->hasAssociatedValues()
88-
&& findRecursiveRefType(Ty.getEnumElementType(ElemDecl, Mod), F,
89-
mustBeRef))
89+
if (ElemDecl->hasAssociatedValues() &&
90+
findRecursiveRefType(
91+
Ty.getEnumElementType(ElemDecl, Mod, F.getTypeExpansionContext()),
92+
F, mustBeRef))
9093
return true;
9194
}
9295
return false;

lib/SILOptimizer/Analysis/TypeExpansionAnalysis.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ using namespace swift;
2424
// memory usage of this cache.
2525
static const int TypeExpansionAnalysisMaxCacheSize = 4096;
2626

27-
const ProjectionPathList&
28-
TypeExpansionAnalysis::getTypeExpansion(SILType B, SILModule *Mod) {
27+
const ProjectionPathList &
28+
TypeExpansionAnalysis::getTypeExpansion(SILType B, SILModule *Mod,
29+
TypeExpansionContext context) {
2930
// Check whether we have the type expansion.
3031
auto Iter = ExpansionCache.find(B);
32+
// TODO: FIXME: ExpansionCache.find((B,context))
33+
//
34+
//
3135
if (Iter != ExpansionCache.end()) {
3236
return Iter->second;
3337
}
@@ -46,7 +50,8 @@ TypeExpansionAnalysis::getTypeExpansion(SILType B, SILModule *Mod) {
4650
}
4751

4852
// Build the type expansion for the leaf nodes.
49-
ProjectionPath::expandTypeIntoLeafProjectionPaths(B, Mod, ExpansionCache[B]);
53+
ProjectionPath::expandTypeIntoLeafProjectionPaths(B, Mod, context,
54+
ExpansionCache[B]);
5055
return ExpansionCache[B];
5156
}
5257

0 commit comments

Comments
 (0)