[llvm] [X86] Fix out-of-range extractBits in combineAVX512SetCCToKMOV (PR #224268)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 04:26:34 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Timur Golubovich (timurgol007)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+2-1) 
- (modified) llvm/test/CodeGen/X86/kmov.ll (+24) 


``````````diff
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
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/224268


More information about the llvm-commits mailing list