This is an archive of the discontinued LLVM Phabricator instance.

Create a new interface addSuccessorWithoutWeight(MBB*) in MBB to add successors when optimization is disabled.
ClosedPublic

Authored by congh on Oct 21 2015, 4:27 PM.

Details

Summary

When optimization is disabled, edge weights that are stored in MBB won't be used so that we don't have to store them. Currently, this is done by adding successors with default weight 0, and if all successors have default weights, the weight list will be empty. But that the weight list is empty doesn't mean disabled optimization (as is stated several times in MachineBasicBlock.cpp): it may also mean all successors just have default weights.

We should discourage using default weights when adding successors, because it is very easy for users to forget update the correct edge weights instead of using default ones (one exception is that the MBB only has one successor). In order to detect such usages, it is better to differentiate using default weights from the case when optimizations is disabled.

In this patch, a new interface addSuccessorWithoutWeight(MBB*) is created for when optimization is disabled. In this case, MBB will try to maintain an empty weight list, but it cannot guarantee this as for many uses of addSuccessor() whether optimization is disabled or not is not checked. But it can guarantee that if optimization is enabled, then the weight list always has the same size of the successor list.

Diff Detail

Repository
rL LLVM

Event Timeline

congh updated this revision to Diff 38059.Oct 21 2015, 4:27 PM
congh retitled this revision from to Create a new interface addSuccessorWithoutWeight(MBB*) in MBB to add successors when optimization is disabled..
congh updated this object.
congh added reviewers: dexonsmith, davidxl.
congh added a subscriber: llvm-commits.
congh added a comment.Oct 21 2015, 4:56 PM

Note that I was also proposing the patch that replaces all edge weights in MBB with branch probabilities (http://reviews.llvm.org/D13745 http://reviews.llvm.org/D13908). This patch serves as an intermediate state during this transition, and if it's checked in I will also change it into the branch probability version.

davidxl edited edge metadata.Oct 22 2015, 12:09 PM

This looks reasonable, but I think it is better to be folded into branch probability interface patch.

This looks reasonable, but I think it is better to be folded into branch probability interface patch.

You mean merge this patch with that one, and add the BP version of this new interface?

Merge it into http://reviews.llvm.org/D13908, but make the interface BP
based instead of weight based.

davidxl added inline comments.Oct 23 2015, 2:14 PM
lib/CodeGen/MachineBasicBlock.cpp
510 ↗(On Diff #38059)

diabled -- disabled.

511 ↗(On Diff #38059)

I find it more readable to say

if (! (Weights.empty() && !Successors.empty())) {

...

}

Since this interface is called only when optimization enabled, so
(Weights.empty () && Successors.empty()) == false should be asserted -- basically the above if condition is always true?

congh marked an inline comment as done.Oct 23 2015, 3:46 PM
congh added inline comments.
lib/CodeGen/MachineBasicBlock.cpp
511 ↗(On Diff #38059)

I find it more readable to say

if (! (Weights.empty() && !Successors.empty())) {

...
}

Yes.

Since this interface is called only when optimization enabled, so
(Weights.empty () && Successors.empty()) == false should be asserted -- basically the above if condition is always true?

This is not true. When optimization is disabled, this interface can also be called, even the added weights are never used. That is to say, we cannot not guarantee empty weight list when optimization is disabled.

davidxl added inline comments.Oct 23 2015, 4:08 PM
lib/CodeGen/MachineBasicBlock.cpp
511 ↗(On Diff #38059)

Basically if the addSuccessor(..) interface is used for the first successor (when opt is disabled), then the weight list will be created. Does that explain the test case result difference?

Also what happens if addSuccessor is first called, and then followed by addSuccessorWithoutWeight -- then the assert (Weights.empty()) will be triggered.

congh added inline comments.Oct 23 2015, 4:14 PM
lib/CodeGen/MachineBasicBlock.cpp
511 ↗(On Diff #38059)

Basically if the addSuccessor(..) interface is used for the first successor (when opt is disabled), then the weight list will be created. Does that explain the test case result difference?

The test case is run with optimization enabled, so this cannot explain the difference. The different comes from the fact if a MBB has all successors with default weight, then we have an empty weight list before this patch but with this patch we will get a weight list with default weights (0 here).

Also what happens if addSuccessor is first called, and then followed by addSuccessorWithoutWeight -- then the assert (Weights.empty()) will be triggered.

Yes, that assertion should be triggered. But it is never triggered on all test cases.

congh updated this revision to Diff 38280.Oct 23 2015, 5:13 PM
congh edited edge metadata.

Update the patch by replacing an assertion in addSuccessorWithoutWeight() with a statement that clears all weights.

congh updated this revision to Diff 38282.Oct 23 2015, 5:16 PM

Update the patch according to David's comment.

This looks ok as an interim clean up step for BP based interfaces.

Thanks for the review, checking in...

This revision was automatically updated to reflect the committed changes.