@@ -168,44 +168,19 @@ updateBranches(MachineFunction &MF,
168
168
}
169
169
}
170
170
171
- // This function provides the BBCluster information associated with a function.
172
- // Returns true if a valid association exists and false otherwise.
173
- bool getBBClusterInfoForFunction (
174
- const MachineFunction &MF,
175
- BasicBlockSectionsProfileReader *BBSectionsProfileReader,
176
- DenseMap<unsigned , BBClusterInfo> &V) {
177
-
178
- // Find the assoicated cluster information.
179
- std::pair<bool , SmallVector<BBClusterInfo, 4 >> P =
180
- BBSectionsProfileReader->getBBClusterInfoForFunction (MF.getName ());
181
- if (!P.first )
182
- return false ;
183
-
184
- if (P.second .empty ()) {
185
- // This indicates that sections are desired for all basic blocks of this
186
- // function. We clear the BBClusterInfo vector to denote this.
187
- V.clear ();
188
- return true ;
189
- }
190
-
191
- for (const BBClusterInfo &BBCI : P.second )
192
- V[BBCI.BBID ] = BBCI;
193
- return true ;
194
- }
195
-
196
171
// This function sorts basic blocks according to the cluster's information.
197
172
// All explicitly specified clusters of basic blocks will be ordered
198
173
// accordingly. All non-specified BBs go into a separate "Cold" section.
199
174
// Additionally, if exception handling landing pads end up in more than one
200
175
// clusters, they are moved into a single "Exception" section. Eventually,
201
176
// clusters are ordered in increasing order of their IDs, with the "Exception"
202
177
// and "Cold" succeeding all other clusters.
203
- // FuncBBClusterInfo represent the cluster information for basic blocks. It
178
+ // BBProfilesByBBID represents the cluster information for basic blocks. It
204
179
// maps from BBID of basic blocks to their cluster information. If this is
205
180
// empty, it means unique sections for all basic blocks in the function.
206
- static void
207
- assignSections ( MachineFunction &MF,
208
- const DenseMap<unsigned , BBClusterInfo> &FuncBBClusterInfo ) {
181
+ static void assignSections (
182
+ MachineFunction &MF,
183
+ const DenseMap<unsigned , BBProfile< unsigned >> &BBProfilesByBBID ) {
209
184
assert (MF.hasBBSections () && " BB Sections is not set for function." );
210
185
// This variable stores the section ID of the cluster containing eh_pads (if
211
186
// all eh_pads are one cluster). If more than one cluster contain eh_pads, we
@@ -216,17 +191,17 @@ assignSections(MachineFunction &MF,
216
191
// With the 'all' option, every basic block is placed in a unique section.
217
192
// With the 'list' option, every basic block is placed in a section
218
193
// associated with its cluster, unless we want individual unique sections
219
- // for every basic block in this function (if FuncBBClusterInfo is empty).
194
+ // for every basic block in this function (if BBProfilesByBBID is empty).
220
195
if (MF.getTarget ().getBBSectionsType () == llvm::BasicBlockSection::All ||
221
- FuncBBClusterInfo .empty ()) {
196
+ BBProfilesByBBID .empty ()) {
222
197
// If unique sections are desired for all basic blocks of the function, we
223
198
// set every basic block's section ID equal to its original position in
224
199
// the layout (which is equal to its number). This ensures that basic
225
200
// blocks are ordered canonically.
226
201
MBB.setSectionID (MBB.getNumber ());
227
202
} else {
228
- auto I = FuncBBClusterInfo .find (*MBB.getBBID ());
229
- if (I != FuncBBClusterInfo .end ()) {
203
+ auto I = BBProfilesByBBID .find (*MBB.getBBID ());
204
+ if (I != BBProfilesByBBID .end ()) {
230
205
MBB.setSectionID (I->second .ClusterID );
231
206
} else {
232
207
// BB goes into the special cold section if it is not specified in the
@@ -333,16 +308,25 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
333
308
return true ;
334
309
}
335
310
336
- BBSectionsProfileReader = &getAnalysis<BasicBlockSectionsProfileReader>();
311
+ DenseMap<unsigned , BBProfile<unsigned >> BBProfilesByBBID;
312
+ if (BBSectionsType == BasicBlockSection::List) {
313
+ auto [HasProfile, RawProfile] =
314
+ getAnalysis<BasicBlockSectionsProfileReader>().getRawProfileForFunction (
315
+ MF.getName ());
316
+ if (!HasProfile)
317
+ return true ;
318
+ // TODO: Apply the path cloning profile.
319
+ for (const BBProfile<ProfileBBID> &BBP : RawProfile.RawBBProfiles ) {
320
+ assert (!BBP.BasicBlockID .CloneID && " Path cloning is not supported yet." );
321
+ BBProfilesByBBID.try_emplace (BBP.BasicBlockID .BBID ,
322
+ BBProfile<unsigned >{BBP.BasicBlockID .BBID ,
323
+ BBP.ClusterID ,
324
+ BBP.PositionInCluster });
325
+ }
326
+ }
337
327
338
- // Map from BBID of blocks to their cluster information.
339
- DenseMap<unsigned , BBClusterInfo> FuncBBClusterInfo;
340
- if (BBSectionsType == BasicBlockSection::List &&
341
- !getBBClusterInfoForFunction (MF, BBSectionsProfileReader,
342
- FuncBBClusterInfo))
343
- return true ;
344
328
MF.setBBSectionsType (BBSectionsType);
345
- assignSections (MF, FuncBBClusterInfo );
329
+ assignSections (MF, BBProfilesByBBID );
346
330
347
331
// We make sure that the cluster including the entry basic block precedes all
348
332
// other clusters.
@@ -376,8 +360,8 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
376
360
// If the two basic block are in the same section, the order is decided by
377
361
// their position within the section.
378
362
if (XSectionID.Type == MBBSectionID::SectionType::Default)
379
- return FuncBBClusterInfo .lookup (*X.getBBID ()).PositionInCluster <
380
- FuncBBClusterInfo .lookup (*Y.getBBID ()).PositionInCluster ;
363
+ return BBProfilesByBBID .lookup (*X.getBBID ()).PositionInCluster <
364
+ BBProfilesByBBID .lookup (*Y.getBBID ()).PositionInCluster ;
381
365
return X.getNumber () < Y.getNumber ();
382
366
};
383
367
0 commit comments