[llvm] [llvm] Use ABI instead of preferred alignment for const prop checks (PR #142500)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 2 16:02:33 PDT 2025


https://github.com/PiJoules created https://github.com/llvm/llvm-project/pull/142500

We'd hit an assertion checking proper alignment for an i8 when building chromium because we used the prefered alignment (which is 4 bytes) instead of the ABI alignment (which is 1 byte). The ABI alignment should be used because that's the actual alignment needed to load a constant from the vtable.

This also updates the two `virtual-const-prop-small-alignment-*` to explicitly give ABI alignments for i64s.

>From 40e7e5d9e62ce471c7c97f95e73bc93af24f3b6a Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Mon, 2 Jun 2025 15:57:00 -0700
Subject: [PATCH] [llvm] Use ABI instead of preferred alignment for const prop
 checks

We'd hit an assertion checking proper alignment for an i8 when building
chromium because we used the prefered alignment (which is 4 bytes)
instead of the ABI alignment (which is 1 byte). The ABI alignment should
be used because that's the actual alignment needed to load a constant
from the vtable.

This also updates the two `virtual-const-prop-small-alignment-*` to
explicitly give ABI alignments for i64s.
---
 llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp                  | 2 +-
 .../WholeProgramDevirt/virtual-const-prop-small-alignment-32.ll | 2 +-
 .../WholeProgramDevirt/virtual-const-prop-small-alignment-64.ll | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
index 3a25255d0a4c8..a7d9f3ba24b24 100644
--- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
+++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
@@ -1845,7 +1845,7 @@ bool DevirtModule::tryVirtualConstProp(
   if (BitWidth > 64)
     return false;
 
-  Align TypeAlignment = M.getDataLayout().getPrefTypeAlign(RetType);
+  Align TypeAlignment = M.getDataLayout().getABIIntegerTypeAlignment(BitWidth);
 
   // Make sure that each function is defined, does not access memory, takes at
   // least one argument, does not use its first argument (which we assume is
diff --git a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-32.ll b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-32.ll
index ab76f2c22e343..d501694bc4737 100644
--- a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-32.ll
+++ b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-32.ll
@@ -1,7 +1,7 @@
 ; RUN: opt -S -passes=wholeprogramdevirt -whole-program-visibility %s | FileCheck %s
 
 ;; This target uses 32-bit sized and aligned pointers.
-target datalayout = "e-p:32:32"
+target datalayout = "e-p:32:32-i64:64"
 
 ;; Constant propagation should be agnostic towards sections.
 ;; Also the new global should be in the original vtable's section.
diff --git a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-64.ll b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-64.ll
index c83fbc6ed5a19..b8e4a25bdc88d 100644
--- a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-64.ll
+++ b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-64.ll
@@ -1,7 +1,7 @@
 ; RUN: opt -S -passes=wholeprogramdevirt -whole-program-visibility %s | FileCheck %s
 
 ;; This target uses 64-bit sized and aligned pointers.
-target datalayout = "e-p:64:64"
+target datalayout = "e-p:64:64-i64:64"
 
 ;; Constant propagation should be agnostic towards sections.
 ;; Also the new global should be in the original vtable's section.



More information about the llvm-commits mailing list