From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 135913858C52 for ; Fri, 23 Sep 2022 08:05:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 135913858C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-ot1-x32e.google.com with SMTP id j17-20020a9d7f11000000b0065a20212349so7802196otq.12 for ; Fri, 23 Sep 2022 01:05:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=+koIN2mwSOuJRVj8LndIa91FhYPc+fPlYOUnuR06GsY=; b=HO1fUMZINZfNOl2M3TwEFXsevFkMhML1qU3QgwgJ8Jz4m83c4AMbZj26DUV41Bvqip GkdXeM6y/Fir1fvOqVN/YiEiTg+ay1mqAFGCrMR7lnF3O3G6AOcEJ2BNKOymWb4PoREY aED7e9VaSoT9zRgEniM9DebUC48zvOt5RKUPm0HbrRYDEK7Ukt9OZe3BF47YEEPxJHRz Fqs+5OY7xVvidV1GcwQQU32HgJLaXbWLRzDBQXbnfeqsq5LWYinKduBU6D4aJ9hyGWb1 W+eJv4xU60pAVrdwO2A3UBnwM21CYHapx7OEctIARTsIONfmU5bDGPKAx/eEIUF8ixxa 3tHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=+koIN2mwSOuJRVj8LndIa91FhYPc+fPlYOUnuR06GsY=; b=dbyTmQw6Rd9dN1itTzGX4k7rvuilwpf0y/i4lmynQCW4XuyLzhOP0++oaV/LIFneMc hBqqlJXvpI1wq0xIs3Q0cuTOYFWG+Bzp/U9SxXoLlrNO74pVgvBR+esc1jiSfutAoSHt SW9oMzfSNs087sTHUMTHnbcyL4G3TWohrWJ/3xtyRxMpE3d3hdSGLAJnXckKEOo/GlwN iq6+U6gDyai/ht1xkMwamb6E4az42dKdJDTWvBouqrAyahns0UunlqYiVgx90HJGG6jA lSGPKLMqtcUjWbi95VGBlFnK0gvCQ88Om32lsmTy0WIFi93VfsXdqQECkxi9np2TX0MQ sEaw== X-Gm-Message-State: ACrzQf3xP4WT3cHCmmoUut3s3rBkjkaLtiDHpB29dQy7MnVQ2mHkBTuO hEENH17ktGADvdul/91/M+QNLkIv10CXY+PjqwK+4g== X-Google-Smtp-Source: AMsMyM7mxRSm3vEvDFsCIsff6aglh5QOkvvRzau7XzpLvwVz89MzYYDZ71t1VE7iNtTer7f4mzXgFFEBjfHSTjbKByU= X-Received: by 2002:a05:6830:650b:b0:657:4e60:9973 with SMTP id cm11-20020a056830650b00b006574e609973mr3511215otb.208.1663920350460; Fri, 23 Sep 2022 01:05:50 -0700 (PDT) MIME-Version: 1.0 References: <20220923064726.9639-1-lifang_xia@linux.alibaba.com> In-Reply-To: <20220923064726.9639-1-lifang_xia@linux.alibaba.com> From: Nelson Chu Date: Fri, 23 Sep 2022 16:05:39 +0800 Message-ID: Subject: Re: [PATCH] RISC-V: Optimize relax of GP with max_alignment. To: lifang_xia@linux.alibaba.com Cc: binutils@sourceware.org, Palmer Dabbelt , Greg McGary Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: On Fri, Sep 23, 2022 at 2:48 PM wrote: > > From: Lifang Xia > > The max_alignment defined out of [gp-2K, gp+2k), the max_alignment > shouldn't affect the relax of gp. > If the symbol is in [gp-2K, gp+2k), the max_alignment would be > replaced with the max_alignment of the section in [gp-2k, gp+2k). This idea should works! But I need to pass the riscv-gnu-toolchain regression at least, though the regression shouldn't be broken by the change. Btw, also cc Palmer and Greg that they may have interested in and have some though on this. Thanks Nelson > bfd/ > * elfnn-riscv.c (_bfd_riscv_get_max_alignment_in_gp): New. > (_bfd_riscv_relax_lui): The max_alignment of sections is from > [gp-2K, gp+2K). > (_bfd_riscv_relax_pc): Likewise. > ld/ > * ld/testsuite/ld-riscv-elf/relax-max-align.*: New tests. > --- > bfd/elfnn-riscv.c | 36 ++++++++++++++++ > ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 1 + > ld/testsuite/ld-riscv-elf/relax-max-align.d | 46 +++++++++++++++++++++ > ld/testsuite/ld-riscv-elf/relax-max-align.s | 29 +++++++++++++ > 4 files changed, 112 insertions(+) > create mode 100644 ld/testsuite/ld-riscv-elf/relax-max-align.d > create mode 100644 ld/testsuite/ld-riscv-elf/relax-max-align.s > > diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c > index 3d2ddf4e651..76f888d805c 100644 > --- a/bfd/elfnn-riscv.c > +++ b/bfd/elfnn-riscv.c > @@ -4262,6 +4262,28 @@ _bfd_riscv_get_max_alignment (asection *sec) > return (bfd_vma) 1 << max_alignment_power; > } > > +/* Traverse all output sections in [gp-2K, gp+2K) and return the max alignment. */ > + > +static bfd_vma > +_bfd_riscv_get_max_alignment_in_gp (asection *sec, bfd_vma gp) > +{ > + unsigned int max_alignment_power = 0; > + asection *o; > + > + if (sec == NULL) > + return 0; > + > + for (o = sec->output_section->owner->sections; o != NULL; o = o->next) > + { > + if (VALID_ITYPE_IMM (sec_addr(o) - gp) > + || VALID_ITYPE_IMM (sec_addr(o) + o->size - gp)) > + if (o->alignment_power > max_alignment_power) > + max_alignment_power = o->alignment_power; > + } probably can use only one if rather than two. > + return (bfd_vma) 1 << max_alignment_power; > +} > + > /* Relax non-PIC global variable references to GP-relative references. */ > > static bool > @@ -4293,6 +4315,13 @@ _bfd_riscv_relax_lui (bfd *abfd, > if (h->u.def.section->output_section == sym_sec->output_section > && sym_sec->output_section != bfd_abs_section_ptr) > max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power; > + > + /* Otherwise, consider the alignment of sections in [gp-2K,gp+2K). */ > + else if (!undefined_weak){ > + bfd_vma new_max_alignment = _bfd_riscv_get_max_alignment_in_gp (sym_sec, gp); > + if (new_max_alignment) > + max_alignment = new_max_alignment; > + } > } if (...) max_alignment = ... else if (!undefined_weak) { /* Otherwise, ... */ ... } > /* Is the reference in range of x0 or gp? > @@ -4559,6 +4588,13 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED, > if (h->u.def.section->output_section == sym_sec->output_section > && sym_sec->output_section != bfd_abs_section_ptr) > max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power; > + > + /* Otherwise, consider the alignment of sections in [gp-2K,gp+2K). */ > + else if (!undefined_weak){ > + bfd_vma new_max_alignment = _bfd_riscv_get_max_alignment_in_gp (sym_sec, gp); > + if (new_max_alignment) > + max_alignment = new_max_alignment; > + } > } Likewise, it is minor though. > /* Is the reference in range of x0 or gp? > diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > index df89e0ee68b..e53f16facfa 100644 > --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > @@ -169,6 +169,7 @@ if [istarget "riscv*-*-*"] { > run_dump_test "attr-merge-priv-spec-failed-05" > run_dump_test "attr-merge-priv-spec-failed-06" > run_dump_test "attr-phdr" > + run_dump_test "relax-max-align" > run_ld_link_tests [list \ > [list "Weak reference 32" "-T weakref.ld -m[riscv_choose_ilp32_emul]" "" \ > "-march=rv32i -mabi=ilp32" {weakref32.s} \ > diff --git a/ld/testsuite/ld-riscv-elf/relax-max-align.d b/ld/testsuite/ld-riscv-elf/relax-max-align.d > new file mode 100644 > index 00000000000..4ba920da970 > --- /dev/null > +++ b/ld/testsuite/ld-riscv-elf/relax-max-align.d > @@ -0,0 +1,46 @@ > +#source: relax-max-align.s > +#ld: > +#objdump: -d > + > +.*:[ ]+file format .* > + > + > +Disassembly of section .text: > + > +0+[0-9a-f]+ <_start>: > +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+.* > +.*:[ ]+[0-9a-f]+[ ]+jal[ ]+.* > +.*:[ ]+[0-9a-f]+[ ]+j[ ]+.* > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > +.*:[ ]+[0-9a-f]+[ ]+nop > + > +0+[0-9a-f]+ : > +.*:[ ]+[0-9a-f]+[ ]+ret > +[ ]+... > diff --git a/ld/testsuite/ld-riscv-elf/relax-max-align.s b/ld/testsuite/ld-riscv-elf/relax-max-align.s > new file mode 100644 > index 00000000000..e8abe0f34a1 > --- /dev/null > +++ b/ld/testsuite/ld-riscv-elf/relax-max-align.s > @@ -0,0 +1,29 @@ > + > +.global _start > +_start: > + lui a0, %hi(gdata) > + addi a0, a0, %lo(gdata) > + call func > + j . > + .size _start, . - _start > + > +.global func > +.align 7 > +func: > + ret > + .size func, . - func > + > +.data > +padding: > + .long 0 > + .long 0 > + .long 0 > + .long 0 > + .size padding, . - padding > + > +.global gdata > +.type gdata, object > +gdata: > + .zero 4 > + .size gdata, . - gdata > + > -- > 2.32.1 (Apple Git-133) >