Skip to content

Commit 0294f24

Browse files
author
Razvan Lupusoru
committed
[flang] Ensure lowering diagnostic handler does not outlive lowering
When the LoweringBridge is created, it registers an MLIR Diagnostics handler with the MLIRContext. However, it never deregisters it once lowering is finished. This fixes this particular scenario. It also makes it so that the Diagnostics handler is optional.
1 parent 507b879 commit 0294f24

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

flang/include/flang/Lower/Bridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class LoweringBridge {
7676
loweringOptions, envDefaults, languageFeatures,
7777
targetMachine, targetOptions, codeGenOptions);
7878
}
79+
~LoweringBridge();
7980

8081
//===--------------------------------------------------------------------===//
8182
// Getters
@@ -174,6 +175,7 @@ class LoweringBridge {
174175
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults;
175176
const Fortran::common::LanguageFeatureControl &languageFeatures;
176177
std::set<std::string> tempNames;
178+
std::optional<mlir::DiagnosticEngine::HandlerID> diagHandlerID;
177179
};
178180

179181
} // namespace lower

flang/include/flang/Lower/LoweringOptions.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,10 @@ ENUM_LOWERINGOPT(SkipExternalRttiDefinition, unsigned, 1, 0)
7474
/// If false, lower to the complex dialect of MLIR.
7575
/// On by default.
7676
ENUM_LOWERINGOPT(ComplexDivisionToRuntime, unsigned, 1, 1)
77+
78+
/// When true, it registers MLIRDiagnosticsHandler for the duration
79+
/// of the lowering pipeline.
80+
ENUM_LOWERINGOPT(RegisterMLIRDiagnosticsHandler, unsigned, 1, 1)
81+
7782
#undef LOWERINGOPT
7883
#undef ENUM_LOWERINGOPT

flang/lib/Lower/Bridge.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6707,27 +6707,30 @@ Fortran::lower::LoweringBridge::LoweringBridge(
67076707
loweringOptions{loweringOptions}, envDefaults{envDefaults},
67086708
languageFeatures{languageFeatures} {
67096709
// Register the diagnostic handler.
6710-
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
6711-
llvm::raw_ostream &os = llvm::errs();
6712-
switch (diag.getSeverity()) {
6713-
case mlir::DiagnosticSeverity::Error:
6714-
os << "error: ";
6715-
break;
6716-
case mlir::DiagnosticSeverity::Remark:
6717-
os << "info: ";
6718-
break;
6719-
case mlir::DiagnosticSeverity::Warning:
6720-
os << "warning: ";
6721-
break;
6722-
default:
6723-
break;
6724-
}
6725-
if (!mlir::isa<mlir::UnknownLoc>(diag.getLocation()))
6726-
os << diag.getLocation() << ": ";
6727-
os << diag << '\n';
6728-
os.flush();
6729-
return mlir::success();
6730-
});
6710+
if (loweringOptions.getRegisterMLIRDiagnosticsHandler()) {
6711+
diagHandlerID =
6712+
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
6713+
llvm::raw_ostream &os = llvm::errs();
6714+
switch (diag.getSeverity()) {
6715+
case mlir::DiagnosticSeverity::Error:
6716+
os << "error: ";
6717+
break;
6718+
case mlir::DiagnosticSeverity::Remark:
6719+
os << "info: ";
6720+
break;
6721+
case mlir::DiagnosticSeverity::Warning:
6722+
os << "warning: ";
6723+
break;
6724+
default:
6725+
break;
6726+
}
6727+
if (!mlir::isa<mlir::UnknownLoc>(diag.getLocation()))
6728+
os << diag.getLocation() << ": ";
6729+
os << diag << '\n';
6730+
os.flush();
6731+
return mlir::success();
6732+
});
6733+
}
67316734

67326735
auto getPathLocation = [&semanticsContext, &context]() -> mlir::Location {
67336736
std::optional<std::string> path;
@@ -6769,6 +6772,12 @@ Fortran::lower::LoweringBridge::LoweringBridge(
67696772
fir::setCommandline(*module, *cgOpts.RecordCommandLine);
67706773
}
67716774

6775+
Fortran::lower::LoweringBridge::~LoweringBridge() {
6776+
if (diagHandlerID) {
6777+
context.getDiagEngine().eraseHandler(*diagHandlerID);
6778+
}
6779+
}
6780+
67726781
void Fortran::lower::genCleanUpInRegionIfAny(
67736782
mlir::Location loc, fir::FirOpBuilder &builder, mlir::Region &region,
67746783
Fortran::lower::StatementContext &context) {

0 commit comments

Comments
 (0)