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

Conversation

david-arm
Copy link
Contributor

The LoopIdiomVectorize pass already creates calls to the intrinsic
experimental_cttz_elts, but PR #88385 will start calling this more
too so I've created a helper for it.

The LoopIdiomVectorize pass already creates calls to the intrinsic
experimental_cttz_elts, but PR llvm#88385 will start calling this more
too so I've created a helper for it.
@llvmbot
Copy link
Member

llvmbot commented Aug 30, 2024

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: David Sherwood (david-arm)

Changes

The LoopIdiomVectorize pass already creates calls to the intrinsic
experimental_cttz_elts, but PR #88385 will start calling this more
too so I've created a helper for it.


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

2 Files Affected:

  • (modified) llvm/include/llvm/IR/IRBuilder.h (+8)
  • (modified) llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp (+2-3)
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 0dbcbc0b2cb76f..d85f51a0a01210 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -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);
+  }
+
 private:
   /// Create a call to a masked intrinsic with given Id.
   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
diff --git a/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
index cb31e2a2ecaec4..45c0eae29c9e9a 100644
--- a/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
@@ -470,9 +470,8 @@ 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);

Copy link

github-actions bot commented Aug 30, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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.

Copy link
Contributor

@kmclaughlin-arm kmclaughlin-arm left a comment

Choose a reason for hiding this comment

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

LGTM!

@david-arm david-arm merged commit dc6c3ba into llvm:main Sep 2, 2024
5 of 8 checks passed
@david-arm david-arm deleted the cttz_elts_nfc branch October 3, 2024 08:54
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.

3 participants