[llvm] [X86] Fix out-of-range extractBits in combineAVX512SetCCToKMOV (PR #224268)
Timur Golubovich via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 04:25:57 PDT 2026
https://github.com/timurgol007 created https://github.com/llvm/llvm-project/pull/224268
When the widened constant table's last element is undef, the trailing-undef check called APInt::extractBits(0, Len), which asserts since bitPosition must be less than BitWidth. Nothing remains to check in that case, so skip the extract and accept the mask.
>From 63a934a3265b2421d92458a0f4a4aa9e9a8e7d3d Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Thu, 17 Sep 2026 13:23:48 +0200
Subject: [PATCH] [X86] Fix out-of-range extractBits in
combineAVX512SetCCToKMOV
When the widened constant table's last element is undef, the trailing-undef
check called APInt::extractBits(0, Len), which asserts since bitPosition
must be less than BitWidth. Nothing remains to check in that case, so skip
the extract and accept the mask.
---
llvm/lib/Target/X86/X86ISelLowering.cpp | 3 ++-
llvm/test/CodeGen/X86/kmov.ll | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 64a4a191e5881..7b3af6152fcfe 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -58778,7 +58778,8 @@ static SDValue combineAVX512SetCCToKMOV(EVT VT, SDValue Op0, ISD::CondCode CC,
unsigned Len = UndefElts.getBitWidth();
for (unsigned I = 1; I != Len; ++I) {
if (UndefElts[I]) {
- if (!UndefElts.extractBits(Len - (I + 1), I + 1).isAllOnes())
+ if (I + 1 != Len &&
+ !UndefElts.extractBits(Len - (I + 1), I + 1).isAllOnes())
return SDValue();
break;
}
diff --git a/llvm/test/CodeGen/X86/kmov.ll b/llvm/test/CodeGen/X86/kmov.ll
index 691bb736e5d50..b92213b569468 100644
--- a/llvm/test/CodeGen/X86/kmov.ll
+++ b/llvm/test/CodeGen/X86/kmov.ll
@@ -642,3 +642,27 @@ define <64 x i1> @invert_i64_mask_extract_64(i64 %mask) {
ret <64 x i1> %cmp.45
}
+define <8 x i1> @i64_mask_extract_8_undef_last_element(i64 %mask) {
+; X64-AVX512-LABEL: i64_mask_extract_8_undef_last_element:
+; X64-AVX512: # %bb.0:
+; X64-AVX512-NEXT: shrq $57, %rdi
+; X64-AVX512-NEXT: kmovd %edi, %k0
+; X64-AVX512-NEXT: knotb %k0, %k0
+; X64-AVX512-NEXT: vpmovm2w %k0, %xmm0
+; X64-AVX512-NEXT: retq
+;
+; X64-KNL-LABEL: i64_mask_extract_8_undef_last_element:
+; X64-KNL: # %bb.0:
+; X64-KNL-NEXT: shrq $57, %rdi
+; X64-KNL-NEXT: kmovw %edi, %k0
+; X64-KNL-NEXT: knotw %k0, %k1
+; X64-KNL-NEXT: vpternlogd {{.*#+}} zmm0 {%k1} {z} = -1
+; X64-KNL-NEXT: vpmovdw %zmm0, %ymm0
+; X64-KNL-NEXT: # kill: def $xmm0 killed $xmm0 killed $ymm0
+; X64-KNL-NEXT: retq
+ %.splatinsert = insertelement <8 x i64> poison, i64 %mask, i64 0
+ %.splat = shufflevector <8 x i64> %.splatinsert, <8 x i64> poison, <8 x i32> zeroinitializer
+ %1 = and <8 x i64> %.splat, <i64 144115188075855872, i64 288230376151711744, i64 576460752303423488, i64 1152921504606846976, i64 2305843009213693952, i64 4611686018427387904, i64 -9223372036854775808, i64 poison>
+ %cmp = icmp eq <8 x i64> %1, zeroinitializer
+ ret <8 x i1> %cmp
+}
More information about the llvm-commits
mailing list