[clang] 8082a00 - [CodeGen] Keep track of decls that were deferred and have been emitted.
Jun Zhang via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 13 05:01:10 PDT 2022
Author: Jun Zhang
Date: 2022-07-13T20:00:59+08:00
New Revision: 8082a00286380d0dafa05bfe5ddfe6075b9769f9
URL: https://github.com/llvm/llvm-project/commit/8082a00286380d0dafa05bfe5ddfe6075b9769f9
DIFF: https://github.com/llvm/llvm-project/commit/8082a00286380d0dafa05bfe5ddfe6075b9769f9.diff
LOG: [CodeGen] Keep track of decls that were deferred and have been emitted.
This patch adds a new field called EmittedDeferredDecls in CodeGenModule
that keeps track of decls that were deferred and have been emitted.
The intention of this patch is to solve issues in the incremental c++,
we'll lose info of decls that are lazily emitted when we undo their
usage.
See example below:
clang-repl> inline int foo() { return 42;}
clang-repl> int bar = foo();
clang-repl> %undo
clang-repl> int baz = foo();
JIT session error: Symbols not found: [ _Z3foov ]
error: Failed to materialize symbols: { (main, { baz, $.incr_module_2.inits.0,
orc_init_func.incr_module_2 }) }
Signed-off-by: Jun Zhang <jun at junz.org>
Differential Revision: https://reviews.llvm.org/D128782
Added:
Modified:
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/CodeGen/CodeGenModule.h
clang/test/Interpreter/code-undo.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index a6d58aeef11bd..f832a067237eb 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -445,6 +445,7 @@ void CodeGenModule::checkAliases() {
void CodeGenModule::clear() {
DeferredDeclsToEmit.clear();
+ EmittedDeferredDecls.clear();
if (OpenMPRuntime)
OpenMPRuntime->clear();
}
@@ -510,6 +511,9 @@ static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO,
void CodeGenModule::Release() {
EmitDeferred();
+ DeferredDecls.insert(EmittedDeferredDecls.begin(),
+ EmittedDeferredDecls.end());
+ EmittedDeferredDecls.clear();
EmitVTablesOpportunistically();
applyGlobalValReplacements();
applyReplacements();
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index da43b9616c88e..10b49da27dab7 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -344,6 +344,20 @@ class CodeGenModule : public CodeGenTypeCache {
std::vector<GlobalDecl> DeferredDeclsToEmit;
void addDeferredDeclToEmit(GlobalDecl GD) {
DeferredDeclsToEmit.emplace_back(GD);
+ addEmittedDeferredDecl(GD);
+ }
+
+ /// Decls that were DeferredDecls and have now been emitted.
+ llvm::DenseMap<llvm::StringRef, GlobalDecl> EmittedDeferredDecls;
+
+ void addEmittedDeferredDecl(GlobalDecl GD) {
+ if (!llvm::isa<FunctionDecl>(GD.getDecl()))
+ return;
+ llvm::GlobalVariable::LinkageTypes L = getFunctionLinkage(GD);
+ if (llvm::GlobalValue::isLinkOnceLinkage(L) ||
+ llvm::GlobalValue::isWeakLinkage(L)) {
+ EmittedDeferredDecls[getMangledName(GD)] = GD;
+ }
}
/// List of alias we have emitted. Used to make sure that what they point to
@@ -1516,6 +1530,11 @@ class CodeGenModule : public CodeGenTypeCache {
NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
NewBuilder->TBAA = std::move(TBAA);
+
+ assert(NewBuilder->EmittedDeferredDecls.empty() &&
+ "Still have (unmerged) EmittedDeferredDecls deferred decls");
+
+ NewBuilder->EmittedDeferredDecls = std::move(EmittedDeferredDecls);
}
private:
diff --git a/clang/test/Interpreter/code-undo.cpp b/clang/test/Interpreter/code-undo.cpp
index d825460f3b4a4..9a908d6b7e455 100644
--- a/clang/test/Interpreter/code-undo.cpp
+++ b/clang/test/Interpreter/code-undo.cpp
@@ -20,4 +20,9 @@ int foo() { return 2; }
auto r3 = printf("foo() = %d\n", foo());
// CHECK-NEXT: foo() = 2
+inline int bar() { return 42;}
+auto r4 = bar();
+%undo
+auto r5 = bar();
+
%quit
More information about the cfe-commits
mailing list