public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result.
@ 2020-12-01  9:29 Kito Cheng
  2020-12-01  9:29 ` [PATCH 2/2] RISC-V: Implement TARGET_COMPUTE_MULTILIB Kito Cheng
  2020-12-02  5:27 ` [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Jeff Law
  0 siblings, 2 replies; 6+ messages in thread
From: Kito Cheng @ 2020-12-01  9:29 UTC (permalink / raw)
  To: gcc-patches, kito.cheng, jimw; +Cc: Kito Cheng

Create a new hook to let target could override the multi-lib result,
the motivation is RISC-V might have very complicated multi-lib re-use
rule*, which is hard to maintain and use current multi-lib scripts,
we even hit the "argument list too long" error when we tried to add more
multi-lib reuse rule.

So I think it would be great to have a target specific way to determine
the multi-lib re-use rule, then we could write those rule in C, instead
of expand every possible case in MULTILIB_REUSE.

* Here is an example for RISC-V multi-lib rules:
https://gist.github.com/kito-cheng/0289cd42d9a756382e5afeb77b42b73b

gcc/ChangeLog:

	* common/common-target.def (compute_multilib): New.
	* common/common-targhooks.c (default_compute_multilib): New.
	* doc/tm.texi.in (TARGET_COMPUTE_MULTILIB): New.
	* doc/tm.texi: Regen.
	* gcc.c: Include common/common-target.h.
	(set_multilib_dir) Call targetm_common.compute_multilib.
	(SWITCH_LIVE): Move to opts.h.
	(SWITCH_FALSE): Ditto.
	(SWITCH_IGNORE): Ditto.
	(SWITCH_IGNORE_PERMANENTLY): Ditto.
	(SWITCH_KEEP_FOR_GCC): Ditto.
	(struct switchstr): Ditto.
	* opts.h (SWITCH_LIVE): Move from gcc.c.
	(SWITCH_FALSE): Ditto.
	(SWITCH_IGNORE): Ditto.
	(SWITCH_IGNORE_PERMANENTLY): Ditto.
	(SWITCH_KEEP_FOR_GCC): Ditto.
	(struct switchstr): Ditto.
---
 gcc/common/common-target.def  | 25 ++++++++++++++++++
 gcc/common/common-targhooks.c | 15 +++++++++++
 gcc/doc/tm.texi               |  5 ++++
 gcc/doc/tm.texi.in            |  3 +++
 gcc/gcc.c                     | 48 +++++++++--------------------------
 gcc/opts.h                    | 36 ++++++++++++++++++++++++++
 6 files changed, 96 insertions(+), 36 deletions(-)

diff --git a/gcc/common/common-target.def b/gcc/common/common-target.def
index 2f64ebe55de..296de01177d 100644
--- a/gcc/common/common-target.def
+++ b/gcc/common/common-target.def
@@ -84,6 +84,31 @@ DEFHOOK
  vec<const char *>, (int option_code, const char *prefix),
  default_get_valid_option_values)
 
+DEFHOOK
+(compute_multilib,
+ "Some target like RISC-V might have complicated multilib reuse rule which is\
+  hard to implemented on current multilib scheme, this hook allow target to\
+  override the result from built-in multilib mechanism.\
+  @var{switches} is the raw option list with @var{n_switches} items;\
+  @var{multilib_dir} is the multi-lib result which compute by the built-in\
+  multi-lib mechanism;\
+  @var{multilib_defaults} is the default options list for multi-lib; \
+  @var{multilib_select} is the string contain the list of supported multi-lib, \
+  and the option checking list. \
+  @var{multilib_matches}, @var{multilib_exclusions}, and @var{multilib_reuse} \
+  are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS} \
+  @var{MULTILIB_REUSE}. \
+  The default definition does nothing but return @var{multilib_dir} directly.",
+ const char *, (const struct switchstr *switches,
+		int n_switches,
+		const char *multilib_dir,
+		const char *multilib_defaults,
+		const char *multilib_select,
+		const char *multilib_matches,
+		const char *multilib_exclusions,
+		const char *multilib_reuse),
+ default_compute_multilib)
+
 /* Leave the boolean fields at the end.  */
 
 /* True if unwinding tables should be generated by default.  */
diff --git a/gcc/common/common-targhooks.c b/gcc/common/common-targhooks.c
index 554ed36c3d2..16a1a35e141 100644
--- a/gcc/common/common-targhooks.c
+++ b/gcc/common/common-targhooks.c
@@ -90,3 +90,18 @@ const struct default_options empty_optimization_table[] =
   {
     { OPT_LEVELS_NONE, 0, NULL, 0 }
   };
+
+/* Default version of TARGET_COMPUTE_MULTILIB.  */
+const char *
+default_compute_multilib(
+  const struct switchstr *,
+  int,
+  const char *multilib,
+  const char *,
+  const char *,
+  const char *,
+  const char *,
+  const char *)
+{
+  return multilib;
+}
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index 9f700b1c774..5d3015cb930 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -758,6 +758,11 @@ options are changed via @code{#pragma GCC optimize} or by using the
 Set target-dependent initial values of fields in @var{opts}.
 @end deftypefn
 
+@deftypefn {Common Target Hook} {const char *} TARGET_COMPUTE_MULTILIB (const struct switchstr *@var{switches}, int @var{n_switches}, const char *@var{multilib_dir}, const char *@var{multilib_defaults}, const char *@var{multilib_select}, const char *@var{multilib_matches}, const char *@var{multilib_exclusions}, const char *@var{multilib_reuse})
+Some target like RISC-V might have complicated multilib reuse rule which is  hard to implemented on current multilib scheme, this hook allow target to  override the result from built-in multilib mechanism.  @var{switches} is the raw option list with @var{n_switches} items;  @var{multilib_dir} is the multi-lib result which compute by the built-in  multi-lib mechanism;  @var{multilib_defaults} is the default options list for multi-lib;   @var{multilib_select} is the string contain the list of supported multi-lib,   and the option checking list.   @var{multilib_matches}, @var{multilib_exclusions}, and @var{multilib_reuse}   are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS}   @var{MULTILIB_REUSE}.   The default definition does nothing but return @var{multilib_dir} directly.
+@end deftypefn
+
+
 @defmac SWITCHABLE_TARGET
 Some targets need to switch between substantially different subtargets
 during compilation.  For example, the MIPS target has one subtarget for
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index 012cb1c53f0..a33dd79d6c4 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -736,6 +736,9 @@ options are changed via @code{#pragma GCC optimize} or by using the
 
 @hook TARGET_OPTION_INIT_STRUCT
 
+@hook TARGET_COMPUTE_MULTILIB
+
+
 @defmac SWITCHABLE_TARGET
 Some targets need to switch between substantially different subtargets
 during compilation.  For example, the MIPS target has one subtarget for
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 1d32375f648..9f8df9420aa 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -43,6 +43,7 @@ compilation is specified by a string called a "spec".  */
 #include "opts.h"
 #include "filenames.h"
 #include "spellcheck.h"
+#include "common/common-target.h"
 
 \f
 
@@ -3535,42 +3536,6 @@ execute (void)
   }
 }
 \f
-/* Find all the switches given to us
-   and make a vector describing them.
-   The elements of the vector are strings, one per switch given.
-   If a switch uses following arguments, then the `part1' field
-   is the switch itself and the `args' field
-   is a null-terminated vector containing the following arguments.
-   Bits in the `live_cond' field are:
-   SWITCH_LIVE to indicate this switch is true in a conditional spec.
-   SWITCH_FALSE to indicate this switch is overridden by a later switch.
-   SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
-   SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored.
-   SWITCH_KEEP_FOR_GCC to indicate that this switch, otherwise ignored,
-   should be included in COLLECT_GCC_OPTIONS.
-   in all do_spec calls afterwards.  Used for %<S from self specs.
-   The `known' field describes whether this is an internal switch.
-   The `validated' field describes whether any spec has looked at this switch;
-   if it remains false at the end of the run, the switch must be meaningless.
-   The `ordering' field is used to temporarily mark switches that have to be
-   kept in a specific order.  */
-
-#define SWITCH_LIVE    			(1 << 0)
-#define SWITCH_FALSE   			(1 << 1)
-#define SWITCH_IGNORE			(1 << 2)
-#define SWITCH_IGNORE_PERMANENTLY	(1 << 3)
-#define SWITCH_KEEP_FOR_GCC		(1 << 4)
-
-struct switchstr
-{
-  const char *part1;
-  const char **args;
-  unsigned int live_cond;
-  bool known;
-  bool validated;
-  bool ordering;
-};
-
 static struct switchstr *switches;
 
 static int n_switches;
@@ -9749,6 +9714,17 @@ set_multilib_dir (void)
       ++p;
     }
 
+  multilib_dir =
+    targetm_common.compute_multilib (
+      switches,
+      n_switches,
+      multilib_dir,
+      multilib_defaults,
+      multilib_select,
+      multilib_matches,
+      multilib_exclusions,
+      multilib_reuse);
+
   if (multilib_dir == NULL && multilib_os_dir != NULL
       && strcmp (multilib_os_dir, ".") == 0)
     {
diff --git a/gcc/opts.h b/gcc/opts.h
index d62bfcfdf6a..25fd41644c2 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -492,4 +492,40 @@ extern void prepend_xassembler_to_collect_as_options (const char *, obstack *);
   } \
   while (false)
 
+/* Find all the switches given to us
+   and make a vector describing them.
+   The elements of the vector are strings, one per switch given.
+   If a switch uses following arguments, then the `part1' field
+   is the switch itself and the `args' field
+   is a null-terminated vector containing the following arguments.
+   Bits in the `live_cond' field are:
+   SWITCH_LIVE to indicate this switch is true in a conditional spec.
+   SWITCH_FALSE to indicate this switch is overridden by a later switch.
+   SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
+   SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored.
+   SWITCH_KEEP_FOR_GCC to indicate that this switch, otherwise ignored,
+   should be included in COLLECT_GCC_OPTIONS.
+   in all do_spec calls afterwards.  Used for %<S from self specs.
+   The `known' field describes whether this is an internal switch.
+   The `validated' field describes whether any spec has looked at this switch;
+   if it remains false at the end of the run, the switch must be meaningless.
+   The `ordering' field is used to temporarily mark switches that have to be
+   kept in a specific order.  */
+
+#define SWITCH_LIVE    			(1 << 0)
+#define SWITCH_FALSE   			(1 << 1)
+#define SWITCH_IGNORE			(1 << 2)
+#define SWITCH_IGNORE_PERMANENTLY	(1 << 3)
+#define SWITCH_KEEP_FOR_GCC		(1 << 4)
+
+struct switchstr
+{
+  const char *part1;
+  const char **args;
+  unsigned int live_cond;
+  bool known;
+  bool validated;
+  bool ordering;
+};
+
 #endif
-- 
2.29.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/2] RISC-V: Implement TARGET_COMPUTE_MULTILIB
  2020-12-01  9:29 [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Kito Cheng
@ 2020-12-01  9:29 ` Kito Cheng
  2020-12-02  5:27 ` [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Jeff Law
  1 sibling, 0 replies; 6+ messages in thread
From: Kito Cheng @ 2020-12-01  9:29 UTC (permalink / raw)
  To: gcc-patches, kito.cheng, jimw; +Cc: Kito Cheng

Use TARGET_COMPUTE_MULTILIB to search the multi-lib reuse for riscv*-*-elf*,
according following rules:

 1. Check ABI is same.
 2. Check both has atomic extension or both don't have atomic extension.
    - Because mix soft and hard atomic operation doesn't make sense and
      won't work as expect.
 3. Check current arch is superset of the target multi-lib arch.
    - It might result slower performance or larger code size, but it
      safe to run.
 4. Pick most match multi-lib set if more than one multi-lib are pass
    the above checking.

Example for how to select multi-lib:
  We build code with -march=rv32imaf and -mabi=ilp32, and we have
  following 5 multi-lib set:

    1. rv32ia/ilp32
    2. rv32ima/ilp32
    3. rv32imf/ilp32
    4. rv32imaf/ilp32f
    5. rv32imafd/ilp32

  The first and second multi-lib is safe to like, 3rd multi-lib can't
  re-use becasue it don't have atomic extension, which is mismatch according
  rule 2, and the 4th multi-lib can't re-use too due to the ABI mismatch,
  the last multi-lib can't use since current arch is not superset of the
  arch of multi-lib.

And emit error if not found suitable multi-lib set, the error message
only emit when link with standard libraries.

Example for when error will be emitted:

  $ riscv64-unknown-elf-gcc -print-multi-lib
  .;
  rv32i/ilp32;@march=rv32i@mabi=ilp32
  rv32im/ilp32;@march=rv32im@mabi=ilp32
  rv32iac/ilp32;@march=rv32iac@mabi=ilp32
  rv32imac/ilp32;@march=rv32imac@mabi=ilp32
  rv32imafc/ilp32f;@march=rv32imafc@mabi=ilp32f
  rv64imac/lp64;@march=rv64imac@mabi=lp64

  // No actual linking, so no error emitted.
  $ riscv64-unknown-elf-gcc -print-multi-directory -march=rv32ia -mabi=ilp32
  .

  // Link to default libc and libgcc, so check the multi-lib, and emit
  // error because not found suitable multilib.
  $ riscv64-unknown-elf-gcc -march=rv32ia -mabi=ilp32 ~/hello.c
  riscv64-unknown-elf-gcc: fatal error: can't found suitable multilib set for '-march=rv32ia'/'-mabi=ilp32'
  compilation terminated.

  // No error emitted, because not link to stdlib.
  $ riscv64-unknown-elf-gcc -march=rv32ia -mabi=ilp32 ~/hello.c -nostdlib

  // No error emitted, because compile only.
  $ riscv64-unknown-elf-gcc -march=rv32ia -mabi=ilp32 ~/hello.c -c

gcc/ChangeLog:

	* common/config/riscv/riscv-common.c (riscv_subset_list::match_score):
	New.
	(find_last_appear_switch): Ditto.
	(struct multi_lib_info_t): Ditto.
	(riscv_no_matched_multi_lib): Ditto.
	(riscv_current_arch_str): Ditto.
	(riscv_current_abi_str): Ditto.
	(riscv_compute_multilib): Ditto.
	(TARGET_COMPUTE_MULTILIB): Defined.
	* config/riscv/elf.h (LIB_SPEC): Call riscv_multi_lib_check if
	doing link.
	(RISCV_USE_CUSTOMISED_MULTI_LIB): New.
	* config/riscv/riscv.h (riscv_multi_lib_check): New.
	(EXTRA_SPEC_FUNCTIONS): Add riscv_multi_lib_check.
---
 gcc/common/config/riscv/riscv-common.c | 253 +++++++++++++++++++++++++
 gcc/config/riscv/elf.h                 |   6 +-
 gcc/config/riscv/riscv.h               |   4 +-
 3 files changed, 261 insertions(+), 2 deletions(-)

diff --git a/gcc/common/config/riscv/riscv-common.c b/gcc/common/config/riscv/riscv-common.c
index 5e3ddcf3f81..481f0be9944 100644
--- a/gcc/common/config/riscv/riscv-common.c
+++ b/gcc/common/config/riscv/riscv-common.c
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "flags.h"
 #include "diagnostic-core.h"
 #include "config/riscv/riscv-protos.h"
+#include <vector>
 
 #define RISCV_DONT_CARE_VERSION -1
 
@@ -171,12 +172,31 @@ public:
 
   static riscv_subset_list *parse (const char *, location_t);
 
+  int match_score (riscv_subset_list *) const;
 };
 
 static const char *riscv_supported_std_ext (void);
 
 static riscv_subset_list *current_subset_list = NULL;
 
+/* struct for recording multi-lib info.  */
+struct riscv_multi_lib_info_t {
+  std::string path;
+  std::string arch_str;
+  std::string abi_str;
+  riscv_subset_list *subset_list;
+
+  static bool parse (struct riscv_multi_lib_info_t *,
+		     const std::string &);
+};
+
+/* Flag for checking if there is no suitable multi-lib found.  */
+static bool riscv_no_matched_multi_lib;
+
+/* Used for record value of -march and -mabi.  */
+static std::string riscv_current_arch_str;
+static std::string riscv_current_abi_str;
+
 riscv_subset_t::riscv_subset_t ()
   : name (), major_version (0), minor_version (0), next (NULL),
     explicit_version_p (false), implied_p (false)
@@ -202,6 +222,42 @@ riscv_subset_list::~riscv_subset_list ()
     }
 }
 
+/* Compute the match score of two arch string, return 0 if incompatible.  */
+int
+riscv_subset_list::match_score (riscv_subset_list *list) const
+{
+  riscv_subset_t *s;
+  int score = 0;
+  bool has_a_ext, list_has_a_ext;
+
+  /* Impossible to match if XLEN is different.  */
+  if (list->m_xlen != this->m_xlen)
+    return 0;
+
+  /* There is different code gen in libstdc++ and libatomic between w/ A-ext
+     and w/o A-ext, and it not work if using soft and hard atomic mechanism
+     at same time, so they are incompatible.  */
+  has_a_ext = this->lookup ("a") != NULL;
+  list_has_a_ext = list->lookup ("a") != NULL;
+
+  if (has_a_ext != list_has_a_ext)
+    return 0;
+
+
+  /* list must be subset of current this list, otherwise it not safe to
+     link.
+     TODO: We might give different weigth for each extension, but the rule could
+	   be complicated.
+     TODO: We might consider the version of each extension.  */
+  for (s = list->m_head; s != NULL; s = s->next)
+    if (this->lookup (s->name.c_str ()) != NULL)
+      score++;
+    else
+      return 0;
+
+  return score;
+}
+
 /* Get the rank for single-letter subsets, lower value meaning higher
    priority.  */
 
@@ -1109,6 +1165,203 @@ riscv_expand_arch_from_cpu (int argc ATTRIBUTE_UNUSED,
   return xasprintf ("-march=%s", arch.c_str());
 }
 
+static const char *
+find_last_appear_switch (
+  const struct switchstr *switches,
+  int n_switches,
+  const char *prefix)
+{
+  int i;
+  size_t len = strlen (prefix);
+
+  for (i = 0; i < n_switches; ++i) {
+    const struct switchstr *this_switch = &switches[n_switches - i - 1];
+
+    if (this_switch->live_cond & SWITCH_FALSE)
+      continue;
+
+    if (strncmp (this_switch->part1, prefix, len) == 0)
+      return this_switch->part1 + len;
+  }
+
+  return NULL;
+}
+
+bool
+riscv_multi_lib_info_t::parse (
+  struct riscv_multi_lib_info_t *multi_lib_info,
+  const std::string &path)
+{
+  if (path == ".")
+    {
+      multi_lib_info->arch_str = STRINGIZING (TARGET_RISCV_DEFAULT_ARCH);
+      multi_lib_info->abi_str = STRINGIZING (TARGET_RISCV_DEFAULT_ABI);
+      multi_lib_info->subset_list =
+	riscv_subset_list::parse(
+	  STRINGIZING (TARGET_RISCV_DEFAULT_ARCH),
+	  input_location);
+    }
+  else
+    {
+      size_t slash_pos = path.find('/');
+
+      if (slash_pos == std::string::npos)
+	return false;
+
+      multi_lib_info->arch_str = path.substr(0, slash_pos);
+      multi_lib_info->abi_str = path.substr(slash_pos + 1, path.length() - 1);
+   }
+
+  multi_lib_info->subset_list =
+    riscv_subset_list::parse(multi_lib_info->arch_str.c_str (), input_location);
+
+  return true;
+}
+
+const char *
+riscv_multi_lib_check (int argc ATTRIBUTE_UNUSED,
+		       const char **argv ATTRIBUTE_UNUSED)
+{
+  if (riscv_no_matched_multi_lib)
+    fatal_error (
+      input_location,
+      "Can't found suitable multilib set for %<-march=%s%>/%<-mabi=%s%>",
+      riscv_current_arch_str.c_str (),
+      riscv_current_abi_str.c_str ());
+
+  return "";
+}
+
+/* We only override this in bare-matel toolchain.  */
+#ifdef RISCV_USE_CUSTOMISED_MULTI_LIB
+/* Implement TARGET_COMPUTE_MULTILIB.  */
+
+static const char *
+riscv_compute_multilib (
+  const struct switchstr *switches,
+  int n_switches,
+  const char *multilib_dir,
+  const char *multilib_defaults ATTRIBUTE_UNUSED,
+  const char *multilib_select,
+  const char *multilib_matches ATTRIBUTE_UNUSED,
+  const char *multilib_exclusions ATTRIBUTE_UNUSED,
+  const char *multilib_reuse ATTRIBUTE_UNUSED)
+{
+  const char *p;
+  const char *this_path;
+  size_t this_path_len;
+  bool result;
+  riscv_no_matched_multi_lib = false;
+  riscv_subset_list *subset_list = NULL;
+
+  std::vector<riscv_multi_lib_info_t> multilib_infos;
+  riscv_multi_lib_info_t multilib_info;
+
+  /* Already found suitable, multi-lib, just use that.  */
+  if (multilib_dir != NULL)
+    return multilib_dir;
+
+  /* Find last march.  */
+  riscv_current_arch_str =
+    find_last_appear_switch (switches, n_switches, "march=");
+  /* Find mabi.  */
+  riscv_current_abi_str =
+    find_last_appear_switch (switches, n_switches, "mabi=");
+
+  /* Failed to find -march or -mabi, but it should not happened since we have
+     set both in OPTION_DEFAULT_SPECS.  */
+  if (riscv_current_arch_str.empty () || riscv_current_abi_str.empty ())
+    return multilib_dir;
+
+  subset_list = riscv_subset_list::parse (riscv_current_arch_str.c_str (),
+					  input_location);
+
+  /* Failed to parse -march, fallback to using what gcc use.  */
+  if (subset_list == NULL)
+    return multilib_dir;
+
+  /* Parsing MULTILIB_SELECT, ignore MULTILIB_REUSE here, we have our own rules.
+     TODO: most codes are grab from gcc.c, maybe we should refine that?  */
+  p = multilib_select;
+
+  while (*p != '\0')
+    {
+      /* Ignore newlines.  */
+      if (*p == '\n')
+	{
+	  ++p;
+	  continue;
+	}
+
+      /* Get the initial path.  */
+      this_path = p;
+      while (*p != ' ')
+	{
+	  if (*p == '\0')
+	    {
+	      fatal_error (input_location, "multilib select %qs %qs is invalid",
+			   multilib_select, multilib_reuse);
+	    }
+	  ++p;
+	}
+      this_path_len = p - this_path;
+      multilib_info.path = std::string (this_path, this_path_len);
+
+      while (*p != ';')
+	{
+	  if (*p == '\0')
+	    {
+	      fatal_error (input_location, "multilib select %qs %qs is invalid",
+			   multilib_select, multilib_reuse);
+	    }
+	  ++p;
+	}
+
+      result =
+	riscv_multi_lib_info_t::parse (
+	  &multilib_info,
+	  std::string (this_path, this_path_len));
+
+      if (result)
+	multilib_infos.push_back (multilib_info);
+
+      p++;
+    }
+
+  int match_score = 0;
+  int max_match_score = 0;
+  int best_match_multi_lib = -1;
+  /* Try to decition which set we should used.  */
+  /* We have 3 level dection tree here, ABI, A-ext, check input arch/ABI must be
+     superset of multi-lib arch.  */
+  for (size_t i = 0; i < multilib_infos.size (); ++i)
+    {
+      /* Check ABI is same first.  */
+      if (riscv_current_abi_str != multilib_infos[i].abi_str)
+	continue;
+
+      /* Found a potential compatible multi-lib setting!
+	 Calculate the match score.  */
+      match_score = subset_list->match_score (multilib_infos[i].subset_list);
+
+      /* Record highest match score multi-lib setting.  */
+      if (match_score > max_match_score)
+	best_match_multi_lib = i;
+    }
+
+  if (best_match_multi_lib == -1)
+    {
+      riscv_no_matched_multi_lib = true;
+      return multilib_dir;
+    }
+  else
+    return xstrdup (multilib_infos[best_match_multi_lib].path.c_str ());
+}
+
+#undef TARGET_COMPUTE_MULTILIB
+#define TARGET_COMPUTE_MULTILIB riscv_compute_multilib
+#endif
+
 
 /* Implement TARGET_OPTION_OPTIMIZATION_TABLE.  */
 static const struct default_options riscv_option_optimization_table[] =
diff --git a/gcc/config/riscv/elf.h b/gcc/config/riscv/elf.h
index 4e2d6464d02..4aeb7d6141c 100644
--- a/gcc/config/riscv/elf.h
+++ b/gcc/config/riscv/elf.h
@@ -25,10 +25,14 @@ along with GCC; see the file COPYING3.  If not see
 /* Link against Newlib libraries, because the ELF backend assumes Newlib.
    Handle the circular dependence between libc and libgloss. */
 #undef  LIB_SPEC
-#define LIB_SPEC "--start-group -lc %{!specs=nosys.specs:-lgloss} --end-group"
+#define LIB_SPEC \
+  "--start-group -lc %{!specs=nosys.specs:-lgloss} --end-group " \
+  "%{!nostartfiles:%{!nodefaultlibs:%{!nolibc:%{!nostdlib:%:riscv_multi_lib_check()}}}}"
 
 #undef  STARTFILE_SPEC
 #define STARTFILE_SPEC "crt0%O%s crtbegin%O%s"
 
 #undef  ENDFILE_SPEC
 #define ENDFILE_SPEC "crtend%O%s"
+
+#define RISCV_USE_CUSTOMISED_MULTI_LIB 1
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index df3003fbaa0..a92e0e220ad 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -43,11 +43,13 @@ along with GCC; see the file COPYING3.  If not see
 extern const char *riscv_expand_arch (int argc, const char **argv);
 extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv);
 extern const char *riscv_default_mtune (int argc, const char **argv);
+extern const char *riscv_multi_lib_check (int argc, const char **argv);
 
 # define EXTRA_SPEC_FUNCTIONS						\
   { "riscv_expand_arch", riscv_expand_arch },				\
   { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu },		\
-  { "riscv_default_mtune", riscv_default_mtune },
+  { "riscv_default_mtune", riscv_default_mtune },			\
+  { "riscv_multi_lib_check", riscv_multi_lib_check },
 
 /* Support for a compile-time default CPU, et cetera.  The rules are:
    --with-arch is ignored if -march or -mcpu is specified.
-- 
2.29.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result.
  2020-12-01  9:29 [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Kito Cheng
  2020-12-01  9:29 ` [PATCH 2/2] RISC-V: Implement TARGET_COMPUTE_MULTILIB Kito Cheng
@ 2020-12-02  5:27 ` Jeff Law
  2020-12-02  5:39   ` Kito Cheng
  1 sibling, 1 reply; 6+ messages in thread
From: Jeff Law @ 2020-12-02  5:27 UTC (permalink / raw)
  To: Kito Cheng, gcc-patches, kito.cheng, jimw



On 12/1/20 2:29 AM, Kito Cheng wrote:
> Create a new hook to let target could override the multi-lib result,
> the motivation is RISC-V might have very complicated multi-lib re-use
> rule*, which is hard to maintain and use current multi-lib scripts,
> we even hit the "argument list too long" error when we tried to add more
> multi-lib reuse rule.
>
> So I think it would be great to have a target specific way to determine
> the multi-lib re-use rule, then we could write those rule in C, instead
> of expand every possible case in MULTILIB_REUSE.
>
> * Here is an example for RISC-V multi-lib rules:
> https://gist.github.com/kito-cheng/0289cd42d9a756382e5afeb77b42b73b
>
> gcc/ChangeLog:
>
> 	* common/common-target.def (compute_multilib): New.
> 	* common/common-targhooks.c (default_compute_multilib): New.
> 	* doc/tm.texi.in (TARGET_COMPUTE_MULTILIB): New.
> 	* doc/tm.texi: Regen.
> 	* gcc.c: Include common/common-target.h.
> 	(set_multilib_dir) Call targetm_common.compute_multilib.
> 	(SWITCH_LIVE): Move to opts.h.
> 	(SWITCH_FALSE): Ditto.
> 	(SWITCH_IGNORE): Ditto.
> 	(SWITCH_IGNORE_PERMANENTLY): Ditto.
> 	(SWITCH_KEEP_FOR_GCC): Ditto.
> 	(struct switchstr): Ditto.
> 	* opts.h (SWITCH_LIVE): Move from gcc.c.
> 	(SWITCH_FALSE): Ditto.
> 	(SWITCH_IGNORE): Ditto.
> 	(SWITCH_IGNORE_PERMANENTLY): Ditto.
> 	(SWITCH_KEEP_FOR_GCC): Ditto.
> 	(struct switchstr): Ditto.
Can this defer to gcc-12?  We're well into stage3 at this point.

jeff


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result.
  2020-12-02  5:27 ` [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Jeff Law
@ 2020-12-02  5:39   ` Kito Cheng
  0 siblings, 0 replies; 6+ messages in thread
From: Kito Cheng @ 2020-12-02  5:39 UTC (permalink / raw)
  To: Jeff Law; +Cc: GCC Patches, Kito Cheng, Jim Wilson

Hi Jeff:

Defer to gcc-12 is OK to me, we could hold this on downstream :)

On Wed, Dec 2, 2020 at 1:27 PM Jeff Law <law@redhat.com> wrote:

>
>
> On 12/1/20 2:29 AM, Kito Cheng wrote:
> > Create a new hook to let target could override the multi-lib result,
> > the motivation is RISC-V might have very complicated multi-lib re-use
> > rule*, which is hard to maintain and use current multi-lib scripts,
> > we even hit the "argument list too long" error when we tried to add more
> > multi-lib reuse rule.
> >
> > So I think it would be great to have a target specific way to determine
> > the multi-lib re-use rule, then we could write those rule in C, instead
> > of expand every possible case in MULTILIB_REUSE.
> >
> > * Here is an example for RISC-V multi-lib rules:
> > https://gist.github.com/kito-cheng/0289cd42d9a756382e5afeb77b42b73b
> >
> > gcc/ChangeLog:
> >
> >       * common/common-target.def (compute_multilib): New.
> >       * common/common-targhooks.c (default_compute_multilib): New.
> >       * doc/tm.texi.in (TARGET_COMPUTE_MULTILIB): New.
> >       * doc/tm.texi: Regen.
> >       * gcc.c: Include common/common-target.h.
> >       (set_multilib_dir) Call targetm_common.compute_multilib.
> >       (SWITCH_LIVE): Move to opts.h.
> >       (SWITCH_FALSE): Ditto.
> >       (SWITCH_IGNORE): Ditto.
> >       (SWITCH_IGNORE_PERMANENTLY): Ditto.
> >       (SWITCH_KEEP_FOR_GCC): Ditto.
> >       (struct switchstr): Ditto.
> >       * opts.h (SWITCH_LIVE): Move from gcc.c.
> >       (SWITCH_FALSE): Ditto.
> >       (SWITCH_IGNORE): Ditto.
> >       (SWITCH_IGNORE_PERMANENTLY): Ditto.
> >       (SWITCH_KEEP_FOR_GCC): Ditto.
> >       (struct switchstr): Ditto.
> Can this defer to gcc-12?  We're well into stage3 at this point.
>
> jeff
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result.
  2021-07-21  9:28 ` [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Kito Cheng
@ 2021-09-01  0:16   ` Jim Wilson
  0 siblings, 0 replies; 6+ messages in thread
From: Jim Wilson @ 2021-09-01  0:16 UTC (permalink / raw)
  To: Kito Cheng; +Cc: GCC Patches, Kito Cheng

On Wed, Jul 21, 2021 at 2:28 AM Kito Cheng <kito.cheng@sifive.com> wrote:

> Create a new hook to let target could override the multi-lib result,
> the motivation is RISC-V might have very complicated multi-lib re-use
> rule*, which is hard to maintain and use current multi-lib scripts,
> we even hit the "argument list too long" error when we tried to add more
> multi-lib reuse rule.
>

This looks OK to me, though I would rewrite the docs a bit.

> +DEFHOOK
> +(compute_multilib,
> + "Some target like RISC-V might have complicated multilib reuse rule
> which is\
> +  hard to implemented on current multilib scheme, this hook allow target
> to\
> +  override the result from built-in multilib mechanism.\
> +  @var{switches} is the raw option list with @var{n_switches} items;\
> +  @var{multilib_dir} is the multi-lib result which compute by the
> built-in\
> +  multi-lib mechanism;\
> +  @var{multilib_defaults} is the default options list for multi-lib; \
> +  @var{multilib_select} is the string contain the list of supported
> multi-lib, \
> +  and the option checking list. \
> +  @var{multilib_matches}, @var{multilib_exclusions}, and
> @var{multilib_reuse} \
> +  are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS} \
> +  @var{MULTILIB_REUSE}. \
> +  The default definition does nothing but return @var{multilib_dir}
> directly.",
>

I'd suggest instead

"Some targets like RISC-V might have complicated multilib reuse rules
which\n\
are hard to implement with the current multilib scheme.  This hook allows\n\
targets to override the result from the built-in multilib mechanism.\n\
@var{switches} is the raw option list with @var{n_switches} items;\n\
@var{multilib_dir} is the multi-lib result which is computed by the
built-in\n\
multi-lib mechanism;\n\
@var{multilib_defaults} is the default options list for multi-lib;\n\
@var{multilib_select} is the string containing the list of supported\n\
multi-libs, and the option checking list.\n\
@var{multilib_matches}, @var{multilib_exclusions}, and
@var{multilib_reuse}\n\
are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS},\n\
and @var{MULTILIB_REUSE}.\n\
The default definition does nothing but return @var{multilib_dir} directly."

Jim

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result.
  2021-07-21  9:28 [PATCH 0/2] New target hook TARGET_COMPUTE_MULTILIB and implementation for RISC-V Kito Cheng
@ 2021-07-21  9:28 ` Kito Cheng
  2021-09-01  0:16   ` Jim Wilson
  0 siblings, 1 reply; 6+ messages in thread
From: Kito Cheng @ 2021-07-21  9:28 UTC (permalink / raw)
  To: gcc-patches, kito.cheng, jimw; +Cc: Kito Cheng

Create a new hook to let target could override the multi-lib result,
the motivation is RISC-V might have very complicated multi-lib re-use
rule*, which is hard to maintain and use current multi-lib scripts,
we even hit the "argument list too long" error when we tried to add more
multi-lib reuse rule.

So I think it would be great to have a target specific way to determine
the multi-lib re-use rule, then we could write those rule in C, instead
of expand every possible case in MULTILIB_REUSE.

* Here is an example for RISC-V multi-lib rules:
https://gist.github.com/kito-cheng/0289cd42d9a756382e5afeb77b42b73b

gcc/ChangeLog:

	* common/common-target.def (compute_multilib): New.
	* common/common-targhooks.c (default_compute_multilib): New.
	* doc/tm.texi.in (TARGET_COMPUTE_MULTILIB): New.
	* doc/tm.texi: Regen.
	* gcc.c: Include common/common-target.h.
	(set_multilib_dir) Call targetm_common.compute_multilib.
	(SWITCH_LIVE): Move to opts.h.
	(SWITCH_FALSE): Ditto.
	(SWITCH_IGNORE): Ditto.
	(SWITCH_IGNORE_PERMANENTLY): Ditto.
	(SWITCH_KEEP_FOR_GCC): Ditto.
	(struct switchstr): Ditto.
	* opts.h (SWITCH_LIVE): Move from gcc.c.
	(SWITCH_FALSE): Ditto.
	(SWITCH_IGNORE): Ditto.
	(SWITCH_IGNORE_PERMANENTLY): Ditto.
	(SWITCH_KEEP_FOR_GCC): Ditto.
	(struct switchstr): Ditto.
---
 gcc/common/common-target.def  | 25 ++++++++++++++++++
 gcc/common/common-targhooks.c | 15 +++++++++++
 gcc/doc/tm.texi               |  5 ++++
 gcc/doc/tm.texi.in            |  3 +++
 gcc/gcc.c                     | 48 +++++++++--------------------------
 gcc/opts.h                    | 36 ++++++++++++++++++++++++++
 6 files changed, 96 insertions(+), 36 deletions(-)

diff --git a/gcc/common/common-target.def b/gcc/common/common-target.def
index f54590a2a54..a720ecbea98 100644
--- a/gcc/common/common-target.def
+++ b/gcc/common/common-target.def
@@ -84,6 +84,31 @@ The result will be pruned to cases with PREFIX if not NULL.",
  vec<const char *>, (int option_code, const char *prefix),
  default_get_valid_option_values)
 
+DEFHOOK
+(compute_multilib,
+ "Some target like RISC-V might have complicated multilib reuse rule which is\
+  hard to implemented on current multilib scheme, this hook allow target to\
+  override the result from built-in multilib mechanism.\
+  @var{switches} is the raw option list with @var{n_switches} items;\
+  @var{multilib_dir} is the multi-lib result which compute by the built-in\
+  multi-lib mechanism;\
+  @var{multilib_defaults} is the default options list for multi-lib; \
+  @var{multilib_select} is the string contain the list of supported multi-lib, \
+  and the option checking list. \
+  @var{multilib_matches}, @var{multilib_exclusions}, and @var{multilib_reuse} \
+  are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS} \
+  @var{MULTILIB_REUSE}. \
+  The default definition does nothing but return @var{multilib_dir} directly.",
+ const char *, (const struct switchstr *switches,
+		int n_switches,
+		const char *multilib_dir,
+		const char *multilib_defaults,
+		const char *multilib_select,
+		const char *multilib_matches,
+		const char *multilib_exclusions,
+		const char *multilib_reuse),
+ default_compute_multilib)
+
 /* Leave the boolean fields at the end.  */
 
 /* True if unwinding tables should be generated by default.  */
diff --git a/gcc/common/common-targhooks.c b/gcc/common/common-targhooks.c
index 325f199bff3..1477aeeb536 100644
--- a/gcc/common/common-targhooks.c
+++ b/gcc/common/common-targhooks.c
@@ -90,3 +90,18 @@ const struct default_options empty_optimization_table[] =
   {
     { OPT_LEVELS_NONE, 0, NULL, 0 }
   };
+
+/* Default version of TARGET_COMPUTE_MULTILIB.  */
+const char *
+default_compute_multilib(
+  const struct switchstr *,
+  int,
+  const char *multilib,
+  const char *,
+  const char *,
+  const char *,
+  const char *,
+  const char *)
+{
+  return multilib;
+}
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index c8f4abe3e41..0268ea77996 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -778,6 +778,11 @@ options are changed via @code{#pragma GCC optimize} or by using the
 Set target-dependent initial values of fields in @var{opts}.
 @end deftypefn
 
+@deftypefn {Common Target Hook} {const char *} TARGET_COMPUTE_MULTILIB (const struct switchstr *@var{switches}, int @var{n_switches}, const char *@var{multilib_dir}, const char *@var{multilib_defaults}, const char *@var{multilib_select}, const char *@var{multilib_matches}, const char *@var{multilib_exclusions}, const char *@var{multilib_reuse})
+Some target like RISC-V might have complicated multilib reuse rule which is  hard to implemented on current multilib scheme, this hook allow target to  override the result from built-in multilib mechanism.  @var{switches} is the raw option list with @var{n_switches} items;  @var{multilib_dir} is the multi-lib result which compute by the built-in  multi-lib mechanism;  @var{multilib_defaults} is the default options list for multi-lib;   @var{multilib_select} is the string contain the list of supported multi-lib,   and the option checking list.   @var{multilib_matches}, @var{multilib_exclusions}, and @var{multilib_reuse}   are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS}   @var{MULTILIB_REUSE}.   The default definition does nothing but return @var{multilib_dir} directly.
+@end deftypefn
+
+
 @defmac SWITCHABLE_TARGET
 Some targets need to switch between substantially different subtargets
 during compilation.  For example, the MIPS target has one subtarget for
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index 9c4b5016053..6f93edc15c0 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -736,6 +736,9 @@ options are changed via @code{#pragma GCC optimize} or by using the
 
 @hook TARGET_OPTION_INIT_STRUCT
 
+@hook TARGET_COMPUTE_MULTILIB
+
+
 @defmac SWITCHABLE_TARGET
 Some targets need to switch between substantially different subtargets
 during compilation.  For example, the MIPS target has one subtarget for
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 3e98bc7973e..dafe754267a 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -43,6 +43,7 @@ compilation is specified by a string called a "spec".  */
 #include "opts.h"
 #include "filenames.h"
 #include "spellcheck.h"
+#include "common/common-target.h"
 
 \f
 
@@ -3573,42 +3574,6 @@ execute (void)
   }
 }
 \f
-/* Find all the switches given to us
-   and make a vector describing them.
-   The elements of the vector are strings, one per switch given.
-   If a switch uses following arguments, then the `part1' field
-   is the switch itself and the `args' field
-   is a null-terminated vector containing the following arguments.
-   Bits in the `live_cond' field are:
-   SWITCH_LIVE to indicate this switch is true in a conditional spec.
-   SWITCH_FALSE to indicate this switch is overridden by a later switch.
-   SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
-   SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored.
-   SWITCH_KEEP_FOR_GCC to indicate that this switch, otherwise ignored,
-   should be included in COLLECT_GCC_OPTIONS.
-   in all do_spec calls afterwards.  Used for %<S from self specs.
-   The `known' field describes whether this is an internal switch.
-   The `validated' field describes whether any spec has looked at this switch;
-   if it remains false at the end of the run, the switch must be meaningless.
-   The `ordering' field is used to temporarily mark switches that have to be
-   kept in a specific order.  */
-
-#define SWITCH_LIVE    			(1 << 0)
-#define SWITCH_FALSE   			(1 << 1)
-#define SWITCH_IGNORE			(1 << 2)
-#define SWITCH_IGNORE_PERMANENTLY	(1 << 3)
-#define SWITCH_KEEP_FOR_GCC		(1 << 4)
-
-struct switchstr
-{
-  const char *part1;
-  const char **args;
-  unsigned int live_cond;
-  bool known;
-  bool validated;
-  bool ordering;
-};
-
 static struct switchstr *switches;
 
 static int n_switches;
@@ -9855,6 +9820,17 @@ set_multilib_dir (void)
       ++p;
     }
 
+  multilib_dir =
+    targetm_common.compute_multilib (
+      switches,
+      n_switches,
+      multilib_dir,
+      multilib_defaults,
+      multilib_select,
+      multilib_matches,
+      multilib_exclusions,
+      multilib_reuse);
+
   if (multilib_dir == NULL && multilib_os_dir != NULL
       && strcmp (multilib_os_dir, ".") == 0)
     {
diff --git a/gcc/opts.h b/gcc/opts.h
index bafc790112b..f99a469364c 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -500,4 +500,40 @@ extern char *gen_producer_string (const char *language_string,
   } \
   while (false)
 
+/* Find all the switches given to us
+   and make a vector describing them.
+   The elements of the vector are strings, one per switch given.
+   If a switch uses following arguments, then the `part1' field
+   is the switch itself and the `args' field
+   is a null-terminated vector containing the following arguments.
+   Bits in the `live_cond' field are:
+   SWITCH_LIVE to indicate this switch is true in a conditional spec.
+   SWITCH_FALSE to indicate this switch is overridden by a later switch.
+   SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
+   SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored.
+   SWITCH_KEEP_FOR_GCC to indicate that this switch, otherwise ignored,
+   should be included in COLLECT_GCC_OPTIONS.
+   in all do_spec calls afterwards.  Used for %<S from self specs.
+   The `known' field describes whether this is an internal switch.
+   The `validated' field describes whether any spec has looked at this switch;
+   if it remains false at the end of the run, the switch must be meaningless.
+   The `ordering' field is used to temporarily mark switches that have to be
+   kept in a specific order.  */
+
+#define SWITCH_LIVE    			(1 << 0)
+#define SWITCH_FALSE   			(1 << 1)
+#define SWITCH_IGNORE			(1 << 2)
+#define SWITCH_IGNORE_PERMANENTLY	(1 << 3)
+#define SWITCH_KEEP_FOR_GCC		(1 << 4)
+
+struct switchstr
+{
+  const char *part1;
+  const char **args;
+  unsigned int live_cond;
+  bool known;
+  bool validated;
+  bool ordering;
+};
+
 #endif
-- 
2.31.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-09-01  0:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-01  9:29 [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Kito Cheng
2020-12-01  9:29 ` [PATCH 2/2] RISC-V: Implement TARGET_COMPUTE_MULTILIB Kito Cheng
2020-12-02  5:27 ` [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Jeff Law
2020-12-02  5:39   ` Kito Cheng
2021-07-21  9:28 [PATCH 0/2] New target hook TARGET_COMPUTE_MULTILIB and implementation for RISC-V Kito Cheng
2021-07-21  9:28 ` [PATCH 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result Kito Cheng
2021-09-01  0:16   ` Jim Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).