Skip to content

[flang][driver] do not crash when fc1 process multiple files #138875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions flang/include/flang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ class CompilerInstance {
/// @name Semantic analysis
/// {

Fortran::semantics::SemanticsContext &createNewSemanticsContext() {
semaContext =
getInvocation().getSemanticsCtx(*allCookedSources, getTargetMachine());
return *semaContext;
}

Fortran::semantics::SemanticsContext &getSemanticsContext() {
return *semaContext;
}
Expand Down
2 changes: 0 additions & 2 deletions flang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
7 changes: 7 additions & 0 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Driver/multiple-fc1-input.f90
Original file line number Diff line number Diff line change
@@ -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()
Loading