diff mbox series

[RFA] gcc: fix PR rtl-optimization/107482

Message ID 20221107094645.3718427-1-jcmvbkbc@gmail.com
State New
Headers show
Series [RFA] gcc: fix PR rtl-optimization/107482 | expand

Commit Message

Max Filippov Nov. 7, 2022, 9:46 a.m. UTC
gcc/
	* ira-color.cc (update_costs_from_allocno): Check that allocno
	is in the consideration_allocno_bitmap before dereferencing
	ALLOCNO_COLOR_DATA (allocno).
---
This fixes the invalid memory access, but I'm not sure if that's
sufficient and there's no remaining higher level logical issue.

Regtested for target=xtensa-linux-uclibc, no new regressions.

 gcc/ira-color.cc | 2 ++
 1 file changed, 2 insertions(+)

Comments

Vladimir Makarov Nov. 7, 2022, 8:52 p.m. UTC | #1
On 2022-11-07 04:46, Max Filippov wrote:
> gcc/
> 	* ira-color.cc (update_costs_from_allocno): Check that allocno
> 	is in the consideration_allocno_bitmap before dereferencing
> 	ALLOCNO_COLOR_DATA (allocno).
> ---
> This fixes the invalid memory access, but I'm not sure if that's
> sufficient and there's no remaining higher level logical issue.

Thank you for reporting and working on this issue.

I believe your approach is sufficient.  Although the patch could be 
improved by three ways:

The simplest one is to move consideration allocno check out of loop by 
using the following patch

diff --git a/gcc/ira-color.cc b/gcc/ira-color.cc
index 4a1a325e8e3..a8e52b6b265 100644
--- a/gcc/ira-color.cc
+++ b/gcc/ira-color.cc
@@ -1413,7 +1413,9 @@ update_costs_from_allocno (ira_allocno_t allocno, 
int hard_regno,
    ira_copy_t cp, next_cp;

    rclass = REGNO_REG_CLASS (hard_regno);
-  do
+  if (!bitmap_bit_p (consideration_allocno_bitmap, ALLOCNO_NUM (allocno)))
+    return;
+  do
      {
        mode = ALLOCNO_MODE (allocno);
        ira_init_register_move_cost_if_necessary (mode);


or by even better patch:

diff --git a/gcc/ira-color.cc b/gcc/ira-color.cc
index 4a1a325e8e3..ffe73b61c45 100644
--- a/gcc/ira-color.cc
+++ b/gcc/ira-color.cc
@@ -2209,8 +2209,8 @@ assign_hard_reg (ira_allocno_t a, bool retry_p)
      restore_costs_from_copies (a);
    ALLOCNO_HARD_REGNO (a) = best_hard_regno;
    ALLOCNO_ASSIGNED_P (a) = true;
-  if (best_hard_regno >= 0)
-    update_costs_from_copies (a, true, ! retry_p);
+  if (best_hard_regno >= 0 && !retry_p)
+    update_costs_from_copies (a, true, true);
    ira_assert (ALLOCNO_CLASS (a) == aclass);
    /* We don't need updated costs anymore.  */
    ira_free_allocno_updated_costs (a);

Probably the best way would be to allocate and set up data for new 
allocnos of pseudos created on the borders of the allocation regions.  
But it is too complicated and I am not sure it will give some visible 
performance improvement.

So I'd prefer the second patch with change in assign_hard_reg.

Please, check that my proposed patch works and commit it in the case of 
success.

Thank you.

> Regtested for target=xtensa-linux-uclibc, no new regressions.
>
>   gcc/ira-color.cc | 2 ++
>   1 file changed, 2 insertions(+)
>
> diff --git a/gcc/ira-color.cc b/gcc/ira-color.cc
> index 4a1a325e8e31..4527eab39bb7 100644
> --- a/gcc/ira-color.cc
> +++ b/gcc/ira-color.cc
> @@ -1434,6 +1434,8 @@ update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
>   
>   	  if (another_allocno == from
>   	      || (ALLOCNO_COLOR_DATA (another_allocno) != NULL
> +		  && bitmap_bit_p (consideration_allocno_bitmap,
> +				   ALLOCNO_NUM (allocno))
>   		  && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
>   		      != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno)))
>   	    continue;
Max Filippov Nov. 8, 2022, 10:05 a.m. UTC | #2
On Mon, Nov 7, 2022 at 12:52 PM Vladimir Makarov <vmakarov@redhat.com> wrote:
> even better patch:
>
> diff --git a/gcc/ira-color.cc b/gcc/ira-color.cc
> index 4a1a325e8e3..ffe73b61c45 100644
> --- a/gcc/ira-color.cc
> +++ b/gcc/ira-color.cc
> @@ -2209,8 +2209,8 @@ assign_hard_reg (ira_allocno_t a, bool retry_p)
>       restore_costs_from_copies (a);
>     ALLOCNO_HARD_REGNO (a) = best_hard_regno;
>     ALLOCNO_ASSIGNED_P (a) = true;
> -  if (best_hard_regno >= 0)
> -    update_costs_from_copies (a, true, ! retry_p);
> +  if (best_hard_regno >= 0 && !retry_p)
> +    update_costs_from_copies (a, true, true);
>     ira_assert (ALLOCNO_CLASS (a) == aclass);
>     /* We don't need updated costs anymore.  */
>     ira_free_allocno_updated_costs (a);
>
...
> Please, check that my proposed patch works and commit it in the case of
> success.

Thank you for taking a look and suggesting a better fix.
I've tested your version for target=xtensa-linux-uclibc, it fixes
the issue without new regressions. I've committed the fix to the
master branch and will backport it to gcc-10, -11 and -12 in a few
days.
diff mbox series

Patch

diff --git a/gcc/ira-color.cc b/gcc/ira-color.cc
index 4a1a325e8e31..4527eab39bb7 100644
--- a/gcc/ira-color.cc
+++ b/gcc/ira-color.cc
@@ -1434,6 +1434,8 @@  update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
 
 	  if (another_allocno == from
 	      || (ALLOCNO_COLOR_DATA (another_allocno) != NULL
+		  && bitmap_bit_p (consideration_allocno_bitmap,
+				   ALLOCNO_NUM (allocno))
 		  && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
 		      != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno)))
 	    continue;