Skip to content

Commit 2d9b63e

Browse files
committed
A SILModule Always Has An Associated Context
Now that the integrated REPL has been removed, there is always an associated decl context, and we can assert as such.
1 parent cfd0362 commit 2d9b63e

File tree

9 files changed

+12
-55
lines changed

9 files changed

+12
-55
lines changed

include/swift/SIL/SILModule.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,15 @@ class SILModule {
366366
ASTContext &getASTContext() const;
367367
SourceManager &getSourceManager() const { return getASTContext().SourceMgr; }
368368

369-
/// Get the Swift DeclContext associated with this SIL module.
369+
/// Get the Swift DeclContext associated with this SIL module. This is never
370+
/// null.
370371
///
371372
/// All AST declarations within this context are assumed to have been fully
372373
/// processed as part of generating this module. This allows certain passes
373374
/// to make additional assumptions about these declarations.
374375
///
375376
/// If this is the same as TheSwiftModule, the entire module is being
376-
/// compiled as a single unit. If this is null, no context-based assumptions
377-
/// can be made.
377+
/// compiled as a single unit.
378378
const DeclContext *getAssociatedContext() const {
379379
return AssociatedDeclContext;
380380
}

lib/IRGen/IRGenModule.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,14 +1203,9 @@ static bool isFirstObjectFileInModule(IRGenModule &IGM) {
12031203
if (IGM.getSILModule().isWholeModule())
12041204
return IGM.IRGen.getPrimaryIGM() == &IGM;
12051205

1206-
const DeclContext *DC = IGM.getSILModule().getAssociatedContext();
1207-
if (!DC)
1208-
return false;
1209-
1210-
assert(!isa<ModuleDecl>(DC) && "that would be a whole module build");
1211-
assert(isa<FileUnit>(DC) && "compiling something smaller than a file?");
1212-
ModuleDecl *containingModule = cast<FileUnit>(DC)->getParentModule();
1213-
return containingModule->getFiles().front() == DC;
1206+
auto *file = cast<FileUnit>(IGM.getSILModule().getAssociatedContext());
1207+
auto *containingModule = file->getParentModule();
1208+
return containingModule->getFiles().front() == file;
12141209
}
12151210

12161211
void IRGenModule::emitAutolinkInfo() {

lib/SIL/IR/SIL.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ bool SILModule::isTypeMetadataAccessible(CanType type) {
127127
// Private declarations are inaccessible from different files unless
128128
// this is WMO and we're in the same module.
129129
case FormalLinkage::Private: {
130-
// The only time we don't have an associated DC is in the
131-
// integrated REPL, where we also don't have a concept of other
132-
// source files within the current module.
133-
if (!AssociatedDeclContext)
134-
return (decl->getModuleContext() != getSwiftModule());
135-
136130
// The associated DC should be either a SourceFile or, in WMO mode,
137131
// a ModuleDecl. In the WMO modes, IRGen will ensure that private
138132
// declarations are usable throughout the module. Therefore, in

lib/SIL/IR/SILModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ SILModule::SILModule(ModuleDecl *SwiftModule, TypeConverter &TC,
9999
AssociatedDeclContext(associatedDC),
100100
Stage(SILStage::Raw), wholeModule(wholeModule), Options(Options),
101101
serialized(false), SerializeSILAction(), Types(TC) {
102+
assert(AssociatedDeclContext);
103+
102104
// We always add the base SILModule serialization callback.
103105
std::unique_ptr<DeserializationNotificationHandler> callback(
104106
new SILModule::SerializationCallback());

lib/SILGen/SILGenExpr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,8 +4099,6 @@ static bool isVerbatimNullableTypeInC(SILModule &M, Type ty) {
40994099

41004100
// Other types like UnsafePointer can also be nullable.
41014101
const DeclContext *DC = M.getAssociatedContext();
4102-
if (!DC)
4103-
DC = M.getSwiftModule();
41044102
ty = OptionalType::get(ty);
41054103
return ty->isTriviallyRepresentableIn(ForeignLanguage::C, DC);
41064104
}

lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ static bool isDefaultCaseKnown(ClassHierarchyAnalysis *CHA,
285285
auto *Method = CMI->getMember().getAbstractFunctionDecl();
286286
assert(Method && "not a function");
287287

288-
const DeclContext *DC = AI.getModule().getAssociatedContext();
289-
290288
if (CD->isFinal())
291289
return true;
292290

@@ -295,13 +293,8 @@ static bool isDefaultCaseKnown(ClassHierarchyAnalysis *CHA,
295293
if (CD->checkAncestry(AncestryFlags::ObjC))
296294
return false;
297295

298-
// Without an associated context we cannot perform any
299-
// access-based optimizations.
300-
if (!DC)
301-
return false;
302-
303296
// Only handle classes defined within the SILModule's associated context.
304-
if (!CD->isChildContextOf(DC))
297+
if (!CD->isChildContextOf(AI.getModule().getAssociatedContext()))
305298
return false;
306299

307300
if (!CD->hasAccess())

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ static bool isEffectivelyFinalMethod(FullApplySite applySite, CanType classType,
8989
if (cd && cd->isFinal())
9090
return true;
9191

92-
const DeclContext *dc = applySite.getModule().getAssociatedContext();
93-
94-
// Without an associated context we cannot perform any
95-
// access-based optimizations.
96-
if (!dc)
97-
return false;
98-
9992
auto *cmi = cast<MethodInst>(applySite.getCallee());
10093

10194
if (!calleesAreStaticallyKnowable(applySite.getModule(), cmi->getMember()))
@@ -149,18 +142,11 @@ static bool isEffectivelyFinalMethod(FullApplySite applySite, CanType classType,
149142
/// it is a whole-module compilation.
150143
static bool isKnownFinalClass(ClassDecl *cd, SILModule &module,
151144
ClassHierarchyAnalysis *cha) {
152-
const DeclContext *dc = module.getAssociatedContext();
153-
154145
if (cd->isFinal())
155146
return true;
156147

157-
// Without an associated context we cannot perform any
158-
// access-based optimizations.
159-
if (!dc)
160-
return false;
161-
162148
// Only handle classes defined within the SILModule's associated context.
163-
if (!cd->isChildContextOf(dc))
149+
if (!cd->isChildContextOf(module.getAssociatedContext()))
164150
return false;
165151

166152
if (!cd->hasAccess())

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,12 +1815,8 @@ bool swift::calleesAreStaticallyKnowable(SILModule &module, SILDeclRef decl) {
18151815
/// knowable based on the Decl and the compilation mode?
18161816
bool swift::calleesAreStaticallyKnowable(SILModule &module,
18171817
AbstractFunctionDecl *afd) {
1818-
const DeclContext *assocDC = module.getAssociatedContext();
1819-
if (!assocDC)
1820-
return false;
1821-
18221818
// Only handle members defined within the SILModule's associated context.
1823-
if (!afd->isChildContextOf(assocDC))
1819+
if (!afd->isChildContextOf(module.getAssociatedContext()))
18241820
return false;
18251821

18261822
if (afd->isDynamic()) {
@@ -1859,12 +1855,8 @@ bool swift::calleesAreStaticallyKnowable(SILModule &module,
18591855
// FIXME: Merge this with calleesAreStaticallyKnowable above
18601856
bool swift::calleesAreStaticallyKnowable(SILModule &module,
18611857
EnumElementDecl *eed) {
1862-
const DeclContext *assocDC = module.getAssociatedContext();
1863-
if (!assocDC)
1864-
return false;
1865-
18661858
// Only handle members defined within the SILModule's associated context.
1867-
if (!eed->isChildContextOf(assocDC))
1859+
if (!eed->isChildContextOf(module.getAssociatedContext()))
18681860
return false;
18691861

18701862
if (eed->isDynamic()) {

lib/Serialization/SerializeSIL.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,9 +2645,6 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
26452645
// Go through all SILVTables in SILMod and write them if we should
26462646
// serialize everything.
26472647
// FIXME: Resilience: could write out vtable for fragile classes.
2648-
const DeclContext *assocDC = SILMod->getAssociatedContext();
2649-
assert(assocDC && "cannot serialize SIL without an associated DeclContext");
2650-
(void)assocDC;
26512648
for (const SILVTable &vt : SILMod->getVTables()) {
26522649
if ((ShouldSerializeAll || vt.isSerialized()) &&
26532650
SILMod->shouldSerializeEntitiesAssociatedWithDeclContext(vt.getClass()))

0 commit comments

Comments
 (0)