This is an archive of the discontinued LLVM Phabricator instance.

[LoopInfo] Fix a bug in the function getInductionVariable
ClosedPublic

Authored by duan.db on Nov 1 2021, 7:46 PM.

Details

Summary

There is a bug in getInductionVariable. From code, the way this function gets the induction variable is by judging whether StepInst or IndVar in the phi statement is one of the operands of CMP. It will perform dyn_cast operations. But if the LatchCmpOp0/LatchCmpOp1 is a constant, it will be cast to null. The same situation exists when getting the Stepinst, the subsequent comparison may result in null == null, which is meaningless. The induction variable will be the first phi of the loop, and this does not match the logic of this function.

Diff Detail

Event Timeline

duan.db created this revision.Nov 1 2021, 7:46 PM
duan.db requested review of this revision.Nov 1 2021, 7:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 1 2021, 7:46 PM

Thanks for fixing this. Can you please provide a test case?

duan.db updated this revision to Diff 384707.Nov 4 2021, 4:57 AM

Hi, This is the test case, any other suggestions?

Whitney accepted this revision.Nov 4 2021, 6:11 AM
This revision is now accepted and ready to land.Nov 4 2021, 6:11 AM
fhahn added a comment.Nov 4 2021, 6:20 AM

The induction variable will be the first phi of the loop, and this does not match the logic of this function.

Is the code relying on the IV being the first phi in the loop? That is not a guarantee that can be relied on I think.

llvm/lib/Analysis/LoopInfo.cpp
312

nit: no need for this->?

llvm/unittests/Analysis/LoopInfoTest.cpp
1551

Can you clean up the IR? There are multiple things that should not be needed, like

"; Function Attrs:

dso_local sigext

the metadata and more.

duan.db updated this revision to Diff 385494.Nov 8 2021, 7:34 AM