This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
patch: vector_size attribute
- From: Aldy Hernandez <aldyh at redhat dot com>
- To: rth at redhat dot com
- Cc: gcc patches <gcc-patches at gcc dot gnu dot org>
- Date: 04 Dec 2001 12:49:12 -0800
- Subject: patch: vector_size attribute
as per, the altivec discussion, here is a patch to add a vector_size
attribute-- complete with documentation and a test case. and waddaya
know, it actually works...
bootstrapped on x86.
is this ok?
--
Aldy Hernandez E-mail: aldyh@redhat.com
Professional Gypsy
Red Hat, Inc.
2001-12-04 Aldy Hernandez <aldyh@redhat.com>
* extend.texi (Variable Attributes): Document vector_size.
* gcc.dg/altivec-2.c: New.
* attribs.c (c_common_attribute_table): Add vector_size.
(handle_vector_size_attribute): New.
Index: doc/extend.texi
===================================================================
RCS file: /cvs/uberbaum/gcc/doc/extend.texi,v
retrieving revision 1.37
diff -c -p -r1.37 extend.texi
*** extend.texi 2001/11/18 03:30:55 1.37
--- extend.texi 2001/12/04 20:42:08
*************** This attribute, attached to a variable,
*** 3035,3040 ****
--- 3035,3055 ----
to be possibly unused. GCC will not produce a warning for this
variable.
+ @item vector_size (@var{bytes})
+ This attribute specifies the vector size for the variable, measured in
+ bytes. For example, the declaration:
+
+ @smallexample
+ int foo __attribute__ ((vector_size (16)));
+ @end smallexample
+
+ @noindent
+ causes the compiler to set the mode for @code{foo}, to be 16 bytes,
+ divided into @code{int} sized units. Assuming a 32 bit int (a vector of
+ 4 units of 4 bytes), the corresponding mode of @code{foo} will be V4SI.
+
+ This attribute is only applicable to integral and float scalars.
+
@item weak
The @code{weak} attribute is described in @xref{Function Attributes}.
Index: testsuite/gcc.dg/altivec-2.c
===================================================================
RCS file: altivec-2.c
diff -N altivec-2.c
*** /dev/null Tue May 5 13:32:27 1998
--- altivec-2.c Tue Dec 4 12:42:08 2001
***************
*** 0 ****
--- 1,7 ----
+ /* { dg-do compile { target powerpc-*-* } } */
+ /* { dg-options "-maltivec" } */
+
+ /* Program to test the vector_size attribute. This needs to run on a
+ target that has vectors, so use AltiVec. */
+
+ __attribute__((vector_size(16))) int foobar;
Index: attribs.c
===================================================================
RCS file: /cvs/uberbaum/gcc/attribs.c,v
retrieving revision 1.4
diff -c -p -r1.4 attribs.c
*** attribs.c 2001/11/04 02:51:21 1.4
--- attribs.c 2001/12/04 20:42:08
*************** static tree handle_no_limit_stack_attrib
*** 84,89 ****
--- 84,91 ----
bool *));
static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
bool *));
+ static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
+ bool *));
/* Table of machine-independent attributes common to all C-like languages. */
static const struct attribute_spec c_common_attribute_table[] =
*************** static const struct attribute_spec c_com
*** 139,144 ****
--- 141,148 ----
handle_no_limit_stack_attribute },
{ "pure", 0, 0, true, false, false,
handle_pure_attribute },
+ { "vector_size", 1, 1, true, false, false,
+ handle_vector_size_attribute },
{ NULL, 0, 0, false, false, false, NULL }
};
*************** handle_pure_attribute (node, name, args,
*** 1153,1158 ****
--- 1157,1233 ----
*no_add_attrs = true;
}
+ return NULL_TREE;
+ }
+
+ /* Handle a "vector_size" attribute; arguments as in
+ struct attribute_spec.handler. */
+
+ static tree
+ handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
+ tree *node;
+ tree name;
+ tree args;
+ int flags ATTRIBUTE_UNUSED;
+ bool *no_add_attrs;
+ {
+ unsigned int vecsize, nunits;
+ enum machine_mode mode, orig_mode, new_mode;
+ tree decl = *node;
+ tree type = TREE_TYPE (decl);
+
+ if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
+ {
+ warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+ return NULL_TREE;
+ }
+
+ /* Get the vector size (in bytes). */
+ vecsize = TREE_INT_CST_LOW (TREE_VALUE (args));
+
+ /* Calculate how many units fit in the vector. */
+ nunits = vecsize / (TYPE_PRECISION (TREE_TYPE (*node)) / 8);
+
+ /* Get the mode of the type being modified. */
+ orig_mode = TYPE_MODE (TREE_TYPE (*node));
+
+ if (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
+ && GET_MODE_CLASS (orig_mode) != MODE_INT)
+ {
+ error ("invalid vector type for attribute `%s'",
+ IDENTIFIER_POINTER (name));
+ return NULL_TREE;
+ }
+
+ /* Find a suitably sized vector. */
+ new_mode = VOIDmode;
+ for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
+ ? MODE_VECTOR_INT
+ : MODE_VECTOR_FLOAT);
+ mode != VOIDmode;
+ mode = GET_MODE_WIDER_MODE (mode))
+ if (vecsize == GET_MODE_SIZE (mode) && nunits == GET_MODE_NUNITS (mode))
+ {
+ new_mode = mode;
+ break;
+ }
+
+ if (new_mode == VOIDmode)
+ error ("No relevant vector mode found");
+ else
+ {
+ type = type_for_mode (new_mode, TREE_UNSIGNED (type));
+ if (!type)
+ error ("No data type for mode `%s'", GET_MODE_NAME (new_mode));
+ else
+ {
+ TREE_TYPE (decl) = type;
+ DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
+ if (TREE_CODE (decl) != FIELD_DECL)
+ layout_decl (decl, 0);
+ }
+ }
+
return NULL_TREE;
}