Skip to content

Commit 9325a61

Browse files
Revert "[GlobalMerge][NFC] Skip sorting by profitability when it is not needed" (#124411)
Reverts #124146 -- new comparator is not a strict-weak as required by stable_sort. Co-authored-by: Michael Maitland <[email protected]>
1 parent 6383a12 commit 9325a61

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

llvm/lib/CodeGen/GlobalMerge.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,24 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
423423
}
424424
}
425425

426+
// Now we found a bunch of sets of globals used together. We accumulated
427+
// the number of times we encountered the sets (i.e., the number of functions
428+
// that use that exact set of globals).
429+
//
430+
// Multiply that by the size of the set to give us a crude profitability
431+
// metric.
432+
llvm::stable_sort(UsedGlobalSets,
433+
[](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
434+
return UGS1.Globals.count() * UGS1.UsageCount <
435+
UGS2.Globals.count() * UGS2.UsageCount;
436+
});
437+
426438
// We can choose to merge all globals together, but ignore globals never used
427439
// with another global. This catches the obviously non-profitable cases of
428440
// having a single global, but is aggressive enough for any other case.
429441
if (GlobalMergeIgnoreSingleUse) {
430442
BitVector AllGlobals(Globals.size());
431-
for (const UsedGlobalSet &UGS : UsedGlobalSets) {
443+
for (const UsedGlobalSet &UGS : llvm::reverse(UsedGlobalSets)) {
432444
if (UGS.UsageCount == 0)
433445
continue;
434446
if (UGS.Globals.count() > 1)
@@ -437,16 +449,6 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
437449
return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
438450
}
439451

440-
// Now we found a bunch of sets of globals used together. We accumulated
441-
// the number of times we encountered the sets (i.e., the number of functions
442-
// that use that exact set of globals). Multiply that by the size of the set
443-
// to give us a crude profitability metric.
444-
llvm::stable_sort(UsedGlobalSets,
445-
[](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
446-
return UGS1.Globals.count() * UGS1.UsageCount >=
447-
UGS2.Globals.count() * UGS2.UsageCount;
448-
});
449-
450452
// Starting from the sets with the best (=biggest) profitability, find a
451453
// good combination.
452454
// The ideal (and expensive) solution can only be found by trying all
@@ -456,7 +458,7 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
456458
BitVector PickedGlobals(Globals.size());
457459
bool Changed = false;
458460

459-
for (const UsedGlobalSet &UGS : UsedGlobalSets) {
461+
for (const UsedGlobalSet &UGS : llvm::reverse(UsedGlobalSets)) {
460462
if (UGS.UsageCount == 0)
461463
continue;
462464
if (PickedGlobals.anyCommon(UGS.Globals))

0 commit comments

Comments
 (0)