[llvm] [AMDGPU] Add folding ISD::SELECT from vXiY into vZi32 with X * Y = Z * 32 (PR #173328)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 9 17:06:46 PDT 2026
================
@@ -6983,6 +6983,82 @@ static SDValue combineSelectAsExtAnd(SDValue Cond, SDValue T, SDValue F,
return DAG.getNode(ISD::AND, DL, OpVT, CondMask, T.getOperand(0));
}
+// Widen int vector is not power of 2 in length.
+static SDValue widenIntVectorSelect(SDNode *N, SelectionDAG &DAG,
+ const TargetLowering &TLI, SDValue Cond,
+ SDValue TrueVal, SDValue FalseVal) {
+ EVT ResultVT = N->getValueType(0);
+ if (ResultVT.isSimple() || ResultVT.isPow2VectorType() ||
+ TLI.getTypeAction(*DAG.getContext(), ResultVT) !=
+ TargetLowering::TypeWidenVector)
+ return SDValue();
+
+ EVT EltVT = ResultVT.getVectorElementType();
+ if (!EltVT.isInteger())
+ return SDValue();
+
+ SDValue WidenTrue = DAG.WidenVector(TrueVal, SDLoc(TrueVal));
+ SDValue WidenFalse = DAG.WidenVector(FalseVal, SDLoc(FalseVal));
+
+ EVT WidenVT = WidenTrue.getValueType();
+ SDValue WidenSelect =
+ DAG.getNode(ISD::SELECT, SDLoc(N), WidenVT, Cond, WidenTrue, WidenFalse);
+ return DAG.getExtractSubvector(SDLoc(N), ResultVT, WidenSelect, 0);
+}
+
+// Try to convert vXiY into vPiQ with:
+// 1. vXiY is not legal type
+// 2. vPiQ is legal type
+// 3. X * Y = P * Q
+// This prevent promotion of integer vectors like v32i4 to v32i16
+// which can create extra operations on type casting.
+static SDValue castIntVectorSelect(SDNode *N, SelectionDAG &DAG,
+ const TargetLowering &TLI, SDValue Cond,
+ SDValue TrueVal, SDValue FalseVal) {
+ EVT ResultVT = N->getValueType(0);
+ if (ResultVT.isScalableVector() ||
+ TLI.getTypeAction(*DAG.getContext(), ResultVT) ==
+ TargetLowering::TypeLegal)
+ return SDValue();
+
+ EVT EltVT = ResultVT.getVectorElementType();
+ if (!EltVT.isInteger())
+ return SDValue();
+
+ TypeSize NewEltBitSize = EltVT.getSizeInBits() * 2;
+ EVT NewVT = ResultVT.getIntegerVectorWithElementWidth(*DAG.getContext(),
+ NewEltBitSize);
+
+ while (true) {
----------------
Shoreshen wrote:
Hi @arsenm , the 2 checking (is type legal && `getLegalTypeToTransformTo`) is trying to handle cases that can convert and cases that with v1iN.
The original example of v32i4:
```llvm
define void @v32i4(i1 %cond, ptr addrspace(1) %x, ptr addrspace(3) %y) {
%v = load <32 x i4>, ptr addrspace(1) %x, align 16
%vMasked = select i1 %cond, <32 x i4> %v, <32 x i4> zeroinitializer
store <32 x i4> %vMasked, ptr addrspace(3) %y, align 16
ret void
}
```
Can not use `getLegalTypeToTransformTo` because it will return v32i16 for element size expansion, which is the cause of extra instructions created. Thus we need the loop to try types with same bit but smaller element count until it is legal (which is v4i16).
The `getLegalTypeToTransformTo` combine with the legal action check for select op handles that if we should transform v1iN to iN.
If `iN = getLegalTypeToTransformTo(v1iN)`, select on v1iN is custom and select on iN is legal, than we should bitcast it to iN. One of the example case is the v7i4 case for AMDGPU, it will expand to v8i4 and finally bitcast to i32:
```llvm
define void @v_select_v7i4(i1 %cond, ptr addrspace(1) %x, ptr addrspace(3) %y) {
%v = load <7 x i4>, ptr addrspace(1) %x, align 16
%vMasked = select i1 %cond, <7 x i4> %v, <7 x i4> zeroinitializer
store <7 x i4> %vMasked, ptr addrspace(3) %y, align 16
ret void
}
```
A counter example would be the following from X86
```llvm
; llvm-project/build_release/bin/llc -mtriple=x86_64-unknown-unknown -mattr=+SSSE3
define i8 @v8i32_or_select(<8 x i32> %a0, <8 x i32> %a1, <8 x i32> %a2, <8 x i32> %a3, i1 %a4) {
%cmp0 = icmp eq <8 x i32> %a0, %a1
%cmp1 = icmp eq <8 x i32> %a0, %a2
%cmp2 = icmp slt <8 x i32> %a3, zeroinitializer
%sel = select i1 %a4, <8 x i1> %cmp1, <8 x i1> zeroinitializer
%or0 = or <8 x i1> %cmp2, %cmp0
%or1 = or <8 x i1> %or0, %sel
%res = bitcast <8 x i1> %or1 to i8
ret i8 %res
}
```
In this case X86 have its own optimization for v8i16, simply using i8 to replace v8i1 will increase in instruction count.
https://github.com/llvm/llvm-project/pull/173328
More information about the llvm-commits
mailing list