This is an archive of the discontinued LLVM Phabricator instance.

[LoopFlatten] Forget all block and loop dispositions after flatten
ClosedPublic

Authored by StephenFan on Nov 8 2022, 8:50 AM.

Details

Summary

Method forgetLoop only forgets expression of phi or its users. SCEV
expressions except the above mentioned may still has loop dispositions
that point to the destroyed loop, which might cause a crash.

Fixes: https://github.com/llvm/llvm-project/issues/58865

Diff Detail

Event Timeline

StephenFan created this revision.Nov 8 2022, 8:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 8 2022, 8:50 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
StephenFan requested review of this revision.Nov 8 2022, 8:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 8 2022, 8:50 AM

This looks good to me, but I would like to get a second opinion from @fhahn or @nikic just to be sure on the use of these scev helpers.

This looks good to me, but I would like to get a second opinion from @fhahn or @nikic just to be sure on the use of these scev helpers.

Thanks for taking a look!

StephenFan updated this revision to Diff 474026.Nov 8 2022, 9:16 AM
StephenFan retitled this revision from [LoopFlatten] Forget SCEV value for removed incomming values of phi to [LoopFlatten] Forget SCEV block and loop dispositions for removed incomming values of phi.

Replace forgetValue as fogetBlockAndLoopDispositions.

nikic added a comment.Nov 8 2022, 9:38 AM

Are you sure we need to forget the incoming value rather than the phi? I haven't looked at the code in detail, but I'd generally expect that removing an incoming value doesn't change dispositions of that value, but may change dispositions of the using phi.

llvm/test/Transforms/LoopFlatten/pr58865.ll
2

I'd suggest adding check lines as well.

uabelho added a subscriber: uabelho.Nov 8 2022, 9:45 PM

Are you sure we need to forget the incoming value rather than the phi? I haven't looked at the code in detail, but I'd generally expect that removing an incoming value doesn't change dispositions of that value, but may change dispositions of the using phi.

If I understand correctly, the phi will be forgotten when calling forgetLoop method in the below. But I found that just forgetting the incoming value is still not sufficient or is wrong.

StephenFan retitled this revision from [LoopFlatten] Forget SCEV block and loop dispositions for removed incomming values of phi to [LoopFlatten] Forget all block and loop dispositions after flatten.
StephenFan edited the summary of this revision. (Show Details)

Replace forgetting incoming values as forget all of the block and loop dispositions.

Add checking lines.

nikic accepted this revision.Nov 9 2022, 2:25 AM

LGTM

This revision is now accepted and ready to land.Nov 9 2022, 2:25 AM
fhahn accepted this revision.Nov 9 2022, 3:04 AM

LGTM, thanks!

This is (slightly) offtopic, but I am wondering what the lesson learned here is for me. I.e., I forgot forgetBlockAndLoopDispositions, but was wondering how I could/should have known.... I will check some SCEV documentation, that's one thing, but I am also wondering if SCEV could be made more robust. It was running in an assert because it thought it was in an invalid state, so could it call forgetBlockAndLoopDispositions instead of asserting?

fhahn added a comment.Nov 9 2022, 3:20 AM

I will check some SCEV documentation, that's one thing, but I am also wondering if SCEV could be made more robust. It was running in an assert because it thought it was in an invalid state, so could it call forgetBlockAndLoopDispositions instead of asserting?

Unfortunately that's unlikely. We only catch this with verification, which basically computes most SCEV information again from scratch and compares it to the cached values, so it is super expensive.

Is this the same issue I previously reported in D109958? Did we lose that fix when that was reverted?

Is this the same issue I previously reported in D109958? Did we lose that fix when that was reverted?

Forgot to reply, but yeah, sorry, I think that was my bad. I think we accidentally lost that with a revert.