This is an archive of the discontinued LLVM Phabricator instance.

[DebugInfo][LSR] Add more stringent checks on IV selection and cached dbg.value location-op SCEVs
ClosedPublic

Authored by chrisjackson on Oct 14 2021, 8:14 AM.

Details

Summary

Reported in Bug PR52161, there were two issues.

  1. That IVs with SCEVs containing 'undef' could be selected. This would lead to fruitless salvage attempts.
  2. Sometimes dbg.value arguments were cached along with a SCEVUnknown type for the value. Effectively this SCEV is a wrapped llvm::value, so no scev-based salvaging can usefully be performed in this case.

While I was in the area I tidied up the IV selection logic a little, extracting a lambda that can be applied to the two sets of IV potentials, each of which has different types (a PHI node or a handle to a PHI).

As ScalarEvolution already contained a static method to traverse SCEVs and find undefs, I changed the visibility so that containsUndefs(SCEV*) was now a public method of ScalarEvolution.

Diff Detail

Event Timeline

chrisjackson created this revision.Oct 14 2021, 8:14 AM
chrisjackson requested review of this revision.Oct 14 2021, 8:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2021, 8:14 AM
chrisjackson edited the summary of this revision. (Show Details)Oct 14 2021, 8:22 AM
chrisjackson edited the summary of this revision. (Show Details)Oct 14 2021, 8:24 AM

Applied clang-format.

chrisjackson edited the summary of this revision. (Show Details)Oct 18 2021, 3:28 AM
StephenTozer added inline comments.Oct 20 2021, 8:38 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6221–6226

Should we check for an undef SCEV here as well, or is the SCEV expression guaranteed to not be undef at this point?

Hi @chrisjackson, I added a couple of nits inline. Is there anyone you can add as reviewer that are more familiar with LSR/SCEV?

llvm/include/llvm/Analysis/ScalarEvolution.h
537

nit: in the comment ; -> .

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6239–6240

Could you fix this comment while you're here? (Also ensure that the DVI is not deleted before ...)

6302–6303

Is this change necessary before IV might be UndefValue? If that's the case, it may be clearer to do this:

if (isa<UndefValue>(&*IV))
    continue;
PHINode *P = cast<PHINode>(&*IV);

Or am I misunderstanding?

llvm/test/Transforms/LoopStrengthReduce/pr52161.ll
4 ↗(On Diff #379951)

nit: missing full stop.

6–7 ↗(On Diff #379951)

I wonder if the -debug output is necessary? As far as the IR output is concerned, we just want to see an undef dbg.value is generated but don't need to know how we arrived there. What do you think?

Updated after reviewer comments.

  • Remove double check for SCEVUknown
  • Remove check of opt debug output in lit test
chrisjackson marked 3 inline comments as done.Oct 29 2021, 6:51 AM
chrisjackson added inline comments.
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6302–6303

Not exactly. The IV's SCEV may contain an undef. I don't think that is the same as the IV value being undef,

chrisjackson added inline comments.Oct 29 2021, 6:54 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6221–6226

This additional check was unnecessary, as DbgGatherSalvagableDVI() will prevent SCEVUknown or SCEVs containing an undef from being cached.

chrisjackson marked an inline comment as done.Oct 29 2021, 6:55 AM
chrisjackson added inline comments.
llvm/test/Transforms/LoopStrengthReduce/pr52161.ll
6–7 ↗(On Diff #379951)

Agreed, I've removed the need for -debug

Orlando added inline comments.Oct 29 2021, 6:57 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6302–6303

Ah ok, then - sorry if this is a silly question - why have you weakened the assert assert(isa<PHINode>(&*IV) && "Expected PhI node."); to a dyn_cast-check?

Reinstated the assert in GetInductionVariable().

chrisjackson marked an inline comment as done.Oct 29 2021, 7:32 AM
chrisjackson added inline comments.
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6302–6303

This was an oversight, Thanks for the correction.

Orlando added inline comments.Oct 29 2021, 7:39 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6302–6303

I think you could simplify this (sorry for the continued nits), from:

assert(isa<PHINode>(&*IV) && "Expected PhI node.");
PHINode *P = dyn_cast<PHINode>(&*IV);
if (!P)
  continue;

to

PHINode *P = cast<PHINode>(&*IV);
chrisjackson marked an inline comment as done.

Amended assert and dyn_cast to cast.

Orlando accepted this revision.Nov 5 2021, 9:38 AM

LGTM

This revision is now accepted and ready to land.Nov 5 2021, 9:38 AM

The lit test that was reviewed here was added with commit c63adfb8.