[llvm] [MCA] Enable customization of individual instructions (PR #155420)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 26 07:22:14 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- llvm/include/llvm/MCA/InstrBuilder.h llvm/lib/MCA/InstrBuilder.cpp llvm/tools/llvm-mca/CodeRegion.h llvm/tools/llvm-mca/CodeRegionGenerator.cpp llvm/tools/llvm-mca/CodeRegionGenerator.h llvm/tools/llvm-mca/llvm-mca.cpp llvm/unittests/tools/llvm-mca/MCATestBase.cpp llvm/unittests/tools/llvm-mca/MCATestBase.h llvm/unittests/tools/llvm-mca/X86/TestIncrementalMCA.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/include/llvm/MCA/InstrBuilder.h b/llvm/include/llvm/MCA/InstrBuilder.h
index 9bb8c5b28..7b2fbde2c 100644
--- a/llvm/include/llvm/MCA/InstrBuilder.h
+++ b/llvm/include/llvm/MCA/InstrBuilder.h
@@ -93,7 +93,7 @@ class InstrBuilder {
   Expected<unsigned> getVariantSchedClassID(const MCInst &MCI, unsigned SchedClassID);
   Expected<const InstrDesc &>
   createInstrDescImpl(const MCInst &MCI, const SmallVector<Instrument *> &IVec,
-                      std::function<void(InstrDesc&)> Customizer = {});
+                      std::function<void(InstrDesc &)> Customizer = {});
   Expected<const InstrDesc &>
   getOrCreateInstrDesc(const MCInst &MCI,
                        const SmallVector<Instrument *> &IVec);
@@ -123,7 +123,7 @@ public:
 
   LLVM_ABI Expected<std::unique_ptr<Instruction>>
   createInstruction(const MCInst &MCI, const SmallVector<Instrument *> &IVec,
-                    std::function<void(InstrDesc&)> Customizer = {});
+                    std::function<void(InstrDesc &)> Customizer = {});
 };
 } // namespace mca
 } // namespace llvm
diff --git a/llvm/lib/MCA/InstrBuilder.cpp b/llvm/lib/MCA/InstrBuilder.cpp
index 4a0f09c41..8b3e52a87 100644
--- a/llvm/lib/MCA/InstrBuilder.cpp
+++ b/llvm/lib/MCA/InstrBuilder.cpp
@@ -559,7 +559,7 @@ Expected<unsigned> InstrBuilder::getVariantSchedClassID(const MCInst &MCI,
 Expected<const InstrDesc &>
 InstrBuilder::createInstrDescImpl(const MCInst &MCI,
                                   const SmallVector<Instrument *> &IVec,
-                                  std::function<void(InstrDesc&)> Customizer) {
+                                  std::function<void(InstrDesc &)> Customizer) {
   assert(STI.getSchedModel().hasInstrSchedModel() &&
          "Itineraries are not yet supported!");
 
@@ -638,7 +638,7 @@ InstrBuilder::createInstrDescImpl(const MCInst &MCI,
     Customizer(*ID);
     return *CustomDescriptors.emplace_back(std::move(ID));
   }
-  
+
   bool IsVariadic = MCDesc.isVariadic();
   if ((ID->IsRecyclable = !IsVariadic && !IsVariant)) {
     auto DKey = std::make_pair(MCI.getOpcode(), SchedClassID);
@@ -683,9 +683,10 @@ STATISTIC(NumVariantInst, "Number of MCInsts that doesn't have static Desc");
 Expected<std::unique_ptr<Instruction>>
 InstrBuilder::createInstruction(const MCInst &MCI,
                                 const SmallVector<Instrument *> &IVec,
-                                std::function<void(InstrDesc&)> Customizer) {
-  Expected<const InstrDesc &> DescOrErr = Customizer? createInstrDescImpl(MCI, IVec, Customizer) :
-      getOrCreateInstrDesc(MCI, IVec);
+                                std::function<void(InstrDesc &)> Customizer) {
+  Expected<const InstrDesc &> DescOrErr =
+      Customizer ? createInstrDescImpl(MCI, IVec, Customizer)
+                 : getOrCreateInstrDesc(MCI, IVec);
   if (!DescOrErr)
     return DescOrErr.takeError();
   const InstrDesc &D = *DescOrErr;
diff --git a/llvm/tools/llvm-mca/CodeRegion.h b/llvm/tools/llvm-mca/CodeRegion.h
index d16f976fe..52c085477 100644
--- a/llvm/tools/llvm-mca/CodeRegion.h
+++ b/llvm/tools/llvm-mca/CodeRegion.h
@@ -68,8 +68,8 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/SMLoc.h"
 #include "llvm/Support/SourceMgr.h"
-#include <vector>
 #include <utility>
+#include <vector>
 
 namespace llvm {
 namespace mca {
@@ -161,7 +161,7 @@ protected:
   bool FoundErrors;
 
   // Annotations specified in comments, indexed by SMLoc value
-  llvm::DenseMap<const char*, InstAnnotation> Annotations;
+  llvm::DenseMap<const char *, InstAnnotation> Annotations;
 
 public:
   CodeRegions(llvm::SourceMgr &S) : SM(S), FoundErrors(false) {}
@@ -178,7 +178,9 @@ public:
   void addInstruction(const llvm::MCInst &Instruction);
   llvm::SourceMgr &getSourceMgr() const { return SM; }
 
-  void Annotate(llvm::SMLoc Loc, const InstAnnotation& A) { Annotations[Loc.getPointer()] = A; }
+  void Annotate(llvm::SMLoc Loc, const InstAnnotation &A) {
+    Annotations[Loc.getPointer()] = A;
+  }
   std::optional<unsigned> getExplicitLatency(llvm::SMLoc Loc) const {
     const auto It = Annotations.find(Loc.getPointer());
     if (It != Annotations.end()) {
diff --git a/llvm/tools/llvm-mca/CodeRegionGenerator.cpp b/llvm/tools/llvm-mca/CodeRegionGenerator.cpp
index 767f92bf6..0d400760f 100644
--- a/llvm/tools/llvm-mca/CodeRegionGenerator.cpp
+++ b/llvm/tools/llvm-mca/CodeRegionGenerator.cpp
@@ -96,15 +96,15 @@ void AnalysisRegionCommentConsumer::HandleComment(SMLoc Loc,
 
   Comment = Comment.drop_front(Position);
   if (Comment.starts_with("LLVM-MCA-LATENCY")) {
-      auto Parts = Comment.split(':');
-      Position = Parts.second.find_first_not_of(" \t");
-      if (Position >= Parts.second.size())
-        return;
-      auto LatStr = Parts.second.drop_front(Position);
-      unsigned Latency = 0;
-      if (!LatStr.getAsInteger(10, Latency))
-        Streamer.AddLatencyAnnotation(Latency);
+    auto Parts = Comment.split(':');
+    Position = Parts.second.find_first_not_of(" \t");
+    if (Position >= Parts.second.size())
       return;
+    auto LatStr = Parts.second.drop_front(Position);
+    unsigned Latency = 0;
+    if (!LatStr.getAsInteger(10, Latency))
+      Streamer.AddLatencyAnnotation(Latency);
+    return;
   }
 
   if (Comment.consume_front("LLVM-MCA-END")) {
diff --git a/llvm/tools/llvm-mca/CodeRegionGenerator.h b/llvm/tools/llvm-mca/CodeRegionGenerator.h
index 2c632be5e..1f43a8b26 100644
--- a/llvm/tools/llvm-mca/CodeRegionGenerator.h
+++ b/llvm/tools/llvm-mca/CodeRegionGenerator.h
@@ -52,7 +52,8 @@ public:
   }
 
   void AddLatencyAnnotation(unsigned Lat) {
-    if (!CurrentAnnotation) CurrentAnnotation = InstAnnotation();
+    if (!CurrentAnnotation)
+      CurrentAnnotation = InstAnnotation();
     CurrentAnnotation->Latency = Lat;
   }
 
@@ -91,7 +92,8 @@ class AnalysisRegionCommentConsumer : public MCACommentConsumer {
   MCStreamerWrapper &Streamer;
 
 public:
-  AnalysisRegionCommentConsumer(AnalysisRegions &R, MCStreamerWrapper &S) : Regions(R), Streamer(S) {}
+  AnalysisRegionCommentConsumer(AnalysisRegions &R, MCStreamerWrapper &S)
+      : Regions(R), Streamer(S) {}
 
   /// Parses a comment. It begins a new region if it is of the form
   /// LLVM-MCA-BEGIN. It ends a region if it is of the form LLVM-MCA-END.
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index 5f653ffcd..73ac11794 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -634,11 +634,14 @@ int main(int argc, char **argv) {
           InstrumentRegions.getActiveInstruments(Loc);
 
       auto Latency = Regions.getExplicitLatency(Loc);
-      Expected<std::unique_ptr<mca::Instruction>> Inst = Latency ?
-          IB.createInstruction(MCI, Instruments, [=](llvm::mca::InstrDesc& ID) {
-            for (auto& W : ID.Writes) W.Latency = *Latency;
-            ID.MaxLatency = *Latency; }) :
-          IB.createInstruction(MCI, Instruments);
+      Expected<std::unique_ptr<mca::Instruction>> Inst =
+          Latency ? IB.createInstruction(MCI, Instruments,
+                                         [=](llvm::mca::InstrDesc &ID) {
+                                           for (auto &W : ID.Writes)
+                                             W.Latency = *Latency;
+                                           ID.MaxLatency = *Latency;
+                                         })
+                  : IB.createInstruction(MCI, Instruments);
       if (!Inst) {
         if (auto NewE = handleErrors(
                 Inst.takeError(),
diff --git a/llvm/unittests/tools/llvm-mca/MCATestBase.cpp b/llvm/unittests/tools/llvm-mca/MCATestBase.cpp
index 96413168b..e2ff1dbe7 100644
--- a/llvm/unittests/tools/llvm-mca/MCATestBase.cpp
+++ b/llvm/unittests/tools/llvm-mca/MCATestBase.cpp
@@ -61,8 +61,7 @@ void MCATestBase::SetUp() {
 
 Error MCATestBase::runBaselineMCA(json::Object &Result, ArrayRef<MCInst> Insts,
                                   ArrayRef<mca::View *> Views,
-                                  const mca::PipelineOptions *PO,
-                                  Builder B) {
+                                  const mca::PipelineOptions *PO, Builder B) {
   mca::Context MCA(*MRI, *STI);
 
   // Default InstrumentManager
diff --git a/llvm/unittests/tools/llvm-mca/MCATestBase.h b/llvm/unittests/tools/llvm-mca/MCATestBase.h
index 399a8e892..e991252cd 100644
--- a/llvm/unittests/tools/llvm-mca/MCATestBase.h
+++ b/llvm/unittests/tools/llvm-mca/MCATestBase.h
@@ -71,8 +71,9 @@ protected:
 
   void SetUp() override;
 
-  using Builder = std::function<Expected<std::unique_ptr<mca::Instruction>>
-      (mca::InstrBuilder&, const MCInst&, const SmallVector<mca::Instrument *>&)>;
+  using Builder = std::function<Expected<std::unique_ptr<mca::Instruction>>(
+      mca::InstrBuilder &, const MCInst &,
+      const SmallVector<mca::Instrument *> &)>;
 
   /// Utility function to run MCA with (nearly) the same configuration as the
   /// `llvm-mca` tool to verify result correctness.
diff --git a/llvm/unittests/tools/llvm-mca/X86/TestIncrementalMCA.cpp b/llvm/unittests/tools/llvm-mca/X86/TestIncrementalMCA.cpp
index cf66460b1..8aa879b3e 100644
--- a/llvm/unittests/tools/llvm-mca/X86/TestIncrementalMCA.cpp
+++ b/llvm/unittests/tools/llvm-mca/X86/TestIncrementalMCA.cpp
@@ -236,7 +236,7 @@ TEST_F(X86TestBase, TestVariantInstructionsSameAddress) {
 }
 
 TEST_F(X86TestBase, TestInstructionCustomization) {
-  const unsigned ExplicitLatency = 100;  
+  const unsigned ExplicitLatency = 100;
   SmallVector<MCInst> MCIs;
   MCInst InstructionToAdd = MCInstBuilder(X86::XOR64rr)
                                 .addReg(X86::RAX)
@@ -247,13 +247,15 @@ TEST_F(X86TestBase, TestInstructionCustomization) {
   // Run the baseline.
   json::Object BaselineResult;
   auto E = runBaselineMCA(BaselineResult, MCIs, {}, nullptr,
-      [=](InstrBuilder& IB, const MCInst& MCI, const SmallVector<Instrument*>& Instruments) {
-        return IB.createInstruction(MCI, Instruments,
-          [=](InstrDesc& ID) {
-            for (auto& W : ID.Writes) W.Latency = ExplicitLatency;
-            ID.MaxLatency = ExplicitLatency;
-          });
-      });
+                          [=](InstrBuilder &IB, const MCInst &MCI,
+                              const SmallVector<Instrument *> &Instruments) {
+                            return IB.createInstruction(
+                                MCI, Instruments, [=](InstrDesc &ID) {
+                                  for (auto &W : ID.Writes)
+                                    W.Latency = ExplicitLatency;
+                                  ID.MaxLatency = ExplicitLatency;
+                                });
+                          });
   auto *BaselineObj = BaselineResult.getObject("SummaryView");
   auto V = BaselineObj->getInteger("TotalCycles");
   ASSERT_TRUE(V);

``````````

</details>


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


More information about the llvm-commits mailing list