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

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 29 09:56:36 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-aarch64

Author: Doug Wyatt (dougsonos)

<details>
<summary>Changes</summary>

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?)

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


2 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+11-3) 
- (added) llvm/test/CodeGen/AArch64/func-alignment.ll (+19) 


``````````diff
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
diff --git a/llvm/test/CodeGen/AArch64/func-alignment.ll b/llvm/test/CodeGen/AArch64/func-alignment.ll
new file mode 100644
index 00000000000000..0860578065bcd1
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/func-alignment.ll
@@ -0,0 +1,19 @@
+; RUN: llc -mtriple=aarch64 -O0 -fast-isel < %s | FileCheck %s
+
+; Verify that Clang's C++ ABI alignment for functions of 2 does not
+; result in underaligned functions when they are in special sections.
+; (#90415)
+
+define void @noSection() align 2 {
+; CHECK:	.p2align	2
+entry:
+  ret void
+}
+
+define void @withSection() section "__TEXT,__foo,regular,pure_instructions" align 2 {
+; CHECK:	.p2align	2
+entry:
+  ret void
+}
+
+

``````````

</details>


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


More information about the llvm-commits mailing list