[llvm] [llvm] Ensure propagated constants in the vtable are aligned (PR #136630)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 22 13:51:05 PDT 2025


================
@@ -1861,6 +1877,18 @@ bool DevirtModule::tryVirtualConstProp(
         Fn->arg_empty() || !Fn->arg_begin()->use_empty() ||
         Fn->getReturnType() != RetType)
       return false;
+
+    // This only works if the integer size is at most the alignment of the
+    // vtable. If the table is underaligned, then we can't guarantee that the
+    // constant will always be aligned to the integer type alignment. For
+    // example, if the table is `align 1`, we can never guarantee that an i32
+    // stored before/after the vtable is 32-bit aligned without changing the
+    // alignment of the new global.
+    GlobalVariable *GV = Target.TM->Bits->GV;
+    Align TableAlignment = M.getDataLayout().getValueOrABITypeAlignment(
+        GV->getAlign(), GV->getValueType());
+    if (TypeAlignment > TableAlignment)
+      return false;
----------------
PiJoules wrote:

Having a smaller alignment was one of the nice to have features for relative vtables that helped save some space. 

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


More information about the llvm-commits mailing list