[2/3] LoongArch: Split the function loongarch_cpu_cpp_builtins into two functions.

Message ID 20250211124917.28685-3-chenglulu@loongson.cn
State New
Headers
Series Organize the code and fix PR118828. |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap fail Patch failed to apply

Commit Message

Lulu Cheng Feb. 11, 2025, 12:49 p.m. UTC
  Split the implementation of the function loongarch_cpu_cpp_builtins into two parts:
  1. Macro definitions that do not change (only considering 64-bit architecture)
  2. Macro definitions that change with different compilation options.

gcc/ChangeLog:

	* config/loongarch/loongarch-c.cc (builtin_undef): New macro.
	(loongarch_cpu_cpp_builtins): Split to loongarch_update_cpp_builtins
	and loongarch_define_unconditional_macros.
	(loongarch_def_or_undef): New functions.
	(loongarch_define_unconditional_macros): Likewise.
	(loongarch_update_cpp_builtins): Likewise.

Change-Id: Ifae73ffa2a07a595ed2a7f6ab7b82d8f51328a2a
---
 gcc/config/loongarch/loongarch-c.cc | 109 +++++++++++++++++-----------
 1 file changed, 66 insertions(+), 43 deletions(-)
  

Comments

Xi Ruoyao Feb. 11, 2025, 1:26 p.m. UTC | #1
On Tue, 2025-02-11 at 20:49 +0800, Lulu Cheng wrote:
> Split the implementation of the function loongarch_cpu_cpp_builtins
> into two parts:
>   1. Macro definitions that do not change (only considering 64-bit
> architecture)
>   2. Macro definitions that change with different compilation options.
> 
> gcc/ChangeLog:
> 
> 	* config/loongarch/loongarch-c.cc (builtin_undef): New macro.
> 	(loongarch_cpu_cpp_builtins): Split to
> loongarch_update_cpp_builtins
> 	and loongarch_define_unconditional_macros.
> 	(loongarch_def_or_undef): New functions.
> 	(loongarch_define_unconditional_macros): Likewise.
> 	(loongarch_update_cpp_builtins): Likewise.
> 
> Change-Id: Ifae73ffa2a07a595ed2a7f6ab7b82d8f51328a2a
> ---
>  gcc/config/loongarch/loongarch-c.cc | 109 +++++++++++++++++----------
> -
>  1 file changed, 66 insertions(+), 43 deletions(-)
> 
> diff --git a/gcc/config/loongarch/loongarch-c.cc
> b/gcc/config/loongarch/loongarch-c.cc
> index 5d8c02e094b..9fe911325ab 100644
> --- a/gcc/config/loongarch/loongarch-c.cc
> +++ b/gcc/config/loongarch/loongarch-c.cc
> @@ -31,13 +31,21 @@ along with GCC; see the file COPYING3.  If not see
>  
>  #define preprocessing_asm_p() (cpp_get_options (pfile)->lang ==
> CLK_ASM)
>  #define builtin_define(TXT) cpp_define (pfile, TXT)
> +#define builtin_undef(TXT) cpp_undef (pfile, TXT)
>  #define builtin_assert(TXT) cpp_assert (pfile, TXT)
>  
> -void
> -loongarch_cpu_cpp_builtins (cpp_reader *pfile)
> +static void
> +loongarch_def_or_undef (bool def_p, const char *macro, cpp_reader
> *pfile)
> +{
> +  if (def_p)
> +    cpp_define (pfile, macro);
> +  else
> +    cpp_undef (pfile, macro);
> +}
> +
> +static void
> +loongarch_define_unconditional_macros (cpp_reader *pfile)
>  {
> -  builtin_assert ("machine=loongarch");
> -  builtin_assert ("cpu=loongarch");
>    builtin_define ("__loongarch__");
>  
>    builtin_define_with_value ("__loongarch_arch",
> @@ -66,45 +74,6 @@ loongarch_cpu_cpp_builtins (cpp_reader *pfile)
>        builtin_define ("__loongarch_lp64");
>      }
>  
> -  /* These defines reflect the ABI in use, not whether the
> -     FPU is directly accessible.  */
> -  if (TARGET_DOUBLE_FLOAT_ABI)
> -    builtin_define ("__loongarch_double_float=1");
> -  else if (TARGET_SINGLE_FLOAT_ABI)
> -    builtin_define ("__loongarch_single_float=1");
> -
> -  if (TARGET_DOUBLE_FLOAT_ABI || TARGET_SINGLE_FLOAT_ABI)
> -    builtin_define ("__loongarch_hard_float=1");
> -  else
> -    builtin_define ("__loongarch_soft_float=1");
> -
> -
> -  /* ISA Extensions.  */
> -  if (TARGET_DOUBLE_FLOAT)
> -    builtin_define ("__loongarch_frlen=64");
> -  else if (TARGET_SINGLE_FLOAT)
> -    builtin_define ("__loongarch_frlen=32");
> -  else
> -    builtin_define ("__loongarch_frlen=0");
> -
> -  if (TARGET_HARD_FLOAT && ISA_HAS_FRECIPE)
> -    builtin_define ("__loongarch_frecipe");
> -
> -  if (ISA_HAS_LSX)
> -    {
> -      builtin_define ("__loongarch_simd");
> -      builtin_define ("__loongarch_sx");
> -
> -      if (!ISA_HAS_LASX)
> -	builtin_define ("__loongarch_simd_width=128");
> -    }
> -
> -  if (ISA_HAS_LASX)
> -    {
> -      builtin_define ("__loongarch_asx");
> -      builtin_define ("__loongarch_simd_width=256");
> -    }
> -
>    /* ISA evolution features */
>    int max_v_major = 1, max_v_minor = 0;

I guess the handling for la_evo_macro_name macros (like
__loongarch_div32) and
__loongarch_version_major/__loongarch_version_minor should be moved as
well?  Things like #pragma GCC target("arch=la664") may affect them.
  
Lulu Cheng Feb. 12, 2025, 12:53 a.m. UTC | #2
在 2025/2/11 下午9:26, Xi Ruoyao 写道:
> On Tue, 2025-02-11 at 20:49 +0800, Lulu Cheng wrote:
>> Split the implementation of the function loongarch_cpu_cpp_builtins
>> into two parts:
>>    1. Macro definitions that do not change (only considering 64-bit
>> architecture)
>>    2. Macro definitions that change with different compilation options.
>>
>> gcc/ChangeLog:
>>
>> 	* config/loongarch/loongarch-c.cc (builtin_undef): New macro.
>> 	(loongarch_cpu_cpp_builtins): Split to
>> loongarch_update_cpp_builtins
>> 	and loongarch_define_unconditional_macros.
>> 	(loongarch_def_or_undef): New functions.
>> 	(loongarch_define_unconditional_macros): Likewise.
>> 	(loongarch_update_cpp_builtins): Likewise.
>>
>> Change-Id: Ifae73ffa2a07a595ed2a7f6ab7b82d8f51328a2a
>> ---
/* snip */
>> I guess the handling for la_evo_macro_name macros (like
>> __loongarch_div32) and
>> __loongarch_version_major/__loongarch_version_minor should be moved as
>> well?  Things like #pragma GCC target("arch=la664") may affect them.

It seems that the following four also need to be updated. I will make 
corrections in v2

and add the corresponding test cases.


   builtin_define_with_value ("__loongarch_arch",

loongarch_arch_strings[la_target.cpu_arch], 1);

   builtin_define_with_value ("__loongarch_tune",
loongarch_tune_strings[la_target.cpu_tune], 1);

   builtin_define_with_value ("_LOONGARCH_ARCH",
loongarch_arch_strings[la_target.cpu_arch], 1);

   builtin_define_with_value ("_LOONGARCH_TUNE",
loongarch_tune_strings[la_target.cpu_tune], 1);
  

Patch

diff --git a/gcc/config/loongarch/loongarch-c.cc b/gcc/config/loongarch/loongarch-c.cc
index 5d8c02e094b..9fe911325ab 100644
--- a/gcc/config/loongarch/loongarch-c.cc
+++ b/gcc/config/loongarch/loongarch-c.cc
@@ -31,13 +31,21 @@  along with GCC; see the file COPYING3.  If not see
 
 #define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
 #define builtin_define(TXT) cpp_define (pfile, TXT)
+#define builtin_undef(TXT) cpp_undef (pfile, TXT)
 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
 
-void
-loongarch_cpu_cpp_builtins (cpp_reader *pfile)
+static void
+loongarch_def_or_undef (bool def_p, const char *macro, cpp_reader *pfile)
+{
+  if (def_p)
+    cpp_define (pfile, macro);
+  else
+    cpp_undef (pfile, macro);
+}
+
+static void
+loongarch_define_unconditional_macros (cpp_reader *pfile)
 {
-  builtin_assert ("machine=loongarch");
-  builtin_assert ("cpu=loongarch");
   builtin_define ("__loongarch__");
 
   builtin_define_with_value ("__loongarch_arch",
@@ -66,45 +74,6 @@  loongarch_cpu_cpp_builtins (cpp_reader *pfile)
       builtin_define ("__loongarch_lp64");
     }
 
-  /* These defines reflect the ABI in use, not whether the
-     FPU is directly accessible.  */
-  if (TARGET_DOUBLE_FLOAT_ABI)
-    builtin_define ("__loongarch_double_float=1");
-  else if (TARGET_SINGLE_FLOAT_ABI)
-    builtin_define ("__loongarch_single_float=1");
-
-  if (TARGET_DOUBLE_FLOAT_ABI || TARGET_SINGLE_FLOAT_ABI)
-    builtin_define ("__loongarch_hard_float=1");
-  else
-    builtin_define ("__loongarch_soft_float=1");
-
-
-  /* ISA Extensions.  */
-  if (TARGET_DOUBLE_FLOAT)
-    builtin_define ("__loongarch_frlen=64");
-  else if (TARGET_SINGLE_FLOAT)
-    builtin_define ("__loongarch_frlen=32");
-  else
-    builtin_define ("__loongarch_frlen=0");
-
-  if (TARGET_HARD_FLOAT && ISA_HAS_FRECIPE)
-    builtin_define ("__loongarch_frecipe");
-
-  if (ISA_HAS_LSX)
-    {
-      builtin_define ("__loongarch_simd");
-      builtin_define ("__loongarch_sx");
-
-      if (!ISA_HAS_LASX)
-	builtin_define ("__loongarch_simd_width=128");
-    }
-
-  if (ISA_HAS_LASX)
-    {
-      builtin_define ("__loongarch_asx");
-      builtin_define ("__loongarch_simd_width=256");
-    }
-
   /* ISA evolution features */
   int max_v_major = 1, max_v_minor = 0;
 
@@ -145,7 +114,61 @@  loongarch_cpu_cpp_builtins (cpp_reader *pfile)
   builtin_define_with_int_value ("_LOONGARCH_SZPTR", POINTER_SIZE);
   builtin_define_with_int_value ("_LOONGARCH_FPSET", 32);
   builtin_define_with_int_value ("_LOONGARCH_SPFPSET", 32);
+}
+
+static void
+loongarch_update_cpp_builtins (cpp_reader *pfile)
+{
+  builtin_undef ("__loongarch_double_float");
+  builtin_undef ("__loongarch_single_float");
+  /* These defines reflect the ABI in use, not whether the
+     FPU is directly accessible.  */
+  if (TARGET_DOUBLE_FLOAT_ABI)
+    builtin_define ("__loongarch_double_float=1");
+  else if (TARGET_SINGLE_FLOAT_ABI)
+    builtin_define ("__loongarch_single_float=1");
+
+  builtin_undef ("__loongarch_soft_float");
+  builtin_undef ("__loongarch_hard_float");
+  if (TARGET_DOUBLE_FLOAT_ABI || TARGET_SINGLE_FLOAT_ABI)
+    builtin_define ("__loongarch_hard_float=1");
+  else
+    builtin_define ("__loongarch_soft_float=1");
+
+
+  /* ISA Extensions.  */
+  if (TARGET_DOUBLE_FLOAT)
+    builtin_define ("__loongarch_frlen=64");
+  else if (TARGET_SINGLE_FLOAT)
+    builtin_define ("__loongarch_frlen=32");
+  else
+    builtin_define ("__loongarch_frlen=0");
+
+  loongarch_def_or_undef (TARGET_HARD_FLOAT && ISA_HAS_FRECIPE,
+			  "__loongarch_frecipe", pfile);
+
+  loongarch_def_or_undef (ISA_HAS_LSX, "__loongarch_simd", pfile);
+  loongarch_def_or_undef (ISA_HAS_LSX, "__loongarch_sx", pfile);
+  loongarch_def_or_undef (ISA_HAS_LASX, "__loongarch_asx", pfile);
+
+  builtin_undef ("__loongarch_simd_width");
+  if (ISA_HAS_LSX)
+    {
+      if (ISA_HAS_LASX)
+	builtin_define ("__loongarch_simd_width=256");
+      else
+	builtin_define ("__loongarch_simd_width=128");
+    }
+}
+
+void
+loongarch_cpu_cpp_builtins (cpp_reader *pfile)
+{
+  builtin_assert ("machine=loongarch");
+  builtin_assert ("cpu=loongarch");
 
+  loongarch_define_unconditional_macros (pfile);
+  loongarch_update_cpp_builtins (pfile);
 }
 
 /* Hook to validate the current #pragma GCC target and set the state, and