[llvm] [LoopInterchange] Bail out on reductions with uses outside their chain. (PR #220263)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 07:55:43 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Florian Hahn (fhahn)
<details>
<summary>Changes</summary>
getMinMaxRecurrence() can return a descriptor for a min/max phi that has users outside of the reduction chain, e.g. the comparison of an argmin/argmax idiom. Such descriptors have no loop exit instruction, and the extra uses make the order in which the elements are visited observable.
Bail out for such reductions, matching what
canParallelizeReductionWhenUnrolling() already does.
This fixes a crash on the added test cases.
---
Full diff: https://github.com/llvm/llvm-project/pull/220263.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/LoopInterchange.cpp (+5)
- (added) llvm/test/Transforms/LoopInterchange/reduction-minmax-multi-use.ll (+129)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 031b036191f14..05e5da01d3e2f 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -1061,6 +1061,11 @@ static bool checkReductionKind(Loop *L, PHINode *PHI,
if (RD.getExactFPMathInst() != nullptr)
return false;
+ // The extra uses of a reduction phi outside of its reduction chain make
+ // the order in which the elements are visited observable.
+ if (RD.hasUsesOutsideReductionChain())
+ return false;
+
RecurKind RK = RD.getRecurrenceKind();
switch (RK) {
case RecurKind::Or:
diff --git a/llvm/test/Transforms/LoopInterchange/reduction-minmax-multi-use.ll b/llvm/test/Transforms/LoopInterchange/reduction-minmax-multi-use.ll
new file mode 100644
index 0000000000000..cd94a0c5f28be
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/reduction-minmax-multi-use.ll
@@ -0,0 +1,129 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=loop-interchange -loop-interchange-profitabilities=ignore -S %s | FileCheck %s
+
+; FP maximum reduction with an extra use outside the reduction chain.
+define float @fmaximum_with_extra_use_of_reduction_phi(ptr %A) {
+; CHECK-LABEL: define float @fmaximum_with_extra_use_of_reduction_phi(
+; CHECK-SAME: ptr [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[FOR_I_HEADER:.*]]
+; CHECK: [[FOR_I_HEADER]]:
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[I_INC:%.*]], %[[FOR_I_LATCH:.*]] ]
+; CHECK-NEXT: [[MAX_I:%.*]] = phi float [ 0.000000e+00, %[[ENTRY]] ], [ [[MAX_I_LCSSA:%.*]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: br label %[[FOR_J:.*]]
+; CHECK: [[FOR_J]]:
+; CHECK-NEXT: [[J:%.*]] = phi i32 [ 0, %[[FOR_I_HEADER]] ], [ [[J_INC:%.*]], %[[FOR_J]] ]
+; CHECK-NEXT: [[MAX_J:%.*]] = phi float [ [[MAX_I]], %[[FOR_I_HEADER]] ], [ [[MAX_J_NEXT:%.*]], %[[FOR_J]] ]
+; CHECK-NEXT: [[IDX:%.*]] = getelementptr inbounds [2 x [2 x float]], ptr [[A]], i32 0, i32 [[J]], i32 [[I]]
+; CHECK-NEXT: [[A:%.*]] = load float, ptr [[IDX]], align 4
+; CHECK-NEXT: [[EXTRA:%.*]] = fadd float [[MAX_J]], 1.000000e+00
+; CHECK-NEXT: [[MAX_J_NEXT]] = call float @llvm.maximum.f32(float [[MAX_J]], float [[A]])
+; CHECK-NEXT: [[J_INC]] = add i32 [[J]], 1
+; CHECK-NEXT: [[CMP_J:%.*]] = icmp slt i32 [[J_INC]], 2
+; CHECK-NEXT: br i1 [[CMP_J]], label %[[FOR_J]], label %[[FOR_I_LATCH]]
+; CHECK: [[FOR_I_LATCH]]:
+; CHECK-NEXT: [[MAX_I_LCSSA]] = phi float [ [[MAX_I]], %[[FOR_J]] ]
+; CHECK-NEXT: [[EXTRA_LCSSA:%.*]] = phi float [ [[EXTRA]], %[[FOR_J]] ]
+; CHECK-NEXT: [[I_INC]] = add i32 [[I]], 1
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp slt i32 [[I_INC]], 2
+; CHECK-NEXT: br i1 [[CMP_I]], label %[[FOR_I_HEADER]], label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[RES:%.*]] = phi float [ [[EXTRA_LCSSA]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: ret float [[RES]]
+;
+entry:
+ br label %for.i.header
+
+for.i.header:
+ %i = phi i32 [ 0, %entry ], [ %i.inc, %for.i.latch ]
+ %max.i = phi float [ 0.000000e+00, %entry ], [ %max.i.lcssa, %for.i.latch ]
+ br label %for.j
+
+for.j:
+ %j = phi i32 [ 0, %for.i.header ], [ %j.inc, %for.j ]
+ %max.j = phi float [ %max.i, %for.i.header ], [ %max.j.next, %for.j ]
+ %idx = getelementptr inbounds [2 x [2 x float]], ptr %A, i32 0, i32 %j, i32 %i
+ %a = load float, ptr %idx, align 4
+ %extra = fadd float %max.j, 1.000000e+00
+ %max.j.next = call float @llvm.maximum.f32(float %max.j, float %a)
+ %j.inc = add i32 %j, 1
+ %cmp.j = icmp slt i32 %j.inc, 2
+ br i1 %cmp.j, label %for.j, label %for.i.latch
+
+for.i.latch:
+ %max.i.lcssa = phi float [ %max.i, %for.j ]
+ %extra.lcssa = phi float [ %extra, %for.j ]
+ %i.inc = add i32 %i, 1
+ %cmp.i = icmp slt i32 %i.inc, 2
+ br i1 %cmp.i, label %for.i.header, label %exit
+
+exit:
+ %res = phi float [ %extra.lcssa, %for.i.latch ]
+ ret float %res
+}
+
+; sum accumulates the running maximum, so the result depends on the order in
+; which the elements are visited.
+define i32 @smax_with_extra_use_of_reduction_phi(ptr %A) {
+; CHECK-LABEL: define i32 @smax_with_extra_use_of_reduction_phi(
+; CHECK-SAME: ptr [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[FOR_I_HEADER:.*]]
+; CHECK: [[FOR_I_HEADER]]:
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[I_INC:%.*]], %[[FOR_I_LATCH:.*]] ]
+; CHECK-NEXT: [[MAX_I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[MAX_I_LCSSA:%.*]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: [[SUM_I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[SUM_I_LCSSA:%.*]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: br label %[[FOR_J:.*]]
+; CHECK: [[FOR_J]]:
+; CHECK-NEXT: [[J:%.*]] = phi i32 [ 0, %[[FOR_I_HEADER]] ], [ [[J_INC:%.*]], %[[FOR_J]] ]
+; CHECK-NEXT: [[MAX_J:%.*]] = phi i32 [ [[MAX_I]], %[[FOR_I_HEADER]] ], [ [[MAX_J_NEXT:%.*]], %[[FOR_J]] ]
+; CHECK-NEXT: [[SUM_J:%.*]] = phi i32 [ [[SUM_I]], %[[FOR_I_HEADER]] ], [ [[SUM_J_NEXT:%.*]], %[[FOR_J]] ]
+; CHECK-NEXT: [[IDX:%.*]] = getelementptr inbounds [2 x [2 x i32]], ptr [[A]], i32 0, i32 [[J]], i32 [[I]]
+; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[IDX]], align 4
+; CHECK-NEXT: [[SUM_J_NEXT]] = add i32 [[SUM_J]], [[MAX_J]]
+; CHECK-NEXT: [[MAX_J_NEXT]] = tail call i32 @llvm.smax.i32(i32 [[MAX_J]], i32 [[A]])
+; CHECK-NEXT: [[J_INC]] = add i32 [[J]], 1
+; CHECK-NEXT: [[CMP_J:%.*]] = icmp slt i32 [[J_INC]], 2
+; CHECK-NEXT: br i1 [[CMP_J]], label %[[FOR_J]], label %[[FOR_I_LATCH]]
+; CHECK: [[FOR_I_LATCH]]:
+; CHECK-NEXT: [[MAX_I_LCSSA]] = phi i32 [ [[MAX_J_NEXT]], %[[FOR_J]] ]
+; CHECK-NEXT: [[SUM_I_LCSSA]] = phi i32 [ [[SUM_J_NEXT]], %[[FOR_J]] ]
+; CHECK-NEXT: [[I_INC]] = add i32 [[I]], 1
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp slt i32 [[I_INC]], 2
+; CHECK-NEXT: br i1 [[CMP_I]], label %[[FOR_I_HEADER]], label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[SUM_I_LCSSA]], %[[FOR_I_LATCH]] ]
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ br label %for.i.header
+
+for.i.header:
+ %i = phi i32 [ 0, %entry ], [ %i.inc, %for.i.latch ]
+ %max.i = phi i32 [ 0, %entry ], [ %max.i.lcssa, %for.i.latch ]
+ %sum.i = phi i32 [ 0, %entry ], [ %sum.i.lcssa, %for.i.latch ]
+ br label %for.j
+
+for.j:
+ %j = phi i32 [ 0, %for.i.header ], [ %j.inc, %for.j ]
+ %max.j = phi i32 [ %max.i, %for.i.header ], [ %max.j.next, %for.j ]
+ %sum.j = phi i32 [ %sum.i, %for.i.header ], [ %sum.j.next, %for.j ]
+ %idx = getelementptr inbounds [2 x [2 x i32]], ptr %A, i32 0, i32 %j, i32 %i
+ %a = load i32, ptr %idx, align 4
+ %sum.j.next = add i32 %sum.j, %max.j
+ %max.j.next = tail call i32 @llvm.smax.i32(i32 %max.j, i32 %a)
+ %j.inc = add i32 %j, 1
+ %cmp.j = icmp slt i32 %j.inc, 2
+ br i1 %cmp.j, label %for.j, label %for.i.latch
+
+for.i.latch:
+ %max.i.lcssa = phi i32 [ %max.j.next, %for.j ]
+ %sum.i.lcssa = phi i32 [ %sum.j.next, %for.j ]
+ %i.inc = add i32 %i, 1
+ %cmp.i = icmp slt i32 %i.inc, 2
+ br i1 %cmp.i, label %for.i.header, label %exit
+
+exit:
+ %res = phi i32 [ %sum.i.lcssa, %for.i.latch ]
+ ret i32 %res
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/220263
More information about the llvm-commits
mailing list