Skip to content

Commit 452abe1

Browse files
committed
libgccjit: Add vector permutation and vector access operations
gcc/jit/ChangeLog: PR jit/112602 * docs/topics/compatibility.rst (LIBGCCJIT_ABI_31): New ABI tag. * docs/topics/expressions.rst: Document gcc_jit_context_new_rvalue_vector_perm and gcc_jit_context_new_vector_access. * jit-playback.cc (playback::context::new_rvalue_vector_perm, common_mark_addressable_vec, gnu_vector_type_p, lvalue_p, convert_vector_to_array_for_subscript, new_vector_access): new functions. * jit-playback.h (new_rvalue_vector_perm, new_vector_access): New functions. * jit-recording.cc (recording::context::new_rvalue_vector_perm, recording::context::new_vector_access, memento_of_new_rvalue_vector_perm, recording::memento_of_new_rvalue_vector_perm::replay_into, recording::memento_of_new_rvalue_vector_perm::visit_children, recording::memento_of_new_rvalue_vector_perm::make_debug_string, recording::memento_of_new_rvalue_vector_perm::write_reproducer, recording::vector_access::replay_into, recording::vector_access::visit_children, recording::vector_access::make_debug_string, recording::vector_access::write_reproducer): New methods. * jit-recording.h (class memento_of_new_rvalue_vector_perm, class vector_access): New classes. * libgccjit.cc (gcc_jit_context_new_vector_access, gcc_jit_context_new_rvalue_vector_perm): New functions. * libgccjit.h (gcc_jit_context_new_rvalue_vector_perm, gcc_jit_context_new_vector_access): New functions. * libgccjit.map: New functions. gcc/testsuite/ChangeLog: PR jit/112602 * jit.dg/all-non-failing-tests.h: New test test-vector-perm.c. * jit.dg/test-vector-perm.c: New test.
1 parent 377eff7 commit 452abe1

File tree

11 files changed

+715
-1
lines changed

11 files changed

+715
-1
lines changed

gcc/jit/docs/topics/compatibility.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,13 @@ on functions and variables:
418418
--------------------
419419
``LIBGCCJIT_ABI_30`` covers the addition of
420420
:func:`gcc_jit_context_convert_vector`
421+
=======
422+
423+
.. _LIBGCCJIT_ABI_31:
424+
425+
``LIBGCCJIT_ABI_31``
426+
--------------------
427+
``LIBGCCJIT_ABI_31`` covers the addition of functions to manipulate vectors:
428+
429+
* :func:`gcc_jit_context_new_rvalue_vector_perm`
430+
* :func:`gcc_jit_context_new_vector_access`

gcc/jit/docs/topics/expressions.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,35 @@ Vector expressions
323323
324324
#ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector
325325
326+
.. function:: gcc_jit_rvalue * \
327+
gcc_jit_context_new_rvalue_vector_perm (gcc_jit_context *ctxt, \
328+
gcc_jit_location *loc, \
329+
gcc_jit_rvalue *elements1, \
330+
gcc_jit_rvalue *elements2, \
331+
gcc_jit_rvalue *mask);
332+
333+
Build a permutation of two vectors.
334+
335+
"elements1" and "elements2" should have the same type.
336+
The length of "mask" and "elements1" should be the same.
337+
The element type of "mask" should be integral.
338+
The size of the element type of "mask" and "elements1" should be the same.
339+
340+
This entrypoint was added in :ref:`LIBGCCJIT_ABI_31`; you can test for
341+
its presence using
342+
343+
.. code-block:: c
344+
345+
#ifdef LIBGCCJIT_HAVE_VECTOR_OPERATIONS
346+
347+
Analogous to:
348+
349+
.. code-block:: c
350+
351+
__builtin_shuffle (elements1, elements2, mask)
352+
353+
in C.
354+
326355
Unary Operations
327356
****************
328357

@@ -1096,3 +1125,27 @@ Field access is provided separately for both lvalues and rvalues.
10961125
PTR[INDEX]
10971126
10981127
in C (or, indeed, to ``PTR + INDEX``).
1128+
1129+
.. function:: gcc_jit_lvalue *\
1130+
gcc_jit_context_new_vector_access (gcc_jit_context *ctxt,\
1131+
gcc_jit_location *loc,\
1132+
gcc_jit_rvalue *vector,\
1133+
gcc_jit_rvalue *index)
1134+
1135+
Given an rvalue of vector type ``T __attribute__ ((__vector_size__ (SIZE)))``,
1136+
get the element `T` at the given index.
1137+
1138+
This entrypoint was added in :ref:`LIBGCCJIT_ABI_31`; you can test for
1139+
its presence using
1140+
1141+
.. code-block:: c
1142+
1143+
#ifdef LIBGCCJIT_HAVE_VECTOR_OPERATIONS
1144+
1145+
Analogous to:
1146+
1147+
.. code-block:: c
1148+
1149+
VECTOR[INDEX]
1150+
1151+
in C.

gcc/jit/jit-playback.cc

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,26 @@ playback::context::new_rvalue_from_vector (location *,
11731173
return new rvalue (this, t_ctor);
11741174
}
11751175

1176+
/* Construct a playback::rvalue instance (wrapping a tree) for a
1177+
vector perm. */
1178+
1179+
playback::rvalue *
1180+
playback::context::new_rvalue_vector_perm (location *loc,
1181+
rvalue* elements1,
1182+
rvalue* elements2,
1183+
rvalue* mask)
1184+
{
1185+
tree t_elements1 = elements1->as_tree ();
1186+
tree t_elements2 = elements2->as_tree ();
1187+
tree t_mask = mask->as_tree ();
1188+
1189+
tree t_vector_perm = build3 (VEC_PERM_EXPR, TREE_TYPE (t_elements1),
1190+
t_elements1, t_elements2, t_mask);
1191+
if (loc)
1192+
set_tree_location (t_vector_perm, loc);
1193+
return new rvalue (this, t_vector_perm);
1194+
}
1195+
11761196
/* Coerce a tree expression into a boolean tree expression. */
11771197

11781198
tree
@@ -1717,6 +1737,136 @@ convert_vector (location *loc,
17171737
return new rvalue (this, t_result);
17181738
}
17191739

1740+
/* The following functions come from c-common.h. */
1741+
/* Like c_mark_addressable but don't check register qualifier. */
1742+
void
1743+
common_mark_addressable_vec (tree t)
1744+
{
1745+
while (handled_component_p (t) || TREE_CODE (t) == C_MAYBE_CONST_EXPR)
1746+
{
1747+
t = TREE_OPERAND (t, 0);
1748+
}
1749+
if (!VAR_P (t)
1750+
&& TREE_CODE (t) != PARM_DECL
1751+
&& TREE_CODE (t) != COMPOUND_LITERAL_EXPR
1752+
&& TREE_CODE (t) != TARGET_EXPR)
1753+
return;
1754+
if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
1755+
TREE_ADDRESSABLE (t) = 1;
1756+
if (TREE_CODE (t) == COMPOUND_LITERAL_EXPR)
1757+
TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t)) = 1;
1758+
else if (TREE_CODE (t) == TARGET_EXPR)
1759+
TREE_ADDRESSABLE (TARGET_EXPR_SLOT (t)) = 1;
1760+
}
1761+
1762+
/* Return true if TYPE is a vector type that should be subject to the GNU
1763+
vector extensions (as opposed to a vector type that is used only for
1764+
the purposes of defining target-specific built-in functions). */
1765+
1766+
inline bool
1767+
gnu_vector_type_p (const_tree type)
1768+
{
1769+
return TREE_CODE (type) == VECTOR_TYPE && !TYPE_INDIVISIBLE_P (type);
1770+
}
1771+
1772+
/* Return nonzero if REF is an lvalue valid for this language.
1773+
Lvalues can be assigned, unless their type has TYPE_READONLY.
1774+
Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
1775+
1776+
bool
1777+
lvalue_p (const_tree ref)
1778+
{
1779+
const enum tree_code code = TREE_CODE (ref);
1780+
1781+
switch (code)
1782+
{
1783+
case REALPART_EXPR:
1784+
case IMAGPART_EXPR:
1785+
case COMPONENT_REF:
1786+
return lvalue_p (TREE_OPERAND (ref, 0));
1787+
1788+
case C_MAYBE_CONST_EXPR:
1789+
return lvalue_p (TREE_OPERAND (ref, 1));
1790+
1791+
case COMPOUND_LITERAL_EXPR:
1792+
case STRING_CST:
1793+
return true;
1794+
1795+
case MEM_REF:
1796+
case TARGET_MEM_REF:
1797+
/* MEM_REFs can appear from -fgimple parsing or folding, so allow them
1798+
here as well. */
1799+
case INDIRECT_REF:
1800+
case ARRAY_REF:
1801+
case VAR_DECL:
1802+
case PARM_DECL:
1803+
case RESULT_DECL:
1804+
case ERROR_MARK:
1805+
return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
1806+
&& TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
1807+
1808+
case BIND_EXPR:
1809+
return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
1810+
1811+
default:
1812+
return false;
1813+
}
1814+
}
1815+
1816+
bool
1817+
convert_vector_to_array_for_subscript (tree *vecp)
1818+
{
1819+
bool ret = false;
1820+
if (gnu_vector_type_p (TREE_TYPE (*vecp)))
1821+
{
1822+
tree type = TREE_TYPE (*vecp);
1823+
1824+
ret = !lvalue_p (*vecp);
1825+
1826+
/* We are building an ARRAY_REF so mark the vector as addressable
1827+
to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
1828+
for function parameters. */
1829+
/* NOTE: that was the missing piece for making vector access work with
1830+
optimizations enabled. */
1831+
common_mark_addressable_vec (*vecp);
1832+
1833+
*vecp = build1 (VIEW_CONVERT_EXPR,
1834+
build_array_type_nelts (TREE_TYPE (type),
1835+
TYPE_VECTOR_SUBPARTS (type)),
1836+
*vecp);
1837+
}
1838+
return ret;
1839+
}
1840+
1841+
/* Construct a playback::lvalue instance (wrapping a tree) for a
1842+
vector access. */
1843+
1844+
playback::lvalue *
1845+
playback::context::
1846+
new_vector_access (location *loc,
1847+
rvalue *vector,
1848+
rvalue *index)
1849+
{
1850+
gcc_assert (vector);
1851+
gcc_assert (index);
1852+
1853+
/* For comparison, see:
1854+
c/c-typeck.cc: build_array_ref
1855+
*/
1856+
1857+
tree t_vector = vector->as_tree ();
1858+
bool non_lvalue = convert_vector_to_array_for_subscript (&t_vector);
1859+
tree type = TREE_TYPE (TREE_TYPE (t_vector));
1860+
tree t_result = build4 (ARRAY_REF, type, t_vector, index->as_tree (),
1861+
NULL_TREE, NULL_TREE);
1862+
if (non_lvalue)
1863+
t_result = non_lvalue (t_result);
1864+
1865+
if (loc)
1866+
set_tree_location (t_result, loc);
1867+
return new lvalue (this, t_result);
1868+
}
1869+
17201870
/* Construct a tree for a field access. */
17211871

17221872
tree

gcc/jit/jit-playback.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ class context : public log_user
178178
type *type,
179179
const auto_vec<rvalue *> &elements);
180180

181+
rvalue *
182+
new_rvalue_vector_perm (location *loc,
183+
rvalue* elements1,
184+
rvalue* elements2,
185+
rvalue* mask);
186+
181187
rvalue *
182188
new_unary_op (location *loc,
183189
enum gcc_jit_unary_op op,
@@ -226,6 +232,10 @@ class context : public log_user
226232
convert_vector (location *loc,
227233
rvalue *vector,
228234
type *type);
235+
lvalue *
236+
new_vector_access (location *loc,
237+
rvalue *vector,
238+
rvalue *index);
229239

230240
void
231241
set_str_option (enum gcc_jit_str_option opt,

0 commit comments

Comments
 (0)