https://gcc.gnu.org/g:78d2ef518863a02e9a0d0edcef6e6ecf58b06f85

commit r17-6504-g78d2ef518863a02e9a0d0edcef6e6ecf58b06f85
Author: Bohan Lei <[email protected]>
Date:   Fri Nov 20 15:49:03 2026 -0700

    [PATCH v2] RISC-V: Hoist VXRM assignments despite calls
    
    We found in x264, after the LTO patches, some pixel_avg related
    functions get inlined into their callers and VXRM can no longer be
    hoisted to the function epilogue.  This patch relaxes the hoist
    condition so that it can hoist VXRM writes even if the function has
    calls.  We can ignore the function calls in singleton_vxrm_need, because
    even if we insert a redundant VXRM assignment at the start, later DCE
    can remove that.  There should be no correctness issue either as the
    regular LCM algorithm always works.  This version was suggested by Jeff.
    The test expectations were written by LLM.  Regtested on rv64gcv and
    rv32gcv.
    
    Assisted-by: Codex
    
            PR target/127086
    
    gcc/ChangeLog:
    
            * config/riscv/riscv.cc (singleton_vxrm_need): Allow calls to be
            ignored.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/rvv/autovec/pr127086.c: New test.

Diff:
---
 gcc/config/riscv/riscv.cc                          |  5 ++-
 .../gcc.target/riscv/rvv/autovec/pr127086.c        | 44 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 384365299e31..2e22e24de4db 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -14342,7 +14342,10 @@ singleton_vxrm_need (void)
        continue;
 
       int vxrm_mode = get_attr_vxrm_mode (insn);
-      if (vxrm_mode == VXRM_MODE_NONE)
+
+      /* Even if the VXRM write at the start is redundant because there is
+        a call on every path, DCE should take care of it.  */
+      if (vxrm_mode == VXRM_MODE_NONE || vxrm_mode == VXRM_MODE_CLOBBER)
        continue;
 
       /* If this is the first VXRM need, note it.  */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr127086.c 
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr127086.c
new file mode 100644
index 000000000000..8da6bd0510d9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr127086.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O3" } */
+
+void tick (void);
+
+void
+avg_then_call (unsigned char *dst, const unsigned char *s1,
+              const unsigned char *s2, int w, int h, int ds, int ss1, int ss2)
+{
+  for (int y = 0; y < h; y++)
+    {
+      for (int x = 0; x < w; x++)
+       dst[x] = (s1[x] + s2[x] + 1) >> 1;
+      dst += ds;
+      s1 += ss1;
+      s2 += ss2;
+    }
+  tick ();
+}
+
+void
+call_then_avg (unsigned char *dst, const unsigned char *s1,
+              const unsigned char *s2, int w, int h, int ds, int ss1, int ss2)
+{
+  tick ();
+  for (int y = 0; y < h; y++)
+    {
+      for (int x = 0; x < w; x++)
+       dst[x] = (s1[x] + s2[x] + 1) >> 1;
+      dst += ds;
+      s1 += ss1;
+      s2 += ss2;
+    }
+}
+
+/* avg_then_call sets VXRM before the first local branch label, so neither
+   loop repeats the write.  Allow the prologue and zero-trip guards.  */
+/* { dg-final { scan-assembler 
{avg_then_call:\n(?:(?!\.L[0-9]+:)[^\n]*\n)*?\tcsrwi\tvxrm,0\n} } } */
+
+/* call_then_avg establishes VXRM after tick for the vector average.  */
+/* { dg-final { scan-assembler 
{call_then_avg:\n(?:[^\n]*\n)*?\tcall\ttick(?:@plt)?\n(?:[^\n]*\n)*?\tcsrwi\tvxrm,0\n}
 } } */
+
+/* The two required writes account for every VXRM write in this file.  */
+/* { dg-final { scan-assembler-times {\tcsrwi\tvxrm,} 2 } } */

Reply via email to