[clang] [CIR] Make redeclarations of a global replace/get the right answer (PR #249001)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 09:53:41 PST 2026


https://github.com/erichkeane created https://github.com/llvm/llvm-project/pull/249001

It was brought up that the order of a vtable init (on #248103) would cause crashes, so this makes sure we get the proper replacement of a symbol when the type changes.  At the moment, this is not testible with relative vtables since we can't create the RTTI pointers yet.

>From 3f69268a215f401c05b67bc098b13d362d67a325 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Fri, 4 Dec 2026 09:50:01 -0800
Subject: [PATCH] [CIR] Make redeclarations of a global replace/get the right
 answer

It was brought up that the order of a vtable init (on #248103) would
cause crashes, so this makes sure we get the proper replacement of a
symbol when the type changes.  At the moment, this is not testible with
relative vtables since we can't create the RTTI pointers yet.
---
 clang/lib/CIR/CodeGen/CIRGenBuilder.cpp    |  4 +++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp     | 32 ++++++++++++----------
 clang/test/CIR/CodeGen/rtti-visibility.cpp | 19 +++++++++++++
 3 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp
index f74c392c7cbe8b..bea30eda05db07 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.cpp
@@ -197,6 +197,10 @@ uint64_t CIRGenBuilderTy::computeOffsetFromGlobalViewIndices(
     } else if (mlir::isa<cir::IntType>(ty)) {
       // Integer element type: the index is a flat element count.
       offset += (int64_t)layout.getTypeAllocSize(ty).getFixedValue() * idx;
+    } else if (mlir::isa<cir::PointerType>(ty)) {
+      // A global declared as a pointer, such as the vtable of a runtime class,
+      // is viewed as an array of pointers.
+      offset += (int64_t)layout.getTypeAllocSize(ty).getFixedValue() * idx;
     } else {
       llvm_unreachable("unexpected type");
     }
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index d0624498c612eb..f63203794bff20 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1220,7 +1220,7 @@ static mlir::Attribute createNewGlobalView(CIRGenModule &cgm,
 
   if (isa<cir::RecordType>(oldTy))
     newPtrTy = cir::PointerType::get(newTy);
-  else if (isa<cir::ArrayType>(oldTy))
+  else if (isa<cir::ArrayType, cir::PointerType>(oldTy))
     newPtrTy = cast<cir::PointerType>(attr.getType());
 
   if (newPtrTy)
@@ -1272,6 +1272,8 @@ static mlir::Attribute getNewInitValue(CIRGenModule &cgm, cir::GlobalOp newGlob,
     return cgm.getBuilder().getConstArray(
         newElements, mlir::cast<cir::ArrayType>(oldArray.getType()));
   }
+  if (auto oldTypeInfo = mlir::dyn_cast<cir::TypeInfoAttr>(oldInit))
+    return cgm.getBuilder().getTypeInfo(getNewInitElements(oldTypeInfo.getData()));
   if (auto oldRecord = mlir::dyn_cast<cir::ConstRecordAttr>(oldInit)) {
     mlir::ArrayAttr newMembers = getNewInitElements(oldRecord.getMembers());
     auto recordTy = mlir::cast<cir::RecordType>(oldRecord.getType());
@@ -2149,25 +2151,27 @@ void CIRGenModule::applyReplacements() {
 cir::GlobalOp CIRGenModule::createOrReplaceCXXRuntimeVariable(
     mlir::Location loc, StringRef name, mlir::Type ty,
     cir::GlobalLinkageKind linkage, clang::CharUnits alignment) {
-  auto gv = mlir::dyn_cast_or_null<cir::GlobalOp>(getGlobalValue(name));
+  auto oldGV = mlir::dyn_cast_or_null<cir::GlobalOp>(getGlobalValue(name));
 
-  if (gv) {
+  if (oldGV) {
     // Check if the variable has the right type.
-    if (gv.getSymType() == ty)
-      return gv;
-
-    // Because of C++ name mangling, the only way we can end up with an already
-    // existing global with the same name is if it has been declared extern
-    // "C".
-    assert(gv.isDeclaration() && "Declaration has wrong type!");
+    if (oldGV.getSymType() == ty)
+      return oldGV;
 
-    errorNYI(loc, "createOrReplaceCXXRuntimeVariable: declaration exists with "
-                  "wrong type");
-    return gv;
+    // The existing declaration is a use of a runtime symbol, like the vtable of
+    // a type_info class that is defined in this TU after being referenced by
+    // an RTTI descriptor. It gets replaced below.
+    assert(oldGV.isDeclaration() && "Declaration has wrong type!");
   }
 
   // Create a new variable.
-  gv = createGlobalOp(loc, name, ty, /*isConstant=*/true);
+  cir::GlobalOp gv =
+      createGlobalOp(loc, name, ty, /*isConstant=*/true,
+                     /*addrSpace=*/{}, /*insertPoint=*/oldGV.getOperation());
+
+  // Update the uses of the declaration with the wrong type.
+  if (oldGV)
+    replaceGlobal(oldGV, gv);
 
   // Set up extra information and add to the module
   gv.setLinkageAttr(
diff --git a/clang/test/CIR/CodeGen/rtti-visibility.cpp b/clang/test/CIR/CodeGen/rtti-visibility.cpp
index 0eb4c058af7745..b92ad533a8bc4b 100644
--- a/clang/test/CIR/CodeGen/rtti-visibility.cpp
+++ b/clang/test/CIR/CodeGen/rtti-visibility.cpp
@@ -51,3 +51,22 @@ void Default::f() {}
 // LLVM-DAG: @_ZTS7Default = constant [9 x i8]
 // LLVM-DAG: @_ZTI7Default = constant { ptr, ptr }
 
+
+// The type_info class that the descriptor of a class refers to is defined here,
+// after the descriptor made a declaration of its vtable with a different type.
+struct Derived : Default {
+  void f() override;
+};
+void Derived::f() {}
+namespace __cxxabiv1 {
+class __si_class_type_info {
+public:
+  virtual ~__si_class_type_info();
+};
+__si_class_type_info::~__si_class_type_info() {}
+} // namespace __cxxabiv1
+// CIR-DAG: cir.global constant external @_ZTVN10__cxxabiv120__si_class_type_infoE = #cir.vtable<
+// CIR-DAG: cir.global constant external @_ZTI7Derived = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv120__si_class_type_infoE, {{.*}}>
+
+// LLVM-DAG: @_ZTVN10__cxxabiv120__si_class_type_infoE = constant { [4 x ptr] }
+// LLVM-DAG: @_ZTI7Derived = constant { ptr, ptr, ptr } { ptr getelementptr {{.*}}(i8, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 16), ptr @_ZTS7Derived, ptr @_ZTI7Default }, align 8



More information about the cfe-commits mailing list