[llvm] [RISCV][P-ext] Avoid redundant accumulator extend for reduction sum (PR #206430)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 29 01:59:13 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: SiHuaN (sihuan)
<details>
<summary>Changes</summary>
For a reduction sum with an i32 accumulator on RV64, the result is
computed at i64 and truncated, so the accumulator's upper bits are
unused. Any-extend it instead of sign-/zero-extending, dropping a
redundant sext.w/zext.w. Follow-up to #<!-- -->206004.
---
Full diff: https://github.com/llvm/llvm-project/pull/206430.diff
3 Files Affected:
- (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+5-1)
- (modified) llvm/test/CodeGen/RISCV/rvp-simd-32.ll (-4)
- (modified) llvm/test/CodeGen/RISCV/rvp-simd-64.ll (-4)
``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index ff14ecbd4917c..3a4ec65e4085f 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -15861,7 +15861,11 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
Vec = DAG.getBitcast(WideVT, Wide);
}
- SDValue Res = RedSum(MVT::i64, Vec, Ext(N->getOperand(2)));
+ // The result is truncated to i32, so the accumulator's upper bits are
+ // unused and need no sign/zero extension.
+ SDValue Acc =
+ DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
+ SDValue Res = RedSum(MVT::i64, Vec, Acc);
Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
return;
}
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
index 36a3430be3a1b..2b62ce13c5003 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-32.ll
@@ -2471,7 +2471,6 @@ define i32 @test_predsum_i8x4_i32(<4 x i8> %a, i32 %b) {
;
; RV64-LABEL: test_predsum_i8x4_i32:
; RV64: # %bb.0:
-; RV64-NEXT: sext.w a1, a1
; RV64-NEXT: zext.w a0, a0
; RV64-NEXT: predsum.bs a0, a0, a1
; RV64-NEXT: ret
@@ -2487,7 +2486,6 @@ define i32 @test_predsumu_u8x4_u32(<4 x i8> %a, i32 %b) {
;
; RV64-LABEL: test_predsumu_u8x4_u32:
; RV64: # %bb.0:
-; RV64-NEXT: zext.w a1, a1
; RV64-NEXT: zext.w a0, a0
; RV64-NEXT: predsumu.bs a0, a0, a1
; RV64-NEXT: ret
@@ -2503,7 +2501,6 @@ define i32 @test_predsum_i16x2_i32(<2 x i16> %a, i32 %b) {
;
; RV64-LABEL: test_predsum_i16x2_i32:
; RV64: # %bb.0:
-; RV64-NEXT: sext.w a1, a1
; RV64-NEXT: zext.w a0, a0
; RV64-NEXT: predsum.hs a0, a0, a1
; RV64-NEXT: ret
@@ -2519,7 +2516,6 @@ define i32 @test_predsumu_u16x2_u32(<2 x i16> %a, i32 %b) {
;
; RV64-LABEL: test_predsumu_u16x2_u32:
; RV64: # %bb.0:
-; RV64-NEXT: zext.w a1, a1
; RV64-NEXT: zext.w a0, a0
; RV64-NEXT: predsumu.hs a0, a0, a1
; RV64-NEXT: ret
diff --git a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
index 76838b44a9827..5fefbb394404b 100644
--- a/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-simd-64.ll
@@ -5342,7 +5342,6 @@ define i32 @test_predsum_i8x8_i32(<8 x i8> %a, i32 %b) {
;
; RV64-LABEL: test_predsum_i8x8_i32:
; RV64: # %bb.0:
-; RV64-NEXT: sext.w a1, a1
; RV64-NEXT: predsum.bs a0, a0, a1
; RV64-NEXT: ret
%res = call i32 @llvm.riscv.predsum.i32.v8i8(<8 x i8> %a, i32 %b)
@@ -5357,7 +5356,6 @@ define i32 @test_predsumu_u8x8_u32(<8 x i8> %a, i32 %b) {
;
; RV64-LABEL: test_predsumu_u8x8_u32:
; RV64: # %bb.0:
-; RV64-NEXT: zext.w a1, a1
; RV64-NEXT: predsumu.bs a0, a0, a1
; RV64-NEXT: ret
%res = call i32 @llvm.riscv.predsumu.i32.v8i8(<8 x i8> %a, i32 %b)
@@ -5404,7 +5402,6 @@ define i32 @test_predsum_i16x4_i32(<4 x i16> %a, i32 %b) {
;
; RV64-LABEL: test_predsum_i16x4_i32:
; RV64: # %bb.0:
-; RV64-NEXT: sext.w a1, a1
; RV64-NEXT: predsum.hs a0, a0, a1
; RV64-NEXT: ret
%res = call i32 @llvm.riscv.predsum.i32.v4i16(<4 x i16> %a, i32 %b)
@@ -5419,7 +5416,6 @@ define i32 @test_predsumu_u16x4_u32(<4 x i16> %a, i32 %b) {
;
; RV64-LABEL: test_predsumu_u16x4_u32:
; RV64: # %bb.0:
-; RV64-NEXT: zext.w a1, a1
; RV64-NEXT: predsumu.hs a0, a0, a1
; RV64-NEXT: ret
%res = call i32 @llvm.riscv.predsumu.i32.v4i16(<4 x i16> %a, i32 %b)
``````````
</details>
https://github.com/llvm/llvm-project/pull/206430
More information about the llvm-commits
mailing list