1515
1616#include " llvm/CGData/StableFunctionMap.h"
1717#include " llvm/ADT/SmallSet.h"
18- #include " llvm/CGData/StableFunctionMapRecord.h"
1918#include " llvm/Support/CommandLine.h"
2019#include " llvm/Support/Debug.h"
21- #include < mutex>
2220
2321#define DEBUG_TYPE " stable-function-map"
2422
@@ -95,10 +93,9 @@ void StableFunctionMap::insert(const StableFunction &Func) {
9593
9694void StableFunctionMap::merge (const StableFunctionMap &OtherMap) {
9795 assert (!Finalized && " Cannot merge after finalization" );
98- deserializeLazyLoadingEntries ();
9996 for (auto &[Hash, Funcs] : OtherMap.HashToFuncs ) {
100- auto &ThisFuncs = HashToFuncs[Hash]. Entries ;
101- for (auto &Func : Funcs. Entries ) {
97+ auto &ThisFuncs = HashToFuncs[Hash];
98+ for (auto &Func : Funcs) {
10299 auto FuncNameId =
103100 getIdOrCreateForName (*OtherMap.getNameForId (Func->FunctionNameId ));
104101 auto ModuleNameId =
@@ -117,63 +114,25 @@ size_t StableFunctionMap::size(SizeType Type) const {
117114 case UniqueHashCount:
118115 return HashToFuncs.size ();
119116 case TotalFunctionCount: {
120- deserializeLazyLoadingEntries ();
121117 size_t Count = 0 ;
122118 for (auto &Funcs : HashToFuncs)
123- Count += Funcs.second .Entries . size ();
119+ Count += Funcs.second .size ();
124120 return Count;
125121 }
126122 case MergeableFunctionCount: {
127- deserializeLazyLoadingEntries ();
128123 size_t Count = 0 ;
129124 for (auto &[Hash, Funcs] : HashToFuncs)
130- if (Funcs.Entries . size () >= 2 )
131- Count += Funcs.Entries . size ();
125+ if (Funcs.size () >= 2 )
126+ Count += Funcs.size ();
132127 return Count;
133128 }
134129 }
135130 llvm_unreachable (" Unhandled size type" );
136131}
137132
138- const StableFunctionMap::StableFunctionEntries &
139- StableFunctionMap::at (HashFuncsMapType::key_type FunctionHash) const {
140- auto It = HashToFuncs.find (FunctionHash);
141- if (isLazilyLoaded ())
142- deserializeLazyLoadingEntry (It);
143- return It->second .Entries ;
144- }
145-
146- void StableFunctionMap::deserializeLazyLoadingEntry (
147- HashFuncsMapType::iterator It) const {
148- assert (isLazilyLoaded () && " Cannot deserialize non-lazily-loaded map" );
149- auto &[Hash, Storage] = *It;
150- std::call_once (Storage.LazyLoadFlag ,
151- [this , HashArg = Hash, &StorageArg = Storage]() {
152- for (auto Offset : StorageArg.Offsets )
153- StableFunctionMapRecord::deserializeEntry (
154- reinterpret_cast <const unsigned char *>(Offset),
155- HashArg, const_cast <StableFunctionMap *>(this ));
156- });
157- }
158-
159- void StableFunctionMap::deserializeLazyLoadingEntries () const {
160- if (!isLazilyLoaded ())
161- return ;
162- for (auto It = HashToFuncs.begin (); It != HashToFuncs.end (); ++It)
163- deserializeLazyLoadingEntry (It);
164- }
165-
166- const StableFunctionMap::HashFuncsMapType &
167- StableFunctionMap::getFunctionMap () const {
168- // Ensure all entries are deserialized before returning the raw map.
169- if (isLazilyLoaded ())
170- deserializeLazyLoadingEntries ();
171- return HashToFuncs;
172- }
173-
174133using ParamLocs = SmallVector<IndexPair>;
175- static void
176- removeIdenticalIndexPair ( StableFunctionMap::StableFunctionEntries &SFS) {
134+ static void removeIdenticalIndexPair (
135+ SmallVector<std::unique_ptr< StableFunctionMap::StableFunctionEntry>> &SFS) {
177136 auto &RSF = SFS[0 ];
178137 unsigned StableFunctionCount = SFS.size ();
179138
@@ -200,7 +159,9 @@ removeIdenticalIndexPair(StableFunctionMap::StableFunctionEntries &SFS) {
200159 SF->IndexOperandHashMap ->erase (Pair);
201160}
202161
203- static bool isProfitable (const StableFunctionMap::StableFunctionEntries &SFS) {
162+ static bool isProfitable (
163+ const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>>
164+ &SFS) {
204165 unsigned StableFunctionCount = SFS.size ();
205166 if (StableFunctionCount < GlobalMergingMinMerges)
206167 return false ;
@@ -241,11 +202,8 @@ static bool isProfitable(const StableFunctionMap::StableFunctionEntries &SFS) {
241202}
242203
243204void StableFunctionMap::finalize (bool SkipTrim) {
244- deserializeLazyLoadingEntries ();
245- SmallVector<HashFuncsMapType::iterator> ToDelete;
246205 for (auto It = HashToFuncs.begin (); It != HashToFuncs.end (); ++It) {
247- auto &[StableHash, Storage] = *It;
248- auto &SFS = Storage.Entries ;
206+ auto &[StableHash, SFS] = *It;
249207
250208 // Group stable functions by ModuleIdentifier.
251209 llvm::stable_sort (SFS, [&](const std::unique_ptr<StableFunctionEntry> &L,
@@ -278,7 +236,7 @@ void StableFunctionMap::finalize(bool SkipTrim) {
278236 }
279237 }
280238 if (Invalid) {
281- ToDelete. push_back (It);
239+ HashToFuncs. erase (It);
282240 continue ;
283241 }
284242
@@ -290,10 +248,8 @@ void StableFunctionMap::finalize(bool SkipTrim) {
290248 removeIdenticalIndexPair (SFS);
291249
292250 if (!isProfitable (SFS))
293- ToDelete. push_back (It);
251+ HashToFuncs. erase (It);
294252 }
295- for (auto It : ToDelete)
296- HashToFuncs.erase (It);
297253
298254 Finalized = true ;
299255}
0 commit comments