[llvm] [MachineSink] Only call isConstantPhysReg or isIgnorableUse for uses. (PR #99363)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 17 10:49:48 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/99363

The included test case contains X0 as a def register. X0 is considered a constant register when it is a use. When its a def, it means to throw away the result value.

If we treat it as a constant register here, we will execute the continue and not assign DefReg to any register. This will cause a crash when trying to get the register class for DefReg after the loop.

By only checking isConstantPhysReg for uses, we will reach the return false a little further down and stop processing this instruction.

>From e74b3dc3beda0fa2d67150ec1587955a09ec6be1 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 17 Jul 2024 10:38:11 -0700
Subject: [PATCH] [MachineSink] Only call isConstantPhysReg or isIgnorableUse
 for uses.

The included test case contains X0 as a def register. X0 is considered a
constant register when it is a use. When its a def, it means to
throw away the result value.

If we treat it as a constant register here, we will execute the continue
and not assign DefReg to any register. This will cause a crash
when trying to get the register class for DefReg after the loop.

By only checking isConstantPhysReg for uses, we will reach the
return false a little further down and stop processing this instruction.
---
 llvm/lib/CodeGen/MachineSink.cpp              |  2 +-
 .../CodeGen/RISCV/sink-and-fold-crash.mir     | 39 +++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/RISCV/sink-and-fold-crash.mir

diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp
index d782c8b086319..4b3ff57fb478a 100644
--- a/llvm/lib/CodeGen/MachineSink.cpp
+++ b/llvm/lib/CodeGen/MachineSink.cpp
@@ -417,7 +417,7 @@ bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
       continue;
     }
 
-    if (Reg.isPhysical() &&
+    if (Reg.isPhysical() && MO.isUse() &&
         (MRI->isConstantPhysReg(Reg) || TII->isIgnorableUse(MO)))
       continue;
 
diff --git a/llvm/test/CodeGen/RISCV/sink-and-fold-crash.mir b/llvm/test/CodeGen/RISCV/sink-and-fold-crash.mir
new file mode 100644
index 0000000000000..c109f306b52d7
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/sink-and-fold-crash.mir
@@ -0,0 +1,39 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc %s -mtriple=riscv64 -run-pass=machine-sink | FileCheck %s
+
+---
+name:            main
+tracksRegLiveness: true
+registers:
+  - { id: 0, class: fpr32 }
+  - { id: 1, class: fpr32 }
+  - { id: 2, class: gpr }
+  - { id: 3, class: gpr }
+liveins:
+  - { reg: '$f10_f', virtual-reg: '%0' }
+  - { reg: '$f11_f', virtual-reg: '%1' }
+body:             |
+  bb.0.entry:
+    liveins: $f10_f, $f11_f
+
+    ; CHECK-LABEL: name: main
+    ; CHECK: liveins: $f10_f, $f11_f
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:fpr32 = COPY $f11_f
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:fpr32 = COPY $f10_f
+    ; CHECK-NEXT: [[ReadFFLAGS:%[0-9]+]]:gpr = ReadFFLAGS implicit $fflags
+    ; CHECK-NEXT: [[FLE_S:%[0-9]+]]:gpr = nofpexcept FLE_S [[COPY1]], [[COPY]]
+    ; CHECK-NEXT: WriteFFLAGS killed [[ReadFFLAGS]], implicit-def $fflags
+    ; CHECK-NEXT: $x0 = nofpexcept FEQ_S [[COPY1]], [[COPY]]
+    ; CHECK-NEXT: $x10 = COPY [[FLE_S]]
+    ; CHECK-NEXT: PseudoRET implicit $x10
+    %1:fpr32 = COPY $f11_f
+    %0:fpr32 = COPY $f10_f
+    %3:gpr = ReadFFLAGS implicit $fflags
+    %2:gpr = nofpexcept FLE_S %0, %1
+    WriteFFLAGS killed %3, implicit-def $fflags
+    $x0 = nofpexcept FEQ_S %0, %1
+    $x10 = COPY %2
+    PseudoRET implicit $x10
+
+...



More information about the llvm-commits mailing list