On 3/27/23 00:59, juzhe.zh...@rivai.ai wrote:
From: Juzhe-Zhong <juzhe.zh...@rivai.ai>

         PR 108270

Fix bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108270.

Consider the following testcase:
void f (void * restrict in, void * restrict out, int l, int n, int m)
{
   for (int i = 0; i < l; i++){
     for (int j = 0; j < m; j++){
       for (int k = 0; k < n; k++)
         {
           vint8mf8_t v = __riscv_vle8_v_i8mf8 (in + i + j, 17);
           __riscv_vse8_v_i8mf8 (out + i + j, v, 17);
         }
     }
   }
}

Compile option: -O3

Before this patch:
        mv      a7,a2
        mv      a6,a0   
         mv     t1,a1
        mv      a2,a3
        vsetivli        zero,17,e8,mf8,ta,ma
...

After this patch:
         mv      a7,a2
         mv      a6,a0
         mv      t1,a1
         mv      a2,a3
         ble     a7,zero,.L1
         ble     a4,zero,.L1
         ble     a3,zero,.L1
         add     a1,a0,a4
         li      a0,0
         vsetivli        zero,17,e8,mf8,ta,ma
...

It will produce potential bug when:

int main ()
{
   vsetivli zero, 100,.....
   f (in, out, 0,0,0)
   asm volatile ("csrr a0,vl":::"memory");

   // Before this patch the a0 is 17. (Wrong).
   // After this patch the a0 is 100. (Correct).
   ...
}

gcc/ChangeLog:

         * config/riscv/riscv-vsetvl.cc 
(vector_infos_manager::all_empty_predecessor_p): New function.
         (pass_vsetvl::backward_demand_fusion): Fix bug.
         * config/riscv/riscv-vsetvl.h: New function declare.

gcc/testsuite/ChangeLog:

         * gcc.target/riscv/rvv/vsetvl/imm_bb_prop-1.c: Adapt test.
         * gcc.target/riscv/rvv/vsetvl/imm_conflict-3.c: Adapt test.
         * gcc.target/riscv/rvv/vsetvl/pr108270.c: New test.
I've largely figured this out. But I'd still recommend we wait for gcc-14. The BZ is a missed optimization (poor placement of the vsetvl). We can address is with your patch once gcc-13 branches.

Thanks for walking my through the implementation details.

Jeff

Reply via email to