[llvm] r345147 - [llvm-mca] Refactor class SourceMgr. NFCI

Andrea Di Biagio via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 24 08:06:28 PDT 2018


Author: adibiagio
Date: Wed Oct 24 08:06:27 2018
New Revision: 345147

URL: http://llvm.org/viewvc/llvm-project?rev=345147&view=rev
Log:
[llvm-mca] Refactor class SourceMgr. NFCI

Added begin()/end() methods to allow the usage of SourceMgr in foreach loops.
With this change, method getMCInstFromIndex() (as well as a couple of other
methods) are now redundant, and can be removed from the public interface.

Modified:
    llvm/trunk/tools/llvm-mca/Views/InstructionInfoView.cpp
    llvm/trunk/tools/llvm-mca/Views/ResourcePressureView.cpp
    llvm/trunk/tools/llvm-mca/Views/TimelineView.cpp
    llvm/trunk/tools/llvm-mca/include/SourceMgr.h

Modified: llvm/trunk/tools/llvm-mca/Views/InstructionInfoView.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mca/Views/InstructionInfoView.cpp?rev=345147&r1=345146&r2=345147&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mca/Views/InstructionInfoView.cpp (original)
+++ llvm/trunk/tools/llvm-mca/Views/InstructionInfoView.cpp Wed Oct 24 08:06:27 2018
@@ -22,7 +22,6 @@ void InstructionInfoView::printView(raw_
   std::string Buffer;
   raw_string_ostream TempStream(Buffer);
   const MCSchedModel &SM = STI.getSchedModel();
-  unsigned Instructions = Source.size();
 
   std::string Instruction;
   raw_string_ostream InstrStream(Instruction);
@@ -32,8 +31,7 @@ void InstructionInfoView::printView(raw_
              << "[4]: MayLoad\n[5]: MayStore\n[6]: HasSideEffects (U)\n\n";
 
   TempStream << "[1]    [2]    [3]    [4]    [5]    [6]    Instructions:\n";
-  for (unsigned I = 0, E = Instructions; I < E; ++I) {
-    const MCInst &Inst = Source.getMCInstFromIndex(I);
+  for (const MCInst &Inst : Source) {
     const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode());
 
     // Obtain the scheduling class information from the instruction.

Modified: llvm/trunk/tools/llvm-mca/Views/ResourcePressureView.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mca/Views/ResourcePressureView.cpp?rev=345147&r1=345146&r2=345147&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mca/Views/ResourcePressureView.cpp (original)
+++ llvm/trunk/tools/llvm-mca/Views/ResourcePressureView.cpp Wed Oct 24 08:06:27 2018
@@ -148,13 +148,15 @@ void ResourcePressureView::printResource
   std::string Instruction;
   raw_string_ostream InstrStream(Instruction);
 
-  for (unsigned I = 0, E = Source.size(); I < E; ++I) {
+  unsigned InstrIndex = 0;
+  for (const MCInst &MCI : Source) {
+    unsigned BaseEltIdx = InstrIndex * NumResourceUnits;
     for (unsigned J = 0; J < NumResourceUnits; ++J) {
-      double Usage = ResourceUsage[J + I * NumResourceUnits];
+      double Usage = ResourceUsage[J + BaseEltIdx];
       printResourcePressure(FOS, Usage / Executions, (J + 1) * 7);
     }
 
-    MCIP.printInst(&Source.getMCInstFromIndex(I), InstrStream, "", STI);
+    MCIP.printInst(&MCI, InstrStream, "", STI);
     InstrStream.flush();
     StringRef Str(Instruction);
 
@@ -167,6 +169,8 @@ void ResourcePressureView::printResource
     FOS.flush();
     OS << Buffer;
     Buffer = "";
+
+    ++InstrIndex;
   }
 }
 } // namespace mca

Modified: llvm/trunk/tools/llvm-mca/Views/TimelineView.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mca/Views/TimelineView.cpp?rev=345147&r1=345146&r2=345147&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mca/Views/TimelineView.cpp (original)
+++ llvm/trunk/tools/llvm-mca/Views/TimelineView.cpp Wed Oct 24 08:06:27 2018
@@ -177,11 +177,10 @@ void TimelineView::printAverageWaitTimes
 
   formatted_raw_ostream FOS(OS);
   unsigned Executions = Timeline.size() / AsmSequence.size();
-  for (unsigned I = 0, E = WaitTime.size(); I < E; ++I) {
-    printWaitTimeEntry(FOS, WaitTime[I], I, Executions);
+  unsigned IID = 0;
+  for (const MCInst &Inst : AsmSequence) {
+    printWaitTimeEntry(FOS, WaitTime[IID], IID, Executions);
     // Append the instruction info at the end of the line.
-    const MCInst &Inst = AsmSequence.getMCInstFromIndex(I);
-
     MCIP.printInst(&Inst, InstrStream, "", STI);
     InstrStream.flush();
 
@@ -191,6 +190,8 @@ void TimelineView::printAverageWaitTimes
     FOS << "   " << Str << '\n';
     FOS.flush();
     Instruction = "";
+
+    ++IID;
   }
 }
 
@@ -266,25 +267,29 @@ void TimelineView::printTimeline(raw_ost
   std::string Instruction;
   raw_string_ostream InstrStream(Instruction);
 
-  for (unsigned I = 0, E = Timeline.size(); I < E; ++I) {
-    const TimelineViewEntry &Entry = Timeline[I];
-    if (Entry.CycleRetired == 0)
-      return;
-
-    unsigned Iteration = I / AsmSequence.size();
-    unsigned SourceIndex = I % AsmSequence.size();
-    printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex);
-    // Append the instruction info at the end of the line.
-    const MCInst &Inst = AsmSequence.getMCInstFromIndex(I);
-    MCIP.printInst(&Inst, InstrStream, "", STI);
-    InstrStream.flush();
+  unsigned IID = 0;
+  const unsigned Iterations = Timeline.size() / AsmSequence.size();
+  for (unsigned Iteration = 0; Iteration < Iterations; ++Iteration) {
+    for (const MCInst &Inst : AsmSequence) {
+      const TimelineViewEntry &Entry = Timeline[IID];
+      if (Entry.CycleRetired == 0)
+        return;
+
+      unsigned SourceIndex = IID % AsmSequence.size();
+      printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex);
+      // Append the instruction info at the end of the line.
+      MCIP.printInst(&Inst, InstrStream, "", STI);
+      InstrStream.flush();
+
+      // Consume any tabs or spaces at the beginning of the string.
+      StringRef Str(Instruction);
+      Str = Str.ltrim();
+      FOS << "   " << Str << '\n';
+      FOS.flush();
+      Instruction = "";
 
-    // Consume any tabs or spaces at the beginning of the string.
-    StringRef Str(Instruction);
-    Str = Str.ltrim();
-    FOS << "   " << Str << '\n';
-    FOS.flush();
-    Instruction = "";
+      ++IID;
+    }
   }
 }
 } // namespace mca

Modified: llvm/trunk/tools/llvm-mca/include/SourceMgr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mca/include/SourceMgr.h?rev=345147&r1=345146&r2=345147&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mca/include/SourceMgr.h (original)
+++ llvm/trunk/tools/llvm-mca/include/SourceMgr.h Wed Oct 24 08:06:27 2018
@@ -27,7 +27,7 @@ typedef std::pair<unsigned, const llvm::
 class SourceMgr {
   llvm::ArrayRef<llvm::MCInst> Sequence;
   unsigned Current;
-  unsigned Iterations;
+  const unsigned Iterations;
   static const unsigned DefaultIterations = 100;
 
 public:
@@ -35,27 +35,19 @@ public:
       : Sequence(MCInstSequence), Current(0),
         Iterations(NumIterations ? NumIterations : DefaultIterations) {}
 
-  unsigned getCurrentIteration() const { return Current / Sequence.size(); }
   unsigned getNumIterations() const { return Iterations; }
   unsigned size() const { return Sequence.size(); }
-  llvm::ArrayRef<llvm::MCInst> getSequence() const { return Sequence; }
-
-  bool hasNext() const { return Current < (Iterations * size()); }
-  void updateNext() { Current++; }
+  bool hasNext() const { return Current < (Iterations * Sequence.size()); }
+  void updateNext() { ++Current; }
 
   const SourceRef peekNext() const {
     assert(hasNext() && "Already at end of sequence!");
-    unsigned Index = getCurrentInstructionIndex();
-    return SourceRef(Current, Sequence[Index]);
-  }
-
-  unsigned getCurrentInstructionIndex() const {
-    return Current % Sequence.size();
+    return SourceRef(Current, Sequence[Current % Sequence.size()]);
   }
 
-  const llvm::MCInst &getMCInstFromIndex(unsigned Index) const {
-    return Sequence[Index % size()];
-  }
+  using const_iterator = llvm::ArrayRef<llvm::MCInst>::const_iterator;
+  const_iterator begin() const { return Sequence.begin(); }
+  const_iterator end() const { return Sequence.end(); }
 
   bool isEmpty() const { return size() == 0; }
 };




More information about the llvm-commits mailing list