The current behavior of riscv_class_max_nregs is returning 0 for unknown
classes including ALL_REGS. But for non-FP targets GR_REGS = ALL_REGS
in context of register allocation (because all allocatable registers are general
purpose in such case). That cause allocator to treat ALL_REGS as subset of
GR_REGS meaning ALL_REGS is accounted when calculating
ira_reg_class_min_nregs for GR_REGS.
 
In current implementation it results in GR_REGS being non-uniform as its
max_nregs is 1 (or 2) and its subclass' (ALL_REGS) max_nregs is 0,
and then ultimately in inability to use inheritance in reload pass.
 
On the other hand, when floating-point extensions are active,
ALL_REGS is strict superset of both GR_REGS and FP_REGS which
grants expected behavior of integer optimizations.
 
The bug can be revealed by compiling a function using a big number of GP
registers with -march=rv64im and then with -march=rv64imfd and comparing
the output. I wrote a python script to generate an appropriate example:
 
N_VARS = 28
vars = [f"data[{i}]" for i in range(N_VARS)]
 
print(f"""
int data[{N_VARS}];
 
void f() {{
""")
for i in range(N_VARS - 1):
    print(f"    {vars[i+1]} *= {vars[i]};")
for i in range(N_VARS - 1, 0, -1):
    print(f"    {vars[i-1]} *= {vars[i]};")
print("}")
 
It yields a long series of multiplications like:
    data[1] *= data[0];
    data[2] *= data[1];
    // ...
    data[26] *= data[25];
    data[27] *= data[26];
    data[26] *= data[27];
    // ...
    data[0] *= data[1];
 
If we compare results of invoking gcc with thoses 2 sets of options:
riscv64-unknown-elf-gcc -march=rv64im -mabi=lp64 -O3 -S test.c
riscv64-unknown-elf-gcc -march=rv64imfd -mabi=lp64d -O3 -S test.c
We will find out that outputs differ and the former has extra memory
instructions (2 more ld).
 
This patch fixes the issue so that the variant without fd extensions emits the
same code as the variant with them. But unfortunatly I have no idea how to
integrate provided test into existing testsuit as it requires compiling the
same file 2 times and comparing the outputs, while existing tools (as far as
I can see) aim to check results of single isolated gcc invokation.
I would appreciate any hints on integrating such test.
 
gcc/ChangeLog:
 
        * config/riscv/riscv.cc (riscv_class_max_nregs):
                Change return value for unknown classes.
---
 gcc/config/riscv/riscv.cc | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
 
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index ec3a0342196..fe66826d3f1 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -11742,7 +11742,10 @@ riscv_class_max_nregs (reg_class_t rclass, machine_mode mode)
   if (reg_class_subset_p (rclass, V_REGS))
     return riscv_hard_regno_nregs (V_REG_FIRST, mode);
 
-  return 0;
+  return GET_MODE_SIZE (mode).is_constant ()
+    ? (GET_MODE_SIZE (mode).to_constant () + UNITS_PER_WORD - 1)
+      / UNITS_PER_WORD
+    : 0;
 }
 
 /* Implement TARGET_MEMORY_MOVE_COST.  */
-- 
2.43.0

Reply via email to