From fcbbfebef6affea547cc4201c9f4722148a03719 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Tue, 30 Jul 2024 16:35:28 +0800 Subject: [PATCH] [RISCV] Fix vmerge.vvm/vmv.v.v getting folded into ops with mismatching EEW As noted in https://github.com/llvm/llvm-project/pull/100367/files#r1695448771, we currently fold in vmerge.vvms and vmv.v.vs into their ops even if the EEW is different. This is incorrect if we end up changing the mask or AVL of the op. This gets the op's EEW via its simple value type for now since there doesn't seem to be any existing information about the EEW size of instructions. We'll probably need to encode this at some point if we want to be able to access it at the MachineInstr level in #100367 --- llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp | 10 +++++++++- .../CodeGen/RISCV/rvv/rvv-peephole-vmerge-vops.ll | 12 ++++++++---- llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll | 6 ++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp index 4418905ce21ed..fc0238e4892d6 100644 --- a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp @@ -3855,11 +3855,19 @@ bool RISCVDAGToDAGISel::performCombineVMergeAndVOps(SDNode *N) { // If we end up changing the VL or mask of True, then we need to make sure it // doesn't raise any observable fp exceptions, since changing the active // elements will affect how fflags is set. - if (TrueVL != VL || !IsMasked) + if (TrueVL != VL || !IsMasked) { if (mayRaiseFPException(True.getNode()) && !True->getFlags().hasNoFPExcept()) return false; + // If the EEW of True is different from vmerge's SEW, then we cannot change + // the VL or mask. + if (Log2_64(True.getSimpleValueType().getScalarSizeInBits()) != + N->getConstantOperandVal( + RISCVII::getSEWOpNum(TII->get(N->getMachineOpcode())) - 1)) + return false; + } + SDLoc DL(N); // From the preconditions we checked above, we know the mask and thus glue diff --git a/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-vops.ll b/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-vops.ll index ddf83d87cea6c..c0fb675cb991f 100644 --- a/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-vops.ll +++ b/llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-vops.ll @@ -1200,8 +1200,10 @@ define @true_mask_vmerge_implicit_passthru( define @unfoldable_mismatched_sew_mask( %passthru, %x, %y, %mask, i64 %avl) { ; CHECK-LABEL: unfoldable_mismatched_sew_mask: ; CHECK: # %bb.0: -; CHECK-NEXT: vsetvli zero, a0, e64, m1, tu, mu -; CHECK-NEXT: vadd.vv v8, v9, v10, v0.t +; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, ma +; CHECK-NEXT: vadd.vv v9, v9, v10 +; CHECK-NEXT: vsetvli zero, a0, e32, m1, tu, ma +; CHECK-NEXT: vmerge.vvm v8, v8, v9, v0 ; CHECK-NEXT: ret %a = call @llvm.riscv.vadd.nxv1i64.nxv1i64( poison, %x, %y, i64 %avl) %a.bitcast = bitcast %a to @@ -1218,8 +1220,10 @@ define @unfoldable_mismatched_sew_mask( %pa define @unfoldable_mismatched_sew_avl( %passthru, %x, %y) { ; CHECK-LABEL: unfoldable_mismatched_sew_avl: ; CHECK: # %bb.0: -; CHECK-NEXT: vsetivli zero, 3, e64, m1, tu, ma -; CHECK-NEXT: vadd.vv v8, v9, v10 +; CHECK-NEXT: vsetivli zero, 5, e64, m1, ta, ma +; CHECK-NEXT: vadd.vv v9, v9, v10 +; CHECK-NEXT: vsetivli zero, 3, e32, m1, tu, ma +; CHECK-NEXT: vmv.v.v v8, v9 ; CHECK-NEXT: ret %a = call @llvm.riscv.vadd.nxv1i64.nxv1i64( poison, %x, %y, i64 5) %a.bitcast = bitcast %a to diff --git a/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll b/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll index 65ee91a0e1907..4940e652170ab 100644 --- a/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll +++ b/llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll @@ -184,8 +184,10 @@ define @unfoldable_vredsum( %passthru, @unfoldable_mismatched_sew_diff_vl( %passthru, %x, %y) { ; CHECK-LABEL: unfoldable_mismatched_sew_diff_vl: ; CHECK: # %bb.0: -; CHECK-NEXT: vsetivli zero, 3, e64, m1, tu, ma -; CHECK-NEXT: vadd.vv v8, v9, v10 +; CHECK-NEXT: vsetivli zero, 6, e64, m1, ta, ma +; CHECK-NEXT: vadd.vv v9, v9, v10 +; CHECK-NEXT: vsetivli zero, 3, e32, m1, tu, ma +; CHECK-NEXT: vmv.v.v v8, v9 ; CHECK-NEXT: ret %a = call @llvm.riscv.vadd.nxv1i64.nxv1i64( poison, %x, %y, iXLen 6) %a.bitcast = bitcast %a to