Skip to content

[VPlan] Fix header phi VPInstruction verification. NFC #151472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

lukel97
Copy link
Contributor

@lukel97 lukel97 commented Jul 31, 2025

Noticed this when checking the invariant that all phis in the header block must be header phis. I think there's a missing set of parentheses here, since otherwise it only cast when RecipeI isn't a VPInstruction.

Noticed this when checking the invariant that all phis in the header block must be header phis. I think there's a missing set of parentheses here, since otherwise it only cast<VPInstruction> when RecipeI isn't a VPInstruction.
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-vectorizers

Author: Luke Lau (lukel97)

Changes

Noticed this when checking the invariant that all phis in the header block must be header phis. I think there's a missing set of parentheses here, since otherwise it only cast<VPInstruction> when RecipeI isn't a VPInstruction.


Full diff: https://github.com/llvm/llvm-project/pull/151472.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp (+2-2)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp b/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
index 57d01cbefbe26..b0de97ac5618d 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
@@ -80,8 +80,8 @@ bool VPlanVerifier::verifyPhiRecipes(const VPBasicBlock *VPBB) {
       NumActiveLaneMaskPhiRecipes++;
 
     if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe>(*RecipeI) &&
-        !isa<VPInstruction>(*RecipeI) &&
-        cast<VPInstruction>(RecipeI)->getOpcode() == Instruction::PHI) {
+        !(isa<VPInstruction>(*RecipeI) &&
+          cast<VPInstruction>(RecipeI)->getOpcode() == Instruction::PHI)) {
       errs() << "Found non-header PHI recipe in header VPBB";
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
       errs() << ": ";

Copy link
Contributor

@Mel-Chen Mel-Chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'd like to know if there's a way to find a test case to verify this?

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test with a non-header-phi in header block to llvm/unittests/Transforms/Vectorize/VPlanVerifierTest.cpp?

I guess at the moment this would just be VPPredInstPHI?

@@ -80,8 +80,8 @@ bool VPlanVerifier::verifyPhiRecipes(const VPBasicBlock *VPBB) {
NumActiveLaneMaskPhiRecipes++;

if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe>(*RecipeI) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe>(*RecipeI) &&
if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe, VPPhi>(*RecipeI) &&

This can just use VPPhi I think.

@@ -80,8 +80,8 @@ bool VPlanVerifier::verifyPhiRecipes(const VPBasicBlock *VPBB) {
NumActiveLaneMaskPhiRecipes++;

if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe>(*RecipeI) &&
!isa<VPInstruction>(*RecipeI) &&
cast<VPInstruction>(RecipeI)->getOpcode() == Instruction::PHI) {
!(isa<VPInstruction>(*RecipeI) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the definition of isPhi it seems like we're missing tests where we have a VPPredInstPHIRecipe in the header VPBB, since that seems to be the only one (besides VPInstructions) where isPhi would return true and isa<VPHeaderPHIRecipe, VPWidenPHIRecipe>(*RecipeI) would return false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But looking at VPPredInstPHIRecipe it seems unlikely to be in the header, right? So perhaps there is no way to write a test case that proves this was broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a VPIRPhi, i.e a VPIRInstruction with a PHI opcode should also be caught by this, I've added a test case for it in c170996

@lukel97
Copy link
Contributor Author

lukel97 commented Jul 31, 2025

Can you add a test with a non-header-phi in header block to llvm/unittests/Transforms/Vectorize/VPlanVerifierTest.cpp?

I guess at the moment this would just be VPPredInstPHI?

I added a test case in c170996 before I saw your comment, I think VPIRPhis are considered a non-header-phi too.

Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test! I will let one of the other reviewers sign off on this.

new VPInstruction(VPInstruction::BranchOnCond, {CanIV});

VPBasicBlock *VPBB1 = Plan.getEntry();
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("entry");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VPBB2 is the vector header block, so I named it "header"

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@lukel97 lukel97 merged commit 08c5944 into llvm:main Jul 31, 2025
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 31, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-win-fast running on as-builder-3 while building llvm at step 7 "test-build-unified-tree-check-llvm-unit".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/2/builds/30229

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-llvm-unit) failure: test (failure)
******************** TEST 'LLVM-Unit :: Transforms/Vectorize/./VectorizeTests.exe/50/52' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\build\unittests\Transforms\Vectorize\.\VectorizeTests.exe-LLVM-Unit-6452-50-52.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=52 GTEST_SHARD_INDEX=50 C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\build\unittests\Transforms\Vectorize\.\VectorizeTests.exe
--

Script:
--
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\build\unittests\Transforms\Vectorize\.\VectorizeTests.exe --gtest_filter=VPVerifierTest.NonHeaderPHIInHeader
--
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\unittests\Transforms\Vectorize\VPlanVerifierTest.cpp(317): error: Expected equality of these values:
  "Found non-header PHI recipe in header VPBB: IR   <badref> = phi i32 \n"
  ::testing::internal::GetCapturedStderr().c_str()
    Which is: "Found non-header PHI recipe in header VPBB"


C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\unittests\Transforms\Vectorize\VPlanVerifierTest.cpp:317
Expected equality of these values:
  "Found non-header PHI recipe in header VPBB: IR   <badref> = phi i32 \n"
  ::testing::internal::GetCapturedStderr().c_str()
    Which is: "Found non-header PHI recipe in header VPBB"



********************


@lukel97
Copy link
Contributor Author

lukel97 commented Jul 31, 2025

Looks like I need to change the expected output in the test whenever dumping isn't defined, working on a fix now.

@artagnon
Copy link
Contributor

Maybe just do a prefix-matching?

@lukel97
Copy link
Contributor Author

lukel97 commented Jul 31, 2025

Maybe just do a prefix-matching?

I wish I could, but doesn't look like there's a GTEST macro for prefixes unfortunately. I've just copied what the other tests have done and pushed a fix in 3e579d9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants