From f96bc6134fbc4e9bb74d082a6c2d7c022f1860e1 Mon Sep 17 00:00:00 2001 From: SergeyZ Date: Wed, 19 Mar 2025 16:07:57 -0700 Subject: [PATCH 1/2] [VPlan] Add extra checks for LoopForBB. NFC. Adding the checks for LoopForBB not being nullptr before try to dereference it. --- llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp index 4b8a2420b3037..467c83395b812 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp @@ -379,7 +379,7 @@ void PlainCFGBuilder::buildPlainCFG( VPRegionBlock *Region = VPBB->getParent(); Loop *LoopForBB = LI->getLoopFor(BB); // Set VPBB predecessors in the same order as they are in the incoming BB. - if (!isHeaderBB(BB, LoopForBB)) { + if (LoopForBB && !isHeaderBB(BB, LoopForBB)) { setVPBBPredsFromBB(VPBB, BB); } else if (Region) { // BB is a loop header and there's a corresponding region, set the @@ -390,7 +390,7 @@ void PlainCFGBuilder::buildPlainCFG( // Create VPInstructions for BB. createVPInstructionsForVPBB(VPBB, BB); - if (BB == TheLoop->getLoopLatch()) { + if (LoopForBB && BB == TheLoop->getLoopLatch()) { VPBasicBlock *HeaderVPBB = getOrCreateVPBB(LoopForBB->getHeader()); VPBlockUtils::connectBlocks(VPBB, HeaderVPBB); continue; @@ -423,7 +423,7 @@ void PlainCFGBuilder::buildPlainCFG( BasicBlock *IRSucc1 = BI->getSuccessor(1); VPBasicBlock *Successor0 = getOrCreateVPBB(IRSucc0); VPBasicBlock *Successor1 = getOrCreateVPBB(IRSucc1); - if (BB == LoopForBB->getLoopLatch()) { + if (LoopForBB && BB == LoopForBB->getLoopLatch()) { // For a latch we need to set the successor of the region rather than that // of VPBB and it should be set to the exit, i.e., non-header successor, // except for the top region, which is handled elsewhere. From 78063b2e424b191f8d614e3c67b361fce7c5862b Mon Sep 17 00:00:00 2001 From: SergeyZ Date: Mon, 31 Mar 2025 10:36:39 -0700 Subject: [PATCH 2/2] [NFC][VPlan] Delete redundant check. --- .../Transforms/Vectorize/VPlanHCFGBuilder.cpp | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp index 467c83395b812..9dad45ce1c3ce 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp @@ -379,7 +379,7 @@ void PlainCFGBuilder::buildPlainCFG( VPRegionBlock *Region = VPBB->getParent(); Loop *LoopForBB = LI->getLoopFor(BB); // Set VPBB predecessors in the same order as they are in the incoming BB. - if (LoopForBB && !isHeaderBB(BB, LoopForBB)) { + if (!isHeaderBB(BB, LoopForBB)) { setVPBBPredsFromBB(VPBB, BB); } else if (Region) { // BB is a loop header and there's a corresponding region, set the @@ -390,7 +390,7 @@ void PlainCFGBuilder::buildPlainCFG( // Create VPInstructions for BB. createVPInstructionsForVPBB(VPBB, BB); - if (LoopForBB && BB == TheLoop->getLoopLatch()) { + if (BB == TheLoop->getLoopLatch()) { VPBasicBlock *HeaderVPBB = getOrCreateVPBB(LoopForBB->getHeader()); VPBlockUtils::connectBlocks(VPBB, HeaderVPBB); continue; @@ -423,7 +423,7 @@ void PlainCFGBuilder::buildPlainCFG( BasicBlock *IRSucc1 = BI->getSuccessor(1); VPBasicBlock *Successor0 = getOrCreateVPBB(IRSucc0); VPBasicBlock *Successor1 = getOrCreateVPBB(IRSucc1); - if (LoopForBB && BB == LoopForBB->getLoopLatch()) { + if (BB == LoopForBB->getLoopLatch()) { // For a latch we need to set the successor of the region rather than that // of VPBB and it should be set to the exit, i.e., non-header successor, // except for the top region, which is handled elsewhere. @@ -437,15 +437,13 @@ void PlainCFGBuilder::buildPlainCFG( // Don't connect any blocks outside the current loop except the latch for // now. The latch is handled above. - if (LoopForBB) { - if (!LoopForBB->contains(IRSucc0)) { - VPBB->setOneSuccessor(Successor1); - continue; - } - if (!LoopForBB->contains(IRSucc1)) { - VPBB->setOneSuccessor(Successor0); - continue; - } + if (!LoopForBB->contains(IRSucc0)) { + VPBB->setOneSuccessor(Successor1); + continue; + } + if (!LoopForBB->contains(IRSucc1)) { + VPBB->setOneSuccessor(Successor0); + continue; } VPBB->setTwoSuccessors(Successor0, Successor1);