Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
69e1cc7
new runOn method
Casperento Sep 24, 2024
b6b8b67
Merge branch 'main' into merge-functions
Casperento Nov 1, 2024
e391813
Merge branch 'main' into merge-functions
Casperento Nov 2, 2024
3df9ca1
fix format and stable_hash
Casperento Nov 2, 2024
587f210
Merge branch 'main' into merge-functions
Casperento Nov 3, 2024
58be745
Merge branch 'main' into merge-functions
Casperento Nov 4, 2024
72b0431
Merge branch 'main' into merge-functions
Casperento Nov 5, 2024
e22fd6c
Merge branch 'main' into merge-functions
Casperento Nov 5, 2024
67d70af
fix comments
Casperento Nov 6, 2024
b15b1bf
Merge branch 'main' into merge-functions
Casperento Nov 6, 2024
c338f3d
Merge branch 'main' into merge-functions
Casperento Nov 13, 2024
eae748a
fix comments
Casperento Nov 15, 2024
1fdf94e
fix format
Casperento Nov 15, 2024
d193a63
fix comment
Casperento Nov 16, 2024
c2fa33b
Merge branch 'main' into merge-functions
Casperento Nov 16, 2024
1c91d48
Merge branch 'main' into merge-functions
Casperento Nov 17, 2024
913f418
Merge branch 'main' into merge-functions
Casperento Nov 22, 2024
04c23d0
unused includes removed
Casperento Nov 22, 2024
7daef3c
Merge branch 'main' into merge-functions
Casperento Nov 27, 2024
350395d
comments fix
Casperento Nov 27, 2024
1948974
Merge branch 'main' into merge-functions
Casperento Nov 27, 2024
07aa7c1
comment fix
Casperento Nov 27, 2024
fb694a8
Merge branch 'main' into merge-functions
Casperento Nov 27, 2024
a6f6b3c
comment fix
Casperento Nov 28, 2024
97a440d
Merge branch 'main' into merge-functions
Casperento Nov 28, 2024
8703263
comment fix
Casperento Nov 28, 2024
f758d86
Merge branch 'main' into merge-functions
Casperento Nov 28, 2024
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
7 changes: 7 additions & 0 deletions llvm/include/llvm/Transforms/IPO/MergeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
#ifndef LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H
#define LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H

#include "llvm/IR/Function.h"
#include "llvm/IR/PassManager.h"
#include <map>
#include <set>

namespace llvm {

Expand All @@ -25,6 +28,10 @@ class Module;
class MergeFunctionsPass : public PassInfoMixin<MergeFunctionsPass> {
public:
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);

static bool runOnModule(Module &M);
static std::pair<bool, std::map<Function *, Function *>>
runOnFunctions(std::set<Function *> &F);
};

} // end namespace llvm
Expand Down
63 changes: 61 additions & 2 deletions llvm/lib/Transforms/IPO/MergeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#include <algorithm>
#include <cassert>
#include <iterator>
#include <map>
#include <set>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -197,6 +198,8 @@ class MergeFunctions {
}

bool runOnModule(Module &M);
bool runOnFunctions(std::set<Function *> &F);
std::map<Function *, Function *> &getDelToNewMap();

private:
// The function comparison operator is provided here so that FunctionNodes do
Expand Down Expand Up @@ -297,17 +300,31 @@ class MergeFunctions {
// dangling iterators into FnTree. The invariant that preserves this is that
// there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;

/// Deleted-New functions mapping
std::map<Function *, Function *> DelToNewMap;
};
} // end anonymous namespace

PreservedAnalyses MergeFunctionsPass::run(Module &M,
ModuleAnalysisManager &AM) {
MergeFunctions MF;
if (!MF.runOnModule(M))
if (!MergeFunctionsPass::runOnModule(M))
return PreservedAnalyses::all();
return PreservedAnalyses::none();
}

bool MergeFunctionsPass::runOnModule(Module &M) {
MergeFunctions MF;
return MF.runOnModule(M);
}

std::pair<bool, std::map<Function *, Function *>>
MergeFunctionsPass::runOnFunctions(std::set<Function *> &F) {
MergeFunctions MF;
bool MergeResult = MF.runOnFunctions(F);
return {MergeResult, MF.getDelToNewMap()};
}

#ifndef NDEBUG
bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) {
if (const unsigned Max = NumFunctionsForVerificationCheck) {
Expand Down Expand Up @@ -467,6 +484,47 @@ bool MergeFunctions::runOnModule(Module &M) {
return Changed;
}

bool MergeFunctions::runOnFunctions(std::set<Function *> &F) {
bool Changed = false;
std::vector<std::pair<stable_hash, Function *>> HashedFuncs;
for (Function *Func : F) {
if (isEligibleForMerging(*Func)) {
HashedFuncs.push_back({StructuralHash(*Func), Func});
}
}
llvm::stable_sort(HashedFuncs, less_first());
auto S = HashedFuncs.begin();
for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) {
if ((I != S && std::prev(I)->first == I->first) ||
(std::next(I) != IE && std::next(I)->first == I->first)) {
Deferred.push_back(WeakTrackingVH(I->second));
}
}
do {
std::vector<WeakTrackingVH> Worklist;
Deferred.swap(Worklist);
LLVM_DEBUG(dbgs() << "size of function: " << F.size() << '\n');
LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');
for (WeakTrackingVH &I : Worklist) {
if (!I)
continue;
Function *F = cast<Function>(I);
if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
Changed |= insert(F);
}
}
LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n');
} while (!Deferred.empty());
FnTree.clear();
FNodesInTree.clear();
GlobalNumbers.clear();
return Changed;
}

std::map<Function *, Function *> &MergeFunctions::getDelToNewMap() {
return this->DelToNewMap;
}

// Replace direct callers of Old with New.
void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
for (Use &U : llvm::make_early_inc_range(Old->uses())) {
Expand Down Expand Up @@ -1003,6 +1061,7 @@ bool MergeFunctions::insert(Function *NewFunction) {

Function *DeleteF = NewFunction;
mergeTwoFunctions(OldF.getFunc(), DeleteF);
this->DelToNewMap.emplace(DeleteF, OldF.getFunc());
return true;
}

Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/Transforms/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_llvm_unittest(UtilsTests
LoopUtilsTest.cpp
MemTransferLowering.cpp
ModuleUtilsTest.cpp
MergeFunctionsTest.cpp
ScalarEvolutionExpanderTest.cpp
SizeOptsTest.cpp
SSAUpdaterBulkTest.cpp
Expand Down
Loading
Loading