diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 721d144d7f4c6..c29f9aa97f016 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 0000000000000..0860578065bcd --- /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 +} + +