Skip to content

[NFC][IR] Add CreateCountTrailingZeroElems helper #106711

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
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,14 @@ class IRBuilderBase {
nullptr, Name);
}

/// Create a call to llvm.experimental_cttz_elts
Value *CreateCountTrailingZeroElems(Type *ResTy, Value *Mask,
const Twine &Name = "") {
return CreateIntrinsic(
Intrinsic::experimental_cttz_elts, {ResTy, Mask->getType()},
{Mask, getInt1(/*ZeroIsPoison=*/true)}, nullptr, Name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Although the helper will currently only ever be called when ZeroIsPoison is true, I think the helper should be able to set a different value. Maybe ZeroIsPoison could be passed in with a default value of true instead?

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 suggestion! Uploaded a new patch.

}

private:
/// Create a call to a masked intrinsic with given Id.
CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,7 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch(
VectorFoundIndex->addIncoming(VectorIndexPhi, VectorLoopStartBlock);

Value *PredMatchCmp = Builder.CreateAnd(LastLoopPred, FoundPred);
Value *Ctz = Builder.CreateIntrinsic(
Intrinsic::experimental_cttz_elts, {ResType, PredMatchCmp->getType()},
{PredMatchCmp, /*ZeroIsPoison=*/Builder.getInt1(true)});
Value *Ctz = Builder.CreateCountTrailingZeroElems(ResType, PredMatchCmp);
Ctz = Builder.CreateZExt(Ctz, I64Type);
Value *VectorLoopRes64 = Builder.CreateAdd(VectorFoundIndex, Ctz, "",
/*HasNUW=*/true, /*HasNSW=*/true);
Expand Down
Loading