public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Aldy Hernandez <aldyh@redhat.com>
To: Richard Biener <richard.guenther@gmail.com>,
	Andrew MacLeod <amacleod@redhat.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] PR tree-optimization/108697 - Create a lazy ssa_cache
Date: Thu, 16 Feb 2023 10:36:18 +0100	[thread overview]
Message-ID: <b66347af-1404-41f2-0b6a-7b096ca6117f@redhat.com> (raw)
In-Reply-To: <CAFiYyc2CNCsEdA9WqWDEuizYVNtPsPJ4qBvOx6wZbQbSF_ijTQ@mail.gmail.com>



On 2/16/23 08:55, Richard Biener wrote:
> On Wed, Feb 15, 2023 at 6:07 PM Andrew MacLeod via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>>
>> This patch implements the suggestion that we have an alternative
>> ssa-cache which does not zero memory, and instead uses a bitmap to track
>> whether a value is currently set or not.  It roughly mimics what
>> path_range_query was doing internally.
>>
>> For sparsely used cases, expecially in large programs, this is more
>> efficient.  I changed path_range_query to use this, and removed it old
>> bitmap (and a hack or two around PHI calculations), and also utilized
>> this is the assume_query class.
>>
>> Performance wise, the patch doesn't affect VRP (since that still uses
>> the original version).  Switching to the lazy version caused a slowdown
>> of 2.5% across VRP.
>>
>> There was a noticeable improvement elsewhere.,  across 230 GCC source
>> files, threading ran over 12% faster!.  Overall compilation improved by
>> 0.3%  Not sure it makes much difference in compiler.i, but it shouldn't
>> hurt.
>>
>> bootstraps on x86_64-pc-linux-gnu with no regressions.   OK for trunk?
>> or do you want to wait for the next release...
> 
> I see
> 
> @@ -365,16 +335,8 @@ path_range_query::compute_ranges_in_phis (basic_block bb)
> 
>         Value_Range r (TREE_TYPE (name));
>         if (range_defined_in_block (r, name, bb))
> -       {
> -         unsigned v = SSA_NAME_VERSION (name);
> -         set_cache (r, name);
> -         bitmap_set_bit (phi_set, v);
> -         // Pretend we don't have a cache entry for this name until
> -         // we're done with all PHIs.
> -         bitmap_clear_bit (m_has_cache_entry, v);
> -       }
> +       m_cache.set_global_range (name, r);
>       }
> -  bitmap_ior_into (m_has_cache_entry, phi_set);
>   }
> 
>   // Return TRUE if relations may be invalidated after crossing edge E.
> 
> which I think is not correct - if we have
> 
>   # _1 = PHI <..., _2>
>   # _2 = PHI <..., _1>
> 
> then their effects are supposed to be executed in parallel, that is,
> both PHI argument _2 and _1 are supposed to see the "old" version.
> The previous code tried to make sure the range of the new _1 doesn't
> get seen when processing the argument _1 in the definition of _2.

Yes, the effects should appear in parallel, but ssa_range_in_phi() which 
is the only thing range_defined_in_block does for PHIs, is guaranteed to 
not do any additional cache lookups.  The comment there should be 
adjusted to make this clear:

// Since PHIs are calculated in parallel at the beginning of the
// block, we must be careful to never save anything to the cache here.
// It is the caller's responsibility to adjust the cache.  Also,
// calculating the PHI's range must not trigger additional lookups.

We should instead say:

"we must be careful to never set or access the cache here"...

This was the original intent, but a subtle access to the cache crept in 
here:

       // Try to fold the phi exclusively with global or cached values.
       // This will get things like PHI <5(99), 6(88)>.  We do this by
       // calling range_of_expr with no context.
       unsigned nargs = gimple_phi_num_args (phi);
       Value_Range arg_range (TREE_TYPE (name));
       r.set_undefined ();
       for (size_t i = 0; i < nargs; ++i)
	{
	  tree arg = gimple_phi_arg_def (phi, i);
	  if (range_of_expr (arg_range, arg, /*stmt=*/NULL))

This range_of_expr call will indeed access the cache incorrectly, but 
Andrew fixed that here:

@@ -264,7 +236,7 @@ path_range_query::ssa_range_in_phi (vrange &r, gphi 
*phi)
        for (size_t i = 0; i < nargs; ++i)
  	{
  	  tree arg = gimple_phi_arg_def (phi, i);
-	  if (range_of_expr (arg_range, arg, /*stmt=*/NULL))
+	  if (m_ranger.range_of_expr (arg_range, arg, /*stmt=*/NULL))
  	    r.union_ (arg_range);
  	  else
  	    {

...thus ensuring that function never uses the cache.  All the lookups 
are done with the global ranger at either the path entry or globally as 
above (with stmt=NULL).

I believe the switch from range_of_expr to m_ranger.range_of_expr is 
safe, as the original code was added to handle silly things like PHI 
<5(99), 6(88)> which shouldn't need path aware ranges.

As you've found out, the update to the cache in this case was not 
obvious at all.  Perhaps it should also be commented:

"It is safe to set the cache here, as range_defined_in_block for PHIs 
(ssa_range_in_phi) is guaranteed not to do any cache lookups."

> 
> The new version drops this, possibly resulting in wrong-code.
> 
> While I think it's appropriate to sort out compile-time issues like this
> during stage4 at least the above makes me think it should be defered
> to next stage1.

I defer to the release managers as to whether this is safe in light of 
my explanation above :).

Aldy


  reply	other threads:[~2023-02-16  9:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-15 17:05 Andrew MacLeod
2023-02-16  7:55 ` Richard Biener
2023-02-16  9:36   ` Aldy Hernandez [this message]
2023-02-16 14:34   ` Andrew MacLeod
2023-02-17  7:54     ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b66347af-1404-41f2-0b6a-7b096ca6117f@redhat.com \
    --to=aldyh@redhat.com \
    --cc=amacleod@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).