This is an archive of the discontinued LLVM Phabricator instance.

[IPSCCP] Decouple queries for function analysis results.
ClosedPublic

Authored by labrinea on May 29 2023, 4:36 PM.

Details

Summary

The SCCPSolver is using a structure (AnalysisResultsForFn) where it keeps
pointers to various analyses needed by the IPSCCP pass. These analyses are
requested all at the same time, which can become problematic in some cases.
For example one could be retrieved via getCachedAnalysis() prior to the
actual execution of the analysis. In more detail:

The IPSCCP pass uses a DomTreeUpdater to preserve the PostDominatorTree
in case the PostDominatorTreeAnalysis had run before IPSCCP. Starting with
commit 1b1232047e83b the IPSCCP pass may use BlockFrequencyAnalysis for
some functions in the module. As a result, the PostDominatorTreeAnalysis
may not run until the BlockFrequencyAnalysis has run, since the latter
analysis depends on the former. Currently, we setup the DomTreeUpdater
using getCachedAnalysis to retrieve a PostDominatorTree. This happens
before BlockFrequencyAnalysis has run, therefore the cached analysis can
become invalid by the time we use it. To solve this problem we must
request the PostDominatorTree after BlockFrequencyAnalysis runs.

Diff Detail

Event Timeline

labrinea created this revision.May 29 2023, 4:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 29 2023, 4:36 PM
labrinea requested review of this revision.May 29 2023, 4:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 29 2023, 4:36 PM
labrinea added inline comments.May 29 2023, 4:43 PM
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
711 ↗(On Diff #526483)

Perhaps this could move inside GetBFI, which would have to become specific to the FunctionSpecializer.

I've veriified that the different crashes we've seen with https://reviews.llvm.org/D150375 goes away with this patch.

bjope added inline comments.May 30 2023, 1:05 AM
llvm/lib/Transforms/IPO/SCCP.cpp
231–233

Had been nice if one could just do something like this here:

DominatorTree *DT = FAM->getCachedResult<DominatorTreeAnalysis>(F);
PostDominatorTree *PDT = FAM->getCachedResult<PostDominatorTreeAnalysis>(F);
DomTreeUpdated DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy};

I.e. just use getCachedResult to see if there is a DT/PDT analysis available. If so, we need to update corresponding analyses. If they aren't available we shouldn't run the analysis here.

I think that would work if getCachedResult actually picks up an analysis that is created during the pass execution (any reason why it wouldn't do that?).

Just saying that the PassManager/AnalysisManager should know if PDT has been calculated (so having a map in FunctionSpecialization that keep track of if BlockFrequencyAnalysis has been run seems a bit off, as what we want to know is if the DominatarTree/PostDominiatorTree needs to be updated).

So a solution like this would be more future proof (if it is working) as it wouldn't need to know which other analyses that might end up calculating the PDT.

labrinea added inline comments.May 30 2023, 3:02 AM
llvm/lib/Transforms/IPO/SCCP.cpp
231–233

Good point. I think this should work.

labrinea updated this revision to Diff 526657.May 30 2023, 8:50 AM
labrinea retitled this revision from [IPSCCP] Update Post Dominator Tree if Block Frequency Analysis has run. to [IPSCCP] Request PostDominatorTreeAnalysis after BlockFrequencyAnalysis..
labrinea edited the summary of this revision. (Show Details)

I forgot to use the reproducer from D151648.

labrinea updated this revision to Diff 526735.May 30 2023, 12:07 PM
labrinea edited the summary of this revision. (Show Details)
bjope added a comment.May 31 2023, 1:14 AM

I made another round of testing to verify that this latest version of the patch (Diff 3) would solve the problems seen by uabelho (crashes/asserts). Could not see any problems.

llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
71 ↗(On Diff #526735)

Order of argument evaluation is not well-defined (might be different depending on which compiler you use when building llvm).
Generally I think we want to make sure that passes are being run in a deterministic order (e.g. to be able to use -debug-pass-manager in lit tests verifying things aound the pass manager).
So I think you want to do these GetDT() and GetAC() calls outside the argument list to make the order well-defined.

I made another round of testing to verify that this latest version of the patch (Diff 3) would solve the problems seen by uabelho (crashes/asserts). Could not see any problems.

Unfortunately the patch cannot be applied anymore since @nikic has reverted the dependent commits.

labrinea updated this revision to Diff 527037.May 31 2023, 7:40 AM
labrinea retitled this revision from [IPSCCP] Request PostDominatorTreeAnalysis after BlockFrequencyAnalysis. to Reland "[FuncSpec] Replace LoopInfo with BlockFrequencyInfo".
labrinea edited the summary of this revision. (Show Details)

rebased and merged with 1b1232047e83b

@bjope would it make it easier to review if I separated the fix from 1b1232047e83b? It will require more work that will later be discarded but if it helps I can do that.

labrinea updated this revision to Diff 527421.Jun 1 2023, 8:06 AM
labrinea retitled this revision from Reland "[FuncSpec] Replace LoopInfo with BlockFrequencyInfo" to [IPSCCP] Decouple queries for function analysis results..
labrinea edited the summary of this revision. (Show Details)

separated the fix from 1b1232047e83b

bjope accepted this revision.Jun 1 2023, 8:10 AM

LGTM!

This revision is now accepted and ready to land.Jun 1 2023, 8:10 AM
This revision was landed with ongoing or failed builds.Jun 1 2023, 8:41 AM
This revision was automatically updated to reflect the committed changes.