From patchwork Fri Aug 12 09:17:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 482 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:38f:b0:2d5:3c95:9e21 with SMTP id 15csp739593pxh; Fri, 12 Aug 2022 02:18:39 -0700 (PDT) X-Google-Smtp-Source: AA6agR5b8pHa0tzAe3TMURb34Q2/NxKqZUy/1V6Kj9HMFW4Q6XhjE07dOWmI58LJIcZB424vJiW9 X-Received: by 2002:a17:907:94c7:b0:730:d5bc:14c with SMTP id dn7-20020a17090794c700b00730d5bc014cmr2177732ejc.68.1660295919578; Fri, 12 Aug 2022 02:18:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660295919; cv=none; d=google.com; s=arc-20160816; b=Nw7Ul69OMoeivjZNZWcHGuL8DkoH08OA63aP6lsD6TEfxvMb7zKoHouts5qbwVb0Fb D81ghfr7jd9uilHSBURo5wq84VTkP9OWcsGiFSuba83tS6+KKe6ObTsu8iep9/2PyOfE hZUhHnhKs5KHOqQWj5lTyndpNz/yXrmLMDG8LS8P5dMtZP2zoMXSBdHNKAi92e2OdjdC YZdj7lNdzOoMZhOeoF53BeBL03FX7OBBTm9i9E5YYqsXlwqg00b6BcZhDt9GQO0/0sBf cz7F1TupJaAjhsY2ww/EAihnxE+xI5J0XaPcYoJbSjsdJTWsmYAo3xcja0d0e4UjNMra 03wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:content-transfer-encoding:date:to:subject :message-id:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=CFzZA5ieBZxdST840TPoo65jIXp4DiZ/ecDOtxfmvWc=; b=zGYofB4efSfQ5SFdTH0NN3Jfmnp0gorOBNMXOlLpZMopRoCPr0q/c9mTWorQgA0rtk WCB1MSKGwPIvJyzjvRwmF58dauBK7lXWN1GofZp7TS7KNLnNlHFj/R3ssABldSbKjUzn j6LnA6XgiaNctHOLKYKeXfmy/SNiZ+KTC0tpnjYBvQT6uIHyjuD7wjR5dTqdolzOCX02 MfiTsN+8NVx+yFcjUJHYc6YaPMnI9fCqrZxqTXVwgrlnH28QjQDFQfhNk7eUBq8p9/js ogaZFwRdDI9waEqXd8Ib9yHV2E1IQQ+scm79wDr4jwPTwocTOom1qM/wtV2zDAsbmbPH pNng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pLoEu1I3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id di19-20020a170906731300b0073145b1210bsi1593343ejc.402.2022.08.12.02.18.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Aug 2022 02:18:39 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pLoEu1I3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 80AE0385828F for ; Fri, 12 Aug 2022 09:18:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80AE0385828F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660295918; bh=CFzZA5ieBZxdST840TPoo65jIXp4DiZ/ecDOtxfmvWc=; h=Subject:To:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=pLoEu1I3pmzv5is/c0X/zeVt2wSmUWDxtBSTsK61Xhja/vbJXz/zh2sSY34K1vBEI YEj/KsAdR4u5m6Q8bRQfF6+J/IDAU+RrWHiWhv/VHKorAV1Tt/gQC8EgY0d+XHUF8a ws2nGrL6HOTTG13Zm5BVIA1WTomaq655Ud074suI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by sourceware.org (Postfix) with ESMTPS id 2176F3858D28 for ; Fri, 12 Aug 2022 09:17:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2176F3858D28 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 784A6668A4; Fri, 12 Aug 2022 05:17:50 -0400 (EDT) Message-ID: Subject: [PATCH v6] LoongArch: add addr_global attribute To: gcc-patches@gcc.gnu.org, Lulu Cheng Date: Fri, 12 Aug 2022 17:17:47 +0800 User-Agent: Evolution 3.45.2 MIME-Version: 1.0 X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, FROM_SUSPICIOUS_NTLD_FP, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, LIKELY_SPAM_FROM, PDS_OTHER_BAD_TLD, 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 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-Patchwork-Original-From: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Cc: Jinyang He , Chenghua Xu , Huacai Chen , Youling Tang , Wang Xuerui Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740946453712957441?= X-GMAIL-MSGID: =?utf-8?q?1740946453712957441?= v5 -> v6: * still use "addr_global" as we don't have a better name. * add a test case with -mno-explicit-relocs. -- >8 -- 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. This happens when the Linux kernel loads a module with "local" per-CPU variables. 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. gcc/ChangeLog: * config/loongarch/loongarch.cc (loongarch_attribute_table): New attribute table. (TARGET_ATTRIBUTE_TABLE): Define the target hook. (loongarch_handle_addr_global_attribute): New static function. (loongarch_classify_symbol): Return SYMBOL_GOT_DISP for SYMBOL_REF_DECL with addr_global attribute. (loongarch_use_anchors_for_symbol_p): New static function. (TARGET_USE_ANCHORS_FOR_SYMBOL_P): Define the target hook. * doc/extend.texi (Variable Attributes): Document new LoongArch specific attribute. gcc/testsuite/ChangeLog: * gcc.target/loongarch/attr-addr_global-1.c: New test. * gcc.target/loongarch/attr-addr_global-2.c: New test. --- gcc/config/loongarch/loongarch.cc | 63 +++++++++++++++++++ gcc/doc/extend.texi | 17 +++++ .../gcc.target/loongarch/attr-addr_global-1.c | 29 +++++++++ .../gcc.target/loongarch/attr-addr_global-2.c | 29 +++++++++ 4 files changed, 138 insertions(+) create mode 100644 gcc/testsuite/gcc.target/loongarch/attr-addr_global-1.c create mode 100644 gcc/testsuite/gcc.target/loongarch/attr-addr_global-2.c diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index 79687340dfd..978e66ed549 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -1643,6 +1643,15 @@ loongarch_classify_symbol (const_rtx x) && !loongarch_symbol_binds_local_p (x)) return SYMBOL_GOT_DISP; + if (SYMBOL_REF_P (x)) + { + tree decl = SYMBOL_REF_DECL (x); + /* An addr_global symbol may be out of the +/- 2GiB range around + the PC, so we have to use GOT. */ + if (decl && lookup_attribute ("addr_global", DECL_ATTRIBUTES (decl))) + return SYMBOL_GOT_DISP; + } + return SYMBOL_PCREL; } @@ -6068,6 +6077,54 @@ loongarch_starting_frame_offset (void) return crtl->outgoing_args_size; } +static tree +loongarch_handle_addr_global_attribute (tree *node, tree name, tree, int, + bool *no_add_attrs) +{ + tree decl = *node; + if (TREE_CODE (decl) == VAR_DECL) + { + if (DECL_CONTEXT (decl) + && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL + && !TREE_STATIC (decl)) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute cannot be specified for local " + "variables", name); + *no_add_attrs = true; + } + } + else + { + warning (OPT_Wattributes, "%qE attribute ignored", name); + *no_add_attrs = true; + } + return NULL_TREE; +} + +static const struct attribute_spec loongarch_attribute_table[] = +{ + /* { name, min_len, max_len, decl_req, type_req, fn_type_req, + affects_type_identity, handler, exclude } */ + { "addr_global", 0, 0, true, false, false, false, + loongarch_handle_addr_global_attribute, NULL }, + /* The last attribute spec is set to be NULL. */ + {} +}; + +bool +loongarch_use_anchors_for_symbol_p (const_rtx symbol) +{ + tree decl = SYMBOL_REF_DECL (symbol); + + /* An addr_global attribute indicates the linker may move the symbol away, + so the use of anchor may cause relocation overflow. */ + if (decl && lookup_attribute ("addr_global", DECL_ATTRIBUTES (decl))) + return false; + + return default_use_anchors_for_symbol_p (symbol); +} + /* Initialize the GCC target structure. */ #undef TARGET_ASM_ALIGNED_HI_OP #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" @@ -6256,6 +6313,12 @@ loongarch_starting_frame_offset (void) #undef TARGET_HAVE_SPECULATION_SAFE_VALUE #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed +#undef TARGET_ATTRIBUTE_TABLE +#define TARGET_ATTRIBUTE_TABLE loongarch_attribute_table + +#undef TARGET_USE_ANCHORS_FOR_SYMBOL_P +#define TARGET_USE_ANCHORS_FOR_SYMBOL_P loongarch_use_anchors_for_symbol_p + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-loongarch.h" diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 7fe7f8817cd..b1173e15c7c 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7314,6 +7314,7 @@ attributes. * Blackfin Variable Attributes:: * H8/300 Variable Attributes:: * IA-64 Variable Attributes:: +* LoongArch Variable Attributes:: * M32R/D Variable Attributes:: * MeP Variable Attributes:: * Microsoft Windows Variable Attributes:: @@ -8098,6 +8099,22 @@ defined by shared libraries. @end table +@node LoongArch Variable Attributes +@subsection LoongArch Variable Attributes + +One attribute is currently defined for the LoongArch. + +@table @code +@item addr_global +@cindex @code{addr_global} variable attribute, LoongArch +Use this attribute on the LoongArch to mark an object possible to be +located anywhere in the address space 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. This attribute is mostly useful if a +@code{section} attribute and/or a linker script will place the object +somewhere unexpected by the code model. +@end table + @node M32R/D Variable Attributes @subsection M32R/D Variable Attributes diff --git a/gcc/testsuite/gcc.target/loongarch/attr-addr_global-1.c b/gcc/testsuite/gcc.target/loongarch/attr-addr_global-1.c new file mode 100644 index 00000000000..c1553713d9d --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/attr-addr_global-1.c @@ -0,0 +1,29 @@ +/* { dg-do compile } */ +/* { dg-options "-mexplicit-relocs -mcmodel=normal -O2" } */ +/* { dg-final { scan-assembler-not "%pc" } } */ +/* { dg-final { scan-assembler-times "%got_pc_hi20" 3 } } */ + +/* addr_global attribute should mark x and y possibly outside of the local + data range defined by the code model, so GOT should be used instead of + PC-relative. */ + +int x __attribute__((addr_global)); +int y __attribute__((addr_global)); + +int +test(void) +{ + 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"), addr_global)); + +void +inc_counter(void) +{ + int *ptr = (int *)(per_cpu_base + (long)&counter); + (*ptr)++; +} diff --git a/gcc/testsuite/gcc.target/loongarch/attr-addr_global-2.c b/gcc/testsuite/gcc.target/loongarch/attr-addr_global-2.c new file mode 100644 index 00000000000..708f163975f --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/attr-addr_global-2.c @@ -0,0 +1,29 @@ +/* { dg-do compile } */ +/* { dg-options "-mno-explicit-relocs -mcmodel=normal -O2" } */ +/* { dg-final { scan-assembler-not "la.local" } } */ +/* { dg-final { scan-assembler-times "la.global" 3 } } */ + +/* addr_global attribute should mark x and y possibly outside of the local + data range defined by the code model, so GOT should be used instead of + PC-relative. */ + +int x __attribute__((addr_global)); +int y __attribute__((addr_global)); + +int +test(void) +{ + 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"), addr_global)); + +void +inc_counter(void) +{ + int *ptr = (int *)(per_cpu_base + (long)&counter); + (*ptr)++; +}