llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86] Missed lowering for AVX512F masked trunc+select to vpmovdb/vpmovdw/vpmovqw

Open Men-cotton opened this issue 6 months ago • 1 comments

LLVM misses masked vpmov* lowering for AVX512F 512-bit trunc followed by masked select.

Reproducer

; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx512f | FileCheck %s

; CHECK: vpmovdb {{.*}}{%k{{[0-9]+}}}
define <16 x i8> @masked_trunc_v16i32_v16i8(<16 x i32> %a, <16 x i8> %p, i16 %m) {
  %t = trunc <16 x i32> %a to <16 x i8>
  %mask = bitcast i16 %m to <16 x i1>
  %r = select <16 x i1> %mask, <16 x i8> %t, <16 x i8> %p
  ret <16 x i8> %r
}

Actual output expands the select after an unmasked truncate:

vpmovdb   %zmm0, %xmm0
kmovw     %edi, %k1
vpternlogd $255, %zmm2, %zmm2, %zmm2 {%k1} {z}
vpmovdb   %zmm2, %xmm2
vpblendvb %xmm2, %xmm0, %xmm1, %xmm0

Expected:

kmovw   %edi, %k1
vpmovdb %zmm0, %xmm1 {%k1}
vmovdqa %xmm1, %xmm0

The same issue applies to vpmovdw (<16 x i32> -> <16 x i16>) and vpmovqw (<8 x i64> -> <8 x i16>); zero passthrough should select {z}.

Where the pattern is lost

X86InstrAVX512.td has select_trunc/trunc patterns for VPMOVDB, VPMOVDW, and VPMOVQW, but combineSelect in X86ISelLowering.cpp inserts ISD::SIGN_EXTEND first for the AVX512-without-BWI i8/i16 select case:

// v16i8 (select v16i1, v16i8, v16i8) does not have a proper
// lowering on KNL...
Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond);
return DAG.getNode(N->getOpcode(), DL, VT, Cond, LHS, RHS);

That changes the SelectionDAG shape before the masked truncate becomes X86ISD::VMTRUNC, so the select_trunc patterns do not match.

Background

This came up while extending CIR's pmov{db,dw,qw}512_mask handling to match the trunc + select form already used for pmov{qd,wb}512_mask. That form comes from f608dc1f5775, which auto-upgraded the 256/512-bit vpmovqd/vpmovwb intrinsics. Clang OGCG still lowers masked db/dw/qw through X86 target intrinsics, except for all-ones masks.

Men-cotton avatar May 31 '26 01:05 Men-cotton

@llvm/issue-subscribers-backend-x86

Author: Akimasa Watanuki (Men-cotton)

LLVM misses masked `vpmov*` lowering for AVX512F 512-bit `trunc` followed by masked `select`.

Reproducer

; RUN: llc &lt; %s -mtriple=x86_64-unknown-unknown -mattr=+avx512f | FileCheck %s

; CHECK: vpmovdb {{.*}}{%k{{[0-9]+}}}
define &lt;16 x i8&gt; @<!-- -->masked_trunc_v16i32_v16i8(&lt;16 x i32&gt; %a, &lt;16 x i8&gt; %p, i16 %m) {
  %t = trunc &lt;16 x i32&gt; %a to &lt;16 x i8&gt;
  %mask = bitcast i16 %m to &lt;16 x i1&gt;
  %r = select &lt;16 x i1&gt; %mask, &lt;16 x i8&gt; %t, &lt;16 x i8&gt; %p
  ret &lt;16 x i8&gt; %r
}

Actual output expands the select after an unmasked truncate:

vpmovdb   %zmm0, %xmm0
kmovw     %edi, %k1
vpternlogd $255, %zmm2, %zmm2, %zmm2 {%k1} {z}
vpmovdb   %zmm2, %xmm2
vpblendvb %xmm2, %xmm0, %xmm1, %xmm0

Expected:

kmovw   %edi, %k1
vpmovdb %zmm0, %xmm1 {%k1}
vmovdqa %xmm1, %xmm0

The same issue applies to vpmovdw (&lt;16 x i32&gt; -&gt; &lt;16 x i16&gt;) and vpmovqw (&lt;8 x i64&gt; -&gt; &lt;8 x i16&gt;); zero passthrough should select {z}.

Where the pattern is lost

X86InstrAVX512.td has select_trunc/trunc patterns for VPMOVDB, VPMOVDW, and VPMOVQW, but combineSelect in X86ISelLowering.cpp inserts ISD::SIGN_EXTEND first for the AVX512-without-BWI i8/i16 select case:

// v16i8 (select v16i1, v16i8, v16i8) does not have a proper
// lowering on KNL...
Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond);
return DAG.getNode(N-&gt;getOpcode(), DL, VT, Cond, LHS, RHS);

That changes the SelectionDAG shape before the masked truncate becomes X86ISD::VMTRUNC, so the select_trunc patterns do not match.

Background

This came up while extending CIR's pmov{db,dw,qw}512_mask handling to match the trunc + select form already used for pmov{qd,wb}512_mask. That form comes from f608dc1f5775, which auto-upgraded the 256/512-bit vpmovqd/vpmovwb intrinsics. Clang OGCG still lowers masked db/dw/qw through X86 target intrinsics, except for all-ones masks.