Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion lld/test/ELF/lto/linker-script-symbols-ipo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
; NOIPO: <foo>:
; NOIPO-NEXT: movl $2, %eax
; NOIPO: <_start>:
; NOIPO-NEXT: jmp 0x201160 <foo>
; NOIPO-NEXT: jmp 0x201158 <foo>

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/CodeGen/MachineFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ class LLVM_ABI MachineFunction {
Alignment = A;
}

Align getPreferredAlignment() const;

/// exposesReturnsTwice - Returns true if the function calls setjmp or
/// any other similar functions with attribute "returns twice" without
/// having the attribute itself.
Expand Down
16 changes: 14 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,20 @@ void AsmPrinter::emitFunctionHeader() {
emitVisibility(CurrentFnSym, F.getVisibility());

emitLinkage(&F, CurrentFnSym);
if (MAI->hasFunctionAlignment())
emitAlignment(MF->getAlignment(), &F);
if (MAI->hasFunctionAlignment()) {
// The preferred alignment directive will not have the intended effect
// unless function sections are enabled.
if (MAI->useIntegratedAssembler() && MAI->hasPreferredAlignment() &&
TM.getFunctionSections()) {
Align Alignment = MF->getAlignment();
Align PrefAlignment = MF->getPreferredAlignment();
emitAlignment(Alignment, &F);
if (Alignment != PrefAlignment)
OutStreamer->emitPrefAlign(PrefAlignment);
} else {
emitAlignment(MF->getPreferredAlignment(), &F);
}
}

if (MAI->hasDotTypeDotSizeDirective())
OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/WinException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ void WinException::beginFunclet(const MachineBasicBlock &MBB,

// We want our funclet's entry point to be aligned such that no nops will be
// present after the label.
Asm->emitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()),
&F);
Asm->emitAlignment(
std::max(Asm->MF->getPreferredAlignment(), MBB.getAlignment()), &F);

// Now that we've emitted the alignment directive, point at our funclet.
Asm->OutStreamer->emitLabel(Sym);
Expand Down
17 changes: 12 additions & 5 deletions llvm/lib/CodeGen/MachineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,6 @@ 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.
if (!F.hasOptSize())
Alignment = std::max(Alignment,
STI.getTargetLowering()->getPrefFunctionAlignment());

// -fsanitize=function and -fsanitize=kcfi instrument indirect function calls
// to load a type hash before the function label. Ensure functions are aligned
// by a least 4 to avoid unaligned access, which is especially important for
Expand Down Expand Up @@ -330,6 +325,18 @@ bool MachineFunction::shouldSplitStack() const {
return getFunction().hasFnAttribute("split-stack");
}

Align MachineFunction::getPreferredAlignment() const {
Align PrefAlignment = Align(1);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of reassigning to the variable multiple times, it's better to reverse the order the following two conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


if (!F.hasOptSize())
PrefAlignment = STI.getTargetLowering()->getPrefFunctionAlignment();

if (MaybeAlign A = F.getPreferredAlignment())
PrefAlignment = *A;

return std::max(PrefAlignment, getAlignment());
}

[[nodiscard]] unsigned
MachineFunction::addFrameInst(const MCCFIInstruction &Inst) {
FrameInstructions.push_back(Inst);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void X86AsmPrinter::EmitKCFITypePadding(const MachineFunction &MF,
if (HasType)
PrefixBytes += 5;

emitNops(offsetToAlignment(PrefixBytes, MF.getAlignment()));
emitNops(offsetToAlignment(PrefixBytes, MF.getPreferredAlignment()));
}

/// emitKCFITypeId - Emit the KCFI type information in architecture specific
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/PowerPC/alloca-crspill.ll
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ entry:
declare signext i32 @do_something(ptr)

; CHECK: name: test
; CHECK: alignment: 16
; CHECK: alignment: 4
; CHECK: liveins:
; CHECK64: - { reg: '$x3', virtual-reg: '' }
; CHECK32: - { reg: '$r3', virtual-reg: '' }
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/CodeGen/X86/prefalign.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; RUN: llc < %s | FileCheck --check-prefixes=CHECK,NOFS %s
; RUN: llc -function-sections < %s | FileCheck --check-prefixes=CHECK,FS %s

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

; CHECK: .globl f1
; NOFS-NEXT: .p2align 4
; FS-NEXT: .prefalign 16
define void @f1() {
ret void
}

; CHECK: .globl f2
; CHECK-NOT: .prefalign
; CHECK-NOT: .p2align
define void @f2() prefalign 1 {
ret void
}

; CHECK: .globl f3
; NOFS-NEXT: .p2align 2
; FS-NEXT: .p2align 1
; FS-NEXT: .prefalign 4
define void @f3() align 2 prefalign 4 {
ret void
}
4 changes: 2 additions & 2 deletions llvm/test/Transforms/SampleProfile/pseudo-probe-emit.ll
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ entry:
; CHECK-SEC: [Nr] Name Type {{.*}} ES Flg Lk Inf Al
; CHECK-SEC: [ 3] .text.foo PROGBITS {{.*}} 00 AX 0 0 16
; CHECK-SEC: [ 5] .text.foo2 PROGBITS {{.*}} 00 AX 0 0 16
; CHECK-SEC: [ 8] .text.foo3 PROGBITS {{.*}} 00 AXG 0 0 16
; CHECK-SEC: [ 8] .text.foo3 PROGBITS {{.*}} 00 AXG 0 0 1
; CHECK-SEC-COUNT-3: .pseudo_probe_desc PROGBITS
; CHECK-SEC: .pseudo_probe PROGBITS {{.*}} 00 L 3 0 1
; CHECK-SEC-NEXT: .pseudo_probe PROGBITS {{.*}} 00 L 5 0 1
Expand Down Expand Up @@ -135,7 +135,7 @@ entry:
; CHECK-SEC2: [Nr] Name Type {{.*}} ES Flg Lk Inf Al
; CHECK-SEC2: [ 3] .text PROGBITS {{.*}} 00 AX 0 0 16
; CHECK-SEC2: [ 5] .text PROGBITS {{.*}} 00 AX 0 0 16
; CHECK-SEC2: [ 8] .text PROGBITS {{.*}} 00 AXG 0 0 16
; CHECK-SEC2: [ 8] .text PROGBITS {{.*}} 00 AXG 0 0 1
; CHECK-SEC2-COUNT-3: .pseudo_probe_desc PROGBITS
; CHECK-SEC2: .pseudo_probe PROGBITS {{.*}} 00 L 3 0 1
; CHECK-SEC2-NEXT: .pseudo_probe PROGBITS {{.*}} 00 L 5 0 1
Expand Down
Loading