This is an archive of the discontinued LLVM Phabricator instance.

FunctionImport: add a progressive heuristic to limit importing too deep in the callgraph
ClosedPublic

Authored by mehdi_amini on Feb 10 2016, 10:24 AM.

Details

Summary

The current function importer will walk the callgraph, importing
transitively any callee that is below the threshold. This can
lead to import very deep which is costly in compile time and not
necessarily beneficial as most of the inline would happen in
imported function and not necessarilly in user code.

The actual factor has been carefully chosen by flipping a coin ;)
Some tuning need to be done (just at the existing limiting threshold).

Diff Detail

Event Timeline

mehdi_amini retitled this revision from to FunctionImport: add a progressive heuristic to limit importing too deep in the callgraph.
mehdi_amini updated this object.
mehdi_amini added a reviewer: tejohnson.
mehdi_amini added a subscriber: llvm-commits.
tejohnson edited edge metadata.Feb 10 2016, 11:01 AM

This is a reasonable heuristic until we have more sophisticated cost/benefit analysis, especially with PGO data. However, there is an issue due to the depth first traversal order, see below.

lib/Transforms/IPO/FunctionImport.cpp
291

I think we can get missed importing opportunities due to the depth-first nature of the current traversal. We push_back newly identified external calls (which have the reduced threshold), then pop_back_val when traversing the Worklist above, resulting in a depth-first traversal of the call graph. What this means is that if we first encounter a call to a given external function foo() deep in a call chain, it will get added with a very low threshold. It may be that another external call to foo() is encountered higher up the call chain in something added to the worklist earlier and popped later, but since we skip any functions that were already seen (in the CalledFunctions set), we will miss the opportunity to add it with the higher threshold and possibly lose out on the import.

To fix this the tracking of what is or was already in the worklist needs to be more sophisticated. Perhaps a simple fix would be to change the CalledFunctions set to a map from function name to the max threshold it was added to the worklist with. Then if we encounter another call to the same function, if it currently has a higher threshold than recorded in the CalledFunctions map, add it to the worklist again and updated the map entry. Then each function popped off the worklist should be compared against the existing ModuleToFunctionsToImportMap entry to see if we have already decided to import it since it may end up in the worklist multiple times.

mehdi_amini added inline comments.Feb 10 2016, 11:53 AM
lib/Transforms/IPO/FunctionImport.cpp
291

That's a very good point, improving the CalledFunctions data structure seems a good solution to me.

mehdi_amini edited edge metadata.

Fix the issue reported by Teresa: modified the test to expose the DFS issue
(test as it is now is not passing on the previous revision)

tejohnson accepted this revision.Feb 10 2016, 3:08 PM
tejohnson edited edge metadata.

Couple nits on comments, otherwise LGTM.

lib/Transforms/IPO/FunctionImport.cpp
309

"as long as" doesn't sound right. Did you mean "as well as"?

test/Transforms/FunctionImport/Inputs/adjustable_threshold.ll
21

Size must start out higher than 5 since it looks like the evolution factor had to be reduced to 0.5 to not get this imported.

This revision is now accepted and ready to land.Feb 10 2016, 3:08 PM
mehdi_amini added inline comments.Feb 10 2016, 3:11 PM
lib/Transforms/IPO/FunctionImport.cpp
309

Yes!

test/Transforms/FunctionImport/Inputs/adjustable_threshold.ll
21

Not sure I understand what you mean? I want it to be imported with the default 0.7 factor, but not with the 0.5 one.

tejohnson added inline comments.Feb 10 2016, 3:15 PM
test/Transforms/FunctionImport/Inputs/adjustable_threshold.ll
21

Nevermind, I see what is going on now.

mehdi_amini edited edge metadata.

Update before committing

mehdi_amini closed this revision.Feb 10 2016, 3:36 PM

Thanks Teresa!

r260466