Skip to content

LLVMMergeFunctions: allow more parameters if the function is bigger #33331

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 1 commit into from
Aug 7, 2020
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
61 changes: 35 additions & 26 deletions lib/LLVMPasses/LLVMMergeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,6 @@ class SwiftMergeFunctions : public ModulePass {
bool runOnModule(Module &M) override;

private:
enum {
/// The maximum number of parameters added to a merged functions. This
/// roughly corresponds to the number of differing constants.
maxAddedParams = 4
};

struct FunctionEntry;

/// Describes the set of functions which are considered as "equivalent" (i.e.
Expand Down Expand Up @@ -359,7 +353,7 @@ class SwiftMergeFunctions : public ModulePass {
}
};

using ParamInfos = SmallVector<ParamInfo, maxAddedParams>;
using ParamInfos = SmallVector<ParamInfo, 16>;

GlobalNumberState GlobalNumbers;

Expand Down Expand Up @@ -398,14 +392,15 @@ class SwiftMergeFunctions : public ModulePass {

FunctionInfo removeFuncWithMostParams(FunctionInfos &FInfos);

bool deriveParams(ParamInfos &Params, FunctionInfos &FInfos);
bool deriveParams(ParamInfos &Params, FunctionInfos &FInfos,
unsigned maxParams);

bool numOperandsDiffer(FunctionInfos &FInfos);

bool constsDiffer(const FunctionInfos &FInfos, unsigned OpIdx);

bool tryMapToParameter(FunctionInfos &FInfos, unsigned OpIdx,
ParamInfos &Params);
ParamInfos &Params, unsigned maxParams);

void mergeWithParams(const FunctionInfos &FInfos, ParamInfos &Params);

Expand Down Expand Up @@ -524,17 +519,9 @@ static bool mayMergeCallsToFunction(Function &F) {
return true;
}

/// Returns true if function \p F is eligible for merging.
static bool isEligibleFunction(Function *F) {
if (F->isDeclaration())
return false;

if (F->hasAvailableExternallyLinkage())
return false;

if (F->getFunctionType()->isVarArg())
return false;

/// Returns the benefit, which is approximately the size of the function.
/// Return 0, if the function should not be merged.
static unsigned getBenefit(Function *F) {
unsigned Benefit = 0;

// We don't want to merge very small functions, because the overhead of
Expand All @@ -545,7 +532,7 @@ static bool isEligibleFunction(Function *F) {
if (CallBase *CB = dyn_cast<CallBase>(&I)) {
Function *Callee = CB->getCalledFunction();
if (Callee && !mayMergeCallsToFunction(*Callee))
return false;
return 0;
if (!Callee || !Callee->isIntrinsic()) {
Benefit += 5;
continue;
Expand All @@ -554,6 +541,21 @@ static bool isEligibleFunction(Function *F) {
Benefit += 1;
}
}
return Benefit;
}

/// Returns true if function \p F is eligible for merging.
static bool isEligibleFunction(Function *F) {
if (F->isDeclaration())
return false;

if (F->hasAvailableExternallyLinkage())
return false;

if (F->getFunctionType()->isVarArg())
return false;

unsigned Benefit = getBenefit(F);
if (Benefit < FunctionMergeThreshold)
return false;

Expand Down Expand Up @@ -723,12 +725,17 @@ bool SwiftMergeFunctions::tryMergeEquivalenceClass(FunctionEntry *FirstInClass)
bool Changed = false;
int Try = 0;

unsigned Benefit = getBenefit(FirstInClass->F);

// The bigger the function, the more parameters are allowed.
unsigned maxParams = std::max(4u, Benefit / 100);

// We need multiple tries if there are some functions in FInfos which differ
// too much from the first function in FInfos. But we limit the number of
// tries to a small number, because this is quadratic.
while (FInfos.size() >= 2 && Try++ < 4) {
ParamInfos Params;
bool Merged = deriveParams(Params, FInfos);
bool Merged = deriveParams(Params, FInfos, maxParams);
if (Merged) {
mergeWithParams(FInfos, Params);
Changed = true;
Expand Down Expand Up @@ -767,7 +774,8 @@ removeFuncWithMostParams(FunctionInfos &FInfos) {
/// Returns true on success, i.e. the functions in \p FInfos can be merged with
/// the parameters returned in \p Params.
bool SwiftMergeFunctions::deriveParams(ParamInfos &Params,
FunctionInfos &FInfos) {
FunctionInfos &FInfos,
unsigned maxParams) {
for (FunctionInfo &FI : FInfos)
FI.init();

Expand Down Expand Up @@ -796,7 +804,7 @@ bool SwiftMergeFunctions::deriveParams(ParamInfos &Params,
if (constsDiffer(FInfos, OpIdx)) {
// This instruction has operands which differ in at least some
// functions. So we need to parameterize it.
if (!tryMapToParameter(FInfos, OpIdx, Params)) {
if (!tryMapToParameter(FInfos, OpIdx, Params, maxParams)) {
// We ran out of parameters.
return false;
}
Expand Down Expand Up @@ -845,7 +853,8 @@ bool SwiftMergeFunctions::constsDiffer(const FunctionInfos &FInfos,
/// Returns true if a parameter could be created or found without exceeding the
/// maximum number of parameters.
bool SwiftMergeFunctions::tryMapToParameter(FunctionInfos &FInfos,
unsigned OpIdx, ParamInfos &Params) {
unsigned OpIdx, ParamInfos &Params,
unsigned maxParams) {
ParamInfo *Matching = nullptr;
// Try to find an existing parameter which exactly matches the differing
// operands of the current instruction.
Expand All @@ -858,7 +867,7 @@ bool SwiftMergeFunctions::tryMapToParameter(FunctionInfos &FInfos,
if (!Matching) {
// We need a new parameter.
// Check if we are within the limit.
if (Params.size() >= maxAddedParams)
if (Params.size() >= maxParams)
return false;

Params.resize(Params.size() + 1);
Expand Down