[llvm] [BOLT][NFC] Set minimal alignment for BF (PR #67707)

Vladislav Khmelevsky via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 28 10:04:05 PDT 2023


https://github.com/yota9 created https://github.com/llvm/llvm-project/pull/67707

Currently minimal alignment is a constant, make it adjastable for each
BF to ensure the minimal guaranteed alignment. This might be used for such things as data in code objects.


>From f8874ecdf9d622f3a3732e1c05f0622b4827df85 Mon Sep 17 00:00:00 2001
From: Vladislav Khmelevsky <och95 at yandex.ru>
Date: Thu, 28 Sep 2023 20:52:24 +0400
Subject: [PATCH] [BOLT][NFC] Set minimal alignment for BF

Currently minimal alignment is a constant, make it adjastable for each
BF to ensure the minimal guaranteed alignment. This might be used for such things as data in code objects.
---
 bolt/include/bolt/Core/BinaryFunction.h | 17 +++++++++++++----
 bolt/lib/Core/BinaryEmitter.cpp         |  2 +-
 bolt/lib/Core/BinaryFunction.cpp        |  2 --
 bolt/lib/Passes/Aligner.cpp             | 22 +++++++---------------
 bolt/lib/Passes/LongJmp.cpp             |  4 ++--
 5 files changed, 23 insertions(+), 24 deletions(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index dea1d55b15026e4..20833c314cbffe7 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -192,9 +192,6 @@ class BinaryFunction {
   static constexpr uint64_t COUNT_NO_PROFILE =
       BinaryBasicBlock::COUNT_NO_PROFILE;
 
-  /// We have to use at least 2-byte alignment for functions because of C++ ABI.
-  static constexpr unsigned MinAlign = 2;
-
   static const char TimerGroupName[];
   static const char TimerGroupDesc[];
 
@@ -246,6 +243,10 @@ class BinaryFunction {
   /// Maximum size this function is allowed to have.
   uint64_t MaxSize{std::numeric_limits<uint64_t>::max()};
 
+  /// Minimal guaranteed alinment for the function.
+  /// We have to use at least 2-byte alignment for functions because of C++ ABI.
+  uint16_t MinAlign{2};
+
   /// Alignment requirements for the function.
   uint16_t Alignment{2};
 
@@ -1715,13 +1716,21 @@ class BinaryFunction {
     return *this;
   }
 
+  BinaryFunction &adjustMinAlignment(uint16_t Align) {
+    MinAlign = std::max(MinAlign, Align);
+    return *this;
+  }
+
   BinaryFunction &setAlignment(uint16_t Align) {
     Alignment = Align;
     return *this;
   }
 
-  Align getAlign() const { return Align(Alignment); }
+  uint16_t getMinAlignment() const { return MinAlign; }
+  Align getMinAlign() const { return Align(getMinAlignment()); }
+
   uint16_t getAlignment() const { return Alignment; }
+  Align getAlign() const { return Align(getAlignment()); }
 
   BinaryFunction &setMaxAlignmentBytes(uint16_t MaxAlignBytes) {
     MaxAlignmentBytes = MaxAlignBytes;
diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp
index 95ab63521c06a56..6c17f69d7aeceb5 100644
--- a/bolt/lib/Core/BinaryEmitter.cpp
+++ b/bolt/lib/Core/BinaryEmitter.cpp
@@ -309,7 +309,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
     // tentative layout.
     Section->ensureMinAlignment(Align(opts::AlignFunctions));
 
-    Streamer.emitCodeAlignment(Align(BinaryFunction::MinAlign), &*BC.STI);
+    Streamer.emitCodeAlignment(Function.getMinAlign(), &*BC.STI);
     uint16_t MaxAlignBytes = FF.isSplitFragment()
                                  ? Function.getMaxColdAlignmentBytes()
                                  : Function.getMaxAlignmentBytes();
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 6cf1cfb50a9c12c..02a896558e17ced 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -164,8 +164,6 @@ bool shouldPrint(const BinaryFunction &Function) {
 namespace llvm {
 namespace bolt {
 
-constexpr unsigned BinaryFunction::MinAlign;
-
 template <typename R> static bool emptyRange(const R &Range) {
   return Range.begin() == Range.end();
 }
diff --git a/bolt/lib/Passes/Aligner.cpp b/bolt/lib/Passes/Aligner.cpp
index c5b63d881e17991..cd187cee5948d85 100644
--- a/bolt/lib/Passes/Aligner.cpp
+++ b/bolt/lib/Passes/Aligner.cpp
@@ -158,21 +158,13 @@ void AlignerPass::runOnFunctions(BinaryContext &BC) {
     BinaryContext::IndependentCodeEmitter Emitter =
         BC.createIndependentMCCodeEmitter();
 
-    // Align objects that contains constant islands and no code
-    // to at least 8 bytes.
-    if (!BF.size() && BF.hasIslandsInfo()) {
-      uint16_t Alignment = BF.getConstantIslandAlignment();
-      // Check if we're forcing output alignment and it is greater than minimal
-      // CI required one
-      if (!opts::UseCompactAligner && Alignment < opts::AlignFunctions &&
-          opts::AlignFunctions <= opts::AlignFunctionsMaxBytes)
-        Alignment = opts::AlignFunctions;
-
-      BF.setAlignment(Alignment);
-      BF.setMaxAlignmentBytes(Alignment);
-      BF.setMaxColdAlignmentBytes(Alignment);
-      return;
-    }
+    // Minimal code alignment on AArch64 is 4
+    if (BC.isAArch64())
+      BF.adjustMinAlignment(4);
+
+    // Align data in code BFs minimum to CI alignment
+    if (!BF.size() && BF.hasIslandsInfo())
+      BF.adjustMinAlignment(BF.getConstantIslandAlignment());
 
     if (opts::UseCompactAligner)
       alignCompact(BF, Emitter.MCE.get());
diff --git a/bolt/lib/Passes/LongJmp.cpp b/bolt/lib/Passes/LongJmp.cpp
index 31bb8000dda00c3..ccfc950704b89ee 100644
--- a/bolt/lib/Passes/LongJmp.cpp
+++ b/bolt/lib/Passes/LongJmp.cpp
@@ -290,7 +290,7 @@ uint64_t LongJmpPass::tentativeLayoutRelocColdPart(
   for (BinaryFunction *Func : SortedFunctions) {
     if (!Func->isSplit())
       continue;
-    DotAddress = alignTo(DotAddress, BinaryFunction::MinAlign);
+    DotAddress = alignTo(DotAddress, Func->getMinAlignment());
     uint64_t Pad =
         offsetToAlignment(DotAddress, llvm::Align(Func->getAlignment()));
     if (Pad <= Func->getMaxColdAlignmentBytes())
@@ -349,7 +349,7 @@ uint64_t LongJmpPass::tentativeLayoutRelocMode(
         DotAddress = alignTo(DotAddress, opts::AlignText);
     }
 
-    DotAddress = alignTo(DotAddress, BinaryFunction::MinAlign);
+    DotAddress = alignTo(DotAddress, Func->getMinAlignment());
     uint64_t Pad =
         offsetToAlignment(DotAddress, llvm::Align(Func->getAlignment()));
     if (Pad <= Func->getMaxAlignmentBytes())



More information about the llvm-commits mailing list