Skip to content

CodeGen: Respect function align attribute if less than preferred alignment. #149444

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/MachineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ void MachineFunction::init() {
ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
Alignment = STI->getTargetLowering()->getMinFunctionAlignment();

// FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
// FIXME: Use Function::hasOptSize().
if (!F.hasFnAttribute(Attribute::OptimizeForSize))
if (!F.getAlign() && !F.hasFnAttribute(Attribute::OptimizeForSize))
Copy link
Contributor

Choose a reason for hiding this comment

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

This will also disrespect getMinFunctionAlignment which is probably more of a problem

Copy link
Contributor

Choose a reason for hiding this comment

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

Applying this in the function constructor also seems like the wrong place to do this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see why it doesn't respect getMinFunctionAlignment. getMinFunctionAlignment is read on line 212 and combined with the preferred alignment with std::max here, so the result will be at least getMinFunctionAlignment.

Alignment = std::max(Alignment,
STI->getTargetLowering()->getPrefFunctionAlignment());

Expand Down
18 changes: 18 additions & 0 deletions llvm/test/CodeGen/X86/function-align.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: llc -function-sections < %s | FileCheck %s

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; CHECK: .section .text.f1
; CHECK-NOT: .p2align
; CHECK: f1:
define void @f1() align 1 {
ret void
}

; CHECK: .section .text.f2
; CHECK-NEXT: .globl f2
; CHECK-NEXT: .p2align 1
define void @f2() align 2 {
ret void
}
Loading