This is an archive of the discontinued LLVM Phabricator instance.

[LSR] Fix crash in Phi node with EHPad block
ClosedPublic

Authored by lorenz on Mar 10 2021, 4:49 PM.

Details

Summary

This fixes a crash I observed in issue #48708 where the LSR
pass tries to insert an instruction in a basic block with only a
catchswitch statement in there. This happens because the Phi node
being evaluated assumes the same value for different basic blocks.

If the basic block associated with the incoming value of the operand
being evaluated has an EHPad terminator LSR skips optimizing it.
But if that incoming value can come from multiple different blocks
there can be some incoming basic blocks which are terminated in
an EHPad. If these are then rewritten in RewriteForPhi the ones
containing an EHPad terminator will hit the "Insertion point must
be a normal instruction" assert in AdjustInsertPositionForExpand.

This fix makes CollectLoopInvariantFixupsAndFormulae also ignore
cases where the same value has another incoming basic block with an
EHPad, same as it already does in case the primary value has one.

Diff Detail

Event Timeline

lorenz created this revision.Mar 10 2021, 4:49 PM
lorenz requested review of this revision.Mar 10 2021, 4:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2021, 4:49 PM

Reviewers: Please note that I'm completely unfamiliar with the LLVM codebase. This might be complete nonsense. But it does fix my issue.

lorenz edited the summary of this revision. (Show Details)Mar 13 2021, 2:30 PM
lorenz added a reviewer: qcolombet.

Hi,

Could you add a test case exposing the problem?

Ideally something that just needs to run LSR: opt -loop-reduce problematic_ir.ll -S -o -

Cheers,
-Quentin

@qcolombet Have a look at the associated bug at https://bugs.llvm.org/show_bug.cgi?id=48708, there's a minimized IR reproducer in there.

@qcolombet Have a look at the associated bug at https://bugs.llvm.org/show_bug.cgi?id=48708, there's a minimized IR reproducer in there.

Great! Could you add that to this patch?

lorenz updated this revision to Diff 333171.Mar 24 2021, 4:20 PM

Add reproducer as test case

qcolombet added inline comments.Mar 31 2021, 10:02 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
3452

Period at the end of comments.

3457

Instead of iterating through all the incoming values, can we use Use::getOperandNo to only check the relevant one?

llvm/test/Transforms/LoopStrengthReduce/X86/issue48708.ll
1

Could you add check lines to make sure we do something sensible?
No crashing doesn't mean we are correct :).

2

Could you add a comment on what this test is checking and what we should/shouldn't observe here?
In particular putting the PR id here is a good idea.

3

Could you change the name of the test case to something more descriptive?

11

Could you get rid of the implicit variables (%[0-9]+)?
You can use opt -instnamer -S for that.

@lorenz are you still working on this issue? We are seeing this issue on LLVM for Windows on Arm while compiling TensorFlow with clang-cl.

lorenz updated this revision to Diff 394419.Dec 14 2021, 5:23 PM
lorenz marked 3 inline comments as done.

Improved test case and description of what condition this fixes exactly

lorenz updated this revision to Diff 394421.Dec 14 2021, 5:24 PM

Remove leftover include

lorenz retitled this revision from [LSR] RFC: Fix crash in Phi node with EHPad block to [LSR] Fix crash in Phi node with EHPad block.Dec 14 2021, 5:25 PM
lorenz edited the summary of this revision. (Show Details)
lorenz added inline comments.
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
3457

No, this won't work because this bug is specifically about handling cases where a single incoming value shows up multiple times from different basic blocks. It is necessary to look at all basic blocks.

Another ping for @qcolombet @lebedev.ri @reames after a month

qcolombet accepted this revision.Jan 13 2022, 10:09 AM
This revision is now accepted and ready to land.Jan 13 2022, 10:09 AM

Thanks for the review! I don't have commit permission so arc land doesn't work for me. Can somebody land this?

This revision was landed with ongoing or failed builds.Jan 14 2022, 6:53 PM
This revision was automatically updated to reflect the committed changes.