[llvm] [RISCV] Preserve tail agnostic policy in some vector peepholes (PR #105788)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 22 23:40:06 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: Luke Lau (lukel97)
<details>
<summary>Changes</summary>
This patch helps avoid regressions in an upcoming patch by making sure we don't accidentally lose a tail agnostic policy when doing some peepholes. There are two places this happens:
1. When converting a vmerge.vvm with an all ones mask to a vmv.v.v, if vmerge's passthru was undefined the tail will always be undef, even if the new passthru (vmerge's old false operand) isn't undef. We can use ta here.
2. When folding a vmv.v.v into its source, if the vmv.v.v had a ta policy we can copy it over, provided that we kept the same VL.
---
Full diff: https://github.com/llvm/llvm-project/pull/105788.diff
3 Files Affected:
- (modified) llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp (+10-6)
- (modified) llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-to-vmv.mir (+1-1)
- (added) llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir (+18)
``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
index 9772782ad3d6db..2b6ea66d0021e2 100644
--- a/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
@@ -362,8 +362,10 @@ bool RISCVVectorPeephole::convertVMergeToVMv(MachineInstr &MI) const {
MI.removeOperand(1); // Passthru operand
MI.tieOperands(0, 1); // Tie false to dest
MI.removeOperand(3); // Mask operand
- MI.addOperand(
- MachineOperand::CreateImm(RISCVII::TAIL_UNDISTURBED_MASK_UNDISTURBED));
+ int64_t Policy = RISCVII::TAIL_UNDISTURBED_MASK_UNDISTURBED;
+ if (PassthruReg == RISCV::NoRegister)
+ Policy |= RISCVII::TAIL_AGNOSTIC;
+ MI.addOperand(MachineOperand::CreateImm(Policy));
// vmv.v.v doesn't have a mask operand, so we may be able to inflate the
// register class for the destination and passthru operands e.g. VRNoV0 -> VR
@@ -498,10 +500,12 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
*Src->getParent()->getParent()));
}
- // Use a conservative tu,mu policy, RISCVInsertVSETVLI will relax it if
- // passthru is undef.
- Src->getOperand(RISCVII::getVecPolicyOpNum(Src->getDesc()))
- .setImm(RISCVII::TAIL_UNDISTURBED_MASK_UNDISTURBED);
+ // If MI had a ta policy and the VL didn't increase, we can preserve it.
+ int64_t Policy = RISCVII::TAIL_UNDISTURBED_MASK_UNDISTURBED;
+ if ((MI.getOperand(5).getImm() & RISCVII::TAIL_AGNOSTIC) &&
+ isVLKnownLE(MI.getOperand(3), SrcVL))
+ Policy |= RISCVII::TAIL_AGNOSTIC;
+ Src->getOperand(RISCVII::getVecPolicyOpNum(Src->getDesc())).setImm(Policy);
MRI->replaceRegWith(MI.getOperand(0).getReg(), Src->getOperand(0).getReg());
MI.eraseFromParent();
diff --git a/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-to-vmv.mir b/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-to-vmv.mir
index 01fff3de0aa8bd..9080fcfb0f0420 100644
--- a/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-to-vmv.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-to-vmv.mir
@@ -15,7 +15,7 @@ body: |
; CHECK-NEXT: %avl:gprnox0 = COPY $x1
; CHECK-NEXT: %mask:vmv0 = PseudoVMSET_M_B8 %avl, 5 /* e32 */
; CHECK-NEXT: $v0 = COPY %mask
- ; CHECK-NEXT: %x:vr = PseudoVMV_V_V_M1 %false, %true, %avl, 5 /* e32 */, 0 /* tu, mu */
+ ; CHECK-NEXT: %x:vr = PseudoVMV_V_V_M1 %false, %true, %avl, 5 /* e32 */, 1 /* ta, mu */
%false:vr = COPY $v8
%true:vr = COPY $v9
%avl:gprnox0 = COPY $x1
diff --git a/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir b/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir
new file mode 100644
index 00000000000000..a0edfa96d62485
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir
@@ -0,0 +1,18 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
+# RUN: llc %s -o - -mtriple=riscv64 -mattr=+v -run-pass=riscv-vector-peephole \
+# RUN: -verify-machineinstrs | FileCheck %s
+
+---
+name: tail_agnostic
+body: |
+ bb.0:
+ liveins: $v8
+ ; CHECK-LABEL: name: tail_agnostic
+ ; CHECK: liveins: $v8
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %passthru:vr = COPY $v8
+ ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 1 /* ta, mu */
+ %passthru:vr = COPY $v8
+ %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
+ %y:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5 /* e32 */, 1 /* ta, mu */
+...
``````````
</details>
https://github.com/llvm/llvm-project/pull/105788
More information about the llvm-commits
mailing list