From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 36AEE3858CDB for ; Mon, 5 Sep 2022 02:20:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 36AEE3858CDB Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from [10.20.4.52] (unknown [10.20.4.52]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxnmsIXRVj8l8RAA--.58474S2; Mon, 05 Sep 2022 10:20:56 +0800 (CST) Subject: Re: [PATCH v2] LoongArch: add -mdirect-extern-access option To: Xi Ruoyao , gcc-patches@gcc.gnu.org Cc: Wang Xuerui , Chenghua Xu , Huacai Chen References: <20220904131851.35395-1-xry111@xry111.site> From: Lulu Cheng Message-ID: <71195c1f-bafe-3b30-dbf8-93fd1a6c1e02@loongson.cn> Date: Mon, 5 Sep 2022 10:20:56 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20220904131851.35395-1-xry111@xry111.site> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID:AQAAf8BxnmsIXRVj8l8RAA--.58474S2 X-Coremail-Antispam: 1UD129KBjvJXoW3AFyUZF43uF47Gr4fCFyxAFb_yoWxCw4kpF Zruw1fCr48AFZ7WFn2qa43X3ZxAwsxur4S93WayF18Cayagry7ZF18trsrWF1UAan8J34x Xrn5Ca4Y93WUX37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUva14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr 1j6rxdM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj 6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr 0_Gr1lF7xvr2IY64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7Mxk0xIA0c2IEe2xFo4CE bIxvr21lc2xSY4AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI 8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AK xVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI 8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280 aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyT uYvjfU5WlkUUUUU X-CM-SenderInfo: xfkh0wpoxo3qxorr0wxvrqhubq/ X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,KAM_SHORT,KAM_STOCKGEN,MIME_CHARSET_FARAWAY,NICE_REPLY_A,SPF_HELO_PASS,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: LGTM! Thanks! ÔÚ 2022/9/4 ÏÂÎç9:18, Xi Ruoyao дµÀ: > v1 -> v2: > > Move TARGET_DIRECT_EXTERN_ACCESS check from loongarch_classify_symbol to > loongarch_symbol_binds_local_p, so "%plt" is not output if > -mdirect-extern-access used. > > -- >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_symbol_binds_local_p): Return true 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 | 6 ++++++ > 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, 41 insertions(+) > 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..c9187bf81a7 100644 > --- a/gcc/config/loongarch/loongarch.cc > +++ b/gcc/config/loongarch/loongarch.cc > @@ -1610,6 +1610,9 @@ loongarch_weak_symbol_p (const_rtx x) > bool > loongarch_symbol_binds_local_p (const_rtx x) > { > + if (TARGET_DIRECT_EXTERN_ACCESS) > + return true; > + > if (SYMBOL_REF_P (x)) > return (SYMBOL_REF_DECL (x) > ? targetm.binds_local_p (SYMBOL_REF_DECL (x)) > @@ -6093,6 +6096,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 dd3302fcd15..9d662e35316 100644 > --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -1017,6 +1017,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} > @@ -25090,6 +25091,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; }