From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 3866F3858C39 for ; Tue, 9 Apr 2024 09:24:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3866F3858C39 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3866F3858C39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712654653; cv=none; b=bNodwupJESbVRkN06gpccaB/fMJI3Ktd8JR6svxHDZr0nTwaiCmOMzCaeYvKb+ORAZd1Bh6piWrYc2vsN+oxocga/nCGpQhsoLapddp6bKV+MrnJ8n3TRjnc56B+4CPP3W+Xxae0eQeJDXm4ei10SJZs21QDhQBpd3Gzo8O053Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712654653; c=relaxed/simple; bh=hTozge4xC5CIciib+dn1PMo/hgkldD579EPIv6++mfg=; h=Subject:To:From:Message-ID:Date:MIME-Version; b=OnEwsQs8G2PiSABmjWytPaBR8N04d1BGGt/xVCdTgC+y3R/y2NoKCFedAdrNqU0bUMMTyOFhgX5m4FUIFEpOm/AgKoUcODC9q5vw2sMGipBx6renk+O/3KM4gqIEJX5iCxAU91oti0ynR1KLuRSm3z77YP/WuapUQ5q1oQEH9YU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ru7i1-00045V-Rz for gcc-patches@gcc.gnu.org; Tue, 09 Apr 2024 05:24:10 -0400 Received: from loongson.cn (unknown [10.20.4.107]) by gateway (Coremail) with SMTP id _____8Axx7kpCRVm+cwkAA--.3348S3; Tue, 09 Apr 2024 17:23:53 +0800 (CST) Received: from [10.20.4.107] (unknown [10.20.4.107]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bx7c4oCRVmrWJ2AA--.32010S3; Tue, 09 Apr 2024 17:23:52 +0800 (CST) Subject: Re:[pushed] [PATCH v2] LoongArch: Enable switchable target To: Yang Yujie , gcc-patches@gcc.gnu.org Cc: xry111@xry111.site, xuchenghua@loongson.cn References: <20240408084512.226082-2-yangyujie@loongson.cn> From: Lulu Cheng Message-ID: <6b4869c1-c92a-971d-5891-11d04f826096@loongson.cn> Date: Tue, 9 Apr 2024 17:23:52 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20240408084512.226082-2-yangyujie@loongson.cn> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID:AQAAf8Bx7c4oCRVmrWJ2AA--.32010S3 X-CM-SenderInfo: xfkh0wpoxo3qxorr0wxvrqhubq/ X-Coremail-Antispam: 1Uk129KBj93XoW3WF4UXFW7KFWxCw4rZw1UArc_yoWfZw4Dpr 9rZr15tr48JFZ7GrZ8ta4rXrn5A3y7Gw129FyftrWxCw43Gr9Fq3Z5Kr9xXF1UJay3Wr1I vF1ru3Wa9a1qy3cCm3ZEXasCq-sJn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUv2b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc7I2V7IY0VAS07 AlzVAYIcxG8wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02 F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw 1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7Cj xVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r 1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8cz VUUUUUU== Received-SPF: pass client-ip=114.242.206.163; envelope-from=chenglulu@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -4 X-Spam_score: -0.5 X-Spam_bar: / X-Spam_report: (-0.5 / 5.0 requ) BAYES_00=-1.9,MIME_CHARSET_FARAWAY=2.45,NICE_REPLY_A=-1.071,SPF_HELO_NONE=0.001,SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,MIME_CHARSET_FARAWAY,NICE_REPLY_A,SPF_FAIL,SPF_HELO_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 List-Id: Pushed to r14-9866. ÔÚ 2024/4/8 ÏÂÎç4:45, Yang Yujie дµÀ: > This patch fixes the back-end context switching in cases where functions > should be built with their own target contexts instead of the > global one, such as LTO linking and functions with target attributes (TBD). > > PR target/113233 > > gcc/ChangeLog: > > * config/loongarch/loongarch.cc (loongarch_reg_init): > Reinitialize the loongarch_regno_mode_ok cache. > (loongarch_option_override): Same. > (loongarch_save_restore_target_globals): Restore target globals. > (loongarch_set_current_function): Restore the target contexts > for functions. > (TARGET_SET_CURRENT_FUNCTION): Define. > * config/loongarch/loongarch.h (SWITCHABLE_TARGET): Enable > switchable target context. > * config/loongarch/loongarch-builtins.cc (loongarch_init_builtins): > Initialize all builtin functions at startup. > (loongarch_expand_builtin): Turn assertion of builtin availability > into a test. > > gcc/testsuite/ChangeLog: > > * lib/target-supports.exp: Define condition loongarch_sx_as. > * gcc.dg/lto/pr113233_0.c: New test. > --- > gcc/config/loongarch/loongarch-builtins.cc | 25 +++--- > gcc/config/loongarch/loongarch.cc | 91 ++++++++++++++++++++-- > gcc/config/loongarch/loongarch.h | 2 + > gcc/testsuite/gcc.dg/lto/pr113233_0.c | 14 ++++ > gcc/testsuite/lib/target-supports.exp | 12 +++ > 5 files changed, 127 insertions(+), 17 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/lto/pr113233_0.c > > diff --git a/gcc/config/loongarch/loongarch-builtins.cc b/gcc/config/loongarch/loongarch-builtins.cc > index efe7e5e5ebc..fbe46833c9b 100644 > --- a/gcc/config/loongarch/loongarch-builtins.cc > +++ b/gcc/config/loongarch/loongarch-builtins.cc > @@ -2512,14 +2512,11 @@ loongarch_init_builtins (void) > for (i = 0; i < ARRAY_SIZE (loongarch_builtins); i++) > { > d = &loongarch_builtins[i]; > - if (d->avail ()) > - { > - type = loongarch_build_function_type (d->function_type); > - loongarch_builtin_decls[i] > - = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL, > - NULL); > - loongarch_get_builtin_decl_index[d->icode] = i; > - } > + type = loongarch_build_function_type (d->function_type); > + loongarch_builtin_decls[i] > + = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL, > + NULL); > + loongarch_get_builtin_decl_index[d->icode] = i; > } > } > > @@ -3105,15 +3102,21 @@ loongarch_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED, > int ignore ATTRIBUTE_UNUSED) > { > tree fndecl; > - unsigned int fcode, avail; > + unsigned int fcode; > const struct loongarch_builtin_description *d; > > fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0); > fcode = DECL_MD_FUNCTION_CODE (fndecl); > gcc_assert (fcode < ARRAY_SIZE (loongarch_builtins)); > d = &loongarch_builtins[fcode]; > - avail = d->avail (); > - gcc_assert (avail != 0); > + > + if (!d->avail ()) > + { > + error_at (EXPR_LOCATION (exp), > + "built-in function %qD is not enabled", fndecl); > + return target; > + } > + > switch (d->builtin_type) > { > case LARCH_BUILTIN_DIRECT: > diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc > index c90b701a533..6b92e7034c5 100644 > --- a/gcc/config/loongarch/loongarch.cc > +++ b/gcc/config/loongarch/loongarch.cc > @@ -7570,15 +7570,19 @@ loongarch_global_init (void) > loongarch_dwarf_regno[i] = INVALID_REGNUM; > } > > + /* Function to allocate machine-dependent function status. */ > + init_machine_status = &loongarch_init_machine_status; > +}; > + > +static void > +loongarch_reg_init (void) > +{ > /* Set up loongarch_hard_regno_mode_ok. */ > for (int mode = 0; mode < MAX_MACHINE_MODE; mode++) > for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) > loongarch_hard_regno_mode_ok_p[mode][regno] > = loongarch_hard_regno_mode_ok_uncached (regno, (machine_mode) mode); > - > - /* Function to allocate machine-dependent function status. */ > - init_machine_status = &loongarch_init_machine_status; > -}; > +} > > static void > loongarch_option_override_internal (struct loongarch_target *target, > @@ -7605,20 +7609,92 @@ loongarch_option_override_internal (struct loongarch_target *target, > > /* Override some options according to the resolved target. */ > loongarch_target_option_override (target, opts, opts_set); > + > + target_option_default_node = target_option_current_node > + = build_target_option_node (opts, opts_set); > + > + loongarch_reg_init (); > +} > + > +/* Remember the last target of loongarch_set_current_function. */ > + > +static GTY(()) tree loongarch_previous_fndecl; > + > +/* Restore or save the TREE_TARGET_GLOBALS from or to new_tree. > + Used by loongarch_set_current_function to > + make sure optab availability predicates are recomputed when necessary. */ > + > +static void > +loongarch_save_restore_target_globals (tree new_tree) > +{ > + if (TREE_TARGET_GLOBALS (new_tree)) > + restore_target_globals (TREE_TARGET_GLOBALS (new_tree)); > + else if (new_tree == target_option_default_node) > + restore_target_globals (&default_target_globals); > + else > + TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts (); > +} > + > +/* Implement TARGET_SET_CURRENT_FUNCTION. */ > + > +static void > +loongarch_set_current_function (tree fndecl) > +{ > + if (fndecl == loongarch_previous_fndecl) > + return; > + > + tree old_tree; > + if (loongarch_previous_fndecl == NULL_TREE) > + old_tree = target_option_current_node; > + else if (DECL_FUNCTION_SPECIFIC_TARGET (loongarch_previous_fndecl)) > + old_tree = DECL_FUNCTION_SPECIFIC_TARGET (loongarch_previous_fndecl); > + else > + old_tree = target_option_default_node; > + > + if (fndecl == NULL_TREE) > + { > + if (old_tree != target_option_current_node) > + { > + loongarch_previous_fndecl = NULL_TREE; > + cl_target_option_restore (&global_options, &global_options_set, > + TREE_TARGET_OPTION > + (target_option_current_node)); > + } > + return; > + } > + > + tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl); > + if (new_tree == NULL_TREE) > + new_tree = target_option_default_node; > + > + loongarch_previous_fndecl = fndecl; > + > + if (new_tree == old_tree) > + return; > + > + cl_target_option_restore (&global_options, &global_options_set, > + TREE_TARGET_OPTION (new_tree)); > + > + loongarch_reg_init (); > + > + loongarch_save_restore_target_globals (new_tree); > } > > + > + > /* Implement TARGET_OPTION_OVERRIDE. */ > > static void > loongarch_option_override (void) > { > + /* Global initializations. */ > + loongarch_global_init (); > + > /* Setting up the target configuration. */ > loongarch_option_override_internal (&la_target, > &global_options, > &global_options_set); > > - /* Global initializations. */ > - loongarch_global_init (); > } > > /* Implement TARGET_OPTION_SAVE. */ > @@ -10935,6 +11011,9 @@ loongarch_asm_code_end (void) > #undef TARGET_OPTION_RESTORE > #define TARGET_OPTION_RESTORE loongarch_option_restore > > +#undef TARGET_SET_CURRENT_FUNCTION > +#define TARGET_SET_CURRENT_FUNCTION loongarch_set_current_function > + > #undef TARGET_LEGITIMIZE_ADDRESS > #define TARGET_LEGITIMIZE_ADDRESS loongarch_legitimize_address > > diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h > index 23bb3254883..db8a9eb9516 100644 > --- a/gcc/config/loongarch/loongarch.h > +++ b/gcc/config/loongarch/loongarch.h > @@ -23,6 +23,8 @@ along with GCC; see the file COPYING3. If not see > > #include "config/loongarch/loongarch-opts.h" > > +#define SWITCHABLE_TARGET 1 > + > #define TARGET_SUPPORTS_WIDE_INT 1 > > /* Macros to silence warnings about numbers being signed in traditional > diff --git a/gcc/testsuite/gcc.dg/lto/pr113233_0.c b/gcc/testsuite/gcc.dg/lto/pr113233_0.c > new file mode 100644 > index 00000000000..0a045c51966 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/lto/pr113233_0.c > @@ -0,0 +1,14 @@ > +/* { dg-require-effective-target loongarch_sx_as } */ > +/* { dg-lto-do link } */ > +/* { dg-skip-if "" { ! { loongarch*-linux-* } } } */ > +/* { dg-lto-options { {-mlsx } } } */ > +/* { dg-suppress-ld-options { -mlsx } } */ > + > +#include > + > +int main (void) > +{ > + __m128i a, b, c; > + c = __lsx_vand_v (a, b); > + return 0; > +} > diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp > index 45435586de2..0122440e8d9 100644 > --- a/gcc/testsuite/lib/target-supports.exp > +++ b/gcc/testsuite/lib/target-supports.exp > @@ -13402,6 +13402,18 @@ proc check_effective_target_loongarch_sx { } { > } "-mlsx"] > } > > +proc check_effective_target_loongarch_sx_as { } { > + return [check_no_compiler_messages loongarch_sx_as object { > + #include > + int main (void) > + { > + __m128i a, b, c; > + c = __lsx_vand_v (a, b); > + return 0; > + } > + } "-mlsx"] > +} > + > proc check_effective_target_loongarch_sx_hw { } { > return [check_runtime loongarch_sx_hw { > #include