This is an archive of the discontinued LLVM Phabricator instance.

[llvm][NFC] Inliner: factor cost and reporting out of inlining process
ClosedPublic

Authored by mtrofin on May 1 2020, 4:04 PM.

Details

Summary

This factors cost and reporting out of the inlining workflow, thus
making it easier to reuse when driving inlining from the upcoming
InliningAdvisor.

Depends on: D79215

Diff Detail

Event Timeline

mtrofin created this revision.May 1 2020, 4:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2020, 4:04 PM
dblaikie added inline comments.
llvm/lib/Transforms/IPO/Inliner.cpp
514

If you don't need to mutate it, you can pass DebugLoc by value (quick grep across llvm/include shows that to be quite common) - it's small/cheap to copy.

& changes like this can just be committed for post-commit review

(ah, I see what motivated you to fix/change this - your new/changed callsite, that passes a const& DebugLoc... *goes over there to comment*)

1058

Can /probably skip the "Callee" in these enumerators, since because they're in an enum class, they'll always be qualified by "CalleeStatus" (so "CalleeStatus::Deleted" and "CalleeStatus::Kept" is probably readable, I think? (could even rename the enum to just "Callee" if that helps, since it's scoped inside InliningOutcome, so it's got some context helping it along from there too))

1060

Looks like at least in this change, only the "Result" is used? Probably defer adding the CalleeStatus until you're introducing a use of it, to help justify it, ensure it's tested/exercised, etc?

1062

Would it be practical for this to be a standalone function, or does it use too much state from the outer function? As it stands, this, InlinerPass::run is already quite long - pulling any chunks with clearly describable goals out into separate functions (entirely separate, rather than nested like this) might make it easier to read the high level algorithm at a glance, rather than spread over many pages?

1143–1169

In a follow-up patch (no need for review) this could be refactored to reduce indentation by using early-returns ( https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code ). eg:

if (!Callee.hasLocalLinkage)
  return {InliningOutcome::CalleeStatus::Kept, IR};

Callee.removeDeadConstantUsers();

if (!Callee.use_empty() || CG.isLibFunction(Callee))
  return {InliningOutcome::CalleeStatus::Kept, IR};

Calls.erase(...);
...
return {InliningOutcome::CalleeStatus::Deleted, IR};
1174

Best to just copy (rather than reference) here - same as if you had a function that returned "int" by value.

so there will be a follow up refactoring to move the DoInline lamda into a standalone helper?

mtrofin marked 6 inline comments as done.May 3 2020, 9:44 AM
mtrofin added inline comments.
llvm/lib/Transforms/IPO/Inliner.cpp
1058

Ack - will incorporate when we need it - removed the type for now. Left comment as "undone" for tracking.

1062

Yes, that's my intention. This first patch just keeps the changes to a minimum. Next, I'll peel out the stuff that's bound to the context, but can be executed after the function (which turns out to be all of it, I verified). Then we can hoist the code out in its own function.

1143–1169

Ack, let's do that once this is factored out as a function. Leaving this comment as "undone", for easy tracking.

so there will be a follow up refactoring to move the DoInline lamda into a standalone helper?

That's my intention, yes.

mtrofin updated this revision to Diff 261699.May 3 2020, 9:47 AM

feedback

dblaikie accepted this revision.May 3 2020, 10:21 AM

Looks good to me (I'd probably have pulled it out into a separate function in one go (as it stands the code feels awkwardly complicated - creating a lambda for just one caller - I generally try to make the code self-consistent/justified with each commit, insofar as that's possible) but given you're headed that way, whichever way you find best to get there seems OK)

This revision is now accepted and ready to land.May 3 2020, 10:21 AM
This revision was automatically updated to reflect the committed changes.