Skip to content

[GlobalMerge][NFC] Skip sorting by profitability when it is not needed #124146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions llvm/lib/CodeGen/GlobalMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,24 +423,12 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
}
}

// Now we found a bunch of sets of globals used together. We accumulated
// the number of times we encountered the sets (i.e., the number of functions
// that use that exact set of globals).
//
// Multiply that by the size of the set to give us a crude profitability
// metric.
llvm::stable_sort(UsedGlobalSets,
[](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
return UGS1.Globals.count() * UGS1.UsageCount <
UGS2.Globals.count() * UGS2.UsageCount;
});

// We can choose to merge all globals together, but ignore globals never used
// with another global. This catches the obviously non-profitable cases of
// having a single global, but is aggressive enough for any other case.
if (GlobalMergeIgnoreSingleUse) {
BitVector AllGlobals(Globals.size());
for (const UsedGlobalSet &UGS : llvm::reverse(UsedGlobalSets)) {
for (const UsedGlobalSet &UGS : UsedGlobalSets) {
if (UGS.UsageCount == 0)
continue;
if (UGS.Globals.count() > 1)
Expand All @@ -449,6 +437,16 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
}

// Now we found a bunch of sets of globals used together. We accumulated
// the number of times we encountered the sets (i.e., the number of functions
// that use that exact set of globals). Multiply that by the size of the set
// to give us a crude profitability metric.
llvm::stable_sort(UsedGlobalSets,
[](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
return UGS1.Globals.count() * UGS1.UsageCount >=
UGS2.Globals.count() * UGS2.UsageCount;
});

// Starting from the sets with the best (=biggest) profitability, find a
// good combination.
// The ideal (and expensive) solution can only be found by trying all
Expand All @@ -458,7 +456,7 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
BitVector PickedGlobals(Globals.size());
bool Changed = false;

for (const UsedGlobalSet &UGS : llvm::reverse(UsedGlobalSets)) {
for (const UsedGlobalSet &UGS : UsedGlobalSets) {
if (UGS.UsageCount == 0)
continue;
if (PickedGlobals.anyCommon(UGS.Globals))
Expand Down
Loading