[llvm] [AMDGPU] Improve max3/min3 formation for tree-structured reductions (PR #190734)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 6 22:54:03 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Dark Steve (PrasoonMishra)
<details>
<summary>Changes</summary>
The existing `performMinMaxCombine` forms `max3` by matching `max(max(a,b), c) -> max3(a,b,c)`. For tree reductions like `max(max(a,b), max(c,d))`, this produces `max3(a, b, max(c,d));` placing `max3` on top. At the next tree level, `max(max3, max3)` cannot combine because the 3-op opcode (`FMAXIMUM3`) differs from the 2-op opcode (`FMAXIMUM`).
This patch:
1. Adds a tree combine: `max(max(a,b), max(c,d)) -> max(max3(a,b,c), d)`, keeping a 2-op node on top that enables further combining.
2. Defers the existing combine when the operand is a tree node whose children can still be combined, ensuring inner tree levels are optimized before outer levels consume them.
Deferral is skipped when neither child has a single use, since the inner combine cannot fire in that case.
---
Patch is 92.63 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/190734.diff
9 Files Affected:
- (modified) llvm/lib/Target/AMDGPU/SIISelLowering.cpp (+29-2)
- (modified) llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps.ll (+36-40)
- (modified) llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps_nnan.ll (+12-14)
- (modified) llvm/test/CodeGen/AMDGPU/min3.ll (+1-1)
- (added) llvm/test/CodeGen/AMDGPU/minmax3-tree-reduction.ll (+421)
- (modified) llvm/test/CodeGen/AMDGPU/vector-reduce-smax.ll (+50-64)
- (modified) llvm/test/CodeGen/AMDGPU/vector-reduce-smin.ll (+50-64)
- (modified) llvm/test/CodeGen/AMDGPU/vector-reduce-umax.ll (+54-68)
- (modified) llvm/test/CodeGen/AMDGPU/vector-reduce-umin.ll (+54-68)
``````````diff
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 2e631d2f4a55e..b637371eace0d 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -15743,9 +15743,34 @@ SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
// register pressure for no benefit.
if (supportsMin3Max3(*Subtarget, Opc, VT)) {
+ auto IsTreeWithCombinableChildren = [Opc](SDValue Op) {
+ return Op.getOperand(0).getOpcode() == Opc &&
+ Op.getOperand(1).getOpcode() == Opc &&
+ (Op.getOperand(0).hasOneUse() || Op.getOperand(1).hasOneUse());
+ };
+
+ // Tree reduction: when both operands are the same min/max op, restructure
+ // to keep a 2-op node on top so higher tree levels can still combine.
+ //
+ // max(max(a, b), max(c, d)) -> max(max3(a, b, c), d)
+ // min(min(a, b), min(c, d)) -> min(min3(a, b, c), d)
+ //
+ // Defer when either inner op is a tree node with combinable children.
+ if (Op0.getOpcode() == Opc && Op0.hasOneUse() &&
+ Op1.getOpcode() == Opc && Op1.hasOneUse() &&
+ !IsTreeWithCombinableChildren(Op0) &&
+ !IsTreeWithCombinableChildren(Op1)) {
+ SDLoc DL(N);
+ SDValue Inner = DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, VT, Op0.getOperand(0),
+ Op0.getOperand(1), Op1.getOperand(0));
+ return DAG.getNode(Opc, DL, VT, Inner, Op1.getOperand(1));
+ }
+
// max(max(a, b), c) -> max3(a, b, c)
// min(min(a, b), c) -> min3(a, b, c)
- if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
+ // Deferred when Op0 is a tree node with combinable children.
+ if (Op0.getOpcode() == Opc && Op0.hasOneUse() &&
+ !IsTreeWithCombinableChildren(Op0)) {
SDLoc DL(N);
return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, N->getValueType(0),
Op0.getOperand(0), Op0.getOperand(1), Op1);
@@ -15754,7 +15779,9 @@ SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
// Try commuted.
// max(a, max(b, c)) -> max3(a, b, c)
// min(a, min(b, c)) -> min3(a, b, c)
- if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
+ // Deferred when Op1 is a tree node with combinable children.
+ if (Op1.getOpcode() == Opc && Op1.hasOneUse() &&
+ !IsTreeWithCombinableChildren(Op1)) {
SDLoc DL(N);
return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, N->getValueType(0),
Op0, Op1.getOperand(0), Op1.getOperand(1));
diff --git a/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps.ll b/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps.ll
index ecafe94d4cd55..37d058b2cb400 100644
--- a/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps.ll
+++ b/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps.ll
@@ -1711,9 +1711,8 @@ define i1 @test94(i32 %arg1, i32 %arg2, i32 %arg3, i32 %arg4, i32 %arg5, i32 %ar
; GCN-LABEL: test94:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_u32_e32 v2, v2, v3
; GCN-NEXT: v_min3_u32 v0, v0, v1, v2
-; GCN-NEXT: v_min_u32_e32 v0, v0, v4
+; GCN-NEXT: v_min3_u32 v0, v0, v3, v4
; GCN-NEXT: v_min3_u32 v0, v5, v6, v0
; GCN-NEXT: v_cmp_lt_u32_e32 vcc_lo, v0, v8
; GCN-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc_lo
@@ -1873,8 +1872,8 @@ define i1 @test102(i32 %arg1, i32 %arg2, i32 %arg3, i32 %arg4, i32 %arg5, i32 %a
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
; GCN-NEXT: v_max_u32_e32 v0, v0, v1
-; GCN-NEXT: v_min_u32_e32 v1, v2, v3
-; GCN-NEXT: v_min3_u32 v0, v0, v5, v1
+; GCN-NEXT: v_min3_u32 v0, v0, v5, v2
+; GCN-NEXT: v_min_u32_e32 v0, v0, v3
; GCN-NEXT: v_cmp_lt_u32_e32 vcc_lo, v0, v6
; GCN-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc_lo
; GCN-NEXT: s_setpc_b64 s[30:31]
@@ -1924,14 +1923,14 @@ define i1 @test104(i32 %arg1, i32 %arg2, i32 %arg3, i32 %arg4, i32 %arg5, i32 %a
; GCN-LABEL: test104:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_u32_e32 v8, v8, v9
-; GCN-NEXT: v_max_u32_e32 v2, v2, v3
-; GCN-NEXT: v_min_u32_e32 v3, v4, v5
-; GCN-NEXT: v_max_u32_e32 v4, v6, v7
; GCN-NEXT: v_min3_u32 v0, v0, v1, v8
-; GCN-NEXT: v_cmp_gt_u32_e32 vcc_lo, v2, v10
-; GCN-NEXT: v_cmp_lt_u32_e64 s0, v3, v10
-; GCN-NEXT: v_cmp_gt_u32_e64 s1, v4, v10
+; GCN-NEXT: v_max_u32_e32 v1, v2, v3
+; GCN-NEXT: v_min_u32_e32 v2, v4, v5
+; GCN-NEXT: v_max_u32_e32 v3, v6, v7
+; GCN-NEXT: v_min_u32_e32 v0, v0, v9
+; GCN-NEXT: v_cmp_gt_u32_e32 vcc_lo, v1, v10
+; GCN-NEXT: v_cmp_lt_u32_e64 s0, v2, v10
+; GCN-NEXT: v_cmp_gt_u32_e64 s1, v3, v10
; GCN-NEXT: v_cmp_lt_u32_e64 s2, v0, v10
; GCN-NEXT: s_or_b32 s0, s0, s1
; GCN-NEXT: s_or_b32 s1, s2, vcc_lo
@@ -1999,13 +1998,13 @@ define i1 @test106(i32 %arg1, i32 %arg2, i32 %arg3, i32 %arg4, i32 %arg5, i32 %a
; GCN-LABEL: test106:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_u32_e32 v6, v6, v7
+; GCN-NEXT: v_min3_u32 v4, v4, v5, v6
; GCN-NEXT: v_min_u32_e32 v0, v0, v1
-; GCN-NEXT: v_min_u32_e32 v1, v10, v11
+; GCN-NEXT: v_min3_u32 v1, v8, v9, v10
; GCN-NEXT: v_min_u32_e32 v2, v2, v3
-; GCN-NEXT: v_min3_u32 v3, v4, v5, v6
+; GCN-NEXT: v_min_u32_e32 v3, v4, v7
; GCN-NEXT: v_cmp_lt_u32_e32 vcc_lo, v0, v12
-; GCN-NEXT: v_min3_u32 v0, v8, v9, v1
+; GCN-NEXT: v_min_u32_e32 v0, v1, v11
; GCN-NEXT: v_cmp_lt_u32_e64 s0, v2, v13
; GCN-NEXT: v_cmp_lt_u32_e64 s1, v3, v13
; GCN-NEXT: v_cmp_lt_u32_e64 s2, v0, v12
@@ -2124,10 +2123,8 @@ define i1 @test111(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test111:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_dual_max_f32 v3, v3, v3 :: v_dual_max_f32 v2, v2, v2
-; GCN-NEXT: v_dual_min_f32 v2, v2, v3 :: v_dual_max_f32 v3, v4, v4
; GCN-NEXT: v_min3_f32 v0, v0, v1, v2
-; GCN-NEXT: v_min_f32_e32 v0, v0, v3
+; GCN-NEXT: v_min3_f32 v0, v0, v3, v4
; GCN-NEXT: v_min3_f32 v0, v5, v6, v0
; GCN-NEXT: v_cmp_lt_f32_e32 vcc_lo, v0, v8
; GCN-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc_lo
@@ -2154,12 +2151,12 @@ define i1 @test112(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test112:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_dual_max_f32 v3, v3, v3 :: v_dual_max_f32 v2, v2, v2
-; GCN-NEXT: v_cmp_nge_f32_e32 vcc_lo, v4, v8
-; GCN-NEXT: v_dual_max_f32 v5, v5, v5 :: v_dual_min_f32 v2, v2, v3
+; GCN-NEXT: v_min3_f32 v0, v0, v1, v2
+; GCN-NEXT: v_dual_max_f32 v1, v3, v3 :: v_dual_max_f32 v2, v5, v5
; GCN-NEXT: v_max_f32_e32 v3, v6, v6
+; GCN-NEXT: v_cmp_nge_f32_e32 vcc_lo, v4, v8
; GCN-NEXT: v_min3_f32 v0, v0, v1, v2
-; GCN-NEXT: v_min3_f32 v0, v0, v5, v3
+; GCN-NEXT: v_min_f32_e32 v0, v0, v3
; GCN-NEXT: v_cmp_lt_f32_e64 s0, v0, v8
; GCN-NEXT: s_or_b32 s0, s0, vcc_lo
; GCN-NEXT: v_cndmask_b32_e64 v0, 0, 1, s0
@@ -2248,18 +2245,17 @@ define i1 @test116(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test116:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_dual_max_f32 v9, v9, v9 :: v_dual_max_f32 v8, v8, v8
; GCN-NEXT: v_dual_max_f32 v1, v1, v1 :: v_dual_max_f32 v0, v0, v0
+; GCN-NEXT: v_dual_max_f32 v5, v5, v5 :: v_dual_max_f32 v8, v8, v8
; GCN-NEXT: v_dual_max_f32 v3, v3, v3 :: v_dual_max_f32 v2, v2, v2
-; GCN-NEXT: v_dual_max_f32 v5, v5, v5 :: v_dual_max_f32 v4, v4, v4
-; GCN-NEXT: v_dual_max_f32 v7, v7, v7 :: v_dual_max_f32 v6, v6, v6
-; GCN-NEXT: v_min_f32_e32 v8, v8, v9
-; GCN-NEXT: v_dual_max_f32 v2, v2, v3 :: v_dual_min_f32 v3, v4, v5
-; GCN-NEXT: v_max_f32_e32 v4, v6, v7
+; GCN-NEXT: v_dual_max_f32 v4, v4, v4 :: v_dual_max_f32 v7, v7, v7
+; GCN-NEXT: v_dual_max_f32 v6, v6, v6 :: v_dual_max_f32 v9, v9, v9
; GCN-NEXT: v_min3_f32 v0, v0, v1, v8
-; GCN-NEXT: v_cmp_gt_f32_e32 vcc_lo, v2, v10
-; GCN-NEXT: v_cmp_lt_f32_e64 s0, v3, v10
-; GCN-NEXT: v_cmp_gt_f32_e64 s1, v4, v10
+; GCN-NEXT: v_dual_max_f32 v1, v2, v3 :: v_dual_min_f32 v2, v4, v5
+; GCN-NEXT: v_dual_max_f32 v3, v6, v7 :: v_dual_min_f32 v0, v0, v9
+; GCN-NEXT: v_cmp_gt_f32_e32 vcc_lo, v1, v10
+; GCN-NEXT: v_cmp_lt_f32_e64 s0, v2, v10
+; GCN-NEXT: v_cmp_gt_f32_e64 s1, v3, v10
; GCN-NEXT: v_cmp_lt_f32_e64 s2, v0, v10
; GCN-NEXT: s_or_b32 s0, s0, s1
; GCN-NEXT: s_or_b32 s1, s2, vcc_lo
@@ -2292,18 +2288,18 @@ define i1 @test117(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test117:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_dual_max_f32 v3, v3, v3 :: v_dual_max_f32 v6, v6, v6
-; GCN-NEXT: v_dual_max_f32 v7, v7, v7 :: v_dual_max_f32 v10, v10, v10
; GCN-NEXT: v_dual_max_f32 v1, v1, v1 :: v_dual_max_f32 v0, v0, v0
-; GCN-NEXT: v_dual_max_f32 v11, v11, v11 :: v_dual_max_f32 v2, v2, v2
-; GCN-NEXT: v_min_f32_e32 v6, v6, v7
-; GCN-NEXT: v_dual_min_f32 v0, v0, v1 :: v_dual_min_f32 v1, v10, v11
-; GCN-NEXT: v_min_f32_e32 v2, v2, v3
+; GCN-NEXT: v_max_f32_e32 v2, v2, v2
+; GCN-NEXT: v_dual_min_f32 v0, v0, v1 :: v_dual_max_f32 v1, v3, v3
; GCN-NEXT: v_min3_f32 v3, v4, v5, v6
+; GCN-NEXT: v_max_f32_e32 v4, v7, v7
+; GCN-NEXT: v_min3_f32 v5, v8, v9, v10
+; GCN-NEXT: v_dual_max_f32 v6, v11, v11 :: v_dual_min_f32 v1, v2, v1
; GCN-NEXT: v_cmp_lt_f32_e32 vcc_lo, v0, v12
-; GCN-NEXT: v_min3_f32 v0, v8, v9, v1
-; GCN-NEXT: v_cmp_lt_f32_e64 s0, v2, v13
-; GCN-NEXT: v_cmp_lt_f32_e64 s1, v3, v13
+; GCN-NEXT: v_min_f32_e32 v2, v3, v4
+; GCN-NEXT: v_min_f32_e32 v0, v5, v6
+; GCN-NEXT: v_cmp_lt_f32_e64 s0, v1, v13
+; GCN-NEXT: v_cmp_lt_f32_e64 s1, v2, v13
; GCN-NEXT: v_cmp_lt_f32_e64 s2, v0, v12
; GCN-NEXT: s_or_b32 s0, vcc_lo, s0
; GCN-NEXT: s_or_b32 s0, s0, s1
diff --git a/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps_nnan.ll b/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps_nnan.ll
index 37ef7949fe5c9..929ccaa2d86f0 100644
--- a/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps_nnan.ll
+++ b/llvm/test/CodeGen/AMDGPU/combine_andor_with_cmps_nnan.ll
@@ -805,9 +805,8 @@ define i1 @test111(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test111:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_f32_e32 v2, v2, v3
; GCN-NEXT: v_min3_f32 v0, v0, v1, v2
-; GCN-NEXT: v_min_f32_e32 v0, v0, v4
+; GCN-NEXT: v_min3_f32 v0, v0, v3, v4
; GCN-NEXT: v_min3_f32 v0, v5, v6, v0
; GCN-NEXT: v_cmp_lt_f32_e32 vcc_lo, v0, v8
; GCN-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc_lo
@@ -834,9 +833,8 @@ define i1 @test112(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test112:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_f32_e32 v2, v2, v3
; GCN-NEXT: v_min3_f32 v0, v0, v1, v2
-; GCN-NEXT: v_min_f32_e32 v0, v0, v4
+; GCN-NEXT: v_min3_f32 v0, v0, v3, v4
; GCN-NEXT: v_min3_f32 v0, v5, v6, v0
; GCN-NEXT: v_cmp_lt_f32_e32 vcc_lo, v0, v8
; GCN-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc_lo
@@ -918,13 +916,12 @@ define i1 @test116(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test116:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_f32_e32 v8, v8, v9
-; GCN-NEXT: v_dual_max_f32 v2, v2, v3 :: v_dual_min_f32 v3, v4, v5
-; GCN-NEXT: v_max_f32_e32 v4, v6, v7
; GCN-NEXT: v_min3_f32 v0, v0, v1, v8
-; GCN-NEXT: v_cmp_gt_f32_e32 vcc_lo, v2, v10
-; GCN-NEXT: v_cmp_lt_f32_e64 s0, v3, v10
-; GCN-NEXT: v_cmp_gt_f32_e64 s1, v4, v10
+; GCN-NEXT: v_dual_max_f32 v1, v2, v3 :: v_dual_min_f32 v2, v4, v5
+; GCN-NEXT: v_dual_max_f32 v3, v6, v7 :: v_dual_min_f32 v0, v0, v9
+; GCN-NEXT: v_cmp_gt_f32_e32 vcc_lo, v1, v10
+; GCN-NEXT: v_cmp_lt_f32_e64 s0, v2, v10
+; GCN-NEXT: v_cmp_gt_f32_e64 s1, v3, v10
; GCN-NEXT: v_cmp_lt_f32_e64 s2, v0, v10
; GCN-NEXT: s_or_b32 s0, s0, s1
; GCN-NEXT: s_or_b32 s1, s2, vcc_lo
@@ -957,12 +954,13 @@ define i1 @test117(float %arg1, float %arg2, float %arg3, float %arg4, float %ar
; GCN-LABEL: test117:
; GCN: ; %bb.0:
; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GCN-NEXT: v_min_f32_e32 v6, v6, v7
-; GCN-NEXT: v_dual_min_f32 v0, v0, v1 :: v_dual_min_f32 v1, v10, v11
+; GCN-NEXT: v_min3_f32 v4, v4, v5, v6
+; GCN-NEXT: v_min_f32_e32 v0, v0, v1
+; GCN-NEXT: v_min3_f32 v1, v8, v9, v10
; GCN-NEXT: v_min_f32_e32 v2, v2, v3
-; GCN-NEXT: v_min3_f32 v3, v4, v5, v6
+; GCN-NEXT: v_min_f32_e32 v3, v4, v7
; GCN-NEXT: v_cmp_lt_f32_e32 vcc_lo, v0, v12
-; GCN-NEXT: v_min3_f32 v0, v8, v9, v1
+; GCN-NEXT: v_min_f32_e32 v0, v1, v11
; GCN-NEXT: v_cmp_lt_f32_e64 s0, v2, v13
; GCN-NEXT: v_cmp_lt_f32_e64 s1, v3, v13
; GCN-NEXT: v_cmp_lt_f32_e64 s2, v0, v12
diff --git a/llvm/test/CodeGen/AMDGPU/min3.ll b/llvm/test/CodeGen/AMDGPU/min3.ll
index e30b929ed9a94..82452c861769b 100644
--- a/llvm/test/CodeGen/AMDGPU/min3.ll
+++ b/llvm/test/CodeGen/AMDGPU/min3.ll
@@ -42,8 +42,8 @@ define amdgpu_kernel void @v_test_umin3_ult_i32(ptr addrspace(1) %out, ptr addrs
}
; GCN-LABEL: {{^}}v_test_umin_umin_umin:
-; GCN: v_min_i32
; GCN: v_min3_i32
+; GCN: v_min_i32
define amdgpu_kernel void @v_test_umin_umin_umin(ptr addrspace(1) %out, ptr addrspace(1) %aptr, ptr addrspace(1) %bptr, ptr addrspace(1) %cptr) #0 {
%tid = call i32 @llvm.amdgcn.workitem.id.x()
%tid2 = mul i32 %tid, 2
diff --git a/llvm/test/CodeGen/AMDGPU/minmax3-tree-reduction.ll b/llvm/test/CodeGen/AMDGPU/minmax3-tree-reduction.ll
new file mode 100644
index 0000000000000..e6ec764b13604
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/minmax3-tree-reduction.ll
@@ -0,0 +1,421 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 < %s | FileCheck -check-prefix=GFX9 %s
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1250 < %s | FileCheck -check-prefix=GFX1250 %s
+
+; Test that tree-structured min/max reductions form min3/max3 efficiently.
+; The key pattern is op(op(a,b), op(c,d)) which should become
+; op(op3(a,b,c), d) to enable further combining at higher tree levels.
+
+; Basic 4-value tree: maxnum f32
+define float @v_max3_maxnum_tree4_f32(float %a, float %b, float %c, float %d) {
+; GFX9-LABEL: v_max3_maxnum_tree4_f32:
+; GFX9: ; %bb.0:
+; GFX9-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT: v_max3_f32 v0, v0, v1, v2
+; GFX9-NEXT: v_max_f32_e32 v1, v3, v3
+; GFX9-NEXT: v_max_f32_e32 v0, v0, v1
+; GFX9-NEXT: s_setpc_b64 s[30:31]
+;
+; GFX1250-LABEL: v_max3_maxnum_tree4_f32:
+; GFX1250: ; %bb.0:
+; GFX1250-NEXT: s_wait_loadcnt_dscnt 0x0
+; GFX1250-NEXT: s_wait_kmcnt 0x0
+; GFX1250-NEXT: v_max3_num_f32 v0, v0, v1, v2
+; GFX1250-NEXT: v_max_num_f32_e32 v1, v3, v3
+; GFX1250-NEXT: s_delay_alu instid0(VALU_DEP_1)
+; GFX1250-NEXT: v_max_num_f32_e32 v0, v0, v1
+; GFX1250-NEXT: s_set_pc_i64 s[30:31]
+ %max.ab = call float @llvm.maxnum.f32(float %a, float %b)
+ %max.cd = call float @llvm.maxnum.f32(float %c, float %d)
+ %result = call float @llvm.maxnum.f32(float %max.ab, float %max.cd)
+ ret float %result
+}
+
+; 8-value tree: maxnum f32
+define float @v_max3_maxnum_tree8_f32(float %a, float %b, float %c, float %d,
+; GFX9-LABEL: v_max3_maxnum_tree8_f32:
+; GFX9: ; %bb.0:
+; GFX9-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT: v_max3_f32 v0, v0, v1, v2
+; GFX9-NEXT: v_max_f32_e32 v1, v3, v3
+; GFX9-NEXT: v_max_f32_e32 v0, v0, v1
+; GFX9-NEXT: v_max3_f32 v1, v4, v5, v6
+; GFX9-NEXT: v_max3_f32 v0, v0, v1, v7
+; GFX9-NEXT: s_setpc_b64 s[30:31]
+;
+; GFX1250-LABEL: v_max3_maxnum_tree8_f32:
+; GFX1250: ; %bb.0:
+; GFX1250-NEXT: s_wait_loadcnt_dscnt 0x0
+; GFX1250-NEXT: s_wait_kmcnt 0x0
+; GFX1250-NEXT: v_max3_num_f32 v0, v0, v1, v2
+; GFX1250-NEXT: v_max_num_f32_e32 v1, v3, v3
+; GFX1250-NEXT: s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
+; GFX1250-NEXT: v_max_num_f32_e32 v0, v0, v1
+; GFX1250-NEXT: v_max3_num_f32 v1, v4, v5, v6
+; GFX1250-NEXT: v_max3_num_f32 v0, v0, v1, v7
+; GFX1250-NEXT: s_set_pc_i64 s[30:31]
+ float %e, float %f, float %g, float %h) {
+ %ab = call float @llvm.maxnum.f32(float %a, float %b)
+ %cd = call float @llvm.maxnum.f32(float %c, float %d)
+ %ef = call float @llvm.maxnum.f32(float %e, float %f)
+ %gh = call float @llvm.maxnum.f32(float %g, float %h)
+ %abcd = call float @llvm.maxnum.f32(float %ab, float %cd)
+ %efgh = call float @llvm.maxnum.f32(float %ef, float %gh)
+ %result = call float @llvm.maxnum.f32(float %abcd, float %efgh)
+ ret float %result
+}
+
+; Basic 4-value tree: maximum f32 (IEEE 2019)
+define float @v_maximum3_tree4_f32(float %a, float %b, float %c, float %d) {
+; GFX9-LABEL: v_maximum3_tree4_f32:
+; GFX9: ; %bb.0:
+; GFX9-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT: v_max_f32_e32 v4, v0, v1
+; GFX9-NEXT: v_mov_b32_e32 v5, 0x7fc00000
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v0, v1
+; GFX9-NEXT: v_cndmask_b32_e32 v0, v5, v4, vcc
+; GFX9-NEXT: v_max_f32_e32 v1, v2, v3
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v2, v3
+; GFX9-NEXT: v_cndmask_b32_e32 v1, v5, v1, vcc
+; GFX9-NEXT: v_max_f32_e32 v2, v0, v1
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v0, v1
+; GFX9-NEXT: v_cndmask_b32_e32 v0, v5, v2, vcc
+; GFX9-NEXT: s_setpc_b64 s[30:31]
+;
+; GFX1250-LABEL: v_maximum3_tree4_f32:
+; GFX1250: ; %bb.0:
+; GFX1250-NEXT: s_wait_loadcnt_dscnt 0x0
+; GFX1250-NEXT: s_wait_kmcnt 0x0
+; GFX1250-NEXT: v_maximum3_f32 v0, v0, v1, v2
+; GFX1250-NEXT: s_delay_alu instid0(VALU_DEP_1)
+; GFX1250-NEXT: v_maximum_f32 v0, v0, v3
+; GFX1250-NEXT: s_set_pc_i64 s[30:31]
+ %max.ab = call float @llvm.maximum.f32(float %a, float %b)
+ %max.cd = call float @llvm.maximum.f32(float %c, float %d)
+ %result = call float @llvm.maximum.f32(float %max.ab, float %max.cd)
+ ret float %result
+}
+
+; 8-value tree: maximum f32 (IEEE 2019)
+define float @v_maximum3_tree8_f32(float %a, float %b, float %c, float %d,
+; GFX9-LABEL: v_maximum3_tree8_f32:
+; GFX9: ; %bb.0:
+; GFX9-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT: v_max_f32_e32 v8, v0, v1
+; GFX9-NEXT: v_mov_b32_e32 v9, 0x7fc00000
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v0, v1
+; GFX9-NEXT: v_cndmask_b32_e32 v0, v9, v8, vcc
+; GFX9-NEXT: v_max_f32_e32 v1, v2, v3
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v2, v3
+; GFX9-NEXT: v_cndmask_b32_e32 v1, v9, v1, vcc
+; GFX9-NEXT: v_max_f32_e32 v2, v4, v5
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v4, v5
+; GFX9-NEXT: v_cndmask_b32_e32 v2, v9, v2, vcc
+; GFX9-NEXT: v_max_f32_e32 v3, v6, v7
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v6, v7
+; GFX9-NEXT: v_cndmask_b32_e32 v3, v9, v3, vcc
+; GFX9-NEXT: v_max_f32_e32 v4, v0, v1
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v0, v1
+; GFX9-NEXT: v_cndmask_b32_e32 v0, v9, v4, vcc
+; GFX9-NEXT: v_max_f32_e32 v1, v2, v3
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v2, v3
+; GFX9-NEXT: v_cndmask_b32_e32 v1, v9, v1, vcc
+; GFX9-NEXT: v_max_f32_e32 v2, v0, v1
+; GFX9-NEXT: v_cmp_o_f32_e32 vcc, v0, v1
+; GFX9-NEXT: v_cndmask_b32_e32 v0, v9, v2, vcc
+; GFX9-NEXT: s_setpc_b64 s[30:31]
+;
+; GFX1250-LABEL: v_maximum3_tree8_f32:
+; GFX1250: ; %bb.0:
+; GFX1250-NEXT: s_wait_loadcnt_dscnt 0x0
+; GFX1250-NEXT: s_wait_kmcnt 0x0
+; GFX1250-NEXT: v_maximum3_f32 v0, v0, v1, v2
+; GFX1250-NEXT: v_maximum3_f32 v1, v4, v5, v6
+; GFX1250-NEXT: s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
+; GFX1250-NEXT: v_maximum_f32 v0, v0, v3
+; GFX1250-NEXT: v_maximum3_f32 v0, v0, v1, v7
+; GFX1250-NEXT: s_set_pc_i64 s[30:31]
+ float %e, float %f, float %g, float %h) {
+ %ab = call float @llvm.maximum.f32(float %a, float %b)
+ %cd = call float @llvm.maximum.f32(float %c, float %d)
+ %ef = call float @llvm.maximum.f32(float %e, float %f)
+ %gh = call float @llvm.maximum.f32(float %g...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/190734
More information about the llvm-commits
mailing list