Skip to content

Conversation

@Michael-Chen-NJU
Copy link
Contributor

This patch enables the vectorization of the llvm.frexp intrinsic. Following the suggestion in #112408, frexp is moved from isTriviallyScalarizable to isTriviallyVectorizable.

Fixes #112408

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Dec 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 19, 2025

@llvm/pr-subscribers-vectorizers
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: 陈子昂 (Michael-Chen-NJU)

Changes

This patch enables the vectorization of the llvm.frexp intrinsic. Following the suggestion in #112408, frexp is moved from isTriviallyScalarizable to isTriviallyVectorizable.

Fixes #112408


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/VectorUtils.cpp (+1-3)
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index a3e9b039f9225..2fb502bb26f40 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -81,6 +81,7 @@ bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
   case Intrinsic::exp:
   case Intrinsic::exp10:
   case Intrinsic::exp2:
+  case Intrinsic::frexp:
   case Intrinsic::ldexp:
   case Intrinsic::log:
   case Intrinsic::log10:
@@ -129,10 +130,7 @@ bool llvm::isTriviallyScalarizable(Intrinsic::ID ID,
   if (TTI && Intrinsic::isTargetIntrinsic(ID))
     return TTI->isTargetIntrinsicTriviallyScalarizable(ID);
 
-  // TODO: Move frexp to isTriviallyVectorizable.
-  // https://github.com/llvm/llvm-project/issues/112408
   switch (ID) {
-  case Intrinsic::frexp:
   case Intrinsic::uadd_with_overflow:
   case Intrinsic::sadd_with_overflow:
   case Intrinsic::ssub_with_overflow:

@github-actions
Copy link

github-actions bot commented Dec 19, 2025

🐧 Linux x64 Test Results

  • 187825 tests passed
  • 4988 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 19, 2025

🪟 Windows x64 Test Results

  • 128910 tests passed
  • 2842 tests skipped

✅ The build succeeded and all tests passed.

Copy link
Member

@MacDue MacDue left a comment

Choose a reason for hiding this comment

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

Might be worth adding a basic test to llvm/test/Transforms/LoopVectorize/multiple-result-intrinsics.ll

@Michael-Chen-NJU
Copy link
Contributor Author

@MacDue I have added the test cases, but the LoopVectorize pass currently fails to vectorize them, providing the remark: "instruction return type cannot be vectorized." This is due to the mixed return types (float and i32) in the frexp struct, which LoopVectorize doesn't seem to handle yet. Consequently, my current CHECK lines reflect this scalar fallback. Should I investigate enabling mixed-type struct support in the vectorizer, or is this scalar fallback expected for now?

@MacDue
Copy link
Member

MacDue commented Dec 22, 2025

I think that restriction could be relaxed in this case. When I added support for struct-returns the intrinsics I cared about all returned structs of the same type, but I don't think there's any reason we could not support mixed types.

It may be worth investigating, it might be as simple as updating the check.

@Michael-Chen-NJU
Copy link
Contributor Author

@MacDue,I've updated the patch to support widening intrinsics with mixed-type struct returns (like llvm.frexp).

Initially, lifting the legality restriction caused a crash because VPWidenIntrinsicRecipe::execute tried to vectorize the StructType directly. To fix this, I updated the execution logic to unpack the struct and vectorize its individual elements, which correctly generates mangled names like @llvm.frexp.v2f32.v2i32. I've also updated the tests to reflect that mixed-type structs are now successfully vectorized rather than remaining scalar or failing.

Copy link
Member

@MacDue MacDue left a comment

Choose a reason for hiding this comment

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

LGTM with nits fixed 👍

@MacDue MacDue requested a review from fhahn December 23, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms vectorizers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add frexp to isTriviallyVectorizable

3 participants