21#define DEBUG_TYPE "stable-function-map"
27 cl::desc(
"Minimum number of similar functions with "
28 "the same hash required for merging."),
31 "global-merging-min-instrs",
32 cl::desc(
"The minimum instruction count required when merging functions."),
35 "global-merging-max-params",
37 "The maximum number of parameters allowed when merging functions."),
40 "global-merging-skip-no-params",
44 "global-merging-inst-overhead",
45 cl::desc(
"The overhead cost associated with each instruction when lowering "
46 "to machine instruction."),
49 "global-merging-param-overhead",
50 cl::desc(
"The overhead cost associated with each parameter when merging "
55 cl::desc(
"The overhead cost associated with each "
56 "function call when merging functions."),
59 "global-merging-extra-threshold",
60 cl::desc(
"An additional cost threshold that must be exceeded for merging "
61 "to be considered beneficial."),
66 if (It != NameToId.
end())
68 unsigned Id = IdToName.
size();
69 assert(Id == NameToId.
size() &&
"ID collision");
71 NameToId[IdToName.
back()] = Id;
76 if (Id >= IdToName.
size())
82 assert(!Finalized &&
"Cannot insert after finalization");
85 auto IndexOperandHashMap = std::make_unique<IndexOperandHashMapType>();
86 for (
auto &[Index, Hash] : Func.IndexOperandHashes)
87 (*IndexOperandHashMap)[Index] = Hash;
88 auto FuncEntry = std::make_unique<StableFunctionEntry>(
89 Func.Hash, FuncNameId, ModuleNameId, Func.InstCount,
90 std::move(IndexOperandHashMap));
91 insert(std::move(FuncEntry));
95 assert(!Finalized &&
"Cannot merge after finalization");
96 for (
auto &[Hash, Funcs] : OtherMap.HashToFuncs) {
97 auto &ThisFuncs = HashToFuncs[Hash];
98 for (
auto &Func : Funcs) {
103 auto ClonedIndexOperandHashMap =
104 std::make_unique<IndexOperandHashMapType>(*Func->IndexOperandHashMap);
105 ThisFuncs.emplace_back(std::make_unique<StableFunctionEntry>(
106 Func->Hash, FuncNameId, ModuleNameId, Func->InstCount,
107 std::move(ClonedIndexOperandHashMap)));
115 return HashToFuncs.
size();
118 for (
auto &Funcs : HashToFuncs)
119 Count += Funcs.second.size();
124 for (
auto &[Hash, Funcs] : HashToFuncs)
125 if (Funcs.size() >= 2)
126 Count += Funcs.size();
135 SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>> &SFS) {
137 unsigned StableFunctionCount = SFS.size();
140 for (
auto &[Pair, Hash] : *(RSF->IndexOperandHashMap)) {
141 bool Identical =
true;
142 for (
unsigned J = 1; J < StableFunctionCount; ++J) {
144 const auto &SHash = SF->IndexOperandHashMap->at(Pair);
157 for (
auto &Pair : ToDelete)
159 SF->IndexOperandHashMap->erase(Pair);
163 const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>>
165 unsigned StableFunctionCount = SFS.size();
169 unsigned InstCount = SFS[0]->InstCount;
175 for (
auto &SF : SFS) {
176 UniqueHashVals.
clear();
177 for (
auto &[
IndexPair, Hash] : *SF->IndexOperandHashMap)
178 UniqueHashVals.
insert(Hash);
179 unsigned ParamCount = UniqueHashVals.
size();
195 bool Result = Benefit >
Cost;
196 LLVM_DEBUG(
dbgs() <<
"isProfitable: Hash = " << SFS[0]->Hash <<
", "
197 <<
"StableFunctionCount = " << StableFunctionCount
198 <<
", InstCount = " << InstCount
199 <<
", Benefit = " << Benefit <<
", Cost = " <<
Cost
200 <<
", Result = " << (Result ?
"true" :
"false") <<
"\n");
205 for (
auto It = HashToFuncs.
begin(); It != HashToFuncs.
end(); ++It) {
206 auto &[StableHash, SFS] = *It;
209 std::stable_sort(SFS.begin(), SFS.end(),
210 [&](
const std::unique_ptr<StableFunctionEntry> &L,
211 const std::unique_ptr<StableFunctionEntry> &R) {
212 return *getNameForId(L->ModuleNameId) <
213 *getNameForId(R->ModuleNameId);
220 unsigned StableFunctionCount = SFS.size();
221 for (
unsigned I = 1;
I < StableFunctionCount; ++
I) {
223 assert(RSF->Hash == SF->Hash);
224 if (RSF->InstCount != SF->InstCount) {
228 if (RSF->IndexOperandHashMap->size() != SF->IndexOperandHashMap->size()) {
232 for (
auto &
P : *RSF->IndexOperandHashMap) {
233 auto &InstOpndIndex =
P.first;
234 if (!SF->IndexOperandHashMap->count(InstOpndIndex)) {
241 HashToFuncs.
erase(It);
253 HashToFuncs.
erase(It);
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallSet class.
static bool isProfitable(const SmallVector< std::unique_ptr< StableFunctionMap::StableFunctionEntry > > &SFS)
static void removeIdenticalIndexPair(SmallVector< std::unique_ptr< StableFunctionMap::StableFunctionEntry > > &SFS)
static cl::opt< bool > GlobalMergingSkipNoParams("global-merging-skip-no-params", cl::desc("Skip merging functions with no parameters."), cl::init(true), cl::Hidden)
static cl::opt< double > GlobalMergingParamOverhead("global-merging-param-overhead", cl::desc("The overhead cost associated with each parameter when merging " "functions."), cl::init(2.0), cl::Hidden)
static cl::opt< unsigned > GlobalMergingMinMerges("global-merging-min-merges", cl::desc("Minimum number of similar functions with " "the same hash required for merging."), cl::init(2), cl::Hidden)
static cl::opt< double > GlobalMergingInstOverhead("global-merging-inst-overhead", cl::desc("The overhead cost associated with each instruction when lowering " "to machine instruction."), cl::init(1.2), cl::Hidden)
static cl::opt< double > GlobalMergingCallOverhead("global-merging-call-overhead", cl::desc("The overhead cost associated with each " "function call when merging functions."), cl::init(1.0), cl::Hidden)
static cl::opt< unsigned > GlobalMergingMaxParams("global-merging-max-params", cl::desc("The maximum number of parameters allowed when merging functions."), cl::init(std::numeric_limits< unsigned >::max()), cl::Hidden)
static cl::opt< unsigned > GlobalMergingMinInstrs("global-merging-min-instrs", cl::desc("The minimum instruction count required when merging functions."), cl::init(1), cl::Hidden)
static cl::opt< double > GlobalMergingExtraThreshold("global-merging-extra-threshold", cl::desc("An additional cost threshold that must be exceeded for merging " "to be considered beneficial."), cl::init(0.0), cl::Hidden)
bool erase(const KeyT &Val)
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
iterator find(StringRef Key)
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
std::pair< unsigned, unsigned > IndexPair
The pair of an instruction index and a operand index.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void finalize(bool SkipTrim=false)
Finalize the stable function map by trimming content.
size_t size(SizeType Type=UniqueHashCount) const
void insert(const StableFunction &Func)
Insert a StableFunction object into the function map.
void merge(const StableFunctionMap &OtherMap)
Merge a OtherMap into this function map.
std::optional< std::string > getNameForId(unsigned Id) const
Get the name associated with a given ID.
unsigned getIdOrCreateForName(StringRef Name)
Get an existing ID associated with the given name or create a new ID if it doesn't exist.
A stable function is a function with a stable hash while tracking the locations of ignored operands a...