public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-5415] RISC-V: Add probability model of each block to prevent endless loop of Phase 3
@ 2023-01-26 19:12 Kito Cheng
  0 siblings, 0 replies; only message in thread
From: Kito Cheng @ 2023-01-26 19:12 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:acc10c793127d5683b19158fd89fd0d4f4fc9db0

commit r13-5415-gacc10c793127d5683b19158fd89fd0d4f4fc9db0
Author: Ju-Zhe Zhong <juzhe.zhong@rivai.ai>
Date:   Tue Jan 10 07:17:20 2023 +0800

    RISC-V: Add probability model of each block to prevent endless loop of Phase 3
    
    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.

Diff:
---
 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 2f9de9d2026..02bdfebbb58 100644
--- a/gcc/config/riscv/riscv-vsetvl.cc
+++ b/gcc/config/riscv/riscv-vsetvl.cc
@@ -1466,6 +1466,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 ();
 	}
     }
 }
@@ -1643,6 +1644,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");
     }
 
@@ -1765,6 +1768,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) {}
@@ -2625,6 +2629,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 7b56802faec..ef1064e11bd 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;
 };

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-26 19:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-26 19:12 [gcc r13-5415] RISC-V: Add probability model of each block to prevent endless loop of Phase 3 Kito Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).