Skip to content

Commit 0efa386

Browse files
authored
[RISCV] Check VL dominates and potentially move in tryReduceVL (#106753)
Similar to what we do in foldVMV_V_V with the passthru, if we end up changing the Src's VL in tryReduceVL we need to make sure it dominates. Fixes #106735
1 parent d0d0e12 commit 0efa386

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class RISCVVectorPeephole : public MachineFunctionPass {
7070

7171
bool isAllOnesMask(const MachineInstr *MaskDef) const;
7272
std::optional<unsigned> getConstant(const MachineOperand &VL) const;
73+
bool ensureDominates(const MachineOperand &Use, MachineInstr &Src) const;
7374

7475
/// Maps uses of V0 to the corresponding def of V0.
7576
DenseMap<const MachineInstr *, const MachineInstr *> V0Defs;
@@ -165,6 +166,9 @@ bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) const {
165166
if (VL.isIdenticalTo(SrcVL) || !isVLKnownLE(VL, SrcVL))
166167
return false;
167168

169+
if (!ensureDominates(VL, *Src))
170+
return false;
171+
168172
if (VL.isImm())
169173
SrcVL.ChangeToImmediate(VL.getImm());
170174
else if (VL.isReg())
@@ -456,6 +460,26 @@ static bool dominates(MachineBasicBlock::const_iterator A,
456460
return &*I == A;
457461
}
458462

463+
/// If the register in \p MO doesn't dominate \p Src, try to move \p Src so it
464+
/// does. Returns false if doesn't dominate and we can't move. \p MO must be in
465+
/// the same basic block as \Src.
466+
bool RISCVVectorPeephole::ensureDominates(const MachineOperand &MO,
467+
MachineInstr &Src) const {
468+
assert(MO.getParent()->getParent() == Src.getParent());
469+
if (!MO.isReg() || MO.getReg() == RISCV::NoRegister)
470+
return true;
471+
472+
MachineInstr *Def = MRI->getVRegDef(MO.getReg());
473+
if (Def->getParent() == Src.getParent() && !dominates(Def, Src)) {
474+
if (!isSafeToMove(Src, *Def->getNextNode()))
475+
return false;
476+
// FIXME: Update V0Defs
477+
Src.moveBefore(Def->getNextNode());
478+
}
479+
480+
return true;
481+
}
482+
459483
/// If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
460484
/// into it.
461485
///
@@ -501,15 +525,8 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
501525
return false;
502526

503527
// If the new passthru doesn't dominate Src, try to move Src so it does.
504-
if (Passthru.getReg() != RISCV::NoRegister) {
505-
MachineInstr *PassthruDef = MRI->getVRegDef(Passthru.getReg());
506-
if (PassthruDef->getParent() == Src->getParent() &&
507-
!dominates(PassthruDef, Src)) {
508-
if (!isSafeToMove(*Src, *PassthruDef->getNextNode()))
509-
return false;
510-
Src->moveBefore(PassthruDef->getNextNode());
511-
}
512-
}
528+
if (!ensureDominates(Passthru, *Src))
529+
return false;
513530

514531
if (SrcPassthru.getReg() != Passthru.getReg()) {
515532
SrcPassthru.setReg(Passthru.getReg());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -mtriple=riscv64 -mattr=+v -verify-machineinstrs | FileCheck %s
3+
4+
define void @avl_not_dominated(<vscale x 2 x i32> %v, ptr %p) {
5+
; CHECK-LABEL: avl_not_dominated:
6+
; CHECK: # %bb.0:
7+
; CHECK-NEXT: vsetivli zero, 1, e32, m1, ta, ma
8+
; CHECK-NEXT: vmv.x.s a1, v8
9+
; CHECK-NEXT: slli a1, a1, 32
10+
; CHECK-NEXT: srli a1, a1, 32
11+
; CHECK-NEXT: vsetvli zero, a1, e32, m1, ta, ma
12+
; CHECK-NEXT: vadd.vi v8, v8, 1
13+
; CHECK-NEXT: vse32.v v8, (a0)
14+
; CHECK-NEXT: ret
15+
%w = add <vscale x 2 x i32> %v, splat (i32 1)
16+
%evl = extractelement <vscale x 2 x i32> %v, i32 0
17+
call void @llvm.vp.store(<vscale x 2 x i32> %w, ptr %p, <vscale x 2 x i1> splat(i1 true), i32 %evl)
18+
ret void
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc %s -o - -mtriple=riscv64 -mattr=+v -run-pass=riscv-vector-peephole \
3+
# RUN: -verify-machineinstrs | FileCheck %s
4+
---
5+
name: avl_not_dominated
6+
body: |
7+
bb.0:
8+
; CHECK-LABEL: name: avl_not_dominated
9+
; CHECK: %evl:gprnox0 = ADDI $x0, 1
10+
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, %evl, 5 /* e32 */, 0 /* tu, mu */
11+
; CHECK-NEXT: PseudoVSE32_V_M1 %x, $noreg, %evl, 5 /* e32 */
12+
%x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
13+
%evl:gprnox0 = ADDI $x0, 1
14+
PseudoVSE32_V_M1 %x:vr, $noreg, %evl, 5 /* e32 */
15+
...

0 commit comments

Comments
 (0)