-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-repl] Drop CodeGen module when an input has parse errors #169989
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
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-clang Author: Anutosh Bhat (anutosh491) ChangesWe can see the following while running clang-repl in C mode In debug mode while dumping the generated Module, i see this Basically I see that CodeGen emits IR for a cell before we know whether DiagnosticsEngine has an error. For C code like Previously, when Full diff: https://github.com/llvm/llvm-project/pull/169989.diff 3 Files Affected:
diff --git a/clang/lib/Interpreter/IncrementalAction.cpp b/clang/lib/Interpreter/IncrementalAction.cpp
index 3d489fce54bc6..e2b9d13017ada 100644
--- a/clang/lib/Interpreter/IncrementalAction.cpp
+++ b/clang/lib/Interpreter/IncrementalAction.cpp
@@ -120,6 +120,17 @@ std::unique_ptr<llvm::Module> IncrementalAction::GenModule() {
return nullptr;
}
+void IncrementalAction::discardCurrentCodeGenModule() {
+ if (CodeGenerator *CG = getCodeGen()) {
+ if (auto *CurM = CG->GetModule()) {
+ llvm::LLVMContext &Ctx = CurM->getContext();
+ std::string Name = CurM->getName().str();
+ std::unique_ptr<llvm::Module> Dead(CG->ReleaseModule());
+ CG->StartModule(Name, Ctx);
+ }
+ }
+}
+
CodeGenerator *IncrementalAction::getCodeGen() const {
FrontendAction *WrappedAct = getWrapped();
if (!WrappedAct || !WrappedAct->hasIRSupport())
diff --git a/clang/lib/Interpreter/IncrementalAction.h b/clang/lib/Interpreter/IncrementalAction.h
index 725cdd0c27cf4..485cfaa45f793 100644
--- a/clang/lib/Interpreter/IncrementalAction.h
+++ b/clang/lib/Interpreter/IncrementalAction.h
@@ -74,6 +74,8 @@ class IncrementalAction : public WrapperFrontendAction {
/// Generate an LLVM module for the most recent parsed input.
std::unique_ptr<llvm::Module> GenModule();
+
+ void discardCurrentCodeGenModule();
};
class InProcessPrintingASTConsumer final : public MultiplexConsumer {
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index bf08911e23533..53379603c26da 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -82,6 +82,7 @@ IncrementalParser::ParseOrWrapTopLevelDecl() {
DiagnosticsEngine &Diags = S.getDiagnostics();
if (Diags.hasErrorOccurred()) {
+ Act->discardCurrentCodeGenModule();
CleanUpPTU(C.getTranslationUnitDecl());
Diags.Reset(/*soft=*/true);
|
|
So this fix discards the current CodeGen module whenever |
| /// Generate an LLVM module for the most recent parsed input. | ||
| std::unique_ptr<llvm::Module> GenModule(); | ||
|
|
||
| void discardCurrentCodeGenModule(); |
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.
Can add a comment explaining the purpose behind the function (just like we do for above functions) if needed !
| std::string Name = CurM->getName().str(); | ||
| std::unique_ptr<llvm::Module> Dead(CG->ReleaseModule()); | ||
| CG->StartModule(Name, Ctx); | ||
| } |
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.
Used the variable name Dead for now ! Can you use Discarded or something along those lines I think
|
Looks like the goto approach for fixing this to me. Can add tests if we agree with the same ! |
We can see the following while running clang-repl in C mode
In debug mode while dumping the generated Module, i see this
Basically I see that CodeGen emits IR for a cell before we know whether DiagnosticsEngine has an error. For C code like
printf("hi\n");without <stdio.h>, Sema emits a diagnostic but still produces a "codegen-able"TopLevelStmt, so theprintfcall is IR-generated into the current module.Previously, when
Diags.hasErrorOccurred()was true, we only cleaned up the PTU AST and left the CodeGen module untouched. The next successful cell then calledGenModule(), which returned that same module (now also containing the next cell’s IR), causing side effects from the failed cell (e.g. printf)