From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id A97B03858D28 for ; Tue, 27 Jun 2023 15:43:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A97B03858D28 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-52cb8e5e9f5so3068017a12.0 for ; Tue, 27 Jun 2023 08:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1687880634; x=1690472634; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=jf7nHfu8vU6VORQrfoSo6Gnb7hnfnuU2goAUZWmfTMU=; b=FvoSOiniNTYZEfWgY+Gp7jILurPrJfjsd9tiS/jJ6yGvAszILlOvpv/WKzOA4sKeh8 adrOxy+sq1j939jZQXJwhVpptKrDCbL1C9o40l9pyKe5sHjmFJISETo8AtwcBC8g1JUh neGfOFRZy9VFlsFZj802hLKnQa2rk32wNxVfgn3+FivhApa5uy4edIZnWcAjKn7b0b9B 4ljTUBUCSelfivOzLi/oDDkzVLXR3mphm5wCqec7m8JUGzzhJuIVafoAF5CfjRKVNmVN hwF41jTyOtk+5hDcmzqfPv5Yu7l9S6juLxkvZ1HQU6QDPus3jfJT6HIy1RCRtW+2uXys ZVPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687880634; x=1690472634; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jf7nHfu8vU6VORQrfoSo6Gnb7hnfnuU2goAUZWmfTMU=; b=IoXMIXuE7idStl5hza/KafjnjW+C49WAHJ9lX8sNIM8sr3RG9r+M4IqzAB6vg96it+ ja4IZ+r0nYfR+VpeORJymTbESGCKSzVfyF0JioDeGOMPIw+EGwvKEDxavodrMfPRUrdE d/OyZhXKL0ir6SMKakJgEOOEFRXfT6vu0MWovloeh97+38/ynEuvpiMFS1VyIL67y+4d eHzeML7BD72J+MjuZPHlr4/OBojMvoI2mrfPx6Le+2ixlfU5y27myysU4YeM7FWPF0Jv kgnHSQ64VixU5FdqCvVaq43LiAs02SFbTk3b/BcaHAawU30tx5Y5/99B/Ig8tLF0IO24 D+Xw== X-Gm-Message-State: AC+VfDwdeBfmFMrCWJx+Wc7XFbuhL15d2ELlcj9fiVQH9mUPnQq/Cnhj 5CKur0E07wiTAGli0P6Uz0nAaIQ9O+DzIcJldrNaka4V/OGxKbkZpb4= X-Google-Smtp-Source: ACHHUZ5iBqQuV1Q3v4+Vz4bY16eDYgPawbgyW6hW2X9kLRtvtOtpKzKbelxlkMXNec3pdHOwy4q1JQhvQKzdXNe3Stw= X-Received: by 2002:a17:90a:304e:b0:262:d8e7:abfc with SMTP id q14-20020a17090a304e00b00262d8e7abfcmr11319342pjl.17.1687880634043; Tue, 27 Jun 2023 08:43:54 -0700 (PDT) MIME-Version: 1.0 References: <20230627124728.3563-1-lidie@eswincomputing.com> In-Reply-To: <20230627124728.3563-1-lidie@eswincomputing.com> From: Kito Cheng Date: Tue, 27 Jun 2023 23:43:42 +0800 Message-ID: Subject: Re: [PATCH] [RISC-V] Optimize GP-relative addressing for linker. To: Die Li Cc: binutils@sourceware.org, nelson@rivosinc.com, palmer@dabbelt.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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: Testcase in your git commit has a base offset, but the testcase in the code only contains a base with no offset? I am a little concerned about the case with an offset like "lui a5,%hi(global_array+256) addi a5,a5,%lo(global_array+256)" is still right for this optimization? On Tue, Jun 27, 2023 at 8:47=E2=80=AFPM Die Li w= rote: > > Consider the following test: > > //test.c file > > struct Person { > char name[20]; > int age; > int nation; > int hobby[100]; > }; > > int global_var; > int global_array[920]; > struct Person person; > > int main() { > global_var =3D 3; > global_array[365] =3D 16; > sprintf(person.name, "Lee"); > person.age =3D 27; > person.nation =3D 77; > return 0; > } > > Cflags: > -Xlinker --relax > > Link relaxation can be turned on by the above option, and in fact > is turned on by default. After compiling, linking, and disassembling > the test files, there are the following results: > > Before this patch: > Disassembly of section \.text: > 0+[0-9a-f]+
: > .*:[ ]+[0-9a-f]+[ ]+sw[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f]+ > .*:[ ]+[0-9a-f]+[ ]+c\.lui[ ]+.* > .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,[0-9a-f]+,[0-9]+ # [0= -9a-f]+ > .*:[ ]+[0-9a-f]+[ ]+c\.lui[ ]+.* > .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,[0-9a-f]+,[0-9]+ # [0= -9a-f]+ > > After this patch: > Disassembly of section \.text: > 0+[0-9a-f]+
: > .*:[ ]+[0-9a-f]+[ ]+sw[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f]+ > .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f= ]+ > .*:[ ]+[0-9a-f]+[ ]+addi[ ]+[0-9a-f]+,gp,\-[0-9]+ # [0-9a-f= ]+ > > After applying this patch, both array element and structure member > accesses have been optimized. In fact, for both array elements and > structure members, access is based on the data and the base address > of the structure, which is the address of the first member of the > array or structure. However, current linker takes into account the > size of arrays and structures when performing GP-relative addressing. > As a result, some array and structure bases within the GP-relative > addressing range cannot benefit from relaxation optimization. This > patch resolves this issue. > > Signed-off-by: Die Li > > ChangeLog: > > * bfd/elfnn-riscv.c (_bfd_riscv_relax_section): Update reserve_si= ze. > * ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp: New test entry. > * ld/testsuite/ld-riscv-elf/gp-relax.d: New test. > * ld/testsuite/ld-riscv-elf/gp-relax.s: New test. > --- > bfd/elfnn-riscv.c | 7 +++++ > ld/testsuite/ld-riscv-elf/gp-relax.d | 12 +++++++++ > ld/testsuite/ld-riscv-elf/gp-relax.s | 31 ++++++++++++++++++++++ > ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 1 + > 4 files changed, 51 insertions(+) > create mode 100644 ld/testsuite/ld-riscv-elf/gp-relax.d > create mode 100644 ld/testsuite/ld-riscv-elf/gp-relax.s > > diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c > index 09aa7be225e..29fc5484e0f 100644 > --- a/bfd/elfnn-riscv.c > +++ b/bfd/elfnn-riscv.c > @@ -5169,6 +5169,13 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec= , > if (h->type !=3D STT_FUNC) > reserve_size =3D > (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_= addend; > + > + /* For global pointer relative addressing, it is sufficient to= ensure > + that the symbol's base address falls within the range of gl= obal > + pointer relative addressing. */ > + if (h->type =3D=3D STT_OBJECT) > + reserve_size =3D 0; > + > symtype =3D h->type; > } > > diff --git a/ld/testsuite/ld-riscv-elf/gp-relax.d b/ld/testsuite/ld-riscv= -elf/gp-relax.d > new file mode 100644 > index 00000000000..ec2f59b1b19 > --- /dev/null > +++ b/ld/testsuite/ld-riscv-elf/gp-relax.d > @@ -0,0 +1,12 @@ > +#source: gp-relax.s > +#ld: --relax > +#objdump: -d -Mno-aliases > + > +.*:[ ]+file format .* > + > + > +Disassembly of section \.text: > + > +0+[0-9a-f]+ <_start>: > +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a5,gp,\-[0-9]+ # [0-9a-f]+ > +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a4,gp,[0-9]+ # [0-9a-f]+ > diff --git a/ld/testsuite/ld-riscv-elf/gp-relax.s b/ld/testsuite/ld-riscv= -elf/gp-relax.s > new file mode 100644 > index 00000000000..05548888ebf > --- /dev/null > +++ b/ld/testsuite/ld-riscv-elf/gp-relax.s > @@ -0,0 +1,31 @@ > + .text > + .globl global_var > + .section .sbss,"aw",@nobits > + .align 2 > + .type global_var, @object > + .size global_var, 4 > +global_var: > + .zero 4 > + .globl global_array > + .bss > + .align 3 > + .type global_array, @object > + .size global_array, 3680 > +global_array: > + .zero 3680 > + .globl person > + .align 3 > + .type person, @object > + .size person, 428 > +person: > + .zero 428 > + .text > + .align 1 > + .globl _start > + .type _start, @function > +_start: > + lui a5,%hi(global_array) > + addi a5,a5,%lo(global_array) > + > + lui a4,%hi(person) > + addi a4,a4,%lo(person) > diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld= -riscv-elf/ld-riscv-elf.exp > index 947a266ba72..6a04955b23b 100644 > --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp > @@ -124,6 +124,7 @@ if [istarget "riscv*-*-*"] { > run_dump_test "pcgp-relax-01" > run_dump_test "pcgp-relax-01-norelaxgp" > run_dump_test "pcgp-relax-02" > + run_dump_test "gp-relax" > run_dump_test "c-lui" > run_dump_test "c-lui-2" > run_dump_test "disas-jalr" > -- > 2.17.1 >