[llvm] [SelectionDAG] Fix crash scalarizing FLDEXP with legal <1xi1> exponent under AVX-512 (PR #219840)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 12:34:57 PDT 2026


https://github.com/tadeuszjt created https://github.com/llvm/llvm-project/pull/219840

**Problem**
This crash was caused by `LegalizeVectorTypes.cpp` routing `FLDEXP` through the `ScalarizeVecRes_BinOp` handler which assumed both arguments had the same type. This is incorrect because `FLDEXP` is not a binary operator.

**Solution**
The call is now routed to `ScalarizeVecRes_UnaryOpWithExtraInput`. This caused another crash in `X86ISelLowering.cpp` because `LowerFLDEXP` couldn't handle the vector exponent. To solve this, logic was added to `ScalarizeVecRes_UnaryOpWithExtraInput` which extracts the vector element 0 which is consistent with other similar functions in the file.

**Caveat**
In this case the generated x86 is possibly sub-optimal because it is promoting to the actual libcall somewhere down the chain instead of using the VSCALE vector intrinsic.  

>From df2269bb09004d51d92086b9902f4aa84edcf0d8 Mon Sep 17 00:00:00 2001
From: Tadeusz Tomoszek <tadeuszjt at protonmail.com>
Date: Sun, 30 Aug 2026 19:06:58 +0200
Subject: [PATCH] [SelectionDAG] Fix crash scalarizing FLDEXP with legal <1xi1>
 exponent under AVX-512

---
 .../SelectionDAG/LegalizeVectorTypes.cpp      | 19 ++++++++++++++++---
 llvm/test/CodeGen/X86/ldexp-avx512.ll         | 15 +++++++++++++++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 6292fac464c97..60f4f15ea4836 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -75,6 +75,7 @@ void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
   case ISD::AssertZext:
   case ISD::AssertSext:
   case ISD::FPOWI:
+  case ISD::FLDEXP:
   case ISD::AssertNoFPClass:
     R = ScalarizeVecRes_UnaryOpWithExtraInput(N);
     break;
@@ -184,7 +185,6 @@ void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
   case ISD::FMAXIMUM:
   case ISD::FMINIMUMNUM:
   case ISD::FMAXIMUMNUM:
-  case ISD::FLDEXP:
   case ISD::ABDS:
   case ISD::ABDU:
   case ISD::SMIN:
@@ -543,8 +543,21 @@ SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_TO_ARBITRARY_FP(SDNode *N) {
 
 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOpWithExtraInput(SDNode *N) {
   SDValue Op = GetScalarizedVector(N->getOperand(0));
-  return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op,
-                     N->getOperand(1));
+  SDValue Extra = N->getOperand(1);
+
+  // The result needs scalarizing, but it's not a given that the extra argument
+  // does. For instance, in AVX512 v1i1 is legal. See the similar logic in
+  // ScalarizeVecRes_UnaryOp.
+  EVT ExtraVT = Extra.getValueType();
+  if (ExtraVT.isVector()) {
+    if (getTypeAction(ExtraVT) == TargetLowering::TypeScalarizeVector)
+      Extra = GetScalarizedVector(Extra);
+    else
+      Extra = DAG.getExtractVectorElt(SDLoc(N), ExtraVT.getVectorElementType(),
+                                      Extra, 0);
+  }
+
+  return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op, Extra);
 }
 
 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
diff --git a/llvm/test/CodeGen/X86/ldexp-avx512.ll b/llvm/test/CodeGen/X86/ldexp-avx512.ll
index cce76e969ffff..781ac80915057 100644
--- a/llvm/test/CodeGen/X86/ldexp-avx512.ll
+++ b/llvm/test/CodeGen/X86/ldexp-avx512.ll
@@ -357,5 +357,20 @@ define <8 x double> @test_ldexp_8xdouble_prefer256(<8 x double> %x, <8 x i32> %e
   ret <8 x double> %r
 }
 
+define <1 x float> @test_ldexp_v1f32_v1i1(<1 x float> %x, <1 x i1> %e) nounwind {
+; CHECK-LABEL: test_ldexp_v1f32_v1i1:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    pushq %rax
+; CHECK-NEXT:    andl $1, %edi
+; CHECK-NEXT:    negl %edi
+; CHECK-NEXT:    callq ldexpf at PLT
+; CHECK-NEXT:    popq %rax
+; CHECK-NEXT:    retq
+  %r = call <1 x float> @llvm.ldexp.v1f32.v1i1(<1 x float> %x, <1 x i1> %e)
+  ret <1 x float> %r
+}
+declare <1 x float> @llvm.ldexp.v1f32.v1i1(<1 x float>, <1 x i1>)
+
+
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
 ; AVX512VLF: {{.*}}



More information about the llvm-commits mailing list