This is an archive of the discontinued LLVM Phabricator instance.

[DAG] Fix in ReplaceAllUsesOfValuesWith
ClosedPublic

Authored by bjope on Feb 6 2022, 11:04 AM.

Details

Summary

When doing SelectionDAG::ReplaceAllUsesOfValuesWith a worklist is
prepared containing all users that should be updated. Then we use
the RemoveNodeFromCSEMaps/AddModifiedNodeToCSEMaps helpers to handle
recursive CSE updates while doing the replacements.

This patch aims at solving a problem that could arise if the recursive
CSE updates would result in an SDNode present in the worklist is being
removed as a side-effect of morphing a prio user in the worklist.

To examplify such a scenario, imagine that we have these nodes in
the DAG

t12: i64 = add t8, t11
t13: i64 = add t12, t8
t14: i64 = add t11, t11
t15: i64 = add t14, t8
t16: i64 = sub t13, t15

and that the t8 uses should be replaced by t11. An initial worklist
(listing the users that should be morphed) could be [t12, t13, t15].
When updating t12 we get

t12: i64 = add t11, t11

which results in a CSE update that replaces t14 by t12, so we get

t15: i64 = add t12, t8

which results in a CSE update that replaces t13 by t12, so we get

t16: i64 = sub t12, t15

and then t13 is removed given that it was the last use of t13.

So when being done with the updates triggered by rewriting the use
of t8 in t12 the t13 node no longer exist. And we used to end up
hitting an assertion when continuing with the worklist aiming at
replacing the t8 uses in t13.

The solution is based on using a DAGUpdateListener, making sure that
we prune a user from the worklist if it is removed during the
recursive CSE updates.

The bug was found using an OOT target. I think the problem is quite
old, even if the particular intree target reproducer added in this
patch seem to pass when using LLVM 13.0.0.

Diff Detail

Event Timeline

bjope created this revision.Feb 6 2022, 11:04 AM
bjope requested review of this revision.Feb 6 2022, 11:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 6 2022, 11:04 AM
RKSimon added inline comments.Feb 6 2022, 11:45 AM
llvm/test/CodeGen/AArch64/bbi-63928-ReplaceAllUsesOfValuesWith.ll
2 ↗(On Diff #406267)

What's with the naming for bbi-63928-ReplaceAllUsesOfValuesWith.ll ?

bjope added inline comments.Feb 6 2022, 11:50 AM
llvm/test/CodeGen/AArch64/bbi-63928-ReplaceAllUsesOfValuesWith.ll
2 ↗(On Diff #406267)

Oh, I should have adjusted that. The bbi-63928 happens to be our downstream TR reference. I can rename it to something more generic such as dag-ReplaceAllUsesOfValuesWith.ll.

lattner resigned from this revision.Feb 6 2022, 3:10 PM
bjope updated this revision to Diff 406410.Feb 7 2022, 5:31 AM

Rename the new test case.

ping!

(let me know if I should try to find alternative reviewers)

bjope updated this revision to Diff 409398.Feb 16 2022, 1:43 PM

Fixed clang-format failures.

niravd accepted this revision.Feb 16 2022, 5:02 PM

Since there doesn't seem to be any other suggestions, I see no reason why we should hold off on this landing.

LGTM.

This revision is now accepted and ready to land.Feb 16 2022, 5:02 PM
This revision was landed with ongoing or failed builds.Feb 17 2022, 5:31 AM
This revision was automatically updated to reflect the committed changes.