This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Use dyn_cast instead of cast in visitPHINode
AbandonedPublic

Authored by liaolucy on Aug 24 2022, 3:16 AM.

Diff Detail

Event Timeline

liaolucy created this revision.Aug 24 2022, 3:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 24 2022, 3:16 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
liaolucy requested review of this revision.Aug 24 2022, 3:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 24 2022, 3:16 AM
fhahn added a comment.Aug 24 2022, 3:19 AM

Thanks for tracking this down! Could you please add an instcombine test case for the issue?

Thanks for tracking this down! Could you please add an instcombine test case for the issue?

Any suggestions for test cases? Is it as simple as possible or the complete ir of the issue?

fhahn added a comment.Aug 24 2022, 3:47 AM

Thanks for tracking this down! Could you please add an instcombine test case for the issue?

Any suggestions for test cases? Is it as simple as possible or the complete ir of the issue?

As simple as possible to reproduce the issue by just running opt -passes=instcombine. You can use -mllvm -print-on-crash -mllvm -print-module-scope to get the module before the crash and then use the techniques from https://llvm.org/docs/HowToSubmitABug.html to reduce it to the minimal crashing IR.

My understanding is that a basic block with a PHI node should have first instruction as PHI

My understanding is that a basic block with a PHI node should have first instruction as PHI

That understanding matches the IR verifier

void Verifier::visitPHINode(PHINode &PN) {                                       
  // Ensure that the PHI nodes are all grouped together at the top of the block. 
  // This can be tested by checking whether the instruction before this is       
  // either nonexistent (because this is begin()) or is a PHI node.  If not,     
  // then there is some other instruction before a PHI.                          
  Check(&PN == &PN.getParent()->front() ||                                       
            isa<PHINode>(--BasicBlock::iterator(&PN)),                           
        "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
fhahn requested changes to this revision.Aug 25 2022, 2:03 AM
This revision now requires changes to proceed.Aug 25 2022, 2:03 AM
liaolucy added a comment.EditedAug 25 2022, 2:19 AM

Yes, PHI node should have first instruction as PHI.

Crash:

L3.preheader:                                     ; preds = %L3.preheader.preheader, %L2.loopexit
  %11 = zext i16 %10 to i32 -------------------------------------------------------------------------first
  %i.130 = phi i32 [ 0, %L2.loopexit ], [ %rem20.zext, %L3.preheader.preheader ]
  %tobool5.not = icmp eq i32 %i.130, 0
  br label %L3

No crash:

L3.preheader:                                     ; preds = %L3.preheader.preheader, %L2.loopexit
  %i.130 = phi i32 [ 0, %L2.loopexit ], [ %rem20.zext, %L3.preheader.preheader ]
  %11 = zext i16 %10 to i32 --------------------------------------------------------------------------change to second
  %tobool5.not = icmp eq i32 %i.130, 0
  br label %L3

It maybe the crash caused by this patch: https://reviews.llvm.org/D129710

liaolucy added a comment.EditedAug 25 2022, 2:24 AM

Thanks for tracking this down! Could you please add an instcombine test case for the issue?

Any suggestions for test cases? Is it as simple as possible or the complete ir of the issue?

As simple as possible to reproduce the issue by just running opt -passes=instcombine. You can use -mllvm -print-on-crash -mllvm -print-module-scope to get the module before the crash and then use the techniques from https://llvm.org/docs/HowToSubmitABug.html to reduce it to the minimal crashing IR.

Sorry I can't add test cases for the time being, because there will be a failure.

/update_test_checks.py --opt=opt pr57329.ll 
PHI nodes not grouped at top of basic block!
  %i.129 = phi i32 [ 0, %L2 ], [ %0, %L1 ]

I'm looking for a better way to fix bug.

It has been a very long time since I've worked in this code, but I think the bug is up-stream, whatever is causing the phi nodes to not be adjacent

I've verified that D132571 solves the problem.

Without D132571:

L3.preheader:                                     ; preds = %L3.preheader.preheader, %L2.loopexit
  %11 = zext i16 %10 to i32
  %i.129 = phi i32 [ 0, %L2.loopexit ], [ %rem19.zext, %L3.preheader.preheader ]
  %tobool5.not = icmp eq i32 %i.129, 0
  br label %L3

With D132571, we get:

L1.backedge.loopexit44:                           ; preds = %L2.loopexit
  %1 = zext i16 %8 to i32
  br label %L1.backedge
....
L3.preheader:                                     ; preds = %L3.preheader.preheader, %L2.loopexit
  %i.131 = phi i32 [ 0, %L2.loopexit ], [ %rem22.zext, %L3.preheader.preheader ]
  %tobool7.not = icmp eq i32 %i.131, 0
  br label %L3
liaolucy abandoned this revision.Aug 30 2022, 7:27 PM