From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 8C8EC39C3051; Fri, 25 Jun 2021 10:02:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8C8EC39C3051 Received: by mail-ed1-x530.google.com with SMTP id n20so12605085edv.8; Fri, 25 Jun 2021 03:02:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Q6dzf0JXujnYqtfJ/Gg3H9lMjzr4gHRa/MPhhclw7vg=; b=KTfiPTdWQn1cJx87XyfavPrv2Qd6R6/CzGWyINDw8BLHmpfuz22c/uVWgv8o5EkZmU sAzjmuQgw9cJMt2exvdIrz8Z12Y5yu+Ua77TtAawpSyy8+fkUMiaA7ITVZJzx9XER5Cw VFLTheBz7+xfmuL7GT0YpFoaa6ZVrI8v6Y6LUvViw9wwPxjuDOiqsoYmAE+Z6bbPlN8W SaaviuukaQD5kS7p3J6lZryLE9N0nostjhMXAnL0QQruELSM9Lb0gL6lQeB039n6dVAC BrTTh+FazKbE9VJw2zUDPFFAPYRDf1Duzu0PJ+lTjXcgIQUO1bjde0kzF7IXTBNZfsSl g8iA== X-Gm-Message-State: AOAM530gQz4gs/X15V5mvAWEGzFRCzYwBq6x7vEGZOw+OjFNeWdyphw6 qm0ViAUnU8ntYe3mHGAeWRyCtLpo+JHyvHGPlp0= X-Google-Smtp-Source: ABdhPJwqToKebTsz2i1j+94l163LVspaWLnXjy+ljvATR2Gr17izJ89gcT+r++kmdhnPEPI1AJOzl3MyeIKmjp+8RWo= X-Received: by 2002:a05:6402:42c9:: with SMTP id i9mr13406742edc.61.1624615364338; Fri, 25 Jun 2021 03:02:44 -0700 (PDT) MIME-Version: 1.0 References: <20210625083101.2828805-1-luoxhu@linux.ibm.com> In-Reply-To: From: Richard Biener Date: Fri, 25 Jun 2021 12:02:33 +0200 Message-ID: Subject: Re: [PATCH] New hook adjust_iv_update_pos To: Xionghu Luo Cc: GCC Patches , Segher Boessenkool , Bill Schmidt , linkw@gcc.gnu.org, David Edelsohn , "H. J. Lu" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Jun 2021 10:02:47 -0000 On Fri, Jun 25, 2021 at 11:41 AM Xionghu Luo wrote: > > > > On 2021/6/25 16:54, Richard Biener wrote: > > On Fri, Jun 25, 2021 at 10:34 AM Xionghu Luo via Gcc-patches > > wrote: > >> > >> From: Xiong Hu Luo > >> > >> adjust_iv_update_pos in tree-ssa-loop-ivopts doesn't help performance > >> on Power. For example, it generates mismatched address offset after > >> adjust iv update statement position: > >> > >> [local count: 70988443]: > >> _84 = MEM[(uint8_t *)ip_229 + ivtmp.30_414 * 1]; > >> ivtmp.30_415 = ivtmp.30_414 + 1; > >> _34 = ref_180 + 18446744073709551615; > >> _86 = MEM[(uint8_t *)_34 + ivtmp.30_415 * 1]; > >> if (_84 == _86) > >> goto ; [94.50%] > >> else > >> goto ; [5.50%] > >> > >> Disable it will produce: > >> > >> [local count: 70988443]: > >> _84 = MEM[(uint8_t *)ip_229 + ivtmp.30_414 * 1]; > >> _86 = MEM[(uint8_t *)ref_180 + ivtmp.30_414 * 1]; > >> ivtmp.30_415 = ivtmp.30_414 + 1; > >> if (_84 == _86) > >> goto ; [94.50%] > >> else > >> goto ; [5.50%] > >> > >> Then later pass loop unroll could benefit from same address offset > >> with different base address and reduces register dependency. > >> This patch could improve performance by 10% for typical case on Power, > >> no performance change observed for X86 or Aarch64 due to small loops > >> not unrolled on these platforms. Any comments? > > > > The case you quote is special in that if we hoisted the IV update before > > the other MEM _also_ used in the condition it would be fine again. > > Thanks. I tried to hoist the IV update statement before the first MEM (Fix 2), it > shows even worse performance due to not unroll(two more "base-1" is generated in gimple, > then loop->ninsns is 11 so small loops is not unrolled), change the threshold from > 10 to 12 in rs6000_loop_unroll_adjust would make it also unroll 2 times, the > performance is SAME to the one that IV update statement in the *MIDDLE* (trunk). > From the ASM, we can see the index register %r4 is used in two iterations which > maybe a bottle neck for hiding instruction latency? > > Then it seems reasonable the performance would be better if keep the IV update > statement at *LAST* (Fix 1). > > (Fix 2): > [local count: 70988443]: > ivtmp.30_415 = ivtmp.30_414 + 1; > _34 = ip_229 + 18446744073709551615; > _84 = MEM[(uint8_t *)_34 + ivtmp.30_415 * 1]; > _33 = ref_180 + 18446744073709551615; > _86 = MEM[(uint8_t *)_33 + ivtmp.30_415 * 1]; > if (_84 == _86) > goto ; [94.50%] > else > goto ; [5.50%] > > > .L67: > lbzx %r12,%r24,%r4 > lbzx %r25,%r7,%r4 > cmpw %cr0,%r12,%r25 > bne %cr0,.L11 > mr %r26,%r4 > addi %r4,%r4,1 > lbzx %r12,%r24,%r4 > lbzx %r25,%r7,%r4 > mr %r6,%r26 > cmpw %cr0,%r12,%r25 > bne %cr0,.L11 > mr %r26,%r4 > .L12: > cmpdi %cr0,%r10,1 > addi %r4,%r26,1 > mr %r6,%r26 > addi %r10,%r10,-1 > bne %cr0,.L67 > > > > > Now, adjust_iv_update_pos doesn't seem to check that the > > condition actually uses the IV use stmt def, so it likely applies to > > too many cases. > > > > Unfortunately the introducing rev didn't come with a testcase, > > but still I think fixing up adjust_iv_update_pos is better than > > introducing a way to short-cut it per target decision. > > > > One "fix" might be to add a check that either the condition > > lhs or rhs is the def of the IV use and the other operand > > is invariant. Or if it's of similar structure hoist across the > > other iv-use as well. Not that I understand the argument > > about the overlapping life-range. > > > > You also don't provide a complete testcase ... > > > > Attached the test code, will also add it it patch in future version. > The issue comes from a very small hot loop: > > do { > len++; > } while(len < maxlen && ip[len] == ref[len]); unsigned int foo (unsigned char *ip, unsigned char *ref, unsigned int maxlen) { unsigned int len = 2; do { len++; }while(len < maxlen && ip[len] == ref[len]); return len; } I can see the effect on this loop on x86_64 as well, we end up with .L6: movzbl (%rdi,%rax), %ecx addq $1, %rax cmpb -1(%rsi,%rax), %cl jne .L1 .L3: movl %eax, %r8d cmpl %edx, %eax jb .L6 but without the trick it is .L6: movzbl (%rdi,%rax), %r8d movzbl (%rsi,%rax), %ecx addq $1, %rax cmpb %cl, %r8b jne .L1 .L3: movl %eax, %r9d cmpl %edx, %eax jb .L6 so here you can see the missed fusion. Of course in this case the IV update could have been sunk into the .L3 block and replicated on the exit edge as well. I'm not sure if the motivation for the change introducing this trick was the above kind of combination or not, but I guess so. The dependence distance of the IV increment to the use is now shorter, so I'm not sure the combined variant is better. Richard. > > -- > Thanks, > Xionghu