From: Kyrylo Tkachov <[email protected]>
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.
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 will grow as more intrinsics are migrated to the
pragma framework.
Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill
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]>
---
gcc/config/aarch64/aarch64-acle-builtins.cc | 54 +++++++++++++++++++-
gcc/config/aarch64/aarch64-acle-builtins.h | 56 ++++++++++++---------
2 files changed, 84 insertions(+), 26 deletions(-)
diff --git a/gcc/config/aarch64/aarch64-acle-builtins.cc
b/gcc/config/aarch64/aarch64-acle-builtins.cc
index 2d52443a68c..a3f967e7971 100644
--- a/gcc/config/aarch64/aarch64-acle-builtins.cc
+++ b/gcc/config/aarch64/aarch64-acle-builtins.cc
@@ -566,6 +566,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)
{
@@ -583,8 +624,19 @@ 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.
+ 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 3260140f1e8..2b5ae35d9a9 100644
--- a/gcc/config/aarch64/aarch64-acle-builtins.h
+++ b/gcc/config/aarch64/aarch64-acle-builtins.h
@@ -2079,13 +2079,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 }
};
@@ -2239,97 +2239,103 @@ DEF_SVE_TYPES_ARRAY (neon_reinterpret);
DEF_SVE_TYPES_ARRAY (neon_reinterpretq);
DEF_SVE_TYPES_ARRAY (neon_mul);
-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,
--
2.54.0 (Apple Git-157)