Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025

Conversation

PiJoules
Copy link
Contributor

@PiJoules PiJoules commented Jun 2, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-llvm-transforms

Author: None (PiJoules)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/142500.diff

3 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp (+1-1)
  • (modified) llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-32.ll (+1-1)
  • (modified) llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-small-alignment-64.ll (+1-1)
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.

Copy link
Contributor

ilovepi commented Jun 3, 2025

Why is the only change in the test to the data layout string? Shouldn't there be some check that the case you've brought up is handled?

@PiJoules
Copy link
Contributor Author

PiJoules commented Jun 3, 2025

Why is the only change in the test to the data layout string? Shouldn't there be some check that the case you've brought up is handled?

I can add another test that asserts the ABI alignment is used instead of the preferred alignment. Existing tests only specified the preferred alignment so the ABI alignment could be anything which led to different codegen (for example, some i8 constants weren't propagated since they had an ABI alignment of 4). I updated the tests just to be explicit about the ABI alignment since we were previously explicit about the preferred alignment.

@ilovepi
Copy link
Contributor

ilovepi commented Jun 3, 2025

I can add another test that asserts the ABI alignment is used instead of the preferred alignment. Existing tests only specified the preferred alignment so the ABI alignment could be anything which led to different codegen (for example, some i8 constants weren't propagated since they had an ABI alignment of 4). I updated the tests just to be explicit about the ABI alignment since we were previously explicit about the preferred alignment.

I think that's preferable.

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.
@PiJoules PiJoules force-pushed the const-prop-assertion-fix branch from 40e7e5d to d82809e Compare June 4, 2025 04:23
@PiJoules
Copy link
Contributor Author

PiJoules commented Jun 4, 2025

I can add another test that asserts the ABI alignment is used instead of the preferred alignment. Existing tests only specified the preferred alignment so the ABI alignment could be anything which led to different codegen (for example, some i8 constants weren't propagated since they had an ABI alignment of 4). I updated the tests just to be explicit about the ABI alignment since we were previously explicit about the preferred alignment.

I think that's preferable.

Done

Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@PiJoules PiJoules merged commit f32f048 into llvm:main Jun 4, 2025
10 of 11 checks passed
@PiJoules PiJoules deleted the const-prop-assertion-fix branch June 4, 2025 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants