[llvm] [Scalarizer] Set min-bits=32 may cause a crash issue. (PR #173838)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 27 02:59:44 PST 2026


https://github.com/pRuoyu updated https://github.com/llvm/llvm-project/pull/173838

>From 1860b855169fa5129f203e80b21538fe02fe9a41 Mon Sep 17 00:00:00 2001
From: "Li, Ruoyu" <lryer at msn.com>
Date: Mon, 29 Dec 2025 15:18:32 +0800
Subject: [PATCH 1/2] [Scalarizer] Fix remainder type handling issue for
 multiple scalarized fragments.

When we use <3 x half> with a min-bits constraint, multiple
scalarized fragments are typically generated. However, when
we encounter a function call, usually only the first element
in "tys" is updated to the RemainderTy, which results in incorrect
IR. This caused type mismatches when processing the remainder
fragment.

This patch introduces CachedRemainderTypes to cache RemainderTy for
update first N type in "tys", it can ensuring all overloaded
types are correctly updated when processing remainder fragments.
---
 llvm/lib/Transforms/Scalar/Scalarizer.cpp | 28 +++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Scalarizer.cpp b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
index 46f92c3de04da..b4ff44c9793c6 100644
--- a/llvm/lib/Transforms/Scalar/Scalarizer.cpp
+++ b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
@@ -721,10 +721,20 @@ bool ScalarizerVisitor::splitCall(CallInst &CI) {
   SmallVector<Scatterer, 8> Scattered(NumArgs);
   SmallVector<int> OverloadIdx(NumArgs, -1);
 
+  // Tys array structure:
+  //   [0 ... N-1]: Return type (if overloaded) + struct field types
+  //   [N ... M]:   Argument types (tracked via OverloadIdx)
   SmallVector<llvm::Type *, 3> Tys;
+
+  // Cache RemainderTy pointers for return type and struct fields only.
+  // Indices match Tys[0...N-1]. Argument types are handled separately via OverloadIdx.
+  SmallVector<Type *> CachedRemainderTypes;
+
   // Add return type if intrinsic is overloaded on it.
-  if (isVectorIntrinsicWithOverloadTypeAtArg(ID, -1, TTI))
+  if (isVectorIntrinsicWithOverloadTypeAtArg(ID, -1, TTI)) {
     Tys.push_back(VS->SplitTy);
+    CachedRemainderTypes.push_back(VS->RemainderTy);
+  }
 
   if (AreAllVectorsOfMatchingSize) {
     for (unsigned I = 1; I < CallType->getNumContainedTypes(); I++) {
@@ -738,8 +748,11 @@ bool ScalarizerVisitor::splitCall(CallInst &CI) {
       // the struct elements have the same bitness.
       if (!CurrVS || CurrVS->NumPacked != VS->NumPacked)
         return false;
-      if (isVectorIntrinsicWithStructReturnOverloadAtField(ID, I, TTI))
+      if (isVectorIntrinsicWithStructReturnOverloadAtField(ID, I, TTI)) {
+        // Add both SplitTy and RemainderTy in sync to maintain index alignment
+        CachedRemainderTypes.push_back(CurrVS->RemainderTy);
         Tys.push_back(CurrVS->SplitTy);
+      }
     }
   }
   // Assumes that any vector type has the same number of elements as the return
@@ -785,14 +798,21 @@ bool ScalarizerVisitor::splitCall(CallInst &CI) {
     bool IsRemainder = I == VS->NumFragments - 1 && VS->RemainderTy;
     ScalarCallOps.clear();
 
-    if (IsRemainder)
-      Tys[0] = VS->RemainderTy;
+    if (IsRemainder) {
+      // Update return type and struct field types with RemainderTy.
+      // CachedRemainderTypes indices align with Tys[0...N-1].
+      for (unsigned Idx = 0; Idx < CachedRemainderTypes.size(); ++Idx) {
+        if (CachedRemainderTypes[Idx])
+          Tys[Idx] = CachedRemainderTypes[Idx];
+      }
+    }
 
     for (unsigned J = 0; J != NumArgs; ++J) {
       if (isVectorIntrinsicWithScalarOpAtArg(ID, J, TTI)) {
         ScalarCallOps.push_back(ScalarOperands[J]);
       } else {
         ScalarCallOps.push_back(Scattered[J][I]);
+        // Update argument types separately using OverloadIdx (Tys[N...M])
         if (IsRemainder && OverloadIdx[J] >= 0)
           Tys[OverloadIdx[J]] = Scattered[J][I]->getType();
       }

>From c6f41db2b023d29b234b0fda6886bff14a9a8666 Mon Sep 17 00:00:00 2001
From: "Li, Ruoyu" <lryer at msn.com>
Date: Mon, 29 Dec 2025 15:20:07 +0800
Subject: [PATCH 2/2] [test][Scalarizer] Add test for frexp with odd-sized
 vectors and min-bits

Add regression test for scalarization of frexp intrinsic with odd-sized
vectors (<3 x half>) when using ScalarizeMinBits=32. This tests the
correct handling of remainder fragments for intrinsics returning structs.

The test verifies that when a <3 x half> vector is split into
<2 x half> + half fragments, both the return type and struct field types
are correctly updated to their remainder types { half, i16 } for the
final fragment, preventing type mismatches during intrinsic declaration.
---
 llvm/test/Transforms/Scalarizer/frexp.ll | 28 ++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/llvm/test/Transforms/Scalarizer/frexp.ll b/llvm/test/Transforms/Scalarizer/frexp.ll
index 6397832f6648c..daa2611bd561c 100644
--- a/llvm/test/Transforms/Scalarizer/frexp.ll
+++ b/llvm/test/Transforms/Scalarizer/frexp.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt %s -passes='function(scalarizer)' -S | FileCheck %s
+; RUN: opt %s -passes='function(scalarizer<min-bits=32>)' -S | FileCheck %s --check-prefix=MINBITS32
 
 define void @test_vector_frexp_void(<2 x double> noundef %d) {
 ; CHECK-LABEL: define void @test_vector_frexp_void(
@@ -91,3 +92,30 @@ define noundef <2 x double> @test_vector_double_frexp_int(<2 x double> noundef %
   %3 = extractvalue { <2 x double>, <2 x i32> } %1, 1
   ret <2 x double> %2
 }
+
+define i32 @test_frexp_v3half_with_minbits(ptr %buf) local_unnamed_addr {
+; MINBITS32-LABEL: define i32 @test_frexp_v3half_with_minbits(
+; MINBITS32-SAME: ptr [[BUF:%.*]]) local_unnamed_addr {
+; MINBITS32-NEXT:  .entry:
+; MINBITS32-NEXT:    [[IN:%.*]] = load <4 x half>, ptr [[BUF]], align 8
+; MINBITS32-NEXT:    [[VEC:%.*]] = shufflevector <4 x half> [[IN]], <4 x half> poison, <3 x i32> <i32 0, i32 3, i32 2>
+; MINBITS32-NEXT:    [[VEC_I0:%.*]] = shufflevector <3 x half> [[VEC]], <3 x half> poison, <2 x i32> <i32 0, i32 1>
+; MINBITS32-NEXT:    [[FREXP_I0:%.*]] = call { <2 x half>, <2 x i16> } @llvm.frexp.v2f16.v2i16(<2 x half> [[VEC_I0]])
+; MINBITS32-NEXT:    [[VEC_I1:%.*]] = extractelement <3 x half> [[VEC]], i64 2
+; MINBITS32-NEXT:    [[FREXP_I1:%.*]] = call { half, i16 } @llvm.frexp.f16.i16(half [[VEC_I1]])
+; MINBITS32-NEXT:    [[FRAC_ELEM0:%.*]] = extractvalue { <2 x half>, <2 x i16> } [[FREXP_I0]], 0
+; MINBITS32-NEXT:    [[FRAC_ELEM01:%.*]] = extractvalue { half, i16 } [[FREXP_I1]], 0
+; MINBITS32-NEXT:    [[EXP_ELEM12:%.*]] = extractvalue { half, i16 } [[FREXP_I1]], 1
+; MINBITS32-NEXT:    [[EXP_2_I32:%.*]] = sext i16 [[EXP_ELEM12]] to i32
+; MINBITS32-NEXT:    ret i32 [[EXP_2_I32]]
+;
+.entry:
+  %in = load <4 x half>, ptr %buf
+  %vec = shufflevector <4 x half> %in, <4 x half> poison, <3 x i32> <i32 0, i32 3, i32 2>
+  %frexp = call { <3 x half>, <3 x i16> } @llvm.frexp.v3f16.v3i16(<3 x half> %vec)
+  %frac = extractvalue { <3 x half>, <3 x i16> } %frexp, 0
+  %exp = extractvalue { <3 x half>, <3 x i16> } %frexp, 1
+  %exp.2 = extractelement <3 x i16> %exp, i64 2
+  %exp.2.i32 = sext i16 %exp.2 to i32
+  ret i32 %exp.2.i32
+}
\ No newline at end of file



More information about the llvm-commits mailing list