Skip to content

[NFC] Use cast instead of dyn_cast for Src and Dst vec types in VecCombine folding #134432

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
merged 3 commits into from
Apr 10, 2025

Conversation

calebwat
Copy link
Contributor

@calebwat calebwat commented Apr 4, 2025

SrcVecTy and DstVecTy are used without a null check, and originate from a dyn_cast. This patch adjusts this to use a fixed cast, since it is not checked for null before use otherwise, but is semantically guaranteed from previous checks.

SrcVecTy and DstVecTy are used without a null check, and originate from a dyn_cast. This patch adds an assertion that these values are non-null before they are used/dereferenced.
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-vectorizers

Author: None (calebwat)

Changes

SrcVecTy and DstVecTy are used without a null check, and originate from a dyn_cast. This patch adds an assertion that these values are non-null before they are used/dereferenced.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VectorCombine.cpp (+3)
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 4bfe41a5ed00d..fc04266cac51b 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -2052,6 +2052,9 @@ bool VectorCombine::foldShuffleOfSelects(Instruction &I) {
 
   auto *SrcVecTy = dyn_cast<FixedVectorType>(T1->getType());
   auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType());
+  assert(SrcVecTy && DstVecTy &&
+         "Expected shuffle/select vector types to be defined");
+
   auto SK = TargetTransformInfo::SK_PermuteTwoSrc;
   auto SelOp = Instruction::Select;
   InstructionCost OldCost = TTI.getCmpSelInstrCost(

@@ -2052,6 +2052,9 @@ bool VectorCombine::foldShuffleOfSelects(Instruction &I) {

auto *SrcVecTy = dyn_cast<FixedVectorType>(T1->getType());
auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType());
assert(SrcVecTy && DstVecTy &&
Copy link
Contributor

Choose a reason for hiding this comment

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

If they don't expect to be nullptr, a cast would be more appropriate.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes - replace those 2 dyn_cast with cast - the previous dyn_cast checks for the select condition operands should be ensuring FixedVectorType for everything

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good points, thank you! I've updated the PR accordingly.

@dtcxzyw dtcxzyw requested a review from RKSimon April 6, 2025 07:08
@calebwat calebwat changed the title [NFC] Add assertions for Src and Dst vec types in VecCombine folding [NFC] Use cast instead of dyn_cast for Src and Dst vec types in VecCombine folding Apr 7, 2025
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM - cheers

@RKSimon RKSimon merged commit f989db5 into llvm:main Apr 10, 2025
11 checks passed
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…mbine folding (llvm#134432)

SrcVecTy and DstVecTy are used without a null check, and originate from
a dyn_cast. This patch adjusts this to use a fixed cast, since it is not
checked for null before use otherwise, but is semantically guaranteed
from previous checks.
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.

4 participants