[PATCH] D156324: [MachineBlockPlacement] Respect target limits on padding amount when aligning all blocks

Momchil Velikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 26 05:42:47 PDT 2023


chill created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
chill requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

If the compiler (`llc`) is given `--align-all-blocks=N` or
`--align-all-nofallthru-blocks=N` it will emit alignment directives
without regard to the limit on the number of padding bytes inserted
for alignment.

With this patch, the compiler will respect the limits.

Previous behaviour of having no limits can be obtained by passing
`-max-bytes-for-alignment=0`.


https://reviews.llvm.org/D156324

Files:
  llvm/lib/CodeGen/MachineBlockPlacement.cpp
  llvm/test/CodeGen/AArch64/align-all-blocks.ll


Index: llvm/test/CodeGen/AArch64/align-all-blocks.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AArch64/align-all-blocks.ll
@@ -0,0 +1,42 @@
+; RUN: llc -align-all-blocks=5                            < %s | FileCheck %s --check-prefixes=CHECK,CHECK-ALL
+; RUN: llc -align-all-blocks=5 -max-bytes-for-alignment=0 < %s | FileCheck %s --check-prefixes=CHECK,CHECK-NOLIMIT
+; RUN: llc -align-all-nofallthru-blocks=5                 < %s | FileCheck %s --check-prefixes=CHECK,CHECK-NOFALLTHRU
+
+target triple = "aarch64-linux"
+
+declare void @g(...)
+
+define void @f(i1 %c) "tune-cpu"="neoverse-v1" {
+; CHECK-LABEL: f:
+
+; CHECK-ALL: .p2align 5, , 16
+; CHECK-NOLIMIT: .p2align 5{{$}}
+; CHECK-NOFALLTHRU-NOT: p2align
+; CHECK: // %entry
+entry:
+    br i1 %c, label %if.then, label %if.else
+
+; CHECK-ALL: .p2align 5, , 16
+; CHECK-NOLIMIT: .p2align 5
+; CHECK-NOFALLTHRU-NOT: p2align
+; CHECK: // %if.then
+if.then:
+    call void @g(i32 0)
+    br label %exit
+
+; CHECK-ALL: .p2align 5, , 16
+; CHECK-NOLIMIT: .p2align 5{{$}}
+; CHECK-NOFALLTHRU: .p2align 5, , 16
+; CHECK: // %if.else
+if.else:
+    call void @g(i32 1, i32 2)
+    br label %exit
+
+; CHECK-All: .p2align 5, , 16
+; CHECK-NOLIMIT: .p2align 5{{$}}
+; CHECK-NOFALLTHRU-NOT: p2align
+; CHECK: // %exit
+exit:
+    call void @f(i32 3)
+    ret void
+}
Index: llvm/lib/CodeGen/MachineBlockPlacement.cpp
===================================================================
--- llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -3443,32 +3443,23 @@
   ComputedEdges.clear();
   ChainAllocator.DestroyAll();
 
-  bool HasMaxBytesOverride =
-      MaxBytesForAlignmentOverride.getNumOccurrences() > 0;
-
-  if (AlignAllBlock)
+  if (AlignAllBlock) {
     // Align all of the blocks in the function to a specific alignment.
-    for (MachineBasicBlock &MBB : MF) {
-      if (HasMaxBytesOverride)
-        MBB.setAlignment(Align(1ULL << AlignAllBlock),
-                         MaxBytesForAlignmentOverride);
-      else
-        MBB.setAlignment(Align(1ULL << AlignAllBlock));
-    }
-  else if (AlignAllNonFallThruBlocks) {
+    for (MachineBasicBlock &MBB : MF)
+      MBB.setAlignment(Align(1ULL << AlignAllBlock), MaxBytesForAlignment);
+
+  } else if (AlignAllNonFallThruBlocks) {
     // Align all of the blocks that have no fall-through predecessors to a
     // specific alignment.
     for (auto MBI = std::next(MF.begin()), MBE = MF.end(); MBI != MBE; ++MBI) {
       auto LayoutPred = std::prev(MBI);
-      if (!LayoutPred->isSuccessor(&*MBI)) {
-        if (HasMaxBytesOverride)
-          MBI->setAlignment(Align(1ULL << AlignAllNonFallThruBlocks),
-                            MaxBytesForAlignmentOverride);
-        else
-          MBI->setAlignment(Align(1ULL << AlignAllNonFallThruBlocks));
-      }
+      if (LayoutPred->isSuccessor(&*MBI))
+        continue;
+      MBI->setAlignment(Align(1ULL << AlignAllNonFallThruBlocks),
+                        MaxBytesForAlignment);
     }
   }
+
   if (ViewBlockLayoutWithBFI != GVDT_None &&
       (ViewBlockFreqFuncName.empty() ||
        F->getFunction().getName().equals(ViewBlockFreqFuncName))) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156324.544326.patch
Type: text/x-patch
Size: 3233 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230726/82ae5bfd/attachment.bin>


More information about the llvm-commits mailing list