[llvm] [BOLT][NFC] Add allocator id to createInstrIncMemory (PR #68709)

Job Noorman via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 11 00:29:30 PDT 2023


https://github.com/mtvec updated https://github.com/llvm/llvm-project/pull/68709

>From 348cfafe40d87e6e2f4b636cdd253f8e93e51510 Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Tue, 10 Oct 2023 15:38:44 +0200
Subject: [PATCH 1/2] [BOLT][NFC] Add allocator id to createInstrIncMemory

On RISC-V, this function will have to insert `auipc`+`lw`/`sw` pairs
where the `auipc` needs a label annotation (see #66395). In order to do
this correctly within the context of `ParallelUtilities`, we need an
allocator id when setting the label. This patch adds this allocator id
argument and wires it through to all call sites.

Note that this function is also made non-const since `setLabel` is
non-const.
---
 bolt/include/bolt/Core/MCPlusBuilder.h        |  7 ++--
 bolt/include/bolt/Passes/Instrumentation.h    | 24 ++++++-------
 bolt/lib/Passes/Instrumentation.cpp           | 34 ++++++++++---------
 .../Target/AArch64/AArch64MCPlusBuilder.cpp   |  7 ++--
 bolt/lib/Target/X86/X86MCPlusBuilder.cpp      |  7 ++--
 5 files changed, 42 insertions(+), 37 deletions(-)

diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index c9e0e5d599b1fa4..6fc1582bcc2af26 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -504,9 +504,10 @@ class MCPlusBuilder {
   }
 
   /// Create increment contents of target by 1 for Instrumentation
-  virtual InstructionListType
-  createInstrIncMemory(const MCSymbol *Target, MCContext *Ctx, bool IsLeaf,
-                       unsigned CodePointerSize) const {
+  virtual InstructionListType createInstrIncMemory(const MCSymbol *Target,
+                                                   MCContext *Ctx, bool IsLeaf,
+                                                   unsigned CodePointerSize,
+                                                   AllocatorIdTy AllocatorId) {
     llvm_unreachable("not implemented");
     return InstructionListType();
   }
diff --git a/bolt/include/bolt/Passes/Instrumentation.h b/bolt/include/bolt/Passes/Instrumentation.h
index 1a11f9eab511096..67570e04e9cad74 100644
--- a/bolt/include/bolt/Passes/Instrumentation.h
+++ b/bolt/include/bolt/Passes/Instrumentation.h
@@ -64,8 +64,9 @@ class Instrumentation : public BinaryFunctionPass {
   void createLeafNodeDescription(FunctionDescription &FuncDesc, uint32_t Node);
 
   /// Create the sequence of instructions to increment a counter
-  InstructionListType createInstrumentationSnippet(BinaryContext &BC,
-                                                   bool IsLeaf);
+  InstructionListType
+  createInstrumentationSnippet(BinaryContext &BC, bool IsLeaf,
+                               MCPlusBuilder::AllocatorIdTy AllocatorId);
 
   // Critical edges worklist
   // This worklist keeps track of CFG edges <From-To> that needs to be split.
@@ -81,19 +82,18 @@ class Instrumentation : public BinaryFunctionPass {
   /// if this is a local branch and null if it is a call. Return true if the
   /// location was instrumented with an explicit counter or false if it just
   /// created the description, but no explicit counters were necessary.
-  bool instrumentOneTarget(SplitWorklistTy &SplitWorklist,
-                           SplitInstrsTy &SplitInstrs,
-                           BinaryBasicBlock::iterator &Iter,
-                           BinaryFunction &FromFunction,
-                           BinaryBasicBlock &FromBB, uint32_t From,
-                           BinaryFunction &ToFunc, BinaryBasicBlock *TargetBB,
-                           uint32_t ToOffset, bool IsLeaf, bool IsInvoke,
-                           FunctionDescription *FuncDesc, uint32_t FromNodeID,
-                           uint32_t ToNodeID = 0);
+  bool instrumentOneTarget(
+      SplitWorklistTy &SplitWorklist, SplitInstrsTy &SplitInstrs,
+      BinaryBasicBlock::iterator &Iter, BinaryFunction &FromFunction,
+      BinaryBasicBlock &FromBB, uint32_t From, BinaryFunction &ToFunc,
+      BinaryBasicBlock *TargetBB, uint32_t ToOffset, bool IsLeaf, bool IsInvoke,
+      FunctionDescription *FuncDesc, uint32_t FromNodeID, uint32_t ToNodeID,
+      MCPlusBuilder::AllocatorIdTy AllocatorId);
 
   void instrumentLeafNode(BinaryBasicBlock &BB, BinaryBasicBlock::iterator Iter,
                           bool IsLeaf, FunctionDescription &FuncDesc,
-                          uint32_t Node);
+                          uint32_t Node,
+                          MCPlusBuilder::AllocatorIdTy AllocatorId);
 
   void instrumentIndirectTarget(BinaryBasicBlock &BB,
                                 BinaryBasicBlock::iterator &Iter,
diff --git a/bolt/lib/Passes/Instrumentation.cpp b/bolt/lib/Passes/Instrumentation.cpp
index 72adb319d71dc0e..1f68772c4c3a349 100644
--- a/bolt/lib/Passes/Instrumentation.cpp
+++ b/bolt/lib/Passes/Instrumentation.cpp
@@ -190,13 +190,14 @@ void Instrumentation::createLeafNodeDescription(FunctionDescription &FuncDesc,
   FuncDesc.LeafNodes.emplace_back(IN);
 }
 
-InstructionListType
-Instrumentation::createInstrumentationSnippet(BinaryContext &BC, bool IsLeaf) {
+InstructionListType Instrumentation::createInstrumentationSnippet(
+    BinaryContext &BC, bool IsLeaf, MCPlusBuilder::AllocatorIdTy AllocatorId) {
   auto L = BC.scopeLock();
   MCSymbol *Label = BC.Ctx->createNamedTempSymbol("InstrEntry");
   Summary->Counters.emplace_back(Label);
   return BC.MIB->createInstrIncMemory(Label, BC.Ctx.get(), IsLeaf,
-                                      BC.AsmInfo->getCodePointerSize());
+                                      BC.AsmInfo->getCodePointerSize(),
+                                      AllocatorId);
 }
 
 // Helper instruction sequence insertion function
@@ -210,14 +211,13 @@ insertInstructions(InstructionListType &Instrs, BinaryBasicBlock &BB,
   return Iter;
 }
 
-void Instrumentation::instrumentLeafNode(BinaryBasicBlock &BB,
-                                         BinaryBasicBlock::iterator Iter,
-                                         bool IsLeaf,
-                                         FunctionDescription &FuncDesc,
-                                         uint32_t Node) {
+void Instrumentation::instrumentLeafNode(
+    BinaryBasicBlock &BB, BinaryBasicBlock::iterator Iter, bool IsLeaf,
+    FunctionDescription &FuncDesc, uint32_t Node,
+    MCPlusBuilder::AllocatorIdTy AllocatorId) {
   createLeafNodeDescription(FuncDesc, Node);
   InstructionListType CounterInstrs = createInstrumentationSnippet(
-      BB.getFunction()->getBinaryContext(), IsLeaf);
+      BB.getFunction()->getBinaryContext(), IsLeaf, AllocatorId);
   insertInstructions(CounterInstrs, BB, Iter);
 }
 
@@ -247,7 +247,8 @@ bool Instrumentation::instrumentOneTarget(
     BinaryBasicBlock::iterator &Iter, BinaryFunction &FromFunction,
     BinaryBasicBlock &FromBB, uint32_t From, BinaryFunction &ToFunc,
     BinaryBasicBlock *TargetBB, uint32_t ToOffset, bool IsLeaf, bool IsInvoke,
-    FunctionDescription *FuncDesc, uint32_t FromNodeID, uint32_t ToNodeID) {
+    FunctionDescription *FuncDesc, uint32_t FromNodeID, uint32_t ToNodeID,
+    MCPlusBuilder::AllocatorIdTy AllocatorId) {
   BinaryContext &BC = FromFunction.getBinaryContext();
   {
     auto L = BC.scopeLock();
@@ -263,7 +264,8 @@ bool Instrumentation::instrumentOneTarget(
       return false;
   }
 
-  InstructionListType CounterInstrs = createInstrumentationSnippet(BC, IsLeaf);
+  InstructionListType CounterInstrs =
+      createInstrumentationSnippet(BC, IsLeaf, AllocatorId);
 
   const MCInst &Inst = *Iter;
   if (BC.MIB->isCall(Inst)) {
@@ -422,7 +424,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
           instrumentOneTarget(SplitWorklist, SplitInstrs, I, Function, BB,
                               FromOffset, *TargetFunc, TargetBB, ToOffset,
                               IsLeafFunction, IsInvokeBlock, FuncDesc,
-                              BBToID[&BB]);
+                              BBToID[&BB], 0, AllocId);
         }
         continue;
       }
@@ -438,7 +440,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
         instrumentOneTarget(SplitWorklist, SplitInstrs, I, Function, BB,
                             FromOffset, *TargetFunc, TargetBB, ToOffset,
                             IsLeafFunction, IsInvokeBlock, FuncDesc,
-                            BBToID[&BB], BBToID[TargetBB]);
+                            BBToID[&BB], BBToID[TargetBB], AllocId);
         continue;
       }
 
@@ -455,7 +457,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
           instrumentOneTarget(
               SplitWorklist, SplitInstrs, I, Function, BB, FromOffset, Function,
               &*Succ, Succ->getInputOffset(), IsLeafFunction, IsInvokeBlock,
-              FuncDesc, BBToID[&BB], BBToID[&*Succ]);
+              FuncDesc, BBToID[&BB], BBToID[&*Succ], AllocId);
         }
         continue;
       }
@@ -498,7 +500,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
       instrumentOneTarget(SplitWorklist, SplitInstrs, I, Function, BB,
                           FromOffset, Function, FTBB, FTBB->getInputOffset(),
                           IsLeafFunction, IsInvokeBlock, FuncDesc, BBToID[&BB],
-                          BBToID[FTBB]);
+                          BBToID[FTBB], AllocId);
     }
   } // End of BBs loop
 
@@ -508,7 +510,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
       BinaryBasicBlock &BB = *BBI;
       if (STOutSet[&BB].size() == 0)
         instrumentLeafNode(BB, BB.begin(), IsLeafFunction, *FuncDesc,
-                           BBToID[&BB]);
+                           BBToID[&BB], AllocId);
     }
   }
 
diff --git a/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp b/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
index c2a4607411a49e1..1aa3fbce52df3ed 100644
--- a/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
+++ b/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
@@ -1542,9 +1542,10 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
     return Insts;
   }
 
-  InstructionListType
-  createInstrIncMemory(const MCSymbol *Target, MCContext *Ctx, bool IsLeaf,
-                       unsigned CodePointerSize) const override {
+  InstructionListType createInstrIncMemory(const MCSymbol *Target,
+                                           MCContext *Ctx, bool IsLeaf,
+                                           unsigned CodePointerSize,
+                                           AllocatorIdTy AllocatorId) override {
     unsigned int I = 0;
     InstructionListType Instrs(IsLeaf ? 12 : 10);
 
diff --git a/bolt/lib/Target/X86/X86MCPlusBuilder.cpp b/bolt/lib/Target/X86/X86MCPlusBuilder.cpp
index ce8a4d691485446..fbca503c348dbe7 100644
--- a/bolt/lib/Target/X86/X86MCPlusBuilder.cpp
+++ b/bolt/lib/Target/X86/X86MCPlusBuilder.cpp
@@ -3025,9 +3025,10 @@ class X86MCPlusBuilder : public MCPlusBuilder {
     Inst.clear();
   }
 
-  InstructionListType
-  createInstrIncMemory(const MCSymbol *Target, MCContext *Ctx, bool IsLeaf,
-                       unsigned CodePointerSize) const override {
+  InstructionListType createInstrIncMemory(const MCSymbol *Target,
+                                           MCContext *Ctx, bool IsLeaf,
+                                           unsigned CodePointerSize,
+                                           AllocatorIdTy AllocatorId) override {
     InstructionListType Instrs(IsLeaf ? 13 : 11);
     unsigned int I = 0;
 

>From c6eb0de8ec1db0ee4addfd77dd552d482ba4dba6 Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Wed, 11 Oct 2023 09:28:57 +0200
Subject: [PATCH 2/2] Add default argument value for AllocatorId

---
 bolt/include/bolt/Core/MCPlusBuilder.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index 6fc1582bcc2af26..dfb1f542107f50e 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -504,10 +504,10 @@ class MCPlusBuilder {
   }
 
   /// Create increment contents of target by 1 for Instrumentation
-  virtual InstructionListType createInstrIncMemory(const MCSymbol *Target,
-                                                   MCContext *Ctx, bool IsLeaf,
-                                                   unsigned CodePointerSize,
-                                                   AllocatorIdTy AllocatorId) {
+  virtual InstructionListType
+  createInstrIncMemory(const MCSymbol *Target, MCContext *Ctx, bool IsLeaf,
+                       unsigned CodePointerSize,
+                       AllocatorIdTy AllocatorId = 0) {
     llvm_unreachable("not implemented");
     return InstructionListType();
   }



More information about the llvm-commits mailing list