Skip to content

Commit

Permalink
RISC-V: Implement RESOLVE_OVERLOADED_BUILTIN for RVV intrinsic
Browse files Browse the repository at this point in the history
Update in v4:
* Remove class function_resolver.
* Remove function get_non_overloaded_instance.
* Add overloaded hash traits for non-overloaded intrinsic.
* All overloaded intrinsics are implemented, and the tests pass.

Update in v3:

* Rewrite comment for overloaded function add.
* Move get_non_overloaded_instance to function_base.

Update in v2:

* Add get_non_overloaded_instance for function instance.
* Fix overload check for policy function.
* Enrich the test cases check.

Original log:

This patch would like add the framework to support the RVV overloaded
intrinsic API in riscv-xxx-xxx-gcc, like riscv-xxx-xxx-g++ did.

However, it almost leverage the hook TARGET_RESOLVE_OVERLOADED_BUILTIN
with below steps.

* Register overloaded functions.
* Add function_resolver for overloaded function resolving.
* Add resolve API for function shape with default implementation.
* Implement HOOK for navigating the overloaded API to non-overloaded API.

We validated this framework by the vmv_v intrinsic API(s), and we will
add more intrins API support in the underlying patches.

gcc/ChangeLog:

        * config/riscv/riscv-c.cc (riscv_resolve_overloaded_builtin): New function for the hook.
        (riscv_register_pragmas): Register the hook.
        * config/riscv/riscv-protos.h (resolve_overloaded_builtin): New decl.
        * config/riscv/riscv-vector-builtins-shapes.cc (build_one): Register overloaded function.
        * config/riscv/riscv-vector-builtins.cc (struct non_overloaded_registered_function_hasher): New hash table.
        (function_builder::add_function): Add overloaded arg.
        (function_builder::add_unique_function): Map overloaded function to non-overloaded function.
        (function_builder::add_overloaded_function): New API impl.
        (registered_function::overloaded_hash): Calculate hash value.
        (has_vxrm_or_frm_p): New function impl.
        (non_overloaded_registered_function_hasher::hash): Ditto.
        (non_overloaded_registered_function_hasher::equal): Ditto.
        (handle_pragma_vector): Allocate space for hash table.
        (resolve_overloaded_builtin): New function impl.
        * config/riscv/riscv-vector-builtins.h: Add additional parameters to add_function.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/rvv/base/overloaded_rv32_vadd.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv32_vfadd.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv32_vget_vset.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv32_vloxseg2ei16.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv32_vmv.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv32_vreinterpret.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv64_vadd.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv64_vfadd.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv64_vget_vset.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv64_vloxseg2ei16.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv64_vmv.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_rv64_vreinterpret.c: New test.
        * gcc.target/riscv/rvv/base/overloaded_vadd.h: New test.
        * gcc.target/riscv/rvv/base/overloaded_vfadd.h: New test.
        * gcc.target/riscv/rvv/base/overloaded_vget_vset.h: New test.
        * gcc.target/riscv/rvv/base/overloaded_vloxseg2ei16.h: New test.
        * gcc.target/riscv/rvv/base/overloaded_vmv.h: New test.
        * gcc.target/riscv/rvv/base/overloaded_vreinterpret.h: New test.

Signed-off-by: Li Xu <xuli1@eswincomputing.com>
Co-Authored-By: Pan Li <pan2.li@intel.com>
Signed-off-by: Li Xu <xuli1@eswincomputing.com>
  • Loading branch information
2 people authored and ouuleilei-bot committed Oct 30, 2023
1 parent c2d62cd commit faa230f
Show file tree
Hide file tree
Showing 23 changed files with 653 additions and 11 deletions.
36 changes: 35 additions & 1 deletion gcc/config/riscv/riscv-c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,50 @@ riscv_check_builtin_call (location_t loc, vec<location_t> arg_loc, tree fndecl,

case RISCV_BUILTIN_VECTOR:
return riscv_vector::check_builtin_call (loc, arg_loc, subcode,
orig_fndecl, nargs, args);
fndecl, nargs, args);
}
gcc_unreachable ();
}

/* Implement TARGET_RESOLVE_OVERLOADED_BUILTIN. */
static tree
riscv_resolve_overloaded_builtin (unsigned int uncast_location, tree fndecl,
void *uncast_arglist)
{
vec<tree, va_gc> empty = {};
location_t loc = (location_t) uncast_location;
vec<tree, va_gc> *arglist = (vec<tree, va_gc> *) uncast_arglist;
unsigned int code = DECL_MD_FUNCTION_CODE (fndecl);
unsigned int subcode = code >> RISCV_BUILTIN_SHIFT;
tree new_fndecl = NULL_TREE;

if (!arglist)
arglist = &empty;

switch (code & RISCV_BUILTIN_CLASS)
{
case RISCV_BUILTIN_GENERAL:
break;
case RISCV_BUILTIN_VECTOR:
new_fndecl = riscv_vector::resolve_overloaded_builtin (subcode, arglist);
break;
default:
gcc_unreachable ();
}

if (new_fndecl == NULL_TREE)
return new_fndecl;

return build_function_call_vec (loc, vNULL, new_fndecl, arglist, NULL,
fndecl);
}

/* Implement REGISTER_TARGET_PRAGMAS. */

void
riscv_register_pragmas (void)
{
targetm.resolve_overloaded_builtin = riscv_resolve_overloaded_builtin;
targetm.check_builtin_call = riscv_check_builtin_call;
c_register_pragma ("riscv", "intrinsic", riscv_pragma_intrinsic);
}
1 change: 1 addition & 0 deletions gcc/config/riscv/riscv-protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ gimple *gimple_fold_builtin (unsigned int, gimple_stmt_iterator *, gcall *);
rtx expand_builtin (unsigned int, tree, rtx);
bool check_builtin_call (location_t, vec<location_t>, unsigned int,
tree, unsigned int, tree *);
tree resolve_overloaded_builtin (unsigned int, vec<tree, va_gc> *);
bool const_vec_all_same_in_range_p (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
bool legitimize_move (rtx, rtx, machine_mode);
void emit_vlmax_vsetvl (machine_mode, rtx);
Expand Down
1 change: 1 addition & 0 deletions gcc/config/riscv/riscv-vector-builtins-shapes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ build_one (function_builder &b, const function_group_info &group,
group.ops_infos.types[vec_type_idx].index);
b.allocate_argument_types (function_instance, argument_types);
b.apply_predication (function_instance, return_type, argument_types);
b.add_overloaded_function (function_instance, *group.shape);
b.add_unique_function (function_instance, (*group.shape), return_type,
argument_types);
}
Expand Down
Loading

0 comments on commit faa230f

Please sign in to comment.