[PATCH] RISC-V: Add probability model of each block to prevent endless loop of Phase 3

Kito Cheng kito.cheng@gmail.com
Thu Jan 26 19:16:48 GMT 2023


committed, thanks.

On Tue, Jan 10, 2023 at 7:17 AM <juzhe.zhong@rivai.ai> wrote:

> From: Ju-Zhe Zhong <juzhe.zhong@rivai.ai>
>
> Notice that the PASS is just simpily pick the probability >= 50%
> to do the backward fusion which will create endless loop on Phase 3.
>
> Adding this probability to fix this bug.
> gcc/ChangeLog:
>
>         * config/riscv/riscv-vsetvl.cc
> (vector_infos_manager::vector_infos_manager): Add probability.
>         (vector_infos_manager::dump): Ditto.
>         (pass_vsetvl::compute_probabilities): Ditto.
>         * config/riscv/riscv-vsetvl.h (struct vector_block_info): Ditto.
>
> ---
>  gcc/config/riscv/riscv-vsetvl.cc | 39 ++++++++++++++++++++++++++++++++
>  gcc/config/riscv/riscv-vsetvl.h  |  3 +++
>  2 files changed, 42 insertions(+)
>
> diff --git a/gcc/config/riscv/riscv-vsetvl.cc
> b/gcc/config/riscv/riscv-vsetvl.cc
> index 0f12d4ddb23..7d8c3a32aaa 100644
> --- a/gcc/config/riscv/riscv-vsetvl.cc
> +++ b/gcc/config/riscv/riscv-vsetvl.cc
> @@ -1465,6 +1465,7 @@ vector_infos_manager::vector_infos_manager ()
>           vector_block_infos[bb->index ()].reaching_out = vector_insn_info
> ();
>           for (insn_info *insn : bb->real_insns ())
>             vector_insn_infos[insn->uid ()].parse_insn (insn);
> +         vector_block_infos[bb->index ()].probability =
> profile_probability ();
>         }
>      }
>  }
> @@ -1642,6 +1643,8 @@ vector_infos_manager::dump (FILE *file) const
>         }
>        fprintf (file, "<FOOTER>=");
>        vector_block_infos[cfg_bb->index].reaching_out.dump (file);
> +      fprintf (file, "<Probability>=");
> +      vector_block_infos[cfg_bb->index].probability.dump (file);
>        fprintf (file, "\n\n");
>      }
>
> @@ -1764,6 +1767,7 @@ private:
>
>    void init (void);
>    void done (void);
> +  void compute_probabilities (void);
>
>  public:
>    pass_vsetvl (gcc::context *ctxt) : rtl_opt_pass (pass_data_vsetvl,
> ctxt) {}
> @@ -2629,6 +2633,41 @@ pass_vsetvl::done (void)
>    m_vector_manager = nullptr;
>  }
>
> +/* Compute probability for each block.  */
> +void
> +pass_vsetvl::compute_probabilities (void)
> +{
> +  /* Don't compute it in -O0 since we don't need it.  */
> +  if (!optimize)
> +    return;
> +  edge e;
> +  edge_iterator ei;
> +
> +  for (const bb_info *bb : crtl->ssa->bbs ())
> +    {
> +      basic_block cfg_bb = bb->cfg_bb ();
> +      auto &curr_prob
> +       = m_vector_manager->vector_block_infos[cfg_bb->index].probability;
> +      if (ENTRY_BLOCK_PTR_FOR_FN (cfun) == cfg_bb)
> +       curr_prob = profile_probability::always ();
> +      gcc_assert (curr_prob.initialized_p ());
> +      FOR_EACH_EDGE (e, ei, cfg_bb->succs)
> +       {
> +         auto &new_prob
> +           =
> m_vector_manager->vector_block_infos[e->dest->index].probability;
> +         if (!new_prob.initialized_p ())
> +           new_prob = curr_prob * e->probability;
> +         else if (new_prob == profile_probability::always ())
> +           continue;
> +         else
> +           new_prob += curr_prob * e->probability;
> +       }
> +    }
> +  auto &exit_block
> +    = m_vector_manager->vector_block_infos[EXIT_BLOCK_PTR_FOR_FN
> (cfun)->index];
> +  exit_block.probability = profile_probability::always ();
> +}
> +
>  /* Lazy vsetvl insertion for optimize > 0. */
>  void
>  pass_vsetvl::lazy_vsetvl (void)
> diff --git a/gcc/config/riscv/riscv-vsetvl.h
> b/gcc/config/riscv/riscv-vsetvl.h
> index 563ad3084ed..fb3ebb9db79 100644
> --- a/gcc/config/riscv/riscv-vsetvl.h
> +++ b/gcc/config/riscv/riscv-vsetvl.h
> @@ -291,6 +291,9 @@ struct vector_block_info
>    /* The reaching_out vector insn_info of the block.  */
>    vector_insn_info reaching_out;
>
> +  /* The static execute probability of the demand info.  */
> +  profile_probability probability;
> +
>    vector_block_info () = default;
>  };
>
> --
> 2.36.1
>
>


More information about the Gcc-patches mailing list