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

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 21 16:17:34 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:

I did not since I was under the assumption we wouldn't want to change the underlying table alignment. I think maybe we won't want to do this so we can keep the 4-byte alignment for relative vtables, but lemme see if others have thoughts on this.

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


More information about the llvm-commits mailing list