This is an archive of the discontinued LLVM Phabricator instance.

[BranchFolding] Restrict tail merging loop blocks after machine block placement
ClosedPublic

Authored by haicheng on Aug 4 2016, 8:47 PM.

Details

Summary

To fix PR28014, this patch restricts tail merging to blocks that belong to the same loop after machine block placement (MBP). Otherwise, merging a loop block with a block not belonging to the same loop can change the loop header (and other loop properties) of the current loop, inner loop or outer loop. Instead of updating all the loop info (which can be difficult), i choose to prevent merging blocks in these cases because the layout of the loop is weird even if I updated the loop information.

MBP creates optimized layout which brings new opportunities for tail merging. If tail merging could significantly change the layout such as changing the loop header, then the reason to do tail merging after MBP is gone. Unprofitable tail merging could reduce the fallthrough opportunities and make the layout work harder when we call MBP again.

I checked SPEC2006 and this change makes very small impact.

Diff Detail

Repository
rL LLVM

Event Timeline

haicheng updated this revision to Diff 66900.Aug 4 2016, 8:47 PM
haicheng retitled this revision from to [BranchFolding] Restrict tail merging loop blocks after machine block placement.
haicheng updated this object.
haicheng set the repository for this revision to rL LLVM.
haicheng added subscribers: hans, hfinkel, sebpop and 2 others.

This is for a bug that's blocking 3.9, so would be great if someone could review it.

+iteratee, I heard you know about tail merging?

iteratee edited edge metadata.Aug 10 2016, 5:29 PM

Mostly I'm fine with this Tidy up the comments and the test.

lib/CodeGen/BranchFolding.cpp
1001 ↗(On Diff #66900)

This is tricky to parse. It would be clearer as:
Bail if merging after placement and IBB is the loop header.

Whether IBB is the loop header or not shouldn't be changed by placement.

1006 ↗(On Diff #66900)

nit: flexible.

1010 ↗(On Diff #66900)

reasone

test/CodeGen/X86/tail-merge-after-mbp.ll
13 ↗(On Diff #66900)

I'm not a fan of undef in tests. They have a tendency to be brittle when the optimizer decides to pick a value for you as it is allowed to do. Please take arguments or call a function for the values.

haicheng updated this revision to Diff 67757.Aug 11 2016, 3:20 PM
haicheng edited edge metadata.
haicheng marked 4 inline comments as done.

Fix the comments and test case.

Thank you, Kyle.

iteratee accepted this revision.Aug 11 2016, 3:24 PM
iteratee edited edge metadata.
This revision is now accepted and ready to land.Aug 11 2016, 3:24 PM
This revision was automatically updated to reflect the committed changes.
wmi added a subscriber: wmi.Apr 5 2017, 11:08 AM

I am running into a problem about the testcase tail-merge-after-mbp.ll. I am working on a patch related with register allocation and it somehow triggers the tail merge before block placement, and breaks the checks in the test. The tail merge triggered is doing in exactly the same way as the test describes.

It seems the patch aims to work on tail merge after bb placement, and it is ok to do the tail merge if only it happens before bb placement. However, the test will fail even if the tail merge happens before bb placement.

I guess we need to add -disable-branch-fold in the test to disable the first pass of tail merge, and only check the result of tail merge called by bb placement?

In D23191#719339, @wmi wrote:

I am running into a problem about the testcase tail-merge-after-mbp.ll. I am working on a patch related with register allocation and it somehow triggers the tail merge before block placement, and breaks the checks in the test. The tail merge triggered is doing in exactly the same way as the test describes.

It seems the patch aims to work on tail merge after bb placement, and it is ok to do the tail merge if only it happens before bb placement. However, the test will fail even if the tail merge happens before bb placement.

I guess we need to add -disable-branch-fold in the test to disable the first pass of tail merge, and only check the result of tail merge called by bb placement?

You are right that this test case aims to test tail merging after bb placement. If flag -disable-branch-fold can solve your problem, feel free to add it.

gberry added a subscriber: gberry.Apr 6 2017, 8:17 AM
In D23191#719339, @wmi wrote:

I am running into a problem about the testcase tail-merge-after-mbp.ll. I am working on a patch related with register allocation and it somehow triggers the tail merge before block placement, and breaks the checks in the test. The tail merge triggered is doing in exactly the same way as the test describes.

It seems the patch aims to work on tail merge after bb placement, and it is ok to do the tail merge if only it happens before bb placement. However, the test will fail even if the tail merge happens before bb placement.

I guess we need to add -disable-branch-fold in the test to disable the first pass of tail merge, and only check the result of tail merge called by bb placement?

You are right that this test case aims to test tail merging after bb placement. If flag -disable-branch-fold can solve your problem, feel free to add it.

You could also try rewriting the test to be a MIR test that just runs the pass you are trying to test.