Skip to content

Commit 92d2e13

Browse files
authored
[flang][driver] do not crash when fc1 process multiple files (#138875)
This is a fix for the issue #137126 that turned out to be a driver issue. FrontendActions has a loop to process multiple input files and `flang -fc1` accept multiple files, but the semantic, lowering, and llvm codegen actions were not re-entrant, and crash or weird behaviors occurred when processing multiple files with `-fc1`. This patch makes the actions reentrant by cleaning-up the contexts/modules if needed on entry.
1 parent 76b3ada commit 92d2e13

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

flang/include/flang/Frontend/CompilerInstance.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ class CompilerInstance {
147147
/// @name Semantic analysis
148148
/// {
149149

150+
Fortran::semantics::SemanticsContext &createNewSemanticsContext() {
151+
semaContext =
152+
getInvocation().getSemanticsCtx(*allCookedSources, getTargetMachine());
153+
return *semaContext;
154+
}
155+
150156
Fortran::semantics::SemanticsContext &getSemanticsContext() {
151157
return *semaContext;
152158
}

flang/lib/Frontend/CompilerInstance.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ bool CompilerInstance::executeAction(FrontendAction &act) {
162162
allSources->set_encoding(invoc.getFortranOpts().encoding);
163163
if (!setUpTargetMachine())
164164
return false;
165-
// Create the semantics context
166-
semaContext = invoc.getSemanticsCtx(*allCookedSources, getTargetMachine());
167165
// Set options controlling lowering to FIR.
168166
invoc.setLoweringOptions();
169167

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ bool FrontendAction::runSemanticChecks() {
183183

184184
// Transfer any pending non-fatal messages from parsing to semantics
185185
// so that they are merged and all printed in order.
186-
auto &semanticsCtx{ci.getSemanticsContext()};
186+
auto &semanticsCtx{ci.createNewSemanticsContext()};
187187
semanticsCtx.messages().Annex(std::move(ci.getParsing().messages()));
188188
semanticsCtx.set_debugModuleWriter(ci.getInvocation().getDebugModuleDir());
189189

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ static void addDependentLibs(mlir::ModuleOp mlirModule, CompilerInstance &ci) {
171171
}
172172

173173
bool CodeGenAction::beginSourceFileAction() {
174+
// Delete previous LLVM module depending on old context before making a new
175+
// one.
176+
if (llvmModule)
177+
llvmModule.reset(nullptr);
174178
llvmCtx = std::make_unique<llvm::LLVMContext>();
175179
CompilerInstance &ci = this->getInstance();
176180
mlir::DefaultTimingManager &timingMgr = ci.getTimingManager();
@@ -197,6 +201,9 @@ bool CodeGenAction::beginSourceFileAction() {
197201
return true;
198202
}
199203

204+
// Reset MLIR module if it was set before overriding the old context.
205+
if (mlirModule)
206+
mlirModule = mlir::OwningOpRef<mlir::ModuleOp>(nullptr);
200207
// Load the MLIR dialects required by Flang
201208
mlirCtx = std::make_unique<mlir::MLIRContext>();
202209
fir::support::loadDialects(*mlirCtx);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! Test that flang -fc1 can be called with several input files without
2+
! crashing.
3+
! Regression tests for: https://github.com/llvm/llvm-project/issues/137126
4+
5+
! RUN: %flang_fc1 -emit-fir %s %s -o - | FileCheck %s
6+
subroutine foo()
7+
end subroutine
8+
! CHECK: func @_QPfoo()
9+
! CHECK: func @_QPfoo()

0 commit comments

Comments
 (0)