97 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// Function declarations of utilities related to feature extraction for unit10// testing.11//12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_CODEGEN_MLREGALLOCEVICTIONADVISOR_H15#define LLVM_CODEGEN_MLREGALLOCEVICTIONADVISOR_H16 17#include "llvm/Analysis/MLModelRunner.h"18#include "llvm/CodeGen/MachineBasicBlock.h"19#include "llvm/CodeGen/SlotIndexes.h"20#include "llvm/Support/Compiler.h"21#include <map>22 23namespace llvm {24 25// LRStartEndInfo contains the start and end of a specific live range as26// slot indices as well as storing the index of the physical register it27// is assigned to (or 1 above the phys reg count if its the candidate).28// Used when extracting per-instruction features in the context of a29// specific eviction problem.30struct LRStartEndInfo {31 SlotIndex Begin;32 SlotIndex End;33 size_t Pos = 0;34};35 36LLVM_ABI_FOR_TEST void extractInstructionFeatures(37 llvm::SmallVectorImpl<LRStartEndInfo> &LRPosInfo,38 MLModelRunner *RegallocRunner, function_ref<int(SlotIndex)> GetOpcode,39 function_ref<float(SlotIndex)> GetMBBFreq,40 function_ref<MachineBasicBlock *(SlotIndex)> GetMBBReference,41 const int InstructionsIndex, const int InstructionsMappingIndex,42 const int MBBFreqIndex, const int MBBMappingIndex,43 const SlotIndex LastIndex);44 45LLVM_ABI_FOR_TEST void extractMBBFrequency(46 const SlotIndex CurrentIndex, const size_t CurrentInstructionIndex,47 std::map<MachineBasicBlock *, size_t> &VisitedMBBs,48 function_ref<float(SlotIndex)> GetMBBFreq,49 MachineBasicBlock *CurrentMBBReference, MLModelRunner *RegallocRunner,50 const int MBBFreqIndex, const int MBBMappingIndex);51 52// This is the maximum number of interfererring ranges. That's the number of53// distinct AllocationOrder values, which comes from MCRegisterClass::RegsSize.54// For X86, that's 32.55// TODO: find a way to get this, statically, in a programmatic way.56static const int64_t MaxInterferences = 32;57 58// Logically, we can think of the feature set given to the evaluator as a 2D59// matrix. The rows are the features (see next). The columns correspond to the60// interferences. We treat the candidate virt reg as an 'interference', too, as61// its feature set is the same as that of the interferring ranges. So we'll have62// MaxInterferences + 1 columns and by convention, we will use the last column63// for the virt reg seeking allocation.64static const int64_t CandidateVirtRegPos = MaxInterferences;65static const int64_t NumberOfInterferences = CandidateVirtRegPos + 1;66 67// The number of instructions that a specific live range might have is variable,68// but we're passing in a single matrix of instructions and tensorflow saved69// models only support a fixed input size, so we have to cap the number of70// instructions that can be passed along. The specific value was derived from71// experimentation such that the majority of eviction problems would be72// completely covered.73static const int ModelMaxSupportedInstructionCount = 300;74 75// When extracting per-instruction features, the advisor will currently create76// a vector of size ModelMaxSupportedInstructionCount to hold the opcodes of the77// instructions relevant to the eviction problem, and a NumberOfInterferences *78// ModelMaxSupportedInstructionCount matrix that maps LRs to the instructions79// that they span.80static const std::vector<int64_t> InstructionsShape{81 1, ModelMaxSupportedInstructionCount};82static const std::vector<int64_t> InstructionsMappingShape{83 1, NumberOfInterferences, ModelMaxSupportedInstructionCount};84 85// When extracting mappings between MBBs and individual instructions, we create86// a vector of MBB frequencies, currently of size 100, which was a value87// determined through experimentation to encompass the vast majority of eviction88// problems. The actual mapping is the same shape as the instruction opcodes89// vector.90static const int64_t ModelMaxSupportedMBBCount = 100;91static const std::vector<int64_t> MBBFrequencyShape{1,92 ModelMaxSupportedMBBCount};93 94} // namespace llvm95 96#endif // LLVM_CODEGEN_MLREGALLOCEVICTIONADVISOR_H97