This is an archive of the discontinued LLVM Phabricator instance.

[FuncSpec][NFC] Avoid redundant computations of DominatorTree/LoopInfo
ClosedPublic

Authored by chill on Oct 20 2022, 3:55 AM.

Details

Summary

The FunctionSpecialization pass needs loop analysis results for its
cost function. For this purpose, it computes the DominatorTree and
LoopInfo for a function in getSpecializationBonus. This function,
however, is called O(number of call sites x number of arguments), but
the DominatorTree/LoopInfo can be computed just once.

This patch plugs into the PassManager infrastructure to obtain
LoopInfo for a function and removes ad-hoc computation from
getSpecializatioBonus.

Diff Detail

Event Timeline

chill created this revision.Oct 20 2022, 3:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2022, 3:55 AM
chill requested review of this revision.Oct 20 2022, 3:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2022, 3:55 AM
chill added inline comments.Oct 20 2022, 3:59 AM
llvm/lib/Transforms/IPO/SCCP.cpp
80–82

I don't understand why not preserving the DT is a reason to not pass it. Maybe @fhahn can provide a rationale?

In the meantime, there's a workaround to compute DT/LI on the spot.

ChuanqiXu added inline comments.Oct 20 2022, 7:16 PM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
828

Solver is a member variable of FunctionSpecializer already. So the most of the work to rewrite signatures look odd to me

llvm/lib/Transforms/IPO/SCCP.cpp
80–82

The code lives in Legacy path. And the legacyPM related code can be removed now.

chill updated this revision to Diff 469659.Oct 21 2022, 9:37 AM
chill marked 2 inline comments as done.
chill added inline comments.
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
828

Ah, and oversight on my part, I'll remove the unnecessary parameter.

ChuanqiXu added inline comments.Oct 23 2022, 7:34 PM
llvm/lib/Transforms/Utils/SCCPSolver.cpp
342–347

I feel like the assertion may be triggered if we run specializer on the specialized functions. This looks possible when FuncSpecializationMaxIters>1 but we only calculate the analysis once.

SjoerdMeijer added inline comments.Oct 24 2022, 3:51 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
466

Nit: if this is a simple getter function, not calculating anything, we can just pass Solver.getLoopInfo(*F) in here.

llvm/lib/Transforms/Utils/SCCPSolver.cpp
342–347

Or with the legacy pass manager? Is that how I should read the comment above:

// We cannot preserve the LI, DT, or PDT with the legacy pass ...
labrinea added inline comments.Oct 24 2022, 6:32 AM
llvm/lib/Transforms/Utils/SCCPSolver.cpp
342–347

@ChuanqiXu I don't think we ever pass cloned functions to findSpecializations() (see isCandidateFunction() - it early returns if SpecializedFuncs.contains(F))

chill marked an inline comment as done.Oct 24 2022, 9:15 AM
chill added inline comments.
llvm/lib/Transforms/Utils/SCCPSolver.cpp
342–347

Yeah, I had a workaround (see a previous version of the patch), but AFAICT, LegacyPM is no longer supported, see https://discourse.llvm.org/t/legacy-pm-in-llvm-15/64305
(and was supposed to be removed one LLVM release ago :D )

chill updated this revision to Diff 470198.Oct 24 2022, 9:59 AM
chill marked 4 inline comments as done.
labrinea accepted this revision.Oct 24 2022, 2:48 PM

LGTM.

This revision is now accepted and ready to land.Oct 24 2022, 2:48 PM
ChuanqiXu accepted this revision.Oct 24 2022, 7:04 PM
ChuanqiXu added inline comments.
llvm/lib/Transforms/Utils/SCCPSolver.cpp
342–347

Yeah, it makes sense.

labrinea added inline comments.Oct 25 2022, 2:18 AM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
608–609

clang format this please, it exceeds the 80 char limit

llvm/lib/Transforms/Utils/SCCPSolver.cpp
1537

same here, clang format

chill updated this revision to Diff 471111.Oct 27 2022, 3:48 AM
chill marked 2 inline comments as done.Oct 27 2022, 3:49 AM
chill marked an inline comment as done.
This revision was landed with ongoing or failed builds.Oct 28 2022, 8:22 AM
This revision was automatically updated to reflect the committed changes.