[llvm] [llvm codegen] Don't allow function alignment less than the target's minimum (PR #90415)

Doug Wyatt via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 28 13:37:24 PDT 2024


https://github.com/dougsonos created https://github.com/llvm/llvm-project/pull/90415

See #90358 

The C++ ABI forces all class methods to have an explicit alignment of 2. This ends up overriding `MachineFunction`'s alignment (on arm64, 4) on functions in special text sections. Add a check: don't align code at less than the target's minimum function alignment.

(Are there other objects which can occur in text sections and which should be allowed to be aligned at smaller than the minimum function alignment?)

>From 527872f0e53a7f2a71023b063322258272fd4e0f Mon Sep 17 00:00:00 2001
From: Doug Wyatt <dwyatt at apple.com>
Date: Thu, 25 Apr 2024 13:25:05 -0700
Subject: [PATCH] [llvm codegen] In AsmPrinter::emitAlignment, don't allow the
 possibly smaller alignment of the GlobalObject to reduce a function's
 alignment below the target's minimum function alignment. (#90358)

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 721d144d7f4c67..c29f9aa97f0165 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3147,9 +3147,17 @@ void AsmPrinter::emitAlignment(Align Alignment, const GlobalObject *GV,
 
   if (getCurrentSection()->getKind().isText()) {
     const MCSubtargetInfo *STI = nullptr;
-    if (this->MF)
-      STI = &getSubtargetInfo();
-    else
+    if (this->MF) {
+      // Don't allow the possibly smaller alignment of the GV
+      // (e.g. 2 for a C++ method) to reduce a function's alignment
+      // below the target's minimum function alignment. (#90358)
+      const TargetSubtargetInfo *TSI = &MF->getSubtarget();
+      const Align MinAlign =
+          TSI->getTargetLowering()->getMinFunctionAlignment();
+      if (Alignment < MinAlign)
+        Alignment = MinAlign;
+      STI = TSI;
+    } else
       STI = TM.getMCSubtargetInfo();
     OutStreamer->emitCodeAlignment(Alignment, STI, MaxBytesToEmit);
   } else



More information about the llvm-commits mailing list