[llvm] [VectorCombine] Optimize vector combine in fold binop of reduction (PR #179416)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 18 19:33:02 PDT 2026


Anjian-Wen wrote:





> > @davemgreen This patch won't convert like that, only convert `add(add(vecreduce.add(v16i32 zext v18i6), xx), vecreduce.add(v16i32 zext v18i6))` to `add(add(vecreduce.add(v16i32 zext v18i6), vecreduce.add(v16i32 zext v18i6)), xx)`.
> 
> I see. The first test case looks like
> 
> ```
> define i32 @test_add_add_reduction(i32 %a, <4 x i32> %b, <4 x i32> %c) {
> ; CHECK-LABEL: define i32 @test_add_add_reduction(
> ; CHECK-SAME: i32 [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i32> [[C:%.*]]) {
> ; CHECK-NEXT:    [[TMP1:%.*]] = add <4 x i32> [[B]], [[C]]
> ; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[TMP1]])
> ; CHECK-NEXT:    [[ADD2:%.*]] = add i32 [[TMP2]], [[A]]
> ; CHECK-NEXT:    ret i32 [[ADD2]]
> ;
>   ; Test case 1: add (add (vector_reduce_add b, a), vector_reduce_add c)
>   %reduce_b = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %b)
>   %add1 = add i32 %reduce_b, %a
>   %reduce_c = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %c)
>   %add2 = add i32 %add1, %reduce_c
>   ret i32 %add2
> }
> ```
> 
> But you are saying that the combining of the reductions comes from elsewhere, from #121567. I guess that patch is still causing problems. I am meant to be on holiday at the moment but I will see about adding a phase ordering test for whatever is causing some of the larger performance regressions I am seeing.
> 
> > The Change in test udotabd.ll also just change some order because it match the pattern (X + Y) + C -> (X + C) + Y at last add(add, red), but it won't merge because of the origin cost model.
> 
> It needs `-passes="default<O3>,default<O3>"` to emulate LTO, but is showing more instructions for me when the IR is passed into llc.

@davemgreen Thanks for the reply. This patch is not urgent, so please prioritize your vacation. I'll take a look at https://github.com/llvm/llvm-project/pull/121567, and try to explore whether there are any potential improvements in the cost model for merging the two reductions to prevent any performance regression.

https://github.com/llvm/llvm-project/pull/179416


More information about the llvm-commits mailing list