https://gcc.gnu.org/g:e8d2229382cad527abf48ab056122f3afd8c1263

commit r17-6458-ge8d2229382cad527abf48ab056122f3afd8c1263
Author: Kyrylo Tkachov <[email protected]>
Date:   Tue Nov 10 13:45:30 2026 +0100

    aarch64: Size the ACLE function table for its workload
    
    Handling the arm_neon.h and arm_sve.h pragmas registers several thousand
    functions between them, but the table they go into starts at 1023 slots.  It
    therefore expands repeatedly during registration, rehashing everything it
    holds each time, and the vector of registered functions reallocates its way 
up
    alongside it.
    
    Derive the size from the function group tables rather than hard-coding it, 
so
    that it keeps up as intrinsics are added.  count_functions mirrors the
    expansion that build_all performs over each group's predication, group and
    type suffixes.  That is a lower bound, since a shape may expand a group
    further, so double it, which also keeps the table below the load factor at
    which hash_table would expand.  Both the table and the vector are still
    allocated lazily, on the first pragma, so a translation unit that includes
    neither header is unaffected.  They are shared by all the pragmas, so 
reserve
    the space once, together with the table, rather than every time a pragma is
    handled.  Reserving on every pragma grew the vector again when arm_sme.h was
    included after arm_neon.h and arm_sve.h.
    
    The group tables are already constexpr, and what they are counting is fixed
    when GCC itself is built, so make the counting constexpr too.  That needs 
the
    type, group and predication suffix arrays they point into to be constexpr
    rather than merely const.  max_registered_functions then becomes a constant
    rather than a function with a lazily initialised cache, and no code for it 
is
    emitted at all.
    
    Instructions retired for -fsyntax-only on a file that includes just the
    header, cc1 built with --enable-checking=release:
    
                         arm_neon.h   arm_sve.h   both
      change                   -0.1%       -1.7%  -0.4%
    
    The gain on arm_neon.h is small because most of the Neon intrinsics are not
    registered through this table yet.  It grows as more of them are migrated.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64-acle-builtins.cc (count_functions): New
            constexpr function.
            (max_registered_functions): New constant.
            (function_builder::function_builder): Use it to size function_table
            and reserve registered_functions.
            * config/aarch64/aarch64-acle-builtins.h (DEF_SVE_TYPES_ARRAY)
            (types_none): Make the array constexpr.
            (groups_none, preds_none): Likewise, and the other group and
            predication suffix arrays.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/config/aarch64/aarch64-acle-builtins.cc | 58 ++++++++++++++++++++++++++++-
 gcc/config/aarch64/aarch64-acle-builtins.h  | 56 +++++++++++++++-------------
 2 files changed, 88 insertions(+), 26 deletions(-)

diff --git a/gcc/config/aarch64/aarch64-acle-builtins.cc 
b/gcc/config/aarch64/aarch64-acle-builtins.cc
index 692b1c818b31..a817d971ddfc 100644
--- a/gcc/config/aarch64/aarch64-acle-builtins.cc
+++ b/gcc/config/aarch64/aarch64-acle-builtins.cc
@@ -570,6 +570,47 @@ sve_alignment_switcher::~sve_alignment_switcher ()
   maximum_field_alignment = m_old_maximum_field_alignment;
 }
 
+/* Return the number of functions that the NUM_GROUPS entries of GROUPS
+   define.  build_all expands each group over every combination of its
+   predication, group and type suffixes, so mirror that here.  */
+static constexpr unsigned int
+count_functions (const function_group_info *groups, unsigned int num_groups)
+{
+  unsigned int count = 0;
+  for (unsigned int i = 0; i < num_groups; ++i)
+    {
+      unsigned int npreds = 0;
+      while (groups[i].preds[npreds] != NUM_PREDS)
+       ++npreds;
+
+      unsigned int ngroups = 0;
+      while (groups[i].groups[ngroups] != NUM_GROUP_SUFFIXES)
+       ++ngroups;
+
+      /* build_all always builds the first type suffix, then continues until
+        the sentinel.  */
+      unsigned int ntypes = 1;
+      if (groups[i].types[0][0] != NUM_TYPE_SUFFIXES)
+       while (groups[i].types[ntypes][0] != NUM_TYPE_SUFFIXES)
+         ++ntypes;
+
+      count += npreds * ngroups * ntypes;
+    }
+  return count;
+}
+
+/* The number of functions that the ACLE pragmas can register between them.
+   Derived from the function tables themselves, so that it stays right as
+   intrinsics are added.  The tables are constexpr, so this is folded at
+   compiler build time.  */
+static constexpr unsigned int max_registered_functions
+  = (count_functions (neon_function_groups, ARRAY_SIZE (neon_function_groups))
+     + count_functions (function_groups, ARRAY_SIZE (function_groups))
+     + count_functions (neon_sve_function_groups,
+                       ARRAY_SIZE (neon_sve_function_groups))
+     + count_functions (sme_function_groups,
+                       ARRAY_SIZE (sme_function_groups)));
+
 function_builder::function_builder (handle_pragma_index pragma_index,
                                    bool function_nulls)
 {
@@ -587,8 +628,23 @@ function_builder::function_builder (handle_pragma_index 
pragma_index,
 
   gcc_obstack_init (&m_string_obstack);
 
+  /* The pragmas register several thousand functions between them, so starting
+     from a small table just means rehashing all of them several times over.
+     The table and the vector are shared by all the pragmas, so size them once,
+     for all of the pragmas together, when the first one is handled.
+     max_registered_functions is a lower bound rather than an exact count:
+     it mirrors build_all, but a shape is free to expand a group further, for
+     instance over several mode suffixes.  Double it, which also keeps the
+     table under the m_size * 3 <= m_n_elements * 4 point at which it would
+     expand.  */
   if (!function_table)
-    function_table = hash_table<registered_function_hasher>::create_ggc (1023);
+    {
+      function_table = hash_table<registered_function_hasher>::create_ggc
+       (max_registered_functions * 2);
+
+      /* Each instance can also register a decl for its overloaded alias.  */
+      vec_safe_reserve (registered_functions, max_registered_functions * 2);
+    }
 }
 
 function_builder::~function_builder ()
diff --git a/gcc/config/aarch64/aarch64-acle-builtins.h 
b/gcc/config/aarch64/aarch64-acle-builtins.h
index 9742e7413172..b5b1579c8c00 100644
--- a/gcc/config/aarch64/aarch64-acle-builtins.h
+++ b/gcc/config/aarch64/aarch64-acle-builtins.h
@@ -2151,13 +2151,13 @@ function_expander::result_mode () const
 /* Create an array that can be used in aarch64-sve-builtins.def to
    select the type suffixes in TYPES_<NAME>.  */
 #define DEF_SVE_TYPES_ARRAY(NAME) \
-  static const type_suffix_triple types_##NAME[] = { \
+  static constexpr type_suffix_triple types_##NAME[] = { \
     TYPES_##NAME (DEF_VECTOR_TYPE, DEF_DOUBLE_TYPE, DEF_TRIPLE_TYPE), \
     { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES } \
   }
 
 /* For functions that don't take any type suffixes.  */
-static const type_suffix_triple types_none[] = {
+static constexpr type_suffix_triple types_none[] = {
   { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES },
   { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES }
 };
@@ -2324,97 +2324,103 @@ DEF_SVE_TYPES_ARRAY (neon_tbl_other);
 DEF_SVE_TYPES_ARRAY (neon_luti2_b);
 DEF_SVE_TYPES_ARRAY (neon_luti2_h);
 
-static const group_suffix_index groups_none[] = {
+static constexpr group_suffix_index groups_none[] = {
   GROUP_none, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_x2[] = { GROUP_x2, NUM_GROUP_SUFFIXES };
+static constexpr group_suffix_index groups_x2[] = {
+  GROUP_x2, NUM_GROUP_SUFFIXES
+};
 
-static const group_suffix_index groups_x12[] = {
+static constexpr group_suffix_index groups_x12[] = {
   GROUP_none, GROUP_x2, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_x4[] = { GROUP_x4, NUM_GROUP_SUFFIXES };
+static constexpr group_suffix_index groups_x4[] = {
+  GROUP_x4, NUM_GROUP_SUFFIXES
+};
 
-static const group_suffix_index groups_x24[] = {
+static constexpr group_suffix_index groups_x24[] = {
   GROUP_x2, GROUP_x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_x124[] = {
+static constexpr group_suffix_index groups_x124[] = {
   GROUP_none, GROUP_x2, GROUP_x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_x1234[] = {
+static constexpr group_suffix_index groups_x1234[] = {
   GROUP_none, GROUP_x2, GROUP_x3, GROUP_x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_vg1x2[] = {
+static constexpr group_suffix_index groups_vg1x2[] = {
   GROUP_vg1x2, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_vg1x4[] = {
+static constexpr group_suffix_index groups_vg1x4[] = {
   GROUP_vg1x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_vg1x24[] = {
+static constexpr group_suffix_index groups_vg1x24[] = {
   GROUP_vg1x2, GROUP_vg1x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_vg2[] = {
+static constexpr group_suffix_index groups_vg2[] = {
   GROUP_vg2x1, GROUP_vg2x2, GROUP_vg2x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_vg4[] = {
+static constexpr group_suffix_index groups_vg4[] = {
   GROUP_vg4x1, GROUP_vg4x2, GROUP_vg4x4, NUM_GROUP_SUFFIXES
 };
 
-static const group_suffix_index groups_vg24[] = {
+static constexpr group_suffix_index groups_vg24[] = {
   GROUP_vg2, GROUP_vg4, NUM_GROUP_SUFFIXES
 };
 
 /* Used by functions that have no governing predicate.  */
-static const predication_index preds_none[] = { PRED_none, NUM_PREDS };
+static constexpr predication_index preds_none[] = { PRED_none, NUM_PREDS };
 
 /* Used by functions that have a governing predicate but do not have an
    explicit suffix.  */
-static const predication_index preds_implicit[] = { PRED_implicit, NUM_PREDS };
+static constexpr predication_index preds_implicit[] = {
+  PRED_implicit, NUM_PREDS
+};
 
 /* Used by functions that only support "_m" predication.  */
-static const predication_index preds_m[] = { PRED_m, NUM_PREDS };
+static constexpr predication_index preds_m[] = { PRED_m, NUM_PREDS };
 
 /* Used by functions that allow merging and "don't care" predication,
    but are not suitable for predicated MOVPRFX.  */
-static const predication_index preds_mx[] = {
+static constexpr predication_index preds_mx[] = {
   PRED_m, PRED_x, NUM_PREDS
 };
 
 /* Used by functions that allow merging, zeroing and "don't care"
    predication.  */
-static const predication_index preds_mxz[] = {
+static constexpr predication_index preds_mxz[] = {
   PRED_m, PRED_x, PRED_z, NUM_PREDS
 };
 
 /* Used by functions that have the mxz predicated forms above, and in addition
    have an unpredicated form.  */
-static const predication_index preds_mxz_or_none[] = {
+static constexpr predication_index preds_mxz_or_none[] = {
   PRED_m, PRED_x, PRED_z, PRED_none, NUM_PREDS
 };
 
 /* Used by functions that allow merging and zeroing predication but have
    no "_x" form.  */
-static const predication_index preds_mz[] = { PRED_m, PRED_z, NUM_PREDS };
+static constexpr predication_index preds_mz[] = { PRED_m, PRED_z, NUM_PREDS };
 
 /* Used by functions that have an unpredicated form and a _z predicated
    form.  */
-static const predication_index preds_z_or_none[] = {
+static constexpr predication_index preds_z_or_none[] = {
   PRED_z, PRED_none, NUM_PREDS
 };
 
 /* Used by (mostly predicate) functions that only support "_z" predication.  */
-static const predication_index preds_z[] = { PRED_z, NUM_PREDS };
+static constexpr predication_index preds_z[] = { PRED_z, NUM_PREDS };
 
 /* Used by SME instructions that always merge into ZA.  */
-static const predication_index preds_za_m[] = { PRED_za_m, NUM_PREDS };
+static constexpr predication_index preds_za_m[] = { PRED_za_m, NUM_PREDS };
 
 void build_all (function_builder &b, const char *signature,
                const function_group_info &group,

Reply via email to