This is an archive of the discontinued LLVM Phabricator instance.

[CodeGenPrepare] Check that erased sunken address are not reused
ClosedPublic

Authored by sdardis on Nov 9 2017, 6:15 AM.

Details

Summary

CodeGenPrepare sinks address computations from one basic block to another
and attempts to reuse address computations that have already been sunk. If
the same address computation appears twice with the first instance as an
operand of a load whose result is an operand to a simplifable select,
CodeGenPrepare simplifies the select and recursively erases the now dead
instructions. CodeGenPrepare then attempts to use the erased address
computation for the second load.

Fix this by erasing the cached address value if it has zero uses before
looking for the address value in the sunken address map.

This partially resolves PR35209.

Thanks to Alexander Richardson for reporting the issue!

Event Timeline

sdardis created this revision.Nov 9 2017, 6:15 AM

I just tried applying this patch locally and compiling the FreeBSD kernel again. The runtime crash that goes away if I revert https://reviews.llvm.org/rL314794 is still present even with this patch.

I am not sure what is going wrong there, I will have to try and find a reduced test case.

john.brawn added inline comments.Nov 10 2017, 4:09 AM
test/Transforms/CodeGenPrepare/pr35209.ll
1 ↗(On Diff #122234)

Could you change this to use opt instead with explicit triple? Whether the transformation happens depends on the target's isLegalAddressingMode and llc defaults to the host target, so the test wouldn't always be testing the same thing depending on what machine you're running it on.

3–6 ↗(On Diff #122234)

You could do with some CHECKs in this file to check that the address sinking is actually happening.

sdardis updated this revision to Diff 122430.Nov 10 2017, 5:46 AM

Address review comments.

This revision is now accepted and ready to land.Nov 10 2017, 6:57 AM
sdardis edited the summary of this revision. (Show Details)Nov 13 2017, 3:14 AM
This revision was automatically updated to reflect the committed changes.
sdardis reopened this revision.Nov 14 2017, 1:44 AM

Reopening this as some changes were required to pacify some buildbots.

This revision is now accepted and ready to land.Nov 14 2017, 1:44 AM
sdardis updated this revision to Diff 123006.Nov 15 2017, 4:28 AM

Address the failures on the sanitizer bots by reworking SunkAddrs in a <Value*, WeakTrackingVH*> map, so that if a computation is erased, the end value is not read after deletion to see if it is still live.

john.brawn added inline comments.Nov 16 2017, 7:19 AM
lib/CodeGen/CodeGenPrepare.cpp
233

whose

4369–4370

You no longer have a reference to an element of SunkAddrs here, which means SunkAddrs never has anything added to it so we never reuse a sunk addrmode. test3 in test/Transforms/CodeGenPrepare/X86/sink-addrmode.ll was trying to test this but had a misplaced comma: I've just now fixed that and so I now see a test failure with this patch.

sdardis updated this revision to Diff 124146.Nov 24 2017, 2:57 AM

Address review comments.

This revision was automatically updated to reflect the committed changes.

Thanks for the review