aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjeanPerier <jperier@nvidia.com>2025-05-09 10:36:38 +0200
committerGitHub <noreply@github.com>2025-05-09 10:36:38 +0200
commit92d2e13b99ba1770e6307af7ed7ee877bfabde8c (patch)
tree82a3e302336a4efc8ef6173730d283dc658a66b6
parent76b3adabea10f5224109138e890770f18edbff48 (diff)
downloadllvm-92d2e13b99ba1770e6307af7ed7ee877bfabde8c.zip
llvm-92d2e13b99ba1770e6307af7ed7ee877bfabde8c.tar.gz
llvm-92d2e13b99ba1770e6307af7ed7ee877bfabde8c.tar.bz2
[flang][driver] do not crash when fc1 process multiple files (#138875)
This is a fix for the issue https://github.com/llvm/llvm-project/issues/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.
-rw-r--r--flang/include/flang/Frontend/CompilerInstance.h6
-rw-r--r--flang/lib/Frontend/CompilerInstance.cpp2
-rw-r--r--flang/lib/Frontend/FrontendAction.cpp2
-rw-r--r--flang/lib/Frontend/FrontendActions.cpp7
-rw-r--r--flang/test/Driver/multiple-fc1-input.f909
5 files changed, 23 insertions, 3 deletions
diff --git a/flang/include/flang/Frontend/CompilerInstance.h b/flang/include/flang/Frontend/CompilerInstance.h
index e37ef5e..4ad95c9 100644
--- a/flang/include/flang/Frontend/CompilerInstance.h
+++ b/flang/include/flang/Frontend/CompilerInstance.h
@@ -147,6 +147,12 @@ public:
/// @name Semantic analysis
/// {
+ Fortran::semantics::SemanticsContext &createNewSemanticsContext() {
+ semaContext =
+ getInvocation().getSemanticsCtx(*allCookedSources, getTargetMachine());
+ return *semaContext;
+ }
+
Fortran::semantics::SemanticsContext &getSemanticsContext() {
return *semaContext;
}
diff --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp
index f7ed969..cbd2c58 100644
--- a/flang/lib/Frontend/CompilerInstance.cpp
+++ b/flang/lib/Frontend/CompilerInstance.cpp
@@ -162,8 +162,6 @@ bool CompilerInstance::executeAction(FrontendAction &act) {
allSources->set_encoding(invoc.getFortranOpts().encoding);
if (!setUpTargetMachine())
return false;
- // Create the semantics context
- semaContext = invoc.getSemanticsCtx(*allCookedSources, getTargetMachine());
// Set options controlling lowering to FIR.
invoc.setLoweringOptions();
diff --git a/flang/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp
index ab77d14..d178fd6 100644
--- a/flang/lib/Frontend/FrontendAction.cpp
+++ b/flang/lib/Frontend/FrontendAction.cpp
@@ -183,7 +183,7 @@ bool FrontendAction::runSemanticChecks() {
// Transfer any pending non-fatal messages from parsing to semantics
// so that they are merged and all printed in order.
- auto &semanticsCtx{ci.getSemanticsContext()};
+ auto &semanticsCtx{ci.createNewSemanticsContext()};
semanticsCtx.messages().Annex(std::move(ci.getParsing().messages()));
semanticsCtx.set_debugModuleWriter(ci.getInvocation().getDebugModuleDir());
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index c1f47b1..e5a15c5 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -171,6 +171,10 @@ static void addDependentLibs(mlir::ModuleOp mlirModule, CompilerInstance &ci) {
}
bool CodeGenAction::beginSourceFileAction() {
+ // Delete previous LLVM module depending on old context before making a new
+ // one.
+ if (llvmModule)
+ llvmModule.reset(nullptr);
llvmCtx = std::make_unique<llvm::LLVMContext>();
CompilerInstance &ci = this->getInstance();
mlir::DefaultTimingManager &timingMgr = ci.getTimingManager();
@@ -197,6 +201,9 @@ bool CodeGenAction::beginSourceFileAction() {
return true;
}
+ // Reset MLIR module if it was set before overriding the old context.
+ if (mlirModule)
+ mlirModule = mlir::OwningOpRef<mlir::ModuleOp>(nullptr);
// Load the MLIR dialects required by Flang
mlirCtx = std::make_unique<mlir::MLIRContext>();
fir::support::loadDialects(*mlirCtx);
diff --git a/flang/test/Driver/multiple-fc1-input.f90 b/flang/test/Driver/multiple-fc1-input.f90
new file mode 100644
index 0000000..57f7c5e
--- /dev/null
+++ b/flang/test/Driver/multiple-fc1-input.f90
@@ -0,0 +1,9 @@
+! Test that flang -fc1 can be called with several input files without
+! crashing.
+! Regression tests for: https://github.com/llvm/llvm-project/issues/137126
+
+! RUN: %flang_fc1 -emit-fir %s %s -o - | FileCheck %s
+subroutine foo()
+end subroutine
+! CHECK: func @_QPfoo()
+! CHECK: func @_QPfoo()