MLRegallocEvictAdvisor.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //===- MLRegAllocEvictAdvisor.cpp - ML eviction advisor -------------------===//
  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. // Function declarations of utilities related to feature extraction for unit
  10. // testing.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MLREGALLOCEVICTIONADVISOR_H
  14. #define LLVM_CODEGEN_MLREGALLOCEVICTIONADVISOR_H
  15. #include "llvm/Analysis/MLModelRunner.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/SlotIndexes.h"
  18. using namespace llvm;
  19. // LRStartEndInfo contains the start and end of a specific live range as
  20. // slot indices as well as storing the index of the physical register it
  21. // is assigned to (or 1 above the phys reg count if its the candidate).
  22. // Used when extracting per-instruction features in the context of a
  23. // specific eviction problem.
  24. struct LRStartEndInfo {
  25. SlotIndex Begin;
  26. SlotIndex End;
  27. size_t Pos = 0;
  28. };
  29. void extractInstructionFeatures(
  30. llvm::SmallVectorImpl<LRStartEndInfo> &LRPosInfo,
  31. MLModelRunner *RegallocRunner, function_ref<int(SlotIndex)> GetOpcode,
  32. function_ref<float(SlotIndex)> GetMBBFreq,
  33. function_ref<MachineBasicBlock *(SlotIndex)> GetMBBReference,
  34. const int InstructionsIndex, const int InstructionsMappingIndex,
  35. const int MBBFreqIndex, const int MBBMappingIndex,
  36. const SlotIndex LastIndex);
  37. void extractMBBFrequency(const SlotIndex CurrentIndex,
  38. const size_t CurrentInstructionIndex,
  39. std::map<MachineBasicBlock *, size_t> &VisitedMBBs,
  40. function_ref<float(SlotIndex)> GetMBBFreq,
  41. MachineBasicBlock *CurrentMBBReference,
  42. MLModelRunner *RegallocRunner, const int MBBFreqIndex,
  43. const int MBBMappingIndex);
  44. // This is the maximum number of interfererring ranges. That's the number of
  45. // distinct AllocationOrder values, which comes from MCRegisterClass::RegsSize.
  46. // For X86, that's 32.
  47. // TODO: find a way to get this, statically, in a programmatic way.
  48. static const int64_t MaxInterferences = 32;
  49. // Logically, we can think of the feature set given to the evaluator as a 2D
  50. // matrix. The rows are the features (see next). The columns correspond to the
  51. // interferences. We treat the candidate virt reg as an 'interference', too, as
  52. // its feature set is the same as that of the interferring ranges. So we'll have
  53. // MaxInterferences + 1 columns and by convention, we will use the last column
  54. // for the virt reg seeking allocation.
  55. static const int64_t CandidateVirtRegPos = MaxInterferences;
  56. static const int64_t NumberOfInterferences = CandidateVirtRegPos + 1;
  57. // The number of instructions that a specific live range might have is variable,
  58. // but we're passing in a single matrix of instructions and tensorflow saved
  59. // models only support a fixed input size, so we have to cap the number of
  60. // instructions that can be passed along. The specific value was derived from
  61. // experimentation such that the majority of eviction problems would be
  62. // completely covered.
  63. static const int ModelMaxSupportedInstructionCount = 300;
  64. // When extracting per-instruction features, the advisor will currently create
  65. // a vector of size ModelMaxSupportedInstructionCount to hold the opcodes of the
  66. // instructions relevant to the eviction problem, and a NumberOfInterferences *
  67. // ModelMaxSupportedInstructionCount matrix that maps LRs to the instructions
  68. // that they span.
  69. static const std::vector<int64_t> InstructionsShape{
  70. 1, ModelMaxSupportedInstructionCount};
  71. static const std::vector<int64_t> InstructionsMappingShape{
  72. 1, NumberOfInterferences, ModelMaxSupportedInstructionCount};
  73. // When extracting mappings between MBBs and individual instructions, we create
  74. // a vector of MBB frequencies, currently of size 100, which was a value
  75. // determined through experimentation to encompass the vast majority of eviction
  76. // problems. The actual mapping is the same shape as the instruction opcodes
  77. // vector.
  78. static const int64_t ModelMaxSupportedMBBCount = 100;
  79. static const std::vector<int64_t> MBBFrequencyShape{1,
  80. ModelMaxSupportedMBBCount};
  81. #endif // LLVM_CODEGEN_MLREGALLOCEVICTIONADVISOR_H