This is an archive of the discontinued LLVM Phabricator instance.

[LoopVectorize] Uninitialized phi node leads to a crash in SSAUpdater.
ClosedPublic

Authored by skatkov on Jun 17 2022, 12:08 AM.

Details

Summary

createInductionResumeValues creates a phi node placeholder
without filling incoming values. Then it generates the incoming values.

It includes triggering of SCEV expander which may invoke SSAUpdater.
SSAUpdater has an optimization to detect number of predecessors
basing on incoming values if there is phi node.
In case phi node is not filled with incoming values - the number of predecessors
is detected as 0 and this leads to segmentation fault.

In other words SSAUpdater expects that phi is in good shape while
LoopVectorizer breaks this requirement.

The fix is just prepare all incoming values first and then build a phi node.

Diff Detail

Event Timeline

skatkov created this revision.Jun 17 2022, 12:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2022, 12:08 AM
skatkov requested review of this revision.Jun 17 2022, 12:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2022, 12:08 AM

This change looks sensible to me! I just wondered if it's possible to reduce/tidy the test a bit further?

llvm/test/Transforms/LoopVectorize/create-induction-resume.ll
90

Is it possible to simplify this test a little? It's quite difficult to follow what's going on here. It looks like the actual loop only requires bb8, bb9, bb10, bb11, bb18 and bb19.

91

nit: Instead of undef can you use a real value - perhaps %arg?

106

nit: I think where possible it's good if you can avoid using undef and use deterministic values. I imagine you can just replace undef with a constant like 1 and the crash will still occur.

114

nit: You can delete bb18 entirely and just do

  br i1 %tmp17, label %bb19, label %bb11

bb19:                                             ; preds = %bb11, %bb8
  br label %bb8

The test was obtained by bugpoint. I'll do my best to reduce it more.

fhahn accepted this revision.Jun 21 2022, 11:09 AM

LGTM with the suggested improvements by @david-arm for the tests. It would also increase the readability of the test if the blocks could be ordered so that the loop structure is a bit clearer, with more descriptive block names.

I don't think this is a complete fix (ideally the steps would also expanded before modifying the CFG at all), but it is a straight-forward improvement to fix an existing crash.

As for the title, it would be great if it would explain what issues it fixes.

llvm/test/Transforms/LoopVectorize/create-induction-resume.ll
6

Does the test depend on x86? If so, it needs to be moved to the X86 subdirectory, otherwise it may fail on targets that don't build the backend.

If -force-vector-with=x and -force-vector-interleave=x are sufficient, it would be good to remove the triple.

7

It might be good to include a reference to the bug here

This revision is now accepted and ready to land.Jun 21 2022, 11:09 AM
skatkov marked 3 inline comments as done.Jun 21 2022, 8:31 PM
skatkov added inline comments.
llvm/test/Transforms/LoopVectorize/create-induction-resume.ll
6

The test crashes without triple and any additional flags.

7

I did not file a bug.

90

It seems the first loop is required to force SSA updater.

skatkov updated this revision to Diff 438903.Jun 21 2022, 8:46 PM
skatkov retitled this revision from [LoopVectorize] Fix createInductionResumeValues to [LoopVectorize] Uninitialized phi node leads to a crash in SSAUpdater..
skatkov edited the summary of this revision. (Show Details)

Update test before landing.

skatkov edited the summary of this revision. (Show Details)Jun 21 2022, 8:46 PM
skatkov edited the summary of this revision. (Show Details)
skatkov edited the summary of this revision. (Show Details)Jun 21 2022, 8:47 PM
This revision was landed with ongoing or failed builds.Jun 21 2022, 9:19 PM
This revision was automatically updated to reflect the committed changes.
Ayal added inline comments.Jun 27 2022, 1:08 AM
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
3151

@fhahn - worth leaving behind a TODO to ensure CreateStepValue() is called "before modifying the CFG at all"?