[clang] 9a370a1 - Reland "For #64088: mark vtable as used if we might emit a reference to it."

Dmitry Chernenkov via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 2 01:52:53 PDT 2023


Author: Dmitry Chernenkov
Date: 2023-08-02T08:52:00Z
New Revision: 9a370a1e586ca0ee1367ea58d7d61b8a86f660a3

URL: https://github.com/llvm/llvm-project/commit/9a370a1e586ca0ee1367ea58d7d61b8a86f660a3
DIFF: https://github.com/llvm/llvm-project/commit/9a370a1e586ca0ee1367ea58d7d61b8a86f660a3.diff

LOG: Reland "For #64088: mark vtable as used if we might emit a reference to it."

This reverts commit 3b34d69ac7a643742364be3591b324ddd14ef9aa.

Added: 
    

Modified: 
    clang/lib/Sema/SemaCast.cpp
    clang/test/CodeGenCXX/dynamic-cast-exact.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index d65ecf52c52310d..b338d601db73973 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -935,6 +935,14 @@ void CastOperation::CheckDynamicCast() {
           << isClangCL;
   }
 
+  // For a dynamic_cast to a final type, IR generation might emit a reference
+  // to the vtable.
+  if (DestRecord) {
+    auto *DestDecl = DestRecord->getAsCXXRecordDecl();
+    if (DestDecl->isEffectivelyFinal())
+      Self.MarkVTableUsed(OpRange.getBegin(), DestDecl);
+  }
+
   // Done. Everything else is run-time checks.
   Kind = CK_Dynamic;
 }

diff  --git a/clang/test/CodeGenCXX/dynamic-cast-exact.cpp b/clang/test/CodeGenCXX/dynamic-cast-exact.cpp
index 676aa975a72686a..bd283e85101b4b6 100644
--- a/clang/test/CodeGenCXX/dynamic-cast-exact.cpp
+++ b/clang/test/CodeGenCXX/dynamic-cast-exact.cpp
@@ -76,3 +76,12 @@ H *exact_multi(A *a) {
   // CHECK: phi ptr [ %[[RESULT]], %[[LABEL_NOTNULL]] ], [ null, %[[LABEL_FAILED]] ]
   return dynamic_cast<H*>(a);
 }
+
+namespace GH64088 {
+  // Ensure we mark the B vtable as used here, because we're going to emit a
+  // reference to it.
+  // CHECK: define {{.*}} @_ZN7GH640881BD0
+  struct A { virtual ~A(); };
+  struct B final : A { virtual ~B() = default; };
+  B *cast(A *p) { return dynamic_cast<B*>(p); }
+}


        


More information about the cfe-commits mailing list