Skip to content

Commit

Permalink
RISC-V: Implement Priority syntax parser for Function Multi-Versioning
Browse files Browse the repository at this point in the history
This patch adds the priority syntax parser to support the Function
Multi-Versioning (FMV) feature in RISC-V. This feature allows users to
specify the priority of the function version in the attribute syntax.

Chnages based on RISC-V C-API PR:
riscv-non-isa/riscv-c-api-doc#85

gcc/ChangeLog:

	* config/riscv/riscv-target-attr.cc
	(riscv_target_attr_parser::handle_priority): New function.
	(riscv_target_attr_parser::update_settings): Update priority
	attribute.
	(riscv_process_one_target_attr): Add const qualifier to arg_str
	and split arg_str with ';'.
	* config/riscv/riscv.opt: Add TargetVariable riscv_fmv_priority.
  • Loading branch information
cyyself committed Oct 27, 2024
1 parent d890613 commit be40591
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
35 changes: 33 additions & 2 deletions gcc/config/riscv/riscv-target-attr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ class riscv_target_attr_parser
: m_found_arch_p (false)
, m_found_tune_p (false)
, m_found_cpu_p (false)
, m_found_priority_p (false)
, m_subset_list (nullptr)
, m_loc (loc)
, m_cpu_info (nullptr)
, m_tune (nullptr)
, m_priority (0)
{
}

bool handle_arch (const char *);
bool handle_cpu (const char *);
bool handle_tune (const char *);
bool handle_priority (const char *);

void update_settings (struct gcc_options *opts) const;
private:
Expand All @@ -58,10 +61,12 @@ class riscv_target_attr_parser
bool m_found_arch_p;
bool m_found_tune_p;
bool m_found_cpu_p;
bool m_found_priority_p;
riscv_subset_list *m_subset_list;
location_t m_loc;
const riscv_cpu_info *m_cpu_info;
const char *m_tune;
int m_priority;
};
}

Expand All @@ -80,7 +85,8 @@ struct riscv_attribute_info
static const struct riscv_attribute_info riscv_attributes[]
= {{"arch", &riscv_target_attr_parser::handle_arch},
{"cpu", &riscv_target_attr_parser::handle_cpu},
{"tune", &riscv_target_attr_parser::handle_tune}};
{"tune", &riscv_target_attr_parser::handle_tune},
{"priority", &riscv_target_attr_parser::handle_priority}};

bool
riscv_target_attr_parser::parse_arch (const char *str)
Expand Down Expand Up @@ -210,6 +216,22 @@ riscv_target_attr_parser::handle_tune (const char *str)
return true;
}

bool
riscv_target_attr_parser::handle_priority (const char *str)
{
if (m_found_priority_p)
error_at (m_loc, "%<target()%> attribute: priority appears more than once");
m_found_priority_p = true;

if (sscanf (str, "%d", &m_priority) != 1)
{
error_at (m_loc, "%<target()%> attribute: invalid priority %qs", str);
return false;
}

return true;
}

void
riscv_target_attr_parser::update_settings (struct gcc_options *opts) const
{
Expand All @@ -236,13 +258,16 @@ riscv_target_attr_parser::update_settings (struct gcc_options *opts) const
if (m_cpu_info)
opts->x_riscv_tune_string = m_cpu_info->tune;
}

if (m_priority)
opts->x_riscv_fmv_priority = m_priority;
}

/* Parse ARG_STR which contains the definition of one target attribute.
Show appropriate errors if any or return true if the attribute is valid. */

static bool
riscv_process_one_target_attr (char *arg_str,
riscv_process_one_target_attr (const char *arg_str,
location_t loc,
riscv_target_attr_parser &attr_parser)
{
Expand Down Expand Up @@ -271,6 +296,12 @@ riscv_process_one_target_attr (char *arg_str,

arg[0] = '\0';
++arg;

/* Skip splitter ';' if it exists. */
char *splitter = strchr (arg, ';');
if (splitter)
splitter[0] = '\0';

for (const auto &attr : riscv_attributes)
{
/* If the names don't match up, or the user has given an argument
Expand Down
3 changes: 3 additions & 0 deletions gcc/config/riscv/riscv.opt
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ Mask(XSFVCP) Var(riscv_sifive_subext)

Mask(XSFCEASE) Var(riscv_sifive_subext)

TargetVariable
int riscv_fmv_priority = 0

Enum
Name(isa_spec_class) Type(enum riscv_isa_spec_class)
Supported ISA specs (for use with the -misa-spec= option):
Expand Down

0 comments on commit be40591

Please sign in to comment.