[llvm] [LoopVectorize] Support vectorization of frexp intrinsic (PR #172957)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 22 07:49:06 PST 2025


https://github.com/Michael-Chen-NJU updated https://github.com/llvm/llvm-project/pull/172957

>From e162679d2faab197cb7d0c5e65de811be9732401 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Fri, 19 Dec 2025 14:17:46 +0800
Subject: [PATCH 1/2] Initial support for frexp vectorization.

---
 llvm/lib/Analysis/VectorUtils.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

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:

>From 1ff8215a479de946d98c209527891141da4bafcf Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Mon, 22 Dec 2025 23:48:15 +0800
Subject: [PATCH 2/2] Add test

---
 .../multiple-result-intrinsics.ll             | 69 ++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/LoopVectorize/multiple-result-intrinsics.ll b/llvm/test/Transforms/LoopVectorize/multiple-result-intrinsics.ll
index b19f9c5a3b60d..beeda43f4fe99 100644
--- a/llvm/test/Transforms/LoopVectorize/multiple-result-intrinsics.ll
+++ b/llvm/test/Transforms/LoopVectorize/multiple-result-intrinsics.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --filter "(:|sincos|modf|extract|store)" --version 5
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --filter "(:|sincos|frexp|modf|extract|store)" --version 5
 ; RUN: opt -passes=loop-vectorize -force-vector-interleave=1 -force-vector-width=2 < %s -S -o - | FileCheck %s
 
 define void @sincos_f32(ptr noalias %in, ptr noalias writeonly %out_a, ptr noalias writeonly %out_b) {
@@ -277,3 +277,70 @@ exit:
   ret void
 }
 
+define void @frexp_f32(ptr noalias %in, ptr noalias writeonly %out_mantissa, ptr noalias writeonly %out_exponent) {
+; CHECK-LABEL: define void @frexp_f32(
+; CHECK-SAME: ptr noalias [[IN:%.*]], ptr noalias writeonly [[OUT_MANTISSA:%.*]], ptr noalias writeonly [[OUT_EXPONENT:%.*]]) {
+; CHECK:  [[ENTRY:.*:]]
+; CHECK:  [[FOR_BODY:.*:]]
+; CHECK:    [[CALL:%.*]] = tail call { float, i32 } @llvm.frexp.f32.i32(float [[IN_VAL:%.*]])
+; CHECK:    [[MANTISSA:%.*]] = extractvalue { float, i32 } [[CALL]], 0
+; CHECK:    [[EXPONENT:%.*]] = extractvalue { float, i32 } [[CALL]], 1
+; CHECK:    store float [[MANTISSA]], ptr [[ARRAYIDX2:%.*]], align 4
+; CHECK:    store i32 [[EXPONENT]], ptr [[ARRAYIDX4:%.*]], align 4
+; CHECK:  [[EXIT:.*:]]
+;
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
+  %arrayidx = getelementptr inbounds float, ptr %in, i64 %iv
+  %in_val = load float, ptr %arrayidx, align 4
+  %call = tail call { float, i32 } @llvm.frexp.f32.i32(float %in_val)
+  %mantissa = extractvalue { float, i32 } %call, 0
+  %exponent = extractvalue { float, i32 } %call, 1
+  %arrayidx2 = getelementptr inbounds float, ptr %out_mantissa, i64 %iv
+  store float %mantissa, ptr %arrayidx2, align 4
+  %arrayidx4 = getelementptr inbounds i32, ptr %out_exponent, i64 %iv
+  store i32 %exponent, ptr %arrayidx4, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv.next, 1024
+  br i1 %exitcond.not, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
+define void @frexp_f64(ptr noalias %in, ptr noalias writeonly %out_mantissa, ptr noalias writeonly %out_exponent) {
+; CHECK-LABEL: define void @frexp_f64(
+; CHECK-SAME: ptr noalias [[IN:%.*]], ptr noalias writeonly [[OUT_MANTISSA:%.*]], ptr noalias writeonly [[OUT_EXPONENT:%.*]]) {
+; CHECK:  [[ENTRY:.*:]]
+; CHECK:  [[FOR_BODY:.*:]]
+; CHECK:    [[CALL:%.*]] = tail call { double, i32 } @llvm.frexp.f64.i32(double [[IN_VAL:%.*]])
+; CHECK:    [[MANTISSA:%.*]] = extractvalue { double, i32 } [[CALL]], 0
+; CHECK:    [[EXPONENT:%.*]] = extractvalue { double, i32 } [[CALL]], 1
+; CHECK:    store double [[MANTISSA]], ptr [[ARRAYIDX2:%.*]], align 8
+; CHECK:    store i32 [[EXPONENT]], ptr [[ARRAYIDX4:%.*]], align 4
+; CHECK:  [[EXIT:.*:]]
+;
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
+  %arrayidx = getelementptr inbounds double, ptr %in, i64 %iv
+  %in_val = load double, ptr %arrayidx, align 8
+  %call = tail call { double, i32 } @llvm.frexp.f64.i32(double %in_val)
+  %mantissa = extractvalue { double, i32 } %call, 0
+  %exponent = extractvalue { double, i32 } %call, 1
+  %arrayidx2 = getelementptr inbounds double, ptr %out_mantissa, i64 %iv
+  store double %mantissa, ptr %arrayidx2, align 8
+  %arrayidx4 = getelementptr inbounds i32, ptr %out_exponent, i64 %iv
+  store i32 %exponent, ptr %arrayidx4, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv.next, 1024
+  br i1 %exitcond.not, label %exit, label %for.body
+
+exit:
+  ret void
+}



More information about the llvm-commits mailing list