[llvm-branch-commits] [clang] 313de9d - For #64088: mark vtable as used if we might emit a reference to it.

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jul 27 06:51:56 PDT 2023


Author: Richard Smith
Date: 2023-07-27T15:50:46+02:00
New Revision: 313de9dbf6d07602a51718de128a9eadd3762d57

URL: https://github.com/llvm/llvm-project/commit/313de9dbf6d07602a51718de128a9eadd3762d57
DIFF: https://github.com/llvm/llvm-project/commit/313de9dbf6d07602a51718de128a9eadd3762d57.diff

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

(cherry picked from commit b6847edfc235829b37dd6d734ef5bbfa0a58b6fc)

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 d65ecf52c52310..b338d601db7397 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 676aa975a72686..bd283e85101b4b 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 llvm-branch-commits mailing list