public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: gcc-patches@gcc.gnu.org
Cc: Lulu Cheng <chenglulu@loongson.cn>, Wang Xuerui <i@xen0n.name>,
	Chenghua Xu <xuchenghua@loongson.cn>,
	Huacai Chen <chenhuacai@kernel.org>
Subject: Re: [PATCH] LoongArch: add -mdirect-extern-access option
Date: Fri, 02 Sep 2022 19:30:55 +0800	[thread overview]
Message-ID: <ed3d5d2c85f2e4f5a891316641ced6768f60ef9b.camel@xry111.site> (raw)
In-Reply-To: <20220901105412.42242-1-xry111@xry111.site>

On Thu, 2022-09-01 at 18:54 +0800, Xi Ruoyao wrote:
> We'd like to introduce a new codegen option to align with the old
> "-Wa,-mla-global-with-pcrel" and avoid a performance & size regression
> building the Linux kernel with new-reloc toolchain.  And it should be
> also useful for building statically linked executables, firmwares (EDK2
> for example), and other OS kernels.

Some news: get rid of the GOT will also make the implementation of
relocatable kernel easier, so I hope this can be reviewed quickly.

> OK for trunk?
> 
> -- >8 --
> 
> As a new target, LoongArch does not use copy relocation as it's
> problematic in some circumstances.  One bad consequence is we are
> emitting GOT for all accesses to all extern objects with default
> visibility.  The use of GOT is not needed in statically linked
> executables, OS kernels etc.  The GOT entry just wastes space, and the
> GOT access just slow down the execution in those environments.
> 
> Before -mexplicit-relocs, we used "-Wa,-mla-global-with-pcrel" to tell
> the assembler not to use GOT for extern access.  But with
> -mexplicit-relocs, we have to opt the logic in GCC.
> 
> The name "-mdirect-extern-access" is learnt from x86 port.
> 
> gcc/ChangeLog:
> 
>         * config/loongarch/genopts/loongarch.opt.in: Add
>         -mdirect-extern-access option.
>         * config/loongarch/loongarch.opt: Regenerate.
>         * config/loongarch/loongarch.cc (loongarch_classify_symbol):
>         Don't use SYMBOL_GOT_DISP if TARGET_DIRECT_EXTERN_ACCESS.
>         (loongarch_option_override_internal): Complain if
>         -mdirect-extern-access is used with -fPIC or -fpic.
>         * doc/invoke.texi: Document -mdirect-extern-access for
>         LoongArch.
> 
> gcc/testsuite/ChangeLog:
> 
>         * gcc.target/loongarch/direct-extern-1.c: New test.
>         * gcc.target/loongarch/direct-extern-2.c: New test.
> ---
>  gcc/config/loongarch/genopts/loongarch.opt.in     |  4 ++++
>  gcc/config/loongarch/loongarch.cc                 |  5 ++++-
>  gcc/config/loongarch/loongarch.opt                |  4 ++++
>  gcc/doc/invoke.texi                               | 15
> +++++++++++++++
>  .../gcc.target/loongarch/direct-extern-1.c        |  6 ++++++
>  .../gcc.target/loongarch/direct-extern-2.c        |  6 ++++++
>  6 files changed, 39 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern-
> 1.c
>  create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern-
> 2.c
> 
> diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in
> b/gcc/config/loongarch/genopts/loongarch.opt.in
> index ebdd9538d48..e10618777b2 100644
> --- a/gcc/config/loongarch/genopts/loongarch.opt.in
> +++ b/gcc/config/loongarch/genopts/loongarch.opt.in
> @@ -184,3 +184,7 @@ Enum(cmodel) String(@@STR_CMODEL_EXTREME@@)
> Value(CMODEL_EXTREME)
>  mcmodel=
>  Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel)
> Init(CMODEL_NORMAL)
>  Specify the code model.
> +
> +mdirect-extern-access
> +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0)
> +Avoid using the GOT to access external symbols.
> diff --git a/gcc/config/loongarch/loongarch.cc
> b/gcc/config/loongarch/loongarch.cc
> index 77e3a105390..2875fa5b0f3 100644
> --- a/gcc/config/loongarch/loongarch.cc
> +++ b/gcc/config/loongarch/loongarch.cc
> @@ -1642,7 +1642,7 @@ loongarch_classify_symbol (const_rtx x)
>    if (SYMBOL_REF_TLS_MODEL (x))
>      return SYMBOL_TLS;
>  
> -  if (!loongarch_symbol_binds_local_p (x))
> +  if (!TARGET_DIRECT_EXTERN_ACCESS && !loongarch_symbol_binds_local_p
> (x))
>      return SYMBOL_GOT_DISP;
>  
>    tree t = SYMBOL_REF_DECL (x);
> @@ -6093,6 +6093,9 @@ loongarch_option_override_internal (struct
> gcc_options *opts)
>    if (loongarch_branch_cost == 0)
>      loongarch_branch_cost = loongarch_cost->branch_cost;
>  
> +  if (TARGET_DIRECT_EXTERN_ACCESS && flag_shlib)
> +    error ("%qs cannot be used for compiling a shared library",
> +          "-mdirect-extern-access");
>  
>    switch (la_target.cmodel)
>      {
> diff --git a/gcc/config/loongarch/loongarch.opt
> b/gcc/config/loongarch/loongarch.opt
> index 6395234218b..96c811c850b 100644
> --- a/gcc/config/loongarch/loongarch.opt
> +++ b/gcc/config/loongarch/loongarch.opt
> @@ -191,3 +191,7 @@ Enum(cmodel) String(extreme) Value(CMODEL_EXTREME)
>  mcmodel=
>  Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel)
> Init(CMODEL_NORMAL)
>  Specify the code model.
> +
> +mdirect-extern-access
> +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0)
> +Avoid using the GOT to access external symbols.
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index e5eb525a2c1..d4e86682827 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -1016,6 +1016,7 @@ Objective-C and Objective-C++ Dialects}.
>  -memcpy  -mno-memcpy -mstrict-align -mno-strict-align @gol
>  -mmax-inline-memcpy-size=@var{n} @gol
>  -mexplicit-relocs -mno-explicit-relocs @gol
> +-mdirect-extern-access -mno-direct-extern-access @gol
>  -mcmodel=@var{code-model}}
>  
>  @emph{M32R/D Options}
> @@ -25100,6 +25101,20 @@ GCC build-time by detecting corresponding
> assembler support:
>  @code{-mno-explicit-relocs} otherwise.  This option is mostly useful
> for
>  debugging, or interoperation with assemblers different from the
> build-time
>  one.
> +
> +@item -mdirect-extern-access
> +@itemx -mno-direct-extern-access
> +@opindex mdirect-extern-access
> +Do not use or use GOT to access external symbols.  The default is
> +@option{-mno-direct-extern-access}: GOT is used for external symbols
> with
> +default visibility, but not used for other external symbols.
> +
> +With @option{-mdirect-extern-access}, GOT is not used and all
> external
> +symbols are PC-relatively addressed.  It is @strong{only} suitable
> for
> +environments where no dynamic link is performed, like firmwares, OS
> +kernels, executables linked with @option{-static} or @option{-static-
> pie}.
> +@option{-mdirect-extern-access} is not compatible with @option{-fPIC}
> or
> +@option{-fpic}.
>  @end table
>  
>  @node M32C Options
> diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c
> b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c
> new file mode 100644
> index 00000000000..85c6c1e8a88
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c
> @@ -0,0 +1,6 @@
> +/* { dg-do compile } */
> +/* { dg-options "-mexplicit-relocs -mdirect-extern-access" } */
> +/* { dg-final { scan-assembler-not "got" } } */
> +
> +extern int x;
> +int f() { return x; }
> diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c
> b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c
> new file mode 100644
> index 00000000000..58d8bd68a06
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c
> @@ -0,0 +1,6 @@
> +/* { dg-do compile } */
> +/* { dg-options "-mno-explicit-relocs -mdirect-extern-access" } */
> +/* { dg-final { scan-assembler-not "la.global" } } */
> +
> +extern int x;
> +int f() { return x; }

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  parent reply	other threads:[~2022-09-02 11:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 10:54 Xi Ruoyao
2022-09-02  3:12 ` Huacai Chen
2022-09-02  3:31   ` Xi Ruoyao
2022-09-02 11:30 ` Xi Ruoyao [this message]
2022-09-04  0:52   ` Fangrui Song
2022-09-04  2:26   ` Lulu Cheng
2022-09-04  2:51     ` Xi Ruoyao
2022-09-04  3:22       ` Lulu Cheng
2022-09-04  6:35         ` Xi Ruoyao
2022-09-04  7:00           ` Lulu Cheng
2022-09-04  7:38             ` Fangrui Song
2022-09-04 13:29               ` Xi Ruoyao

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=ed3d5d2c85f2e4f5a891316641ced6768f60ef9b.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=chenglulu@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=i@xen0n.name \
    --cc=xuchenghua@loongson.cn \
    /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).