[clang] [llvm] [ThinLTO] Support dead RTTI data elimination under -fno-split-lto-unit (PR #126336)

Teresa Johnson via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 12 11:01:52 PST 2025


================
@@ -911,6 +916,63 @@ static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
       Summary->setLive(true);
 }
 
+// Return true if the User U is reachable from a non-vtable user
+// through the use-def chain.
+static bool hasNonVTableUsers(const User *U, CXXABI *ABI) {
+  LLVM_DEBUG(dbgs() << "Check if " << *U << "has vtable users\n");
+  if (isa<Instruction>(U)) {
+    // If the type info is used in dynamic_cast or exception handling,
+    // its user must be the instruction.
+    return true;
+  }
+
+  // The virtual table type is either a struct of arrays. For example:
+  // @vtable = constant { [3 x ptr] } { [3 x ptr] [ ptr null, ptr @rtti, ptr @vf] }
+  //
+  // In this case, the user of @rtti is an anonymous ConstantArray.
+  // Therefore, if the user of the type information is anonymous,
+  // we need to perform a depth-first search (DFS) to locate its named users.
+  //
+  // And we also need to iterate its users if the current user is the type
+  // info global variable itself.
+  StringRef Name = U->getName();
+  if (Name.empty() || ABI->isTypeInfo(Name)) {
+    for (const User *It : U->users())
+      if (hasNonVTableUsers(It, ABI))
+        return true;
+    return false;
+  }
+
+  if (!ABI->isVTable(Name))
+    return true;
+
+  return false;
+}
+
+static void analyzeRTTIVars(ModuleSummaryIndex &Index, const Module &M) {
+  Triple TT(M.getTargetTriple());
+
+  std::unique_ptr<CXXABI> ABI = CXXABI::Create(TT);
+  if (!ABI)
+    return;
+
+  for (const GlobalVariable &GV : M.globals()) {
+    if (!ABI->isTypeInfo(GV.getName()))
+      continue;
+
+    if (hasNonVTableUsers(&GV, ABI.get())) {
+      std::string TypeName =
+          ABI->getTypeNameFromTypeInfo(GV.getName());
----------------
teresajohnson wrote:

In WPD we simply hardcode these prefixes - the assumption is if the name starts with one prefix, we can assume what the other is. See https://github.com/llvm/llvm-project/blob/c6654806949f0498fdd2d009b66e62041c21de36/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp#L831-L839. If we can avoid adding all the CXXABI handling to LLVM and the TargetTriple/OS checks to LTO that would be ideal

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


More information about the cfe-commits mailing list