llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Peter Collingbourne (pcc)

<details>
<summary>Changes</summary>

MachineFunction can now be queried for the preferred alignment which
comes from the function attributes (optsize, minsize, prefalign) and
TargetLowering.

Part of this RFC:
https://discourse.llvm.org/t/rfc-enhancing-function-alignment-attributes/88019


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


6 Files Affected:

- (modified) llvm/include/llvm/CodeGen/MachineFunction.h (+2) 
- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+1-1) 
- (modified) llvm/lib/CodeGen/AsmPrinter/WinException.cpp (+2-2) 
- (modified) llvm/lib/CodeGen/MachineFunction.cpp (+13-5) 
- (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+1-1) 
- (modified) llvm/test/CodeGen/PowerPC/alloca-crspill.ll (+1-1) 


``````````diff
diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h 
b/llvm/include/llvm/CodeGen/MachineFunction.h
index ef783f276b7d4..a454ad02df23d 100644
--- a/llvm/include/llvm/CodeGen/MachineFunction.h
+++ b/llvm/include/llvm/CodeGen/MachineFunction.h
@@ -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.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index cd14a4f57f760..7b08098225427 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -978,7 +978,7 @@ void AsmPrinter::emitFunctionHeader() {
 
   emitLinkage(&F, CurrentFnSym);
   if (MAI->hasFunctionAlignment())
-    emitAlignment(MF->getAlignment(), &F);
+    emitAlignment(MF->getPreferredAlignment(), &F);
 
   if (MAI->hasDotTypeDotSizeDirective())
     OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
diff --git a/llvm/lib/CodeGen/AsmPrinter/WinException.cpp 
b/llvm/lib/CodeGen/AsmPrinter/WinException.cpp
index 13fd270ec7410..90d8196ffb82a 100644
--- a/llvm/lib/CodeGen/AsmPrinter/WinException.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/WinException.cpp
@@ -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);
diff --git a/llvm/lib/CodeGen/MachineFunction.cpp 
b/llvm/lib/CodeGen/MachineFunction.cpp
index 224231cb90fe2..478a7ccbdfe41 100644
--- a/llvm/lib/CodeGen/MachineFunction.cpp
+++ b/llvm/lib/CodeGen/MachineFunction.cpp
@@ -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
@@ -330,6 +325,19 @@ bool MachineFunction::shouldSplitStack() const {
   return getFunction().hasFnAttribute("split-stack");
 }
 
+Align MachineFunction::getPreferredAlignment() const {
+  Align PrefAlignment;
+
+  if (MaybeAlign A = F.getPreferredAlignment())
+    PrefAlignment = *A;
+  else if (!F.hasOptSize())
+    PrefAlignment = STI.getTargetLowering()->getPrefFunctionAlignment();
+  else
+    PrefAlignment = Align(1);
+  
+  return std::max(PrefAlignment, getAlignment());
+}
+
 [[nodiscard]] unsigned
 MachineFunction::addFrameInst(const MCCFIInstruction &Inst) {
   FrameInstructions.push_back(Inst);
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index a7734e9200a19..b66ea8e3b440e 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -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
diff --git a/llvm/test/CodeGen/PowerPC/alloca-crspill.ll 
b/llvm/test/CodeGen/PowerPC/alloca-crspill.ll
index cbcfd9a6a0dab..bd88b5d89202d 100644
--- a/llvm/test/CodeGen/PowerPC/alloca-crspill.ll
+++ b/llvm/test/CodeGen/PowerPC/alloca-crspill.ll
@@ -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: '' }

``````````

</details>


https://github.com/llvm/llvm-project/pull/158368
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to