[llvm] [LoopReduceMotion] Improve loop by extract reduction instruction (PR #179215)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 5 21:55:46 PST 2026
================
@@ -9,361 +9,325 @@ define dso_local i32 @test(ptr noundef %p1, i32 noundef %s_p1, ptr noundef %p2,
; CHECK-O3-LABEL: define dso_local i32 @test(
; CHECK-O3-SAME: ptr noundef readonly captures(none) [[P1:%.*]], i32 noundef [[S_P1:%.*]], ptr noundef readonly captures(none) [[P2:%.*]], i32 noundef [[S_P2:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
; CHECK-O3-NEXT: [[ENTRY:.*:]]
-; CHECK-O3-NEXT: [[IDX_EXT8:%.*]] = sext i32 [[S_P2]] to i64
-; CHECK-O3-NEXT: [[IDX_EXT:%.*]] = sext i32 [[S_P1]] to i64
; CHECK-O3-NEXT: [[TMP0:%.*]] = load <16 x i8>, ptr [[P1]], align 1, !tbaa [[CHAR_TBAA0:![0-9]+]]
; CHECK-O3-NEXT: [[TMP1:%.*]] = zext <16 x i8> [[TMP0]] to <16 x i16>
; CHECK-O3-NEXT: [[TMP2:%.*]] = load <16 x i8>, ptr [[P2]], align 1, !tbaa [[CHAR_TBAA0]]
; CHECK-O3-NEXT: [[TMP3:%.*]] = zext <16 x i8> [[TMP2]] to <16 x i16>
; CHECK-O3-NEXT: [[TMP4:%.*]] = sub nsw <16 x i16> [[TMP1]], [[TMP3]]
; CHECK-O3-NEXT: [[TMP5:%.*]] = tail call <16 x i16> @llvm.abs.v16i16(<16 x i16> [[TMP4]], i1 false)
; CHECK-O3-NEXT: [[TMP6:%.*]] = zext <16 x i16> [[TMP5]] to <16 x i32>
-; CHECK-O3-NEXT: [[TMP7:%.*]] = tail call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> [[TMP6]])
+; CHECK-O3-NEXT: [[IDX_EXT:%.*]] = sext i32 [[S_P1]] to i64
----------------
Anjian-Wen wrote:
@davemgreen Thanks, I agree that we need to find a cost model for this pass on different architectures.
Maybe we can discuss how to calculate the cost, if the value for vecreduce.add is ext from i8 vector, we will lower it to udot later with 1 constants, but it seems we still need to add the result of udot in each loop trip, so maybe the Cost can be calculated by
`Cost(udot(v16i8)) * N + Cost(vaddv(v16i32)) * N + Cost(scalar add(i32)) * N`
if we try to add a this pass, we may only have 1 vecreduce at the end of loop and N vector add in loop, this Cost maybe changed to
`Cost(zext(v16i8)) * N + Cost(vector add(v16i32)) * N + Cost(addv(v16i32)) * 1`
I assume that the cost of instruction for example
vaddv = 4
udot = 4
zext = 2
scalar add = 1
vector add(v16i32) = 3
9N
5N+4
but the result seems this pass can optimize on this asssumption just need N > 2? is there any thing I got wrong or the cost of instruction deviates too much from the true value?
https://github.com/llvm/llvm-project/pull/179215
More information about the llvm-commits
mailing list