This is an archive of the discontinued LLVM Phabricator instance.

[MachineOutliner] Add missed optimization remarks based off outliner cost model
ClosedPublic

Authored by paquette on Aug 23 2017, 4:01 PM.

Details

Reviewers
MatzeB
anemet
Summary

This adds missed optimization remarks which report which (possibly viable) candidates were not outlined because they would be more expensive than not outlining them. Other remarks will come in separate patches.

In the case of a code size regression, if the project was built with the outliner, it'd be nice to know if it was a change in the outliner's behaviour that caused that regression. Having remarks will make that a lot easier, since it'll let us use things like the opt-viewer + optdiff.

Diff Detail

Event Timeline

paquette created this revision.Aug 23 2017, 4:01 PM
MatzeB accepted this revision.Aug 23 2017, 4:11 PM

Looks straightforward to me. Though maybe wait for Adam to take a look as he has more experience in the area.

This revision is now accepted and ready to land.Aug 23 2017, 4:11 PM
anemet edited edge metadata.Aug 23 2017, 9:40 PM

You need a test ;)

lib/CodeGen/MachineOutliner.cpp
45

I don't think you need this.

909

This won't support hotness. Apparently we never added a ctor for MORE that would populate MBFI internally. Can you please add one?

It should take a MachineBranchProbabilityInfo in addition to MF but since that is an immutable pass you should be able to require it from here. Then something like this should do the trick in the ctor:

// First create a dominator tree.
MachineDominatorTree MDT;
MDT.getBase().recalculate(*MF);

// Generate LoopInfo from it.
MachineLoopInfo  MLI;
MLI.getBase().analyze(MDT.getBase());

// Finally calculate BFI.
MBFI.calculate(*MF, MBPI, MLI);

I am fine if you want to do this in a follow-on.

913

The SequenceID feels like debugging output. The goal here is to explain the user why an expected optimization not happening and not to help a compiler developer to debug the compiler. That belongs more to DEBUG(dbgs() <<) messages.

916

Just committed r311628 to add a ctor for NV taking unsigned long. You shouldn't need the cast here.

917

The function is already present in the remark via the MBB above.

918–922

I would write this as:

<< "Cost of outlining (" << NV("OutliningCost", OutliningCost) << ") is higher than not outlining (" << NV("NotOutliningCost", NotOutliningCost) << ")"

paquette updated this revision to Diff 113123.Aug 29 2017, 11:21 AM
paquette marked 5 inline comments as done.

Updated patch to address @anemet's comments.

  • Added a test for missed optimization remarks for the MachineOutliner.
  • Changed remark output.
  • Added some comments.
paquette updated this revision to Diff 113125.Aug 29 2017, 11:23 AM

Updated again because I forgot to remove the #include "llvm/CodeGen/MachineBlockFrequencyInfo.h". Whoops.

Nice improvements.

lib/CodeGen/MachineOutliner.cpp
836

Unused now?

909–912

As before, you don't want to output the debugloc here since that is already the debugloc on the remark itself. This is kind of apparent in your test below but it's even more apparent if you used -pass-remarks-missed=machine-outliner. You'd have something like:

remark: machine-outliner-remarks.ll:5:9: machine-outliner-remarks.ll:5:9: Did not outline ...

You want to start at R << "Did not outline " ....

I think it would be a good idea to also run the test below with -pass-remarks-missed=machine-outline and then also match the remark as it is printed on the compiler. I think the register-spilling testcase should be doing both.

921–930

Probably nicer:

for (size_t i = 1, e = CandidateClass.size(); i < e; i++) {
  R << NV(...)
  if (i != e - 1)
     R << ", ";
}
R << ")";
923

Does this work: "StartLoc" + Twine(i)

I would call this OtherStartLoc<i> for easier differentiation form the main loc.

test/CodeGen/AArch64/machine-outliner-remarks.ll
2

Just to repeat what I said above. Replase -pass-remarks with -pass-remarks-missed (since you're only interested in missed remarks on the stderr) and then check that as well.

12–19

I am not sure I actually understand diagnostics here. Shouldn't the non-outlined count (4) be the same as the number of instructions (2)?

paquette marked 3 inline comments as done.Aug 30 2017, 10:46 AM
paquette added inline comments.
lib/CodeGen/MachineOutliner.cpp
923

Twines don't seem to work because apparently there's no conversion to a StringRef? I guess that's something that we should probably add, unless I'm doing something wrong.

test/CodeGen/AArch64/machine-outliner-remarks.ll
12–19

NotOutliningCost is the sum of the lengths of each candidate related to a repeated sequence of instructions. In this case, the sequence is length 2 with 2 occurrences, so we have 2 + 2 = 4.

Similarly, OutliningCost is the sum of the number of instructions taken to call a theoretical outlined function made from some sequence, plus the length of the sequence and anything else we'd have to add to create the frame for the function. In this case, we'd insert 3 instructions for each call, 2 instructions for the sequence, plus 1 for the return. So, we get 9.

Maybe this is somewhat unclear? A name like TotalOutlinedInstructionCount/TotalNotOutlinedInstructionCount might be better?

paquette marked 3 inline comments as done.Aug 30 2017, 10:52 AM
paquette marked an inline comment as done.Aug 30 2017, 11:22 AM
paquette updated this revision to Diff 113306.Aug 30 2017, 1:40 PM

Updated diff to address Adam's comments again.

  • Improved the overall remark output
  • Cleaned up some of the remarks code
  • Added checks for the actual command-line remarks to the test
anemet accepted this revision.Aug 30 2017, 3:42 PM

LGTM.

lib/CodeGen/MachineOutliner.cpp
923

:(

This is how other places seem to be doing:

(Twine("StartLoc") + Twine(i)).str()

which is still probably better since it will only create a single std::string

test/CodeGen/AArch64/machine-outliner-remarks.ll
12–19

Makes sense, thanks for explaining.

paquette updated this revision to Diff 113337.Aug 30 2017, 4:29 PM

Got the twine thing @anemet suggested working so I threw it in.

Since I have a LGTM from him, I'm going to go ahead and commit this.

MatzeB closed this revision.Sep 26 2017, 2:35 PM

Looks like this was committed in r312194