[clang] [clang-repl] Emit the RTTI of a class with a key function only once (PR #239957)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 04:06:35 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Jonas Rembser (guitargeek)

<details>
<summary>Changes</summary>

The typeinfo of a dynamic class with a key function is emitted along with its vtable, into the module of the input that defines the key function. An expression in a later input that needs the typeinfo (`typeid(A)`, `throw A()`) emitted it again, because the key function is defined in the same translation unit. The JIT then failed, and the input's other symbols were not found:
```
  clang-repl> struct A { virtual void f(); };
  clang-repl> void A::f() {}
  clang-repl> try { throw A(); } catch (A&) {}
  error: In incr_module_3, duplicate definition of symbol '_ZTI1A'
```

#<!-- -->185648 fixed the same problem for the vtable itself.

In incremental mode, treat the RTTI of a class whose vtable was emitted with external linkage for an earlier input as external.

Assisted-by: Claude Opus 5.5

This is another PR that addresses a problem when using Clang-Repl instead of Cling in ROOT.


---
Full diff: https://github.com/llvm/llvm-project/pull/239957.diff


3 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.h (+7) 
- (modified) clang/lib/CodeGen/ItaniumCXXABI.cpp (+8) 
- (added) clang/test/Interpreter/rtti-key-function.cpp (+34) 


``````````diff
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index e2bd6bc4701fda..ba9db0e7769663 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1564,6 +1564,13 @@ class CodeGenModule : public CodeGenTypeCache {
   /// of the given class.
   llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
 
+  /// In incremental compilation, whether the vtable of \p RD was emitted
+  /// because its key function was defined, possibly into the module of an
+  /// earlier input.
+  bool isVTableEmittedWithKeyFunction(const CXXRecordDecl *RD) const {
+    return EmittedVTables.contains(RD);
+  }
+
   /// Returns true if a vtable with the given linkage may be emitted with more
   /// than one address in the program, because the vtable is weak and the
   /// target's ABI allows weak vtables to be duplicated across images.
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 5bbb742a7b1efa..72264b784f6223 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -3654,6 +3654,14 @@ static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
     if (CGM.getTriple().isOSCygMing())
       return false;
 
+    // In incremental compilation (clang-repl), the RTTI was emitted with the
+    // vtable into the module of the input that defined the key function. A
+    // later input refers to it; a second definition would clash in the JIT.
+    if (CGM.getLangOpts().IncrementalExtensions &&
+        CGM.isVTableEmittedWithKeyFunction(RD) &&
+        CGM.getVTableLinkage(RD) == llvm::GlobalValue::ExternalLinkage)
+      return true;
+
     if (CGM.getVTables().isVTableExternal(RD)) {
       if (CGM.getTarget().hasPS4DLLImportExport())
         return true;
diff --git a/clang/test/Interpreter/rtti-key-function.cpp b/clang/test/Interpreter/rtti-key-function.cpp
new file mode 100644
index 00000000000000..bbe589c4d216ed
--- /dev/null
+++ b/clang/test/Interpreter/rtti-key-function.cpp
@@ -0,0 +1,34 @@
+// REQUIRES: host-supports-jit
+// UNSUPPORTED: system-windows
+// RUN: cat %s | clang-repl | FileCheck %s
+
+// The typeinfo of a class with a key function is emitted with its vtable,
+// into the module of the input that defines the key function. Later inputs
+// that need it must not define it again: the JIT reported
+//     duplicate definition of symbol '_ZTI1A'
+
+extern "C" int printf(const char *, ...);
+namespace std { class type_info; }
+
+struct A { virtual void f(); };
+void A::f() {}
+const std::type_info *t1 = &typeid(A);
+const std::type_info *t2 = &typeid(A);
+printf("same typeinfo: %d\n", t1 == t2);
+// CHECK: same typeinfo: 1
+
+// The key function and the use of the typeinfo in one input.
+struct B { virtual void g(); };
+void B::g() {} const std::type_info *t3 = &typeid(B);
+const std::type_info *t4 = &typeid(B);
+printf("same typeinfo: %d\n", t3 == t4);
+// CHECK: same typeinfo: 1
+
+// Without a key function, the typeinfo is emitted where it is used.
+struct C { virtual void h() {} };
+const std::type_info *t5 = &typeid(C);
+const std::type_info *t6 = &typeid(C);
+printf("same typeinfo: %d\n", t5 == t6);
+// CHECK: same typeinfo: 1
+
+%quit

``````````

</details>


https://github.com/llvm/llvm-project/pull/239957


More information about the cfe-commits mailing list