-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[flang] Ensure lowering diagnostic handler does not outlive lowering #151608
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
[flang] Ensure lowering diagnostic handler does not outlive lowering #151608
Conversation
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.
@llvm/pr-subscribers-flang-fir-hlfir Author: Razvan Lupusoru (razvanlupusoru) ChangesWhen 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. Full diff: https://github.com/llvm/llvm-project/pull/151608.diff 3 Files Affected:
diff --git a/flang/include/flang/Lower/Bridge.h b/flang/include/flang/Lower/Bridge.h
index a8c2bcfda31c1..3710e74bf38d9 100644
--- a/flang/include/flang/Lower/Bridge.h
+++ b/flang/include/flang/Lower/Bridge.h
@@ -76,6 +76,7 @@ class LoweringBridge {
loweringOptions, envDefaults, languageFeatures,
targetMachine, targetOptions, codeGenOptions);
}
+ ~LoweringBridge();
//===--------------------------------------------------------------------===//
// Getters
@@ -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
diff --git a/flang/include/flang/Lower/LoweringOptions.def b/flang/include/flang/Lower/LoweringOptions.def
index 8135704971aa4..39f197d8d35c8 100644
--- a/flang/include/flang/Lower/LoweringOptions.def
+++ b/flang/include/flang/Lower/LoweringOptions.def
@@ -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)
+
#undef LOWERINGOPT
#undef ENUM_LOWERINGOPT
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 1adfb96ab9a98..e2ba20f968fd1 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -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;
@@ -6769,6 +6772,12 @@ Fortran::lower::LoweringBridge::LoweringBridge(
fir::setCommandline(*module, *cgOpts.RecordCommandLine);
}
+Fortran::lower::LoweringBridge::~LoweringBridge() {
+ if (diagHandlerID) {
+ context.getDiagEngine().eraseHandler(*diagHandlerID);
+ }
+}
+
void Fortran::lower::genCleanUpInRegionIfAny(
mlir::Location loc, fir::FirOpBuilder &builder, mlir::Region ®ion,
Fortran::lower::StatementContext &context) {
|
flang/lib/Lower/Bridge.cpp
Outdated
@@ -6769,6 +6772,12 @@ Fortran::lower::LoweringBridge::LoweringBridge( | |||
fir::setCommandline(*module, *cgOpts.RecordCommandLine); | |||
} | |||
|
|||
Fortran::lower::LoweringBridge::~LoweringBridge() { | |||
if (diagHandlerID) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces
|
||
/// When true, it registers MLIRDiagnosticsHandler for the duration | ||
/// of the lowering pipeline. | ||
ENUM_LOWERINGOPT(RegisterMLIRDiagnosticsHandler, unsigned, 1, 1) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…lvm#151608) 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.
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.