This is an archive of the discontinued LLVM Phabricator instance.

[CGP] Fix the crash for combining address mode when having cyclic dependency
ClosedPublic

Authored by wwei on Aug 24 2021, 8:32 AM.

Details

Summary

In the combination of addressing modes, when replacing the matched phi nodes, sometimes the phi node to be replaced has been modified.
For example, there’s Matcher set [A, B] and [C, A], which will have cyclic dependency: A is replaced by B and C will be replaced by A . So we need to update the second phi node in matcher set when the phi node is already replaced.

Diff Detail

Event Timeline

wwei created this revision.Aug 24 2021, 8:32 AM
wwei requested review of this revision.Aug 24 2021, 8:32 AM

It seems that correct fix should be something different.
The underlying idea of implementation is as follows:
We built phi nodes to get, for example, base address in a point of usage. (in you example it is an offset).
Now we try to find whether there are already existing phi nodes which provide us with the base.
So we try to match all new phi nodes to already existed.
The bug you found actually happened due to we tried to match new phi node to another new phi node and at the end it results in the bug.

So I guess the better fix would be to just ignore new phi nodes when we try to map new phi node to old one.

llvm/lib/CodeGen/CodeGenPrepare.cpp
3721–3722

I guess the change of this comparison to if (PhiNodesToMatch.count(&P)) should fix your problem.

wwei updated this revision to Diff 368635.Aug 25 2021, 7:33 AM
wwei added a comment.Aug 25 2021, 7:36 AM

It seems that correct fix should be something different.
The underlying idea of implementation is as follows:
We built phi nodes to get, for example, base address in a point of usage. (in you example it is an offset).
Now we try to find whether there are already existing phi nodes which provide us with the base.
So we try to match all new phi nodes to already existed.
The bug you found actually happened due to we tried to match new phi node to another new phi node and at the end it results in the bug.

So I guess the better fix would be to just ignore new phi nodes when we try to map new phi node to old one.

@skatkov Thank you for the explanation,I have updated the patch.

skatkov accepted this revision.Aug 25 2021, 8:04 PM
skatkov added inline comments.
llvm/lib/CodeGen/CodeGenPrepare.cpp
3721–3722

Please add a comment, like:
Skip new phi nodes.

This revision is now accepted and ready to land.Aug 25 2021, 8:04 PM