From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 0E6D53858D33 for ; Wed, 3 Aug 2022 01:37:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0E6D53858D33 Received: from localhost.localdomain (xry111.site [IPv6:2001:470:683e::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 17DD8669EF; Tue, 2 Aug 2022 21:36:58 -0400 (EDT) Message-ID: <6cafbcdf79f77b73b9329f3e3a2f24ec85eda94d.camel@xry111.site> Subject: Re: [PATCH v5] LoongArch: add movable attribute From: Xi Ruoyao To: Chenghua Xu , gcc-patches@gcc.gnu.org, Lulu Cheng Cc: Jinyang He , Huacai Chen , Youling Tang , Wang Xuerui Date: Wed, 03 Aug 2022 09:36:56 +0800 In-Reply-To: References: <9b6b0e68cfb7e87ae961ef8a7bb7987f534da19c.camel@xry111.site> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.3 MIME-Version: 1.0 X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, LIKELY_SPAM_FROM, PDS_OTHER_BAD_TLD, SPF_HELO_PASS, 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 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: Wed, 03 Aug 2022 01:37:03 -0000 Is it OK for trunk or I need to change something? By the way, I'm seeking a possibility to include this into 12.2. Then we leaves only 12.1 without this attribute, and we can just say "building the kernel needs GCC 12.2 or later". On Mon, 2022-08-01 at 18:07 +0800, Xi Ruoyao wrote: > Changes v4 -> v5: Fix changelog.=C2=A0 No code change. >=20 > Changes v3 -> v4: >=20 > =C2=A0* Use "movable" as the attribute name as Huacai says it's already > used > =C2=A0=C2=A0 in downstream GCC fork. > =C2=A0* Remove an inaccurate line from the doc. (Initially I tried to > =C2=A0=C2=A0 implement a "model(...)" like IA64 or M32R. Then I changed m= y mind > =C2=A0=C2=A0 but forgot to remove the line copied from M32R doc.) >=20 > -- >8 -- >=20 > A linker script and/or a section attribute may locate a local object > in > some way unexpected by the code model, leading to a link failure.=C2=A0 > This > happens when the Linux kernel loads a module with "local" per-CPU > variables. >=20 > Add an attribute to explicitly mark an variable with the address > unlimited by the code model so we would be able to work around such > problems. >=20 > gcc/ChangeLog: >=20 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0* config/loongarch/loonga= rch.cc (loongarch_attribute_table): > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0New attribute table. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0(TARGET_ATTRIBUTE_TABLE):= Define the target hook. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0(loongarch_handle_addr_gl= obal_attribute): New static function. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0(loongarch_classify_symbo= l): Return SYMBOL_GOT_DISP for > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0SYMBOL_REF_DECL with addr= _global attribute. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0(loongarch_use_anchors_fo= r_symbol_p): New static function. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0(TARGET_USE_ANCHORS_FOR_S= YMBOL_P): Define the target hook. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0* doc/extend.texi (Variab= le Attributes): Document new > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0LoongArch specific attrib= ute. >=20 > gcc/testsuite/ChangeLog: >=20 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0* gcc.target/loongarch/ad= dr-global.c: New test. > --- > =C2=A0gcc/config/loongarch/loongarch.cc=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 63 > +++++++++++++++++++ > =C2=A0gcc/doc/extend.texi=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 16 +++++ > =C2=A0.../gcc.target/loongarch/attr-movable.c=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 | 29 +++++++++ > =C2=A03 files changed, 108 insertions(+) > =C2=A0create mode 100644 gcc/testsuite/gcc.target/loongarch/attr-movable.= c >=20 > diff --git a/gcc/config/loongarch/loongarch.cc > b/gcc/config/loongarch/loongarch.cc > index 79687340dfd..6b6026700a6 100644 > --- a/gcc/config/loongarch/loongarch.cc > +++ b/gcc/config/loongarch/loongarch.cc > @@ -1643,6 +1643,15 @@ loongarch_classify_symbol (const_rtx x) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 && !loongarch_symbol_binds_local_p (= x)) > =C2=A0=C2=A0=C2=A0=C2=A0 return SYMBOL_GOT_DISP; > =C2=A0 > +=C2=A0 if (SYMBOL_REF_P (x)) > +=C2=A0=C2=A0=C2=A0 { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 tree decl =3D SYMBOL_REF_DECL (x); > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* A movable symbol may be moved away fro= m the +/- 2GiB range > around > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 the PC, so we have to use GOT= .=C2=A0 */ > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (decl && lookup_attribute ("movable", = DECL_ATTRIBUTES > (decl))) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return SYMBOL_GOT_DISP; > +=C2=A0=C2=A0=C2=A0 } > + > =C2=A0=C2=A0 return SYMBOL_PCREL; > =C2=A0} > =C2=A0 > @@ -6068,6 +6077,54 @@ loongarch_starting_frame_offset (void) > =C2=A0=C2=A0 return crtl->outgoing_args_size; > =C2=A0} > =C2=A0 > +static tree > +loongarch_handle_movable_attribute (tree *node, tree name, tree, int, > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bool *no_add_attr= s) > +{ > +=C2=A0 tree decl =3D *node; > +=C2=A0 if (TREE_CODE (decl) =3D=3D VAR_DECL) > +=C2=A0=C2=A0=C2=A0 { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (DECL_CONTEXT (decl) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 && TREE_CODE (DECL_CONT= EXT (decl)) =3D=3D FUNCTION_DECL > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 && !TREE_STATIC (decl)) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0{ > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error_at (DECL_SOURCE_L= OCATION (decl), > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "%qE attribute cannot be specified for= local " > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "variables", name); > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *no_add_attrs =3D true; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0} > +=C2=A0=C2=A0=C2=A0 } > +=C2=A0 else > +=C2=A0=C2=A0=C2=A0 { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 warning (OPT_Wattributes, "%qE attribute = ignored", name); > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *no_add_attrs =3D true; > +=C2=A0=C2=A0=C2=A0 } > +=C2=A0 return NULL_TREE; > +} > + > +static const struct attribute_spec loongarch_attribute_table[] =3D > +{ > +=C2=A0 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 affects_type_identity, handler, exc= lude } */ > +=C2=A0 { "movable", 0, 0, true, false, false, false, > +=C2=A0=C2=A0=C2=A0 loongarch_handle_movable_attribute, NULL }, > +=C2=A0 /* The last attribute spec is set to be NULL.=C2=A0 */ > +=C2=A0 {} > +}; > + > +bool > +loongarch_use_anchors_for_symbol_p (const_rtx symbol) > +{ > +=C2=A0 tree decl =3D SYMBOL_REF_DECL (symbol); > + > +=C2=A0 /* A movable attribute indicates the linker may move the symbol > away, > +=C2=A0=C2=A0=C2=A0=C2=A0 so the use of anchor may cause relocation overf= low.=C2=A0 */ > +=C2=A0 if (decl && lookup_attribute ("movable", DECL_ATTRIBUTES (decl))) > +=C2=A0=C2=A0=C2=A0 return false; > + > +=C2=A0 return default_use_anchors_for_symbol_p (symbol); > +} > + > =C2=A0/* Initialize the GCC target structure.=C2=A0 */ > =C2=A0#undef TARGET_ASM_ALIGNED_HI_OP > =C2=A0#define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" > @@ -6256,6 +6313,12 @@ loongarch_starting_frame_offset (void) > =C2=A0#undef=C2=A0 TARGET_HAVE_SPECULATION_SAFE_VALUE > =C2=A0#define TARGET_HAVE_SPECULATION_SAFE_VALUE > speculation_safe_value_not_needed > =C2=A0 > +#undef=C2=A0 TARGET_ATTRIBUTE_TABLE > +#define TARGET_ATTRIBUTE_TABLE loongarch_attribute_table > + > +#undef=C2=A0 TARGET_USE_ANCHORS_FOR_SYMBOL_P > +#define TARGET_USE_ANCHORS_FOR_SYMBOL_P > loongarch_use_anchors_for_symbol_p > + > =C2=A0struct gcc_target targetm =3D TARGET_INITIALIZER; > =C2=A0 > =C2=A0#include "gt-loongarch.h" > diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi > index 7fe7f8817cd..322d8c05a04 100644 > --- a/gcc/doc/extend.texi > +++ b/gcc/doc/extend.texi > @@ -7314,6 +7314,7 @@ attributes. > =C2=A0* Blackfin Variable Attributes:: > =C2=A0* H8/300 Variable Attributes:: > =C2=A0* IA-64 Variable Attributes:: > +* LoongArch Variable Attributes:: > =C2=A0* M32R/D Variable Attributes:: > =C2=A0* MeP Variable Attributes:: > =C2=A0* Microsoft Windows Variable Attributes:: > @@ -8098,6 +8099,21 @@ defined by shared libraries. > =C2=A0 > =C2=A0@end table > =C2=A0 > +@node LoongArch Variable Attributes > +@subsection LoongArch Variable Attributes > + > +One attribute is currently defined for the LoongArch. > + > +@table @code > +@item movable > +@cindex @code{movable} variable attribute, LoongArch > +Use this attribute on the LoongArch to mark an object possible to be > moved > +by the linker, so its address is unlimited by the local data section > range > +specified by the code model even if the object is defined locally.=C2=A0 > This > +attribute is mostly useful if a @code{section} attribute and/or a > linker > +script will move the object somewhere unexpected by the code model. > +@end table > + > =C2=A0@node M32R/D Variable Attributes > =C2=A0@subsection M32R/D Variable Attributes > =C2=A0 > diff --git a/gcc/testsuite/gcc.target/loongarch/attr-movable.c > b/gcc/testsuite/gcc.target/loongarch/attr-movable.c > new file mode 100644 > index 00000000000..85b1dd4c59a > --- /dev/null > +++ b/gcc/testsuite/gcc.target/loongarch/attr-movable.c > @@ -0,0 +1,29 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mexplicit-relocs -mcmodel=3Dnormal -O2" } */ > +/* { dg-final { scan-assembler-not "%pc" } } */ > +/* { dg-final { scan-assembler-times "%got_pc_hi20" 3 } } */ > + > +/* movable attribute should mark x and y possibly outside of the > local > +=C2=A0=C2=A0 data range defined by the code model, so GOT should be used > instead of > +=C2=A0=C2=A0 PC-relative.=C2=A0 */ > + > +int x __attribute__((movable)); > +int y __attribute__((movable)); > + > +int > +test(void) > +{ > +=C2=A0 return x + y; > +} > + > +/* The following will be used for kernel per-cpu storage > implemention. */ > + > +register char *per_cpu_base __asm__("r21"); > +static int counter __attribute__((section(".data..percpu"), > movable)); > + > +void > +inc_counter(void) > +{ > +=C2=A0 int *ptr =3D (int *)(per_cpu_base + (long)&counter); > +=C2=A0 (*ptr)++; > +} --=20 Xi Ruoyao School of Aerospace Science and Technology, Xidian University