From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 44F763858C54 for ; Tue, 23 Jan 2024 12:18:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44F763858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 44F763858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706012312; cv=none; b=QrVMDwwirAvzRmWS1M5V57x7q3GrQyV95UPVrqvvPfcuArLbabUlTOcD+QZdeto/+hH3w64l9vMq2zyv75lbeOAVav8B431fZ9MIjxqsflyZqyV9S2o/0xC9+bI10NwxKJc5ts5968Ot8E9ILXBi0oiMAM4Y3VjL/gTtuMEs57E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706012312; c=relaxed/simple; bh=3JX2A/zmWiBZK5T2LzLrgXHnwcL5po/p17RJq5XcCB8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=U7BZyKOFzKVwc0qeUmudhUKUOmrtKDgOkUs2BKZ4iXrc/K/90Om/98d/GoGyN+jh8ba/d4DPD1Q0Gu0T43ZlgZanunRWHuoEGHBBi8r3dUPFAwVByFVQyf34joW32bDb04QmPu4gaoRSmHoj451bkV4Kxe8lBrPuPFzOExC/2Fw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 304C51FB; Tue, 23 Jan 2024 04:19:14 -0800 (PST) Received: from localhost (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1F0D33F762; Tue, 23 Jan 2024 04:18:28 -0800 (PST) From: Richard Sandiford To: Alex Coplan Mail-Followup-To: Alex Coplan ,gcc-patches@gcc.gnu.org, Kyrylo Tkachov , Richard Earnshaw , richard.sandiford@arm.com Cc: gcc-patches@gcc.gnu.org, Kyrylo Tkachov , Richard Earnshaw Subject: Re: [PATCH 3/3] aarch64: Fix up debug uses in ldp/stp pass [PR113089] References: Date: Tue, 23 Jan 2024 12:18:26 +0000 In-Reply-To: (Alex Coplan's message of "Mon, 22 Jan 2024 22:34:36 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-21.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Alex Coplan 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 (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 (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 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))); >> > + } >> > +}