Skip to content

Commit

Permalink
RISC-V: Part-1: Select suitable vector registers for vector type args…
Browse files Browse the repository at this point in the history
… and returns

I post the vector register calling convention rules from in the proposal[1]
directly here:

v0 is used to pass the first vector mask argument to a function, and to return
vector mask result from a function. v8-v23 are used to pass vector data
arguments, vector tuple arguments and the rest vector mask arguments to a
function, and to return vector data and vector tuple results from a function.

Each vector data type and vector tuple type has an LMUL attribute that
indicates a vector register group. The value of LMUL indicates the number of
vector registers in the vector register group and requires the first vector
register number in the vector register group must be a multiple of it. For
example, the LMUL of `vint64m8_t` is 8, so v8-v15 vector register group can be
allocated to this type, but v9-v16 can not because the v9 register number is
not a multiple of 8. If LMUL is less than 1, it is treated as 1. If it is a
vector mask type, its LMUL is 1.

Each vector tuple type also has an NFIELDS attribute that indicates how many
vector register groups the type contains. Thus a vector tuple type needs to
take up LMUL×NFIELDS registers.

The rules for passing vector arguments are as follows:

1. For the first vector mask argument, use v0 to pass it. The argument has now
been allocated.

2. For vector data arguments or rest vector mask arguments, starting from the
v8 register, if a vector register group between v8-v23 that has not been
allocated can be found and the first register number is a multiple of LMUL,
then allocate this vector register group to the argument and mark these
registers as allocated. Otherwise, pass it by reference. The argument has now
been allocated.

3. For vector tuple arguments, starting from the v8 register, if NFIELDS
consecutive vector register groups between v8-v23 that have not been allocated
can be found and the first register number is a multiple of LMUL, then allocate
these vector register groups to the argument and mark these registers as
allocated. Otherwise, pass it by reference. The argument has now been allocated.

NOTE: It should be stressed that the search for the appropriate vector register
groups starts at v8 each time and does not start at the next register after the
registers are allocated for the previous vector argument. Therefore, it is
possible that the vector register number allocated to a vector argument can be
less than the vector register number allocated to previous vector arguments.
For example, for the function
`void foo (vint32m1_t a, vint32m2_t b, vint32m1_t c)`, according to the rules
of allocation, v8 will be allocated to `a`, v10-v11 will be allocated to `b`
and v9 will be allocated to `c`. This approach allows more vector registers to
be allocated to arguments in some cases.

Vector values are returned in the same manner as the first named argument of
the same type would be passed.

[1] riscv-non-isa/riscv-elf-psabi-doc#389

gcc/ChangeLog:

	* config/riscv/riscv-protos.h (builtin_type_p): New function for checking vector type.
	* config/riscv/riscv-vector-builtins.cc (builtin_type_p): Ditto.
	* config/riscv/riscv.cc (struct riscv_arg_info): New fields.
	(riscv_init_cumulative_args): Setup variant_cc field.
	(riscv_vector_type_p): New function for checking vector type.
	(riscv_hard_regno_nregs): Hoist declare.
	(riscv_get_vector_arg): Subroutine of riscv_get_arg_info.
	(riscv_get_arg_info): Support vector cc.
	(riscv_function_arg_advance): Update cum.
	(riscv_pass_by_reference): Handle vector args.
	(riscv_v_abi): New function return vector abi.
	(riscv_return_value_is_vector_type_p): New function for check vector arguments.
	(riscv_arguments_is_vector_type_p): New function for check vector returns.
	(riscv_fntype_abi): Implement TARGET_FNTYPE_ABI.
	(TARGET_FNTYPE_ABI): Implement TARGET_FNTYPE_ABI.
	* config/riscv/riscv.h (GCC_RISCV_H): Define macros for vector abi.
	(MAX_ARGS_IN_VECTOR_REGISTERS): Ditto.
	(MAX_ARGS_IN_MASK_REGISTERS): Ditto.
	(V_ARG_FIRST): Ditto.
	(V_ARG_LAST): Ditto.
	(enum riscv_cc): Define all RISCV_CC variants.
	* config/riscv/riscv.opt: Add --param=riscv-vector-abi.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/rvv/base/abi-call-args-1-run.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-1.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-2-run.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-2.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-3-run.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-3.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-4-run.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-args-4.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-error-1.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-return-run.c: New test.
	* gcc.target/riscv/rvv/base/abi-call-return.c: New test.
  • Loading branch information
lhtin authored and Liaoshihua committed Mar 25, 2024
1 parent b41b928 commit 3f52555
Show file tree
Hide file tree
Showing 16 changed files with 1,610 additions and 17 deletions.
1 change: 1 addition & 0 deletions gcc/config/riscv/riscv-protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ enum avl_type
void init_builtins (void);
const char *mangle_builtin_type (const_tree);
tree lookup_vector_type_attribute (const_tree);
bool builtin_type_p (const_tree);
#ifdef GCC_TARGET_H
bool verify_type_context (location_t, type_context_kind, const_tree, bool);
bool expand_vec_perm_const (machine_mode, machine_mode, rtx, rtx, rtx,
Expand Down
10 changes: 10 additions & 0 deletions gcc/config/riscv/riscv-vector-builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3992,6 +3992,16 @@ mangle_builtin_type (const_tree type)
return NULL;
}

/* Return true if TYPE is a built-in RVV type defined by the ABI. */
bool
builtin_type_p (const_tree type)
{
if (!type)
return false;

return lookup_vector_type_attribute (type);
}

/* Initialize all compiler built-ins related to RVV that should be
defined at start-up. */
void
Expand Down
234 changes: 217 additions & 17 deletions gcc/config/riscv/riscv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ struct riscv_arg_info {

/* The offset of the first register used, provided num_fprs is nonzero. */
unsigned int fpr_offset;

/* The number of vector registers allocated to this argument. */
unsigned int num_vrs;

/* The offset of the first register used, provided num_vrs is nonzero. */
unsigned int vr_offset;

/* The number of mask registers allocated to this argument. */
unsigned int num_mrs;

/* The offset of the first register used, provided num_mrs is nonzero. */
unsigned int mr_offset;
};

/* One stage in a constant building sequence. These sequences have
Expand Down Expand Up @@ -4413,6 +4425,11 @@ riscv_init_cumulative_args (CUMULATIVE_ARGS *cum,
{
memset (cum, 0, sizeof (*cum));

if (fntype)
cum->variant_cc = (riscv_cc) fntype_abi (fntype).id ();
else
cum->variant_cc = RISCV_CC_BASE;

if (fndecl)
{
const tree_function_decl &fn
Expand All @@ -4423,12 +4440,105 @@ riscv_init_cumulative_args (CUMULATIVE_ARGS *cum,
}
}

/* Fill INFO with information about a single argument, and return an
RTL pattern to pass or return the argument. CUM is the cumulative
state for earlier arguments. MODE is the mode of this argument and
TYPE is its type (if known). NAMED is true if this is a named
(fixed) argument rather than a variable one. RETURN_P is true if
returning the argument, or false if passing the argument. */
/* Return true if TYPE is a vector type that can be passed in vector registers.
*/

static bool
riscv_vector_type_p (const_tree type)
{
/* Currently, only builtin scalabler vector type is allowed, in the future,
more vector types may be allowed, such as GNU vector type, etc. */
return riscv_vector::builtin_type_p (type);
}

static unsigned int
riscv_hard_regno_nregs (unsigned int regno, machine_mode mode);

/* Subroutine of riscv_get_arg_info. */

static rtx
riscv_get_vector_arg (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
machine_mode mode, bool return_p)
{
gcc_assert (riscv_v_ext_mode_p (mode));

info->mr_offset = cum->num_mrs;
if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
{
/* For scalable mask return value. */
if (return_p)
return gen_rtx_REG (mode, V_REG_FIRST);

/* For the first scalable mask argument. */
if (info->mr_offset < MAX_ARGS_IN_MASK_REGISTERS)
{
info->num_mrs = 1;
return gen_rtx_REG (mode, V_REG_FIRST);
}
else
{
/* Rest scalable mask arguments are treated as scalable data
arguments. */
}
}

/* The number and alignment of vector registers need for this scalable vector
argument. When the mode size is less than a full vector, we use 1 vector
register to pass. Just call TARGET_HARD_REGNO_NREGS for the number
information. */
int nregs = riscv_hard_regno_nregs (V_ARG_FIRST, mode);
int LMUL = riscv_v_ext_tuple_mode_p (mode)
? nregs / riscv_vector::get_nf (mode)
: nregs;
int arg_reg_start = V_ARG_FIRST - V_REG_FIRST;
int arg_reg_end = V_ARG_LAST - V_REG_FIRST;
int aligned_reg_start = ROUND_UP (arg_reg_start, LMUL);

/* For scalable data and scalable tuple return value. */
if (return_p)
return gen_rtx_REG (mode, aligned_reg_start + V_REG_FIRST);

/* Iterate through the USED_VRS array to find vector register groups that have
not been allocated and the first register is aligned with LMUL. */
for (int i = aligned_reg_start; i + nregs - 1 <= arg_reg_end; i += LMUL)
{
/* The index in USED_VRS array. */
int idx = i - arg_reg_start;
/* Find the first register unused. */
if (!cum->used_vrs[idx])
{
bool find_set = true;
/* Ensure there are NREGS continuous unused registers. */
for (int j = 1; j < nregs; j++)
if (cum->used_vrs[idx + j])
{
find_set = false;
/* Update I to the last aligned register which
cannot be used and the next iteration will add
LMUL step to I. */
i += (j / LMUL) * LMUL;
break;
}

if (find_set)
{
info->num_vrs = nregs;
info->vr_offset = idx;
return gen_rtx_REG (mode, i + V_REG_FIRST);
}
}
}

return NULL_RTX;
}

/* Fill INFO with information about a single argument, and return an RTL
pattern to pass or return the argument. Return NULL_RTX if argument cannot
pass or return in registers, then the argument may be passed by reference or
through the stack or . CUM is the cumulative state for earlier arguments.
MODE is the mode of this argument and TYPE is its type (if known). NAMED is
true if this is a named (fixed) argument rather than a variable one. RETURN_P
is true if returning the argument, or false if passing the argument. */

static rtx
riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
Expand All @@ -4450,11 +4560,9 @@ riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
riscv_pass_in_vector_p (type);
}

/* All current vector arguments and return values are passed through the
function stack. Ideally, we should either warn the user not to use an RVV
vector type as function argument or support a calling convention
with better performance. */
if (riscv_v_ext_mode_p (mode))
/* When disable vector_abi or scalable vector argument is anonymous, this
argument is passed by reference. */
if (riscv_v_ext_mode_p (mode) && (!riscv_vector_abi || !named))
return NULL_RTX;

if (named)
Expand Down Expand Up @@ -4518,6 +4626,10 @@ riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
gregno, TYPE_MODE (fields[1].type),
fields[1].offset);
}

/* For scalable vector argument. */
if (riscv_vector_type_p (type) && riscv_v_ext_mode_p (mode))
return riscv_get_vector_arg (info, cum, mode, return_p);
}

/* Work out the size of the argument. */
Expand Down Expand Up @@ -4564,12 +4676,28 @@ riscv_function_arg_advance (cumulative_args_t cum_v,

riscv_get_arg_info (&info, cum, arg.mode, arg.type, arg.named, false);

/* Set the corresponding register in USED_VRS to used status. */
for (unsigned int i = 0; i < info.num_vrs; i++)
{
gcc_assert (!cum->used_vrs[info.vr_offset + i]);
cum->used_vrs[info.vr_offset + i] = true;
}

if ((info.num_vrs > 0 || info.num_mrs > 0) && cum->variant_cc != RISCV_CC_V)
{
error ("RVV type %qT cannot be passed to an unprototyped function",
arg.type);
/* Avoid repeating the message */
cum->variant_cc = RISCV_CC_V;
}

/* Advance the register count. This has the effect of setting
num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
argument required us to skip the final GPR and pass the whole
argument on the stack. */
cum->num_fprs = info.fpr_offset + info.num_fprs;
cum->num_gprs = info.gpr_offset + info.num_gprs;
cum->num_mrs = info.mr_offset + info.num_mrs;
}

/* Implement TARGET_ARG_PARTIAL_BYTES. */
Expand Down Expand Up @@ -4631,20 +4759,23 @@ riscv_pass_by_reference (cumulative_args_t cum_v, const function_arg_info &arg)
CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);

/* ??? std_gimplify_va_arg_expr passes NULL for cum. Fortunately, we
never pass variadic arguments in floating-point registers, so we can
avoid the call to riscv_get_arg_info in this case. */
never pass variadic arguments in floating-point and vector registers,
so we can avoid the call to riscv_get_arg_info in this case. */
if (cum != NULL)
{
/* Don't pass by reference if we can use a floating-point register. */
riscv_get_arg_info (&info, cum, arg.mode, arg.type, arg.named, false);
if (info.num_fprs)
return false;

/* Don't pass by reference if we can use vector register groups. */
if (info.num_vrs > 0 || info.num_mrs > 0)
return false;
}

/* All current vector arguments and return values are passed through the
function stack. Ideally, we should either warn the user not to use an RVV
vector type as function argument or support a calling convention
with better performance. */
/* When vector abi disabled(without --param=riscv-vector-abi option) or
scalable vector argument is anonymous or cannot be passed through vector
registers, this argument is passed by reference. */
if (riscv_v_ext_mode_p (arg.mode))
return true;

Expand Down Expand Up @@ -4702,6 +4833,73 @@ riscv_setup_incoming_varargs (cumulative_args_t cum,
cfun->machine->varargs_size = gp_saved * UNITS_PER_WORD;
}

/* Return the descriptor of the Standard Vector Calling Convention Variant. */

static const predefined_function_abi &
riscv_v_abi ()
{
predefined_function_abi &v_abi = function_abis[RISCV_CC_V];
if (!v_abi.initialized_p ())
{
HARD_REG_SET full_reg_clobbers
= default_function_abi.full_reg_clobbers ();
/* Callee-saved vector registers: v1-v7, v24-v31. */
for (int regno = V_REG_FIRST + 1; regno <= V_REG_FIRST + 7; regno += 1)
CLEAR_HARD_REG_BIT (full_reg_clobbers, regno);
for (int regno = V_REG_FIRST + 24; regno <= V_REG_FIRST + 31; regno += 1)
CLEAR_HARD_REG_BIT (full_reg_clobbers, regno);
v_abi.initialize (RISCV_CC_V, full_reg_clobbers);
}
return v_abi;
}

/* Return true if a function with type FNTYPE returns its value in
RISC-V V registers. */

static bool
riscv_return_value_is_vector_type_p (const_tree fntype)
{
tree return_type = TREE_TYPE (fntype);

return riscv_vector_type_p (return_type);
}

/* Return true if a function with type FNTYPE takes arguments in
RISC-V V registers. */

static bool
riscv_arguments_is_vector_type_p (const_tree fntype)
{
for (tree chain = TYPE_ARG_TYPES (fntype); chain && chain != void_list_node;
chain = TREE_CHAIN (chain))
{
tree arg_type = TREE_VALUE (chain);
if (riscv_vector_type_p (arg_type))
return true;
}

return false;
}

/* Implement TARGET_FNTYPE_ABI. */

static const predefined_function_abi &
riscv_fntype_abi (const_tree fntype)
{
/* Implementing an experimental vector calling convention, the proposal
can be viewed at the bellow link:
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/389
You can enable this feature via the `--param=riscv-vector-abi` compiler
option. */
if (riscv_vector_abi
&& (riscv_return_value_is_vector_type_p (fntype)
|| riscv_arguments_is_vector_type_p (fntype)))
return riscv_v_abi ();

return default_function_abi;
}

/* Handle an attribute requiring a FUNCTION_DECL;
arguments as in struct attribute_spec.handler. */
static tree
Expand Down Expand Up @@ -9159,6 +9357,8 @@ riscv_vectorize_create_costs (vec_info *vinfo, bool costing_for_scalar)
#define TARGET_FUNCTION_ARG_ADVANCE riscv_function_arg_advance
#undef TARGET_FUNCTION_ARG_BOUNDARY
#define TARGET_FUNCTION_ARG_BOUNDARY riscv_function_arg_boundary
#undef TARGET_FNTYPE_ABI
#define TARGET_FNTYPE_ABI riscv_fntype_abi

#undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
#define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS \
Expand Down
25 changes: 25 additions & 0 deletions gcc/config/riscv/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with GCC; see the file COPYING3. If not see
#ifndef GCC_RISCV_H
#define GCC_RISCV_H

#include <stdbool.h>
#include "config/riscv/riscv-opts.h"

/* Target CPU builtins. */
Expand Down Expand Up @@ -666,13 +667,18 @@ enum reg_class

#define MAX_ARGS_IN_REGISTERS (riscv_abi == ABI_ILP32E ? 6 : 8)

#define MAX_ARGS_IN_VECTOR_REGISTERS (16)
#define MAX_ARGS_IN_MASK_REGISTERS (1)

/* Symbolic macros for the first/last argument registers. */

#define GP_ARG_FIRST (GP_REG_FIRST + 10)
#define GP_ARG_LAST (GP_ARG_FIRST + MAX_ARGS_IN_REGISTERS - 1)
#define GP_TEMP_FIRST (GP_REG_FIRST + 5)
#define FP_ARG_FIRST (FP_REG_FIRST + 10)
#define FP_ARG_LAST (FP_ARG_FIRST + MAX_ARGS_IN_REGISTERS - 1)
#define V_ARG_FIRST (V_REG_FIRST + 8)
#define V_ARG_LAST (V_ARG_FIRST + MAX_ARGS_IN_VECTOR_REGISTERS - 1)

#define CALLEE_SAVED_REG_NUMBER(REGNO) \
((REGNO) >= 8 && (REGNO) <= 9 ? (REGNO) - 8 : \
Expand All @@ -696,14 +702,33 @@ enum reg_class
(IN_RANGE ((N), GP_ARG_FIRST, GP_ARG_LAST) \
|| (UNITS_PER_FP_ARG && IN_RANGE ((N), FP_ARG_FIRST, FP_ARG_LAST)))

/* Define the standard RISC-V calling convention and variants. */

enum riscv_cc
{
RISCV_CC_BASE = 0, /* Base standard RISC-V ABI. */
RISCV_CC_V, /* For functions that pass or return values in V registers. */
RISCV_CC_UNKNOWN
};

typedef struct {
/* The calling convention that current function used. */
enum riscv_cc variant_cc;

/* Number of integer registers used so far, up to MAX_ARGS_IN_REGISTERS. */
unsigned int num_gprs;

/* Number of floating-point registers used so far, likewise. */
unsigned int num_fprs;

int rvv_psabi_warning;

/* Number of mask registers used so far, up to MAX_ARGS_IN_MASK_REGISTERS. */
unsigned int num_mrs;

/* The used state of args in vector registers, true for used by prev arg,
initial to false. */
bool used_vrs[MAX_ARGS_IN_VECTOR_REGISTERS];
} CUMULATIVE_ARGS;

/* Initialize a variable CUM of type CUMULATIVE_ARGS
Expand Down
Loading

0 comments on commit 3f52555

Please sign in to comment.