[llvm] 354530f - [RISCV] Prevent vsetvli insertion from deleting some vsetvli instructions

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 15 15:18:55 PDT 2023


Author: Craig Topper
Date: 2023-06-15T15:18:47-07:00
New Revision: 354530fe198dff5ead0965e1df8a17426c5def5e

URL: https://github.com/llvm/llvm-project/commit/354530fe198dff5ead0965e1df8a17426c5def5e
DIFF: https://github.com/llvm/llvm-project/commit/354530fe198dff5ead0965e1df8a17426c5def5e.diff

LOG: [RISCV] Prevent vsetvli insertion from deleting some vsetvli instructions

If the result register is used, it is not safe to delete.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D153076

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
    llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index 7a3e313747942..976f22eab587f 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -1444,7 +1444,11 @@ void RISCVInsertVSETVLI::doLocalPostpass(MachineBasicBlock &MBB) {
           MI.setDesc(NextMI->getDesc());
         }
         MI.getOperand(2).setImm(NextMI->getOperand(2).getImm());
-        ToDelete.push_back(NextMI);
+        // Don't delete a vsetvli if its result might be used.
+        Register NextVRefDef = NextMI->getOperand(0).getReg();
+        if (NextVRefDef == RISCV::X0 ||
+            (NextVRefDef.isVirtual() && MRI->use_nodbg_empty(NextVRefDef)))
+          ToDelete.push_back(NextMI);
         // fallthrough
       }
     }

diff  --git a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll
index 7a1b518b29af0..9c2582299a290 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll
@@ -578,6 +578,23 @@ entry:
   ret <vscale x 1 x double> %2
 }
 
+; This used to fail the machine verifier due to the vsetvli being removed
+; while the add was still using it.
+define i64 @bad_removal(<2 x i64> %arg) {
+; CHECK-LABEL: bad_removal:
+; CHECK:       # %bb.0: # %bb
+; CHECK-NEXT:    vsetivli zero, 16, e64, m1, ta, ma
+; CHECK-NEXT:    vmv.x.s a0, v8
+; CHECK-NEXT:    vsetivli a1, 16, e64, m1, ta, ma
+; CHECK-NEXT:    add a0, a0, a1
+; CHECK-NEXT:    ret
+bb:
+  %tmp = extractelement <2 x i64> %arg, i64 0
+  %tmp1 = call i64 @llvm.riscv.vsetvli.i64(i64 16, i64 3, i64 0)
+  %tmp2 = add i64 %tmp, %tmp1
+  ret i64 %tmp2
+}
+
 declare <vscale x 1 x i64> @llvm.riscv.vadd.mask.nxv1i64.nxv1i64(
   <vscale x 1 x i64>,
   <vscale x 1 x i64>,


        


More information about the llvm-commits mailing list