On 3/7/25 5:34 PM, Richard Biener wrote:
On Fri, 7 Mar 2025, Tejas Belagod wrote:

On 3/7/25 4:38 PM, Richard Biener wrote:
On Fri, 7 Mar 2025, Tejas Belagod wrote:

Given a vector mode and its corresponding element mode, this new new
language
hook returns a vector type that has properties of the vector mode and
element
type of that of the element mode.  For eg. on AArch64, given VNx16BI and
QImode
it returns VNx16QI i.e. the wider mode to BImode that is an SVE mode.

What's the rationale for this to be a new frontend hook?  It seems
to be a composition of a target hook (related_mode) and a
frontend hook (type_for_mode).

I don't know this part of the FE very well, so pardon if its wrong way to do
it.

I was trying to find a generic way to determine a wider vtype for a given
vmode in a language-agnostic way. It looks like lang hooks are the generic way
for the mid-end to communicate to the FE to determine types the FE seems fit,
so I decided to make it a langhook.

Who is supposed to call this hook and for what reason?  Why would the
frontend be involved here?


Ah, sorry, I should've mentioned. This hook is called in Patch 4/6 during gimplification (gimplify_compound_lval) that implements the subscript operator for svbool_t - this hook returns a 'container' type for an n-bit boolean type which in this case is a byte vector for the 1-bit svbool_t vector. I involve the FE here in the same principle as for eg. TYPE_FOR_SIZE as the FE is best-placed to return the right 'container' type as defined by the language. The type returned by the FE is used to unpack svbool_t to its container vector type to implement the subscript operator.

And how can it survive without
a default implementation?


I saw a comment in langhooks-def.h that says:

/* Types hooks.  There are no reasonable defaults for most of them,
    so we create a compile-time error instead.  */

So I assumed it was OK to have a NULL default which presumably fails at build
time when a hook is not defined for a language. Is there a more graceful way
to remedy this?

Well, you made the default a NULL pointer - what do you do at the use
point of the hook when it's not defined?


True. I saw some of the other type hooks had NULL, so AIUI, I imagined it could be NULL and it would crash when used for a FE that didn't implement it. I admit I'm not even sure if this is the right way to do this.

So, before I embarked on a 'default' implementation (which I'm not fully sure how to do) my main intention was to clarify (via this RFC) if the langhook approach was indeed the best way for gimple to obtain the related vtype it needed for the vbool type it was unpacking to do the subscript operation on?

Thanks,
Tejas.


Thanks,
Tejas.

gcc/ChangeLog:

  * c-family/c-common.cc (c_common_related_vtype): New.
  * c-family/c-common.h: Declare.
  * c/c-objc-common.h: Likwise.
  * cp/cp-objcp-common.h: Likewise.
  * langhooks-def.h: Declare new hook.
---
   gcc/c-family/c-common.cc | 15 +++++++++++++++
   gcc/c-family/c-common.h  |  1 +
   gcc/c/c-objc-common.h    |  2 ++
   gcc/cp/cp-objcp-common.h |  2 ++
   gcc/langhooks-def.h      |  2 ++
   gcc/langhooks.h          |  4 ++++
   6 files changed, 26 insertions(+)

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index 49508fe9ee6..0895c24ba97 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -2665,6 +2665,21 @@ c_common_type_for_mode (machine_mode mode, int
unsignedp)
     return NULL_TREE;
   }
+/* Given MODE, and INNER_MODE find a vector mode made up of INNER_MODE.
*/
+
+tree
+c_common_related_vtype_for_mode (machine_mode mode, scalar_mode
inner_mode,
+                              int unsignedp)
+{
+  gcc_assert (VECTOR_MODE_P (mode));
+  gcc_assert (!VECTOR_MODE_P (inner_mode));
+
+  opt_machine_mode wvmode = targetm.vectorize.related_mode
+                             (mode, inner_mode, GET_MODE_NUNITS (mode));
+  tree type = c_common_type_for_mode (wvmode.require (), unsignedp);
+  return type;
+}
+
   tree
   c_common_unsigned_type (tree type)
   {
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index ea6c2975056..0178a106343 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -874,6 +874,7 @@ extern bool c_common_handle_option (size_t, const char
*, HOST_WIDE_INT, int,
                                    const struct cl_option_handlers *);
   extern bool default_handle_c_option (size_t, const char *, int);
   extern tree c_common_type_for_mode (machine_mode, int);
+extern tree c_common_related_vtype_for_mode (machine_mode, scalar_mode,
int);
   extern tree c_common_type_for_size (unsigned int, int);
   extern tree c_common_fixed_point_type_for_size (unsigned int, unsigned
   int,
                                                int, int);
diff --git a/gcc/c/c-objc-common.h b/gcc/c/c-objc-common.h
index 84bd3571ffc..93c753e4885 100644
--- a/gcc/c/c-objc-common.h
+++ b/gcc/c/c-objc-common.h
@@ -97,6 +97,8 @@ static const scoped_attribute_specs *const
c_objc_attribute_table[] =
   #define LANG_HOOKS_SIMULATE_RECORD_DECL c_simulate_record_decl
   #undef LANG_HOOKS_TYPE_FOR_MODE
   #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
+#undef LANG_HOOKS_RELATED_VTYPE_FOR_MODE
+#define LANG_HOOKS_RELATED_VTYPE_FOR_MODE c_common_related_vtype_for_mode
   #undef LANG_HOOKS_TYPE_FOR_SIZE
   #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
   #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
diff --git a/gcc/cp/cp-objcp-common.h b/gcc/cp/cp-objcp-common.h
index 13fb80c2da5..821c7bdc881 100644
--- a/gcc/cp/cp-objcp-common.h
+++ b/gcc/cp/cp-objcp-common.h
@@ -151,6 +151,8 @@ static const scoped_attribute_specs *const
cp_objcp_attribute_table[] =
   #define LANG_HOOKS_SIMULATE_RECORD_DECL cxx_simulate_record_decl
   #undef LANG_HOOKS_TYPE_FOR_MODE
   #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
+#undef LANG_HOOKS_RELATED_VTYPE_FOR_MODE
+#define LANG_HOOKS_RELATED_VTYPE_FOR_MODE c_common_related_vtype_for_mode
   #undef LANG_HOOKS_TYPE_FOR_SIZE
   #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
   #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
diff --git a/gcc/langhooks-def.h b/gcc/langhooks-def.h
index 6b34d324ab5..69a354639c3 100644
--- a/gcc/langhooks-def.h
+++ b/gcc/langhooks-def.h
@@ -221,6 +221,7 @@ extern tree lhd_unit_size_without_reusable_padding
(tree);
   #define LANG_HOOKS_TYPE_DWARF_ATTRIBUTE      lhd_type_dwarf_attribute
   #define LANG_HOOKS_UNIT_SIZE_WITHOUT_REUSABLE_PADDING
   lhd_unit_size_without_reusable_padding
   #define LANG_HOOKS_CLASSTYPE_AS_BASE hook_tree_const_tree_null
+#define LANG_HOOKS_RELATED_VTYPE_FOR_MODE NULL
#define LANG_HOOKS_FOR_TYPES_INITIALIZER { \
     LANG_HOOKS_MAKE_TYPE, \
@@ -228,6 +229,7 @@ extern tree lhd_unit_size_without_reusable_padding
(tree);
     LANG_HOOKS_SIMULATE_RECORD_DECL, \
     LANG_HOOKS_CLASSIFY_RECORD, \
     LANG_HOOKS_TYPE_FOR_MODE, \
+  LANG_HOOKS_RELATED_VTYPE_FOR_MODE, \
     LANG_HOOKS_TYPE_FOR_SIZE, \
     LANG_HOOKS_GENERIC_TYPE_P, \
     LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS, \
diff --git a/gcc/langhooks.h b/gcc/langhooks.h
index dc8d878c7fb..45dbea481c7 100644
--- a/gcc/langhooks.h
+++ b/gcc/langhooks.h
@@ -87,6 +87,10 @@ struct lang_hooks_for_types
        mode.  */
     tree (*type_for_mode) (machine_mode, int);
   +  /* Given MODE, SCALAR_MODE and UNSIGNEDP, return a suitable vector
type-tree
+     where the element is of SCALAR_MODE and related to MODE.  */
+  tree (*related_vtype_for_mode) (machine_mode, scalar_mode, int);
+
     /* Given PRECISION and UNSIGNEDP, return a suitable type-tree for an
        integer type with at least that precision.  */
     tree (*type_for_size) (unsigned, int);







Reply via email to