Pipeline.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===--------------------- Pipeline.cpp -------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file implements an ordered container of stages that simulate the
  11. /// pipeline of a hardware backend.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/MCA/Pipeline.h"
  15. #include "llvm/MCA/HWEventListener.h"
  16. #include "llvm/Support/Debug.h"
  17. namespace llvm {
  18. namespace mca {
  19. #define DEBUG_TYPE "llvm-mca"
  20. void Pipeline::addEventListener(HWEventListener *Listener) {
  21. if (Listener)
  22. Listeners.insert(Listener);
  23. for (auto &S : Stages)
  24. S->addListener(Listener);
  25. }
  26. bool Pipeline::hasWorkToProcess() {
  27. return any_of(Stages, [](const std::unique_ptr<Stage> &S) {
  28. return S->hasWorkToComplete();
  29. });
  30. }
  31. Expected<unsigned> Pipeline::run() {
  32. assert(!Stages.empty() && "Unexpected empty pipeline found!");
  33. do {
  34. if (!isPaused())
  35. notifyCycleBegin();
  36. if (Error Err = runCycle())
  37. return std::move(Err);
  38. notifyCycleEnd();
  39. ++Cycles;
  40. } while (hasWorkToProcess());
  41. return Cycles;
  42. }
  43. Error Pipeline::runCycle() {
  44. Error Err = ErrorSuccess();
  45. // Update stages before we start processing new instructions.
  46. for (auto I = Stages.rbegin(), E = Stages.rend(); I != E && !Err; ++I) {
  47. const std::unique_ptr<Stage> &S = *I;
  48. if (isPaused())
  49. Err = S->cycleResume();
  50. else
  51. Err = S->cycleStart();
  52. }
  53. CurrentState = State::Started;
  54. // Now fetch and execute new instructions.
  55. InstRef IR;
  56. Stage &FirstStage = *Stages[0];
  57. while (!Err && FirstStage.isAvailable(IR))
  58. Err = FirstStage.execute(IR);
  59. if (Err.isA<InstStreamPause>()) {
  60. CurrentState = State::Paused;
  61. return Err;
  62. }
  63. // Update stages in preparation for a new cycle.
  64. for (const std::unique_ptr<Stage> &S : Stages) {
  65. Err = S->cycleEnd();
  66. if (Err)
  67. break;
  68. }
  69. return Err;
  70. }
  71. void Pipeline::appendStage(std::unique_ptr<Stage> S) {
  72. assert(S && "Invalid null stage in input!");
  73. if (!Stages.empty()) {
  74. Stage *Last = Stages.back().get();
  75. Last->setNextInSequence(S.get());
  76. }
  77. Stages.push_back(std::move(S));
  78. }
  79. void Pipeline::notifyCycleBegin() {
  80. LLVM_DEBUG(dbgs() << "\n[E] Cycle begin: " << Cycles << '\n');
  81. for (HWEventListener *Listener : Listeners)
  82. Listener->onCycleBegin();
  83. }
  84. void Pipeline::notifyCycleEnd() {
  85. LLVM_DEBUG(dbgs() << "[E] Cycle end: " << Cycles << "\n");
  86. for (HWEventListener *Listener : Listeners)
  87. Listener->onCycleEnd();
  88. }
  89. } // namespace mca.
  90. } // namespace llvm