Skip to content

[VectorCombine] foldShuffleChainsToReduce - add support for partial vector reductions #194617

@RKSimon

Description

@RKSimon
define i16 @test_reduce_v16i16_v8i16(<16 x i16> %a0) {
; CHECK-LABEL: @test_reduce_v16i16_v8i16(
; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <16 x i16> [[A0:%.*]], <16 x i16> poison, <16 x i32> <i32 4, i32 5, i32 6, i32 7, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
; CHECK-NEXT:    [[TMP2:%.*]] = tail call <16 x i16> @llvm.smax.v16i16(<16 x i16> [[A0]], <16 x i16> [[TMP1]])
; CHECK-NEXT:    [[TMP3:%.*]] = shufflevector <16 x i16> [[TMP2]], <16 x i16> poison, <16 x i32> <i32 2, i32 3, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
; CHECK-NEXT:    [[TMP4:%.*]] = tail call <16 x i16> @llvm.smax.v16i16(<16 x i16> [[TMP2]], <16 x i16> [[TMP3]])
; CHECK-NEXT:    [[TMP5:%.*]] = shufflevector <16 x i16> [[TMP4]], <16 x i16> poison, <16 x i32> <i32 1, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
; CHECK-NEXT:    [[TMP6:%.*]] = tail call <16 x i16> @llvm.smax.v16i16(<16 x i16> [[TMP4]], <16 x i16> [[TMP5]])
; CHECK-NEXT:    [[TMP10:%.*]] = extractelement <16 x i16> [[TMP6]], i64 0
; CHECK-NEXT:    ret i16 [[TMP10]]
;
  %1  = shufflevector <16 x i16> %a0, <16 x i16> undef, <16 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
  %2  = icmp sgt <16 x i16> %a0, %1
  %3  = select <16 x i1> %2, <16 x i16> %a0, <16 x i16> %1
  %4  = shufflevector <16 x i16> %3, <16 x i16> undef, <16 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
  %5  = icmp sgt <16 x i16> %3, %4
  %6  = select <16 x i1> %5, <16 x i16> %3, <16 x i16> %4
  %7  = shufflevector <16 x i16> %6, <16 x i16> undef, <16 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
  %8  = icmp sgt <16 x i16> %6, %7
  %9  = select <16 x i1> %8, <16 x i16> %6, <16 x i16> %7
  %10 = extractelement <16 x i16> %9, i32 0
  ret i16 %10
}

we should be able to recognise that this is a partial reduction of just the lower subvector:

define i16 @test_reduce_v16i16_v8i16(<16 x i16> %a0) {
; CHECK-LABEL: @test_reduce_v16i16_v8i16(
; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <16 x i16> [[A0:%.*]], <16 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6 i32 7>
; CHECK-NEXT:    [[TMP2:%.*]] = tail call i16 @llvm.vector.reduce.smax.v8i16(<8 x i16> [[TMP1]])
; CHECK-NEXT:    ret i16 [[TMP2]]
;
  %1  = shufflevector <16 x i16> %a0, <16 x i16> undef, <16 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
  %2  = icmp sgt <16 x i16> %a0, %1
  %3  = select <16 x i1> %2, <16 x i16> %a0, <16 x i16> %1
  %4  = shufflevector <16 x i16> %3, <16 x i16> undef, <16 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
  %5  = icmp sgt <16 x i16> %3, %4
  %6  = select <16 x i1> %5, <16 x i16> %3, <16 x i16> %4
  %7  = shufflevector <16 x i16> %6, <16 x i16> undef, <16 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
  %8  = icmp sgt <16 x i16> %6, %7
  %9  = select <16 x i1> %8, <16 x i16> %6, <16 x i16> %7
  %10 = extractelement <16 x i16> %9, i32 0
  ret i16 %10
}

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions