public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Sandiford <richard.sandiford@arm.com>
To: Alex Coplan <alex.coplan@arm.com>
Cc: gcc-patches@gcc.gnu.org,  Kyrylo Tkachov <kyrylo.tkachov@arm.com>,
	 Richard Earnshaw <richard.earnshaw@arm.com>
Subject: Re: [PATCH 3/3] aarch64: Fix up debug uses in ldp/stp pass [PR113089]
Date: Tue, 23 Jan 2024 12:18:26 +0000	[thread overview]
Message-ID: <mpty1cgkzu5.fsf@arm.com> (raw)
In-Reply-To: <Za7tfAH4Z03Yma2I@arm.com> (Alex Coplan's message of "Mon, 22 Jan 2024 22:34:36 +0000")

Alex Coplan <alex.coplan@arm.com> writes:
>> > +	    writeback_pats[i] = orig_rtl[i];
>> > +
>> > +      // Now that we've characterized the defs involved, go through the
>> > +      // debug uses and determine how to update them (if needed).
>> > +      for (auto use : set->debug_insn_uses ())
>> > +	{
>> > +	  if (*pair_dst < *use->insn () && defs[1])
>> > +	    // We're re-ordering defs[1] above a previous use of the
>> > +	    // same resource.
>> > +	    update_debug_use (use, defs[1], writeback_pats[1]);
>> > +	  else if (*pair_dst >= *use->insn ())
>> > +	    // We're re-ordering defs[0] below its use.
>> > +	    update_debug_use (use, defs[0], writeback_pats[0]);
>> > +	}
>> > +    }
>> > +
>> > +  // Now let's look at registers which are def'd by the second insn
>> > +  // but not by the first insn, there may still be debug uses of a
>> > +  // previous def which can be affected by moving the second insn up.
>> > +  for (auto def : insns[1]->defs ())
>> > +    {
>> > +      // This should be M log N where N is the number of defs in
>> > +      // insns[0] and M is the number of defs in insns[1].
>> > +      if (def->is_mem () || find_access (insns[0]->defs (), def->regno ()))
>> > +	  continue;
>> > +
>> > +      auto prev_set = safe_dyn_cast<set_info *> (def->prev_def ());
>> > +      if (!prev_set)
>> > +	continue;
>> > +
>> > +      rtx writeback_pat = NULL_RTX;
>> > +      if (def->regno () == base_regno && (writeback & 2))
>> > +	writeback_pat = orig_rtl[1];
>> > +
>> > +      // We have a def in insns[1] which isn't def'd by the first insn.
>> > +      // Look to the previous def and see if it has any debug uses.
>> > +      for (auto use : prev_set->debug_insn_uses ())
>> > +	if (*pair_dst < *use->insn ())
>> > +	  // We're ordering DEF above a previous use of the same register.
>> > +	  update_debug_use (use, def, writeback_pat);
>> 
>> This might be more efficient as a reverse walk that breaks as soon as
>> *pair_dst >= *use->insn (), since the previous def could be arbitrarily
>> far away and have arbitrarily many leading uses.  But it probably doesn't
>> matter much in practice.
>
> Agreed, provided it's easy to get at the last debug use in constant time

Yeah, sorry, I should have checked whether that was possible.  Like you
pointed out off-list, it isn't possible as things stand, so never mind. :)

Richard

> (if so I guess 1/3 might need updating to add a last_debug_insn_use
> accessor).  I'll look into that tomorrow.
>
>> 
>> > +    }
>> > +
>> > +  if ((writeback & 2) && !writeback_effect)
>> > +    {
>> > +      // If the second insn initially had writeback but the final
>> > +      // pair does not, then there may be trailing debug uses of the
>> > +      // second writeback def which need re-parenting: do that.
>> > +      auto def = find_access (insns[1]->defs (), base_regno);
>> > +      gcc_assert (def);
>> > +      for (auto use : as_a<set_info *> (def)->debug_insn_uses ())
>> > +	{
>> > +	  insn_change change (use->insn ());
>> > +	  change.new_uses = check_remove_regno_access (attempt,
>> > +						       change.new_uses,
>> > +						       base_regno);
>> > +	  auto new_use = find_access (insns[0]->uses (), base_regno);
>> > +
>> > +	  // N.B. insns must have already shared a common base due to writeback.
>> > +	  gcc_assert (new_use);
>> > +
>> > +	  if (dump_file)
>> > +	    fprintf (dump_file,
>> > +		     "  i%d: cancelling wb, re-parenting trailing debug use\n",
>> > +		     use->insn ()->uid ());
>> > +
>> > +	  change.new_uses = insert_access (attempt, new_use, change.new_uses);
>> > +	  crtl->ssa->change_insn (change);
>> > +	}
>> > +    }
>> > +  else if (trailing_add)
>> > +    fixup_debug_uses_trailing_add (attempt, pair_dst, trailing_add,
>> > +				   writeback_effect);
>> > +}
>> > +
>> >  // Try and actually fuse the pair given by insns I1 and I2.
>> >  //
>> >  // Here we've done enough analysis to know this is safe, we only
>> > @@ -1378,6 +1681,9 @@ ldp_bb_info::fuse_pair (bool load_p,
>> >    insn_info *first = (*i1 < *i2) ? i1 : i2;
>> >    insn_info *second = (first == i1) ? i2 : i1;
>> >  
>> > +  insn_info *pair_dst = move_range.singleton ();
>> > +  gcc_assert (pair_dst);
>> > +
>> >    insn_info *insns[2] = { first, second };
>> >  
>> >    auto_vec<insn_change *> changes;
>> > @@ -1388,6 +1694,13 @@ ldp_bb_info::fuse_pair (bool load_p,
>> >      PATTERN (second->rtl ())
>> >    };
>> >  
>> > +  // Make copies of the patterns as we might need to refer to the original RTL
>> > +  // later, for example when updating debug uses (which is after we've updated
>> > +  // one or both of the patterns in the candidate insns).
>> > +  rtx orig_rtl[2];
>> > +  for (int i = 0; i < 2; i++)
>> > +    orig_rtl[i] = copy_rtx (pats[i]);
>> > +
>> 
>> FWIW, an alternative (that avoids RTL allocation) would be to use
>> temporarily_undo_changes and redo_changes.  But this is fine too.
>
> Presumably using temporarily_undo_changes would require some bookkeping
> around the change numbers that might end up making things more
> complicated?
>
> I thought about making the copies conditional on MAY_HAVE_DEBUG_INSNS as
> that at least saves copying in the common case with no debug insns (and
> perhaps value-initializing orig_rtl to avoid it being used uninitialized
> by potential future patches).  WDYT about that?
>
>> 
>> The patch is OK from my POV with the as_a change above, but I'd be
>> interested in the answer to the writeback question above.
>
> Great, thanks a lot for the reviews!
>
> Alex
>
>> 
>> Thanks,
>> Richard
>> 
>> >    use_array input_uses[2] = { first->uses (), second->uses () };
>> >    def_array input_defs[2] = { first->defs (), second->defs () };
>> >  
>> > @@ -1604,9 +1917,7 @@ ldp_bb_info::fuse_pair (bool load_p,
>> >        using Action = stp_change_builder::action;
>> >        insn_info *store_to_change = try_repurpose_store (first, second,
>> >  							move_range);
>> > -      insn_info *stp_dest = move_range.singleton ();
>> > -      gcc_assert (stp_dest);
>> > -      stp_change_builder builder (insns, store_to_change, stp_dest);
>> > +      stp_change_builder builder (insns, store_to_change, pair_dst);
>> >        insn_change *change;
>> >        set_info *new_set = nullptr;
>> >        for (; !builder.done (); builder.advance ())
>> > @@ -1677,7 +1988,7 @@ ldp_bb_info::fuse_pair (bool load_p,
>> >  		fprintf (dump_file,
>> >  			 "  stp: changing i%d to use mem from new stp "
>> >  			 "(after i%d)\n",
>> > -			 action.insn->uid (), stp_dest->uid ());
>> > +			 action.insn->uid (), pair_dst->uid ());
>> >  	      change->new_uses = drop_memory_access (change->new_uses);
>> >  	      gcc_assert (new_set);
>> >  	      auto new_use = crtl->ssa->create_use (attempt, action.insn,
>> > @@ -1741,6 +2052,11 @@ ldp_bb_info::fuse_pair (bool load_p,
>> >  
>> >    gcc_assert (crtl->ssa->verify_insn_changes (changes));
>> >  
>> > +  // Fix up any debug uses that will be affected by the changes.
>> > +  if (MAY_HAVE_DEBUG_INSNS)
>> > +    fixup_debug_uses (attempt, insns, orig_rtl, pair_dst, trailing_add,
>> > +		      load_p, writeback, writeback_effect, base_regno);
>> > +
>> >    confirm_change_group ();
>> >    crtl->ssa->change_insns (changes);
>> >  
>> > @@ -2807,7 +3123,7 @@ try_promote_writeback (insn_info *insn)
>> >  
>> >    rtx wb_effect = NULL_RTX;
>> >    def_info *add_def;
>> > -  const insn_range_info pair_range (insn->prev_nondebug_insn ());
>> > +  const insn_range_info pair_range (insn);
>> >    insn_info *insns[2] = { nullptr, insn };
>> >    insn_info *trailing_add = find_trailing_add (insns, pair_range, 0, &wb_effect,
>> >  					       &add_def, base_def, offset,
>> > @@ -2830,14 +3146,16 @@ try_promote_writeback (insn_info *insn)
>> >  					pair_change.new_defs);
>> >    gcc_assert (pair_change.new_defs.is_valid ());
>> >  
>> > -  pair_change.move_range = insn_range_info (insn->prev_nondebug_insn ());
>> > -
>> >    auto is_changing = insn_is_changing (changes);
>> >    for (unsigned i = 0; i < ARRAY_SIZE (changes); i++)
>> >      gcc_assert (rtl_ssa::restrict_movement_ignoring (*changes[i], is_changing));
>> >  
>> >    gcc_assert (rtl_ssa::recog_ignoring (attempt, pair_change, is_changing));
>> >    gcc_assert (crtl->ssa->verify_insn_changes (changes));
>> > +
>> > +  if (MAY_HAVE_DEBUG_INSNS)
>> > +    fixup_debug_uses_trailing_add (attempt, insn, trailing_add, wb_effect);
>> > +
>> >    confirm_change_group ();
>> >    crtl->ssa->change_insns (changes);
>> >  }
>> > diff --git a/gcc/testsuite/gcc.c-torture/compile/pr113089.c b/gcc/testsuite/gcc.c-torture/compile/pr113089.c
>> > new file mode 100644
>> > index 00000000000..70c71f23f1c
>> > --- /dev/null
>> > +++ b/gcc/testsuite/gcc.c-torture/compile/pr113089.c
>> > @@ -0,0 +1,26 @@
>> > +/* { dg-do compile } */
>> > +/* { dg-options "-g -funroll-loops" } */
>> > +
>> > +typedef unsigned short uint16;
>> > +
>> > +void intrapred_chroma_plane(uint16 ***mb_preds, int* max_imgpel_values, int crx, int cry, int px) {
>> > +  for (int uv = 0; uv < 2; uv++) {
>> > +    uint16 **mb_pred = mb_preds[uv + 1];
>> > +    uint16 **predU2 = &mb_pred[px - 2];
>> > +    uint16 *upPred = &mb_pred[px][px];
>> > +    int max_imgpel_value = max_imgpel_values[uv];
>> > +
>> > +    int ih = upPred[crx - 1];
>> > +    for (int i = 0; i < crx*3; ++i)
>> > +      ih += upPred[crx*3];
>> > +
>> > +    int iv = (mb_pred[cry - 1][px+1]);
>> > +    for (int i = 0; i < cry - 1; ++i) {
>> > +      iv += (i + 1) * (*(mb_preds[uv][0]) - *(*predU2--));
>> > +    }
>> > +
>> > +    for (int j = 0; j < cry; ++j)
>> > +      for (int i = 0; i < crx; ++i)
>> > +        mb_pred[j][i] = (uint16) (max_imgpel_value * ((i * ih + iv)));
>> > +  }
>> > +}

      reply	other threads:[~2024-01-23 12:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-19  9:05 Alex Coplan
2024-01-22 17:09 ` Richard Sandiford
2024-01-22 22:34   ` Alex Coplan
2024-01-23 12:18     ` Richard Sandiford [this message]

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=mpty1cgkzu5.fsf@arm.com \
    --to=richard.sandiford@arm.com \
    --cc=alex.coplan@arm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=kyrylo.tkachov@arm.com \
    --cc=richard.earnshaw@arm.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).