Skip to content

Commit df58c38

Browse files
authored
[RISCV][DAGCombiner] Fix potential missed combine in VL->VW extension (#168026)
The previous implementation of `combineOp_VLToVWOp_VL` manually replaced old nodes with newly created widened nodes, but only added the new node itself to the `DAGCombiner` worklist. Since the users of the new node were not added, some combine opportunities could be missed when external `DAGCombiner` passes expected those users to be reconsidered. This patch replaces the custom replacement logic with a call to `DCI.CombineTo()`, which performs node replacement in a way consistent with `DAGCombiner::Run`: - Replace all uses of the old node. - Add the new node and its users to the worklist. - Clean up unused nodes when appropriate. Using `CombineTo` ensures that `combineOp_VLToVWOp_VL` behaves consistently with the standard `DAGCombiner` update model, avoiding discrepancies between the private worklist inside this routine and the global worklist managed by the combiner. This resolves missed combine cases involving VL -> VW operator widening. --------- Co-authored-by: Kai Lin <[email protected]>
1 parent 12131d5 commit df58c38

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18396,8 +18396,7 @@ static SDValue combineOp_VLToVWOp_VL(SDNode *N,
1839618396
}
1839718397
}
1839818398
for (std::pair<SDValue, SDValue> OldNewValues : ValuesToReplace) {
18399-
DAG.ReplaceAllUsesOfValueWith(OldNewValues.first, OldNewValues.second);
18400-
DCI.AddToWorklist(OldNewValues.second.getNode());
18399+
DCI.CombineTo(OldNewValues.first.getNode(), OldNewValues.second);
1840118400
}
1840218401
return InputRootReplacement;
1840318402
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
2+
; RUN: llc -mtriple=riscv32 -mattr=+v -verify-machineinstrs < %s | FileCheck %s --check-prefixes=CHECK,RV32
3+
; RUN: llc -mtriple=riscv64 -mattr=+v -verify-machineinstrs < %s | FileCheck %s --check-prefixes=CHECK,RV64
4+
5+
define void @matmul_min(ptr %vptr, ptr %scalars, ptr %acc0_ptr, ptr %acc1_ptr) {
6+
; CHECK-LABEL: matmul_min:
7+
; CHECK: # %bb.0: # %entry
8+
; CHECK-NEXT: li a4, 64
9+
; CHECK-NEXT: li a5, 32
10+
; CHECK-NEXT: vsetvli zero, a5, e8, m2, ta, ma
11+
; CHECK-NEXT: vle8.v v16, (a0)
12+
; CHECK-NEXT: lb a0, 0(a1)
13+
; CHECK-NEXT: lb a1, 1(a1)
14+
; CHECK-NEXT: vsetvli zero, a4, e8, m4, ta, ma
15+
; CHECK-NEXT: vle8.v v8, (a2)
16+
; CHECK-NEXT: vle8.v v12, (a3)
17+
; CHECK-NEXT: vsetvli zero, a5, e8, m2, ta, ma
18+
; CHECK-NEXT: vwmacc.vx v8, a0, v16
19+
; CHECK-NEXT: vwmacc.vx v12, a1, v16
20+
; CHECK-NEXT: vsetvli zero, a4, e8, m4, ta, ma
21+
; CHECK-NEXT: vse8.v v8, (a2)
22+
; CHECK-NEXT: vse8.v v12, (a3)
23+
; CHECK-NEXT: ret
24+
entry:
25+
%acc0 = load <32 x i16>, ptr %acc0_ptr, align 1
26+
%acc1 = load <32 x i16>, ptr %acc1_ptr, align 1
27+
28+
%v8 = load <32 x i8>, ptr %vptr, align 1
29+
%v16 = sext <32 x i8> %v8 to <32 x i16>
30+
31+
%s0_ptr = getelementptr i8, ptr %scalars, i32 0
32+
%s0_i8 = load i8, ptr %s0_ptr, align 1
33+
%s0_i16 = sext i8 %s0_i8 to i16
34+
%tmp0 = insertelement <32 x i16> poison, i16 %s0_i16, i32 0
35+
%splat0 = shufflevector <32 x i16> %tmp0, <32 x i16> poison, <32 x i32> zeroinitializer
36+
%mul0 = mul <32 x i16> %splat0, %v16
37+
%add0 = add <32 x i16> %mul0, %acc0
38+
39+
%s1_ptr = getelementptr i8, ptr %scalars, i32 1
40+
%s1_i8 = load i8, ptr %s1_ptr, align 1
41+
%s1_i16 = sext i8 %s1_i8 to i16
42+
%tmp1 = insertelement <32 x i16> poison, i16 %s1_i16, i32 0
43+
%splat1 = shufflevector <32 x i16> %tmp1, <32 x i16> poison, <32 x i32> zeroinitializer
44+
%mul1 = mul <32 x i16> %splat1, %v16
45+
%add1 = add <32 x i16> %mul1, %acc1
46+
47+
store <32 x i16> %add0, ptr %acc0_ptr, align 1
48+
store <32 x i16> %add1, ptr %acc1_ptr, align 1
49+
50+
ret void
51+
}
52+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
53+
; RV32: {{.*}}
54+
; RV64: {{.*}}

0 commit comments

Comments
 (0)