Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions flang/include/flang/Lower/Bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class LoweringBridge {
loweringOptions, envDefaults, languageFeatures,
targetMachine, targetOptions, codeGenOptions);
}
~LoweringBridge();

//===--------------------------------------------------------------------===//
// Getters
Expand Down Expand Up @@ -174,6 +175,7 @@ class LoweringBridge {
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults;
const Fortran::common::LanguageFeatureControl &languageFeatures;
std::set<std::string> tempNames;
std::optional<mlir::DiagnosticEngine::HandlerID> diagHandlerID;
};

} // namespace lower
Expand Down
5 changes: 5 additions & 0 deletions flang/include/flang/Lower/LoweringOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,10 @@ ENUM_LOWERINGOPT(SkipExternalRttiDefinition, unsigned, 1, 0)
/// If false, lower to the complex dialect of MLIR.
/// On by default.
ENUM_LOWERINGOPT(ComplexDivisionToRuntime, unsigned, 1, 1)

/// When true, it registers MLIRDiagnosticsHandler for the duration
/// of the lowering pipeline.
ENUM_LOWERINGOPT(RegisterMLIRDiagnosticsHandler, unsigned, 1, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case when we don't need to register it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an appropriate handler is already registered with the MLIRContext. Lowering should not need to register its own if one can already handle all of the MLIR diagnostics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that makes sense. Thanks for the explanation!


#undef LOWERINGOPT
#undef ENUM_LOWERINGOPT
51 changes: 30 additions & 21 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6707,27 +6707,30 @@ Fortran::lower::LoweringBridge::LoweringBridge(
loweringOptions{loweringOptions}, envDefaults{envDefaults},
languageFeatures{languageFeatures} {
// Register the diagnostic handler.
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
llvm::raw_ostream &os = llvm::errs();
switch (diag.getSeverity()) {
case mlir::DiagnosticSeverity::Error:
os << "error: ";
break;
case mlir::DiagnosticSeverity::Remark:
os << "info: ";
break;
case mlir::DiagnosticSeverity::Warning:
os << "warning: ";
break;
default:
break;
}
if (!mlir::isa<mlir::UnknownLoc>(diag.getLocation()))
os << diag.getLocation() << ": ";
os << diag << '\n';
os.flush();
return mlir::success();
});
if (loweringOptions.getRegisterMLIRDiagnosticsHandler()) {
diagHandlerID =
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
llvm::raw_ostream &os = llvm::errs();
switch (diag.getSeverity()) {
case mlir::DiagnosticSeverity::Error:
os << "error: ";
break;
case mlir::DiagnosticSeverity::Remark:
os << "info: ";
break;
case mlir::DiagnosticSeverity::Warning:
os << "warning: ";
break;
default:
break;
}
if (!mlir::isa<mlir::UnknownLoc>(diag.getLocation()))
os << diag.getLocation() << ": ";
os << diag << '\n';
os.flush();
return mlir::success();
});
}

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

Fortran::lower::LoweringBridge::~LoweringBridge() {
if (diagHandlerID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces

context.getDiagEngine().eraseHandler(*diagHandlerID);
}
}

void Fortran::lower::genCleanUpInRegionIfAny(
mlir::Location loc, fir::FirOpBuilder &builder, mlir::Region &region,
Fortran::lower::StatementContext &context) {
Expand Down