ValueProfileCollector.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===- ValueProfileCollector.cpp - determine what to value profile --------===//
  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. //
  9. // The implementation of the ValueProfileCollector via ValueProfileCollectorImpl
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ValueProfileCollector.h"
  13. #include "ValueProfilePlugins.inc"
  14. #include "llvm/ProfileData/InstrProf.h"
  15. using namespace llvm;
  16. namespace {
  17. /// A plugin-based class that takes an arbitrary number of Plugin types.
  18. /// Each plugin type must satisfy the following API:
  19. /// 1) the constructor must take a `Function &f`. Typically, the plugin would
  20. /// scan the function looking for candidates.
  21. /// 2) contain a member function with the following signature and name:
  22. /// void run(std::vector<CandidateInfo> &Candidates);
  23. /// such that the plugin would append its result into the vector parameter.
  24. ///
  25. /// Plugins are defined in ValueProfilePlugins.inc
  26. template <class... Ts> class PluginChain;
  27. /// The type PluginChainFinal is the final chain of plugins that will be used by
  28. /// ValueProfileCollectorImpl.
  29. using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;
  30. template <> class PluginChain<> {
  31. public:
  32. PluginChain(Function &F, TargetLibraryInfo &TLI) {}
  33. void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}
  34. };
  35. template <class PluginT, class... Ts>
  36. class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {
  37. PluginT Plugin;
  38. using Base = PluginChain<Ts...>;
  39. public:
  40. PluginChain(Function &F, TargetLibraryInfo &TLI)
  41. : PluginChain<Ts...>(F, TLI), Plugin(F, TLI) {}
  42. void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {
  43. if (K == PluginT::Kind)
  44. Plugin.run(Candidates);
  45. Base::get(K, Candidates);
  46. }
  47. };
  48. } // end anonymous namespace
  49. /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
  50. class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {
  51. public:
  52. using PluginChainFinal::PluginChainFinal;
  53. };
  54. ValueProfileCollector::ValueProfileCollector(Function &F,
  55. TargetLibraryInfo &TLI)
  56. : PImpl(new ValueProfileCollectorImpl(F, TLI)) {}
  57. ValueProfileCollector::~ValueProfileCollector() = default;
  58. std::vector<CandidateInfo>
  59. ValueProfileCollector::get(InstrProfValueKind Kind) const {
  60. std::vector<CandidateInfo> Result;
  61. PImpl->get(Kind, Result);
  62. return Result;
  63. }