Skip to content

Commit 574dd2b

Browse files
committed
Use correct associated context for SIL parsing
Adjust `SILModule::createEmptyModule` to accept a FileUnit or ModuleDecl, and pass the corresponding context for SIL generation and parsing. This change means that SIL parsing will now correctly use a SourceFile associated context when in single-file mode.
1 parent 27ed8b0 commit 574dd2b

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

include/swift/SIL/SILModule.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ enum class SILStage {
107107
/// when a Swift compilation context is lowered to SIL.
108108
class SILModule {
109109
friend class SILFunctionBuilder;
110-
friend class SILGenSourceFileRequest;
111-
friend class SILGenWholeModuleRequest;
112110

113111
public:
114112
using FunctionListType = llvm::ilist<SILFunction>;
@@ -276,8 +274,8 @@ class SILModule {
276274
/// invalidation message is sent.
277275
llvm::SetVector<DeleteNotificationHandler*> NotificationHandlers;
278276

279-
SILModule(ModuleDecl *M, Lowering::TypeConverter &TC,
280-
const SILOptions &Options, const DeclContext *associatedDC);
277+
SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
278+
Lowering::TypeConverter &TC, const SILOptions &Options);
281279

282280
SILModule(const SILModule&) = delete;
283281
void operator=(const SILModule&) = delete;
@@ -348,11 +346,14 @@ class SILModule {
348346
/// Erase a global SIL variable from the module.
349347
void eraseGlobalVariable(SILGlobalVariable *G);
350348

351-
/// Create and return an empty SIL module that we can
352-
/// later parse SIL bodies directly into, without converting from an AST.
349+
/// Create and return an empty SIL module suitable for generating or parsing
350+
/// SIL into.
351+
///
352+
/// \param context The associated decl context. This should be a FileUnit in
353+
/// single-file mode, and a ModuleDecl in whole-module mode.
353354
static std::unique_ptr<SILModule>
354-
createEmptyModule(ModuleDecl *M, Lowering::TypeConverter &TC,
355-
const SILOptions &Options);
355+
createEmptyModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
356+
Lowering::TypeConverter &TC, const SILOptions &Options);
356357

357358
/// Get the Swift module associated with this SIL module.
358359
ModuleDecl *getSwiftModule() const { return TheSwiftModule; }

lib/SIL/IR/SILModule.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,17 @@ class SILModule::SerializationCallback final
9292
}
9393
};
9494

95-
SILModule::SILModule(ModuleDecl *SwiftModule, TypeConverter &TC,
96-
const SILOptions &Options, const DeclContext *associatedDC)
97-
: TheSwiftModule(SwiftModule),
98-
AssociatedDeclContext(associatedDC),
99-
Stage(SILStage::Raw), Options(Options), serialized(false),
95+
SILModule::SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
96+
Lowering::TypeConverter &TC, const SILOptions &Options)
97+
: Stage(SILStage::Raw), Options(Options), serialized(false),
10098
SerializeSILAction(), Types(TC) {
101-
assert(AssociatedDeclContext);
99+
assert(!context.isNull());
100+
if (auto *file = context.dyn_cast<FileUnit *>()) {
101+
AssociatedDeclContext = file;
102+
} else {
103+
AssociatedDeclContext = context.get<ModuleDecl *>();
104+
}
105+
TheSwiftModule = AssociatedDeclContext->getParentModule();
102106

103107
// We always add the base SILModule serialization callback.
104108
std::unique_ptr<DeserializationNotificationHandler> callback(
@@ -123,10 +127,10 @@ SILModule::~SILModule() {
123127
}
124128
}
125129

126-
std::unique_ptr<SILModule>
127-
SILModule::createEmptyModule(ModuleDecl *M, TypeConverter &TC,
128-
const SILOptions &Options) {
129-
return std::unique_ptr<SILModule>(new SILModule(M, TC, Options, M));
130+
std::unique_ptr<SILModule> SILModule::createEmptyModule(
131+
llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
132+
Lowering::TypeConverter &TC, const SILOptions &Options) {
133+
return std::unique_ptr<SILModule>(new SILModule(context, TC, Options));
130134
}
131135

132136
ASTContext &SILModule::getASTContext() const {

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ ParseSILModuleRequest::evaluate(Evaluator &evaluator,
115115
auto bufferID = SF->getBufferID();
116116
assert(bufferID);
117117

118-
auto *mod = SF->getParentModule();
119-
auto silMod = SILModule::createEmptyModule(mod, desc.conv, desc.opts);
118+
auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
119+
desc.opts);
120120
SILParserState parserState(silMod.get());
121121
Parser parser(*bufferID, *SF, parserState.Impl.get());
122122
PrettyStackTraceParser StackTrace(parser);
@@ -125,7 +125,7 @@ ParseSILModuleRequest::evaluate(Evaluator &evaluator,
125125
if (hadError) {
126126
// The rest of the SIL pipeline expects well-formed SIL, so if we encounter
127127
// a parsing error, just return an empty SIL module.
128-
return SILModule::createEmptyModule(mod, desc.conv, desc.opts);
128+
return SILModule::createEmptyModule(desc.context, desc.conv, desc.opts);
129129
}
130130
return silMod;
131131
}

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,7 @@ SILGenSourceFileRequest::evaluate(Evaluator &evaluator,
18671867

18681868
auto *unit = desc.context.get<FileUnit *>();
18691869
auto *mod = unit->getParentModule();
1870-
auto M = std::unique_ptr<SILModule>(
1871-
new SILModule(mod, desc.conv, desc.opts, unit));
1870+
auto M = SILModule::createEmptyModule(desc.context, desc.conv, desc.opts);
18721871
SILGenModuleRAII scope(*M, mod);
18731872

18741873
if (auto *file = dyn_cast<SourceFile>(unit)) {
@@ -1890,8 +1889,7 @@ SILGenWholeModuleRequest::evaluate(Evaluator &evaluator,
18901889
}
18911890

18921891
auto *mod = desc.context.get<ModuleDecl *>();
1893-
auto M = std::unique_ptr<SILModule>(
1894-
new SILModule(mod, desc.conv, desc.opts, mod));
1892+
auto M = SILModule::createEmptyModule(desc.context, desc.conv, desc.opts);
18951893
SILGenModuleRAII scope(*M, mod);
18961894

18971895
for (auto file : mod->getFiles()) {

test/IRGen/multi_file_resilience.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
// RUN: %target-swift-frontend -module-name main -I %t -emit-ir -primary-file %s %S/Inputs/OtherModule.swift | %FileCheck %s -DINT=i%target-ptrsize
99

10+
// Check that we correctly handle resilience when parsing as SIL + SIB.
11+
// RUN: %target-swift-frontend -emit-sib -module-name main %S/Inputs/OtherModule.swift -I %t -o %t/other.sib
12+
// RUN: %target-swift-frontend -emit-silgen -module-name main -primary-file %s %S/Inputs/OtherModule.swift -I %t -o %t/main.sil
13+
// RUN: %target-swift-frontend -emit-ir -module-name main -primary-file %t/main.sil %t/other.sib -I %t | %FileCheck %s -DINT=i%target-ptrsize
14+
1015
// This is a single-module version of the test case in
1116
// multi_module_resilience.
1217
// rdar://39763787

0 commit comments

Comments
 (0)