[10/11] aarch64: Refactor aarch64_rewrite_mcpu

Message ID 0260e863-c9a5-67a3-d371-e5882d15fca7@e124511.cambridge.arm.com
State New
Headers
Series aarch64: Refactor target parsing |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed

Commit Message

Andrew Carlotti Jan. 10, 2025, 5:24 p.m. UTC
  Use aarch64_validate_cpu instead of the existing duplicate (and worse)
version of the -mcpu parsing code.

The original code used fatal_error; I'm guessing that using error
instead should be ok.

gcc/ChangeLog:

	* common/config/aarch64/aarch64-common.cc
	(aarch64_rewrite_selected_cpu): Refactor and inline into...
	(aarch64_rewrite_mcpu): this.
	* config/aarch64/aarch64-protos.h
	(aarch64_rewrite_selected_cpu): Delete.
  

Comments

Richard Sandiford Jan. 16, 2025, 11:56 a.m. UTC | #1
Andrew Carlotti <andrew.carlotti@arm.com> writes:
> Use aarch64_validate_cpu instead of the existing duplicate (and worse)
> version of the -mcpu parsing code.
>
> The original code used fatal_error; I'm guessing that using error
> instead should be ok.
>
> gcc/ChangeLog:
>
> 	* common/config/aarch64/aarch64-common.cc
> 	(aarch64_rewrite_selected_cpu): Refactor and inline into...
> 	(aarch64_rewrite_mcpu): this.
> 	* config/aarch64/aarch64-protos.h
> 	(aarch64_rewrite_selected_cpu): Delete.

OK, thanks.

Richard

> diff --git a/gcc/common/config/aarch64/aarch64-common.cc b/gcc/common/config/aarch64/aarch64-common.cc
> index 297210e3809255d51b1aff4c827501534fae9546..1848d31c2c23e053535458044e0fcfd38b8f659b 100644
> --- a/gcc/common/config/aarch64/aarch64-common.cc
> +++ b/gcc/common/config/aarch64/aarch64-common.cc
> @@ -741,60 +741,29 @@ aarch64_rewrite_march (int argc, const char **argv)
>    return xstrdup (outstr.c_str ());
>  }
>  
> -/* Attempt to rewrite NAME, which has been passed on the command line
> -   as a -mcpu option to an equivalent -march value.  If we can do so,
> -   return the new string, otherwise return an error.  */
> +/* Called by the driver to rewrite a name passed to the -mcpu argument
> +   to an equivalent -march value to be passed to the assembler.  The
> +   names passed from the commend line will be in ARGV, we want
> +   to use the right-most argument, which should be in
> +   ARGV[ARGC - 1].  ARGC should always be greater than 0.  */
>  
>  const char *
> -aarch64_rewrite_selected_cpu (const char *name)
> +aarch64_rewrite_mcpu (int argc, const char **argv)
>  {
> -  std::string original_string (name);
> -  std::string extension_str;
> -  std::string processor;
> -  size_t extension_pos = original_string.find_first_of ('+');
> -
> -  /* Strip and save the extension string.  */
> -  if (extension_pos != std::string::npos)
> -    {
> -      processor = original_string.substr (0, extension_pos);
> -      extension_str = original_string.substr (extension_pos,
> -					      std::string::npos);
> -    }
> -  else
> -    {
> -      /* No extensions.  */
> -      processor = original_string;
> -    }
> -
> -  const struct processor_info* p_to_a;
> -  for (p_to_a = all_cores;
> -       p_to_a->arch != aarch64_no_arch;
> -       p_to_a++)
> -    {
> -      if (p_to_a->name == processor)
> -	break;
> -    }
> -
> -  const struct arch_info* a_to_an;
> -  for (a_to_an = all_architectures;
> -       a_to_an->arch != aarch64_no_arch;
> -       a_to_an++)
> -    {
> -      if (a_to_an->arch == p_to_a->arch)
> -	break;
> -    }
> +  gcc_assert (argc);
> +  const char *name = argv[argc - 1];
> +  aarch64_cpu cpu;
> +  aarch64_feature_flags flags;
>  
> -  /* We couldn't find that processor name, or the processor name we
> -     found does not map to an architecture we understand.  */
> -  if (p_to_a->arch == aarch64_no_arch
> -      || a_to_an->arch == aarch64_no_arch)
> -    fatal_error (input_location, "unknown value %qs for %<-mcpu%>", name);
> +  aarch64_validate_mcpu (name, &cpu, &flags);
>  
> -  aarch64_feature_flags extensions = p_to_a->flags;
> -  aarch64_parse_extension (extension_str.c_str (), &extensions, NULL);
> +  const struct processor_info *entry;
> +  for (entry = all_cores; entry->processor != aarch64_no_cpu; entry++)
> +    if (entry->processor == cpu)
> +      break;
>  
> -  std::string outstr = aarch64_get_arch_string_for_assembler (a_to_an->arch,
> -							      extensions);
> +  std::string outstr = aarch64_get_arch_string_for_assembler (entry->arch,
> +							      flags);
>  
>    /* We are going to memory leak here, nobody elsewhere
>       in the callchain is going to clean up after us.  The alternative is
> @@ -803,19 +772,6 @@ aarch64_rewrite_selected_cpu (const char *name)
>    return xstrdup (outstr.c_str ());
>  }
>  
> -/* Called by the driver to rewrite a name passed to the -mcpu
> -   argument in preparation to be passed to the assembler.  The
> -   names passed from the commend line will be in ARGV, we want
> -   to use the right-most argument, which should be in
> -   ARGV[ARGC - 1].  ARGC should always be greater than 0.  */
> -
> -const char *
> -aarch64_rewrite_mcpu (int argc, const char **argv)
> -{
> -  gcc_assert (argc);
> -  return aarch64_rewrite_selected_cpu (argv[argc - 1]);
> -}
> -
>  /* Checks to see if the host CPU may not be Cortex-A53 or an unknown Armv8-a
>     baseline CPU.  */
>  
> diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h
> index b27da1e25720da06712da0eff1d527e23408a59f..4235f4a0ca51af49c2852a420f1056727b24f345 100644
> --- a/gcc/config/aarch64/aarch64-protos.h
> +++ b/gcc/config/aarch64/aarch64-protos.h
> @@ -1210,7 +1210,6 @@ bool aarch64_validate_march (const char *, aarch64_arch *,
>  bool aarch64_validate_mcpu (const char *, aarch64_cpu *,
>  			    aarch64_feature_flags *);
>  bool aarch64_validate_mtune (const char *, aarch64_cpu *);
> -const char *aarch64_rewrite_selected_cpu (const char *name);
>  std::string aarch64_get_extension_string_for_isa_flags (aarch64_feature_flags,
>  							aarch64_feature_flags);
>  std::string aarch64_get_arch_string_for_assembler (aarch64_arch,
  

Patch

diff --git a/gcc/common/config/aarch64/aarch64-common.cc b/gcc/common/config/aarch64/aarch64-common.cc
index 297210e3809255d51b1aff4c827501534fae9546..1848d31c2c23e053535458044e0fcfd38b8f659b 100644
--- a/gcc/common/config/aarch64/aarch64-common.cc
+++ b/gcc/common/config/aarch64/aarch64-common.cc
@@ -741,60 +741,29 @@  aarch64_rewrite_march (int argc, const char **argv)
   return xstrdup (outstr.c_str ());
 }
 
-/* Attempt to rewrite NAME, which has been passed on the command line
-   as a -mcpu option to an equivalent -march value.  If we can do so,
-   return the new string, otherwise return an error.  */
+/* Called by the driver to rewrite a name passed to the -mcpu argument
+   to an equivalent -march value to be passed to the assembler.  The
+   names passed from the commend line will be in ARGV, we want
+   to use the right-most argument, which should be in
+   ARGV[ARGC - 1].  ARGC should always be greater than 0.  */
 
 const char *
-aarch64_rewrite_selected_cpu (const char *name)
+aarch64_rewrite_mcpu (int argc, const char **argv)
 {
-  std::string original_string (name);
-  std::string extension_str;
-  std::string processor;
-  size_t extension_pos = original_string.find_first_of ('+');
-
-  /* Strip and save the extension string.  */
-  if (extension_pos != std::string::npos)
-    {
-      processor = original_string.substr (0, extension_pos);
-      extension_str = original_string.substr (extension_pos,
-					      std::string::npos);
-    }
-  else
-    {
-      /* No extensions.  */
-      processor = original_string;
-    }
-
-  const struct processor_info* p_to_a;
-  for (p_to_a = all_cores;
-       p_to_a->arch != aarch64_no_arch;
-       p_to_a++)
-    {
-      if (p_to_a->name == processor)
-	break;
-    }
-
-  const struct arch_info* a_to_an;
-  for (a_to_an = all_architectures;
-       a_to_an->arch != aarch64_no_arch;
-       a_to_an++)
-    {
-      if (a_to_an->arch == p_to_a->arch)
-	break;
-    }
+  gcc_assert (argc);
+  const char *name = argv[argc - 1];
+  aarch64_cpu cpu;
+  aarch64_feature_flags flags;
 
-  /* We couldn't find that processor name, or the processor name we
-     found does not map to an architecture we understand.  */
-  if (p_to_a->arch == aarch64_no_arch
-      || a_to_an->arch == aarch64_no_arch)
-    fatal_error (input_location, "unknown value %qs for %<-mcpu%>", name);
+  aarch64_validate_mcpu (name, &cpu, &flags);
 
-  aarch64_feature_flags extensions = p_to_a->flags;
-  aarch64_parse_extension (extension_str.c_str (), &extensions, NULL);
+  const struct processor_info *entry;
+  for (entry = all_cores; entry->processor != aarch64_no_cpu; entry++)
+    if (entry->processor == cpu)
+      break;
 
-  std::string outstr = aarch64_get_arch_string_for_assembler (a_to_an->arch,
-							      extensions);
+  std::string outstr = aarch64_get_arch_string_for_assembler (entry->arch,
+							      flags);
 
   /* We are going to memory leak here, nobody elsewhere
      in the callchain is going to clean up after us.  The alternative is
@@ -803,19 +772,6 @@  aarch64_rewrite_selected_cpu (const char *name)
   return xstrdup (outstr.c_str ());
 }
 
-/* Called by the driver to rewrite a name passed to the -mcpu
-   argument in preparation to be passed to the assembler.  The
-   names passed from the commend line will be in ARGV, we want
-   to use the right-most argument, which should be in
-   ARGV[ARGC - 1].  ARGC should always be greater than 0.  */
-
-const char *
-aarch64_rewrite_mcpu (int argc, const char **argv)
-{
-  gcc_assert (argc);
-  return aarch64_rewrite_selected_cpu (argv[argc - 1]);
-}
-
 /* Checks to see if the host CPU may not be Cortex-A53 or an unknown Armv8-a
    baseline CPU.  */
 
diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h
index b27da1e25720da06712da0eff1d527e23408a59f..4235f4a0ca51af49c2852a420f1056727b24f345 100644
--- a/gcc/config/aarch64/aarch64-protos.h
+++ b/gcc/config/aarch64/aarch64-protos.h
@@ -1210,7 +1210,6 @@  bool aarch64_validate_march (const char *, aarch64_arch *,
 bool aarch64_validate_mcpu (const char *, aarch64_cpu *,
 			    aarch64_feature_flags *);
 bool aarch64_validate_mtune (const char *, aarch64_cpu *);
-const char *aarch64_rewrite_selected_cpu (const char *name);
 std::string aarch64_get_extension_string_for_isa_flags (aarch64_feature_flags,
 							aarch64_feature_flags);
 std::string aarch64_get_arch_string_for_assembler (aarch64_arch,