Extend handleMultiUseReductions to support strict predicates (>, <), matching the first index instead of the last for non-strict predicates. Builds on top of https://github.com/llvm/llvm-project/pull/141431. FindLast reductions with strict predicates are adjusted to compute the correct result as follows: 1. Find the first canonical indices corresponding to partial min/max values, using loop reductions. 2. Find which of the partial min/max values are equal to the overall min/max value. 3. Select among the canonical indices those corresponding to the overall min/max value. 4. Find the first canonical index of overall min/max and scale it back to the original IV using VPDerivedIVRecipe. 5. If the overall min/max equals the starting min/max, the condition in the loop was always false, due to being strict; return the original start value in that case.
56 lines
2.5 KiB
LLVM
56 lines
2.5 KiB
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
|
|
; RUN: opt -passes=loop-vectorize -force-vector-width=4 -pass-remarks-output=%t.yaml -S %s | FileCheck %s
|
|
; RUN: FileCheck --input-file=%t.yaml --check-prefix=REMARKS %s
|
|
|
|
; REMARKS: --- !Missed
|
|
; REMARKS-NEXT: Pass: vplan
|
|
; REMARKS-NEXT: Name: VectorizationMultiUseReductionPredicate
|
|
; REMARKS-NEXT: Function: test_predicate_mismatch
|
|
; REMARKS-NEXT: Args:
|
|
; REMARKS-NEXT: - String: 'Multi-use reduction with predicate '
|
|
; REMARKS-NEXT: - String: ule
|
|
; REMARKS-NEXT: - String: ' incompatible with reduction kind'
|
|
; REMARKS-NEXT: ...
|
|
|
|
define i64 @test_predicate_mismatch(ptr %src, i64 %n) {
|
|
; CHECK-LABEL: define i64 @test_predicate_mismatch(
|
|
; CHECK-SAME: ptr [[SRC:%.*]], i64 [[N:%.*]]) {
|
|
; CHECK-NEXT: [[ENTRY:.*]]:
|
|
; CHECK-NEXT: br label %[[LOOP:.*]]
|
|
; CHECK: [[LOOP]]:
|
|
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
|
|
; CHECK-NEXT: [[MIN_IDX:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[MIN_IDX_NEXT:%.*]], %[[LOOP]] ]
|
|
; CHECK-NEXT: [[MIN_VAL:%.*]] = phi i64 [ 100, %[[ENTRY]] ], [ [[MIN_VAL_NEXT:%.*]], %[[LOOP]] ]
|
|
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i64, ptr [[SRC]], i64 [[IV]]
|
|
; CHECK-NEXT: [[L:%.*]] = load i64, ptr [[GEP]], align 4
|
|
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[L]], [[MIN_VAL]]
|
|
; CHECK-NEXT: [[MIN_VAL_NEXT]] = tail call i64 @llvm.umin.i64(i64 [[MIN_VAL]], i64 [[L]])
|
|
; CHECK-NEXT: [[MIN_IDX_NEXT]] = select i1 [[CMP]], i64 [[MIN_IDX]], i64 [[IV]]
|
|
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
|
|
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
|
|
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[EXIT:.*]], label %[[LOOP]]
|
|
; CHECK: [[EXIT]]:
|
|
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[MIN_IDX_NEXT]], %[[LOOP]] ]
|
|
; CHECK-NEXT: ret i64 [[RES]]
|
|
;
|
|
entry:
|
|
br label %loop
|
|
|
|
loop:
|
|
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
|
|
%min.idx = phi i64 [ 0, %entry ], [ %min.idx.next, %loop ]
|
|
%min.val = phi i64 [ 100, %entry ], [ %min.val.next, %loop ]
|
|
%gep = getelementptr i64, ptr %src, i64 %iv
|
|
%l = load i64, ptr %gep
|
|
%cmp = icmp ult i64 %l, %min.val
|
|
%min.val.next = tail call i64 @llvm.umin.i64(i64 %min.val, i64 %l)
|
|
%min.idx.next = select i1 %cmp, i64 %min.idx, i64 %iv
|
|
%iv.next = add nuw nsw i64 %iv, 1
|
|
%exitcond.not = icmp eq i64 %iv.next, %n
|
|
br i1 %exitcond.not, label %exit, label %loop
|
|
|
|
exit:
|
|
%res = phi i64 [ %min.idx.next, %loop ]
|
|
ret i64 %res
|
|
}
|