Skip to content

Commit 9bf0dbe

Browse files
cyyselfkito-cheng
authored andcommitted
Introduce TARGET_CLONES_ATTR_SEPARATOR for RISC-V
Some architectures may use ',' in the attribute string, but it is not used as the separator for different targets. To avoid conflict, we introduce a new macro TARGET_CLONES_ATTR_SEPARATOR to separate different clones. As an example, according to RISC-V C-API Specification [1], RISC-V allows ',' in the attribute string in the "arch=" option to specify one more ISA extensions in the same target function, which conflict with the default separator to separate different clones. This patch introduces TARGET_CLONES_ATTR_SEPARATOR for RISC-V and choose '#' as the separator, since '#' is not allowed in the target_clones option string. [1] https://github.com/riscv-non-isa/riscv-c-api-doc/blob/c6c5d6d9cf96b342293315a5dff3d25e96ef8191/src/c-api.adoc#__attribute__targetattr-string Signed-off-by: Yangyu Chen <[email protected]> gcc/ChangeLog: * defaults.h (TARGET_CLONES_ATTR_SEPARATOR): Define new macro. * multiple_target.cc (get_attr_str): Use TARGET_CLONES_ATTR_SEPARATOR to separate attributes. (separate_attrs): Likewise. (expand_target_clones): Likewise. * attribs.cc (attr_strcmp): Likewise. (sorted_attr_string): Likewise. * tree.cc (get_target_clone_attr_len): Likewise. * config/riscv/riscv.h (TARGET_CLONES_ATTR_SEPARATOR): Define TARGET_CLONES_ATTR_SEPARATOR for RISC-V. * doc/tm.texi: Document TARGET_CLONES_ATTR_SEPARATOR. * doc/tm.texi.in: Likewise.
1 parent f530a8c commit 9bf0dbe

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

gcc/attribs.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,9 +1103,10 @@ attr_strcmp (const void *v1, const void *v2)
11031103
}
11041104

11051105
/* ARGLIST is the argument to target attribute. This function tokenizes
1106-
the comma separated arguments, sorts them and returns a string which
1107-
is a unique identifier for the comma separated arguments. It also
1108-
replaces non-identifier characters "=,-" with "_". */
1106+
the TARGET_CLONES_ATTR_SEPARATOR separated arguments, sorts them and
1107+
returns a string which is a unique identifier for the
1108+
TARGET_CLONES_ATTR_SEPARATOR separated arguments. It also replaces
1109+
non-identifier characters "=,-" with "_". */
11091110

11101111
char *
11111112
sorted_attr_string (tree arglist)
@@ -1117,6 +1118,7 @@ sorted_attr_string (tree arglist)
11171118
char *attr = NULL;
11181119
unsigned int argnum = 1;
11191120
unsigned int i;
1121+
static const char separator_str[] = { TARGET_CLONES_ATTR_SEPARATOR, 0 };
11201122

11211123
for (arg = arglist; arg; arg = TREE_CHAIN (arg))
11221124
{
@@ -1126,7 +1128,7 @@ sorted_attr_string (tree arglist)
11261128
if (arg != arglist)
11271129
argnum++;
11281130
for (i = 0; i < strlen (str); i++)
1129-
if (str[i] == ',')
1131+
if (str[i] == TARGET_CLONES_ATTR_SEPARATOR)
11301132
argnum++;
11311133
}
11321134

@@ -1137,7 +1139,8 @@ sorted_attr_string (tree arglist)
11371139
const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
11381140
size_t len = strlen (str);
11391141
memcpy (attr_str + str_len_sum, str, len);
1140-
attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
1142+
attr_str[str_len_sum + len]
1143+
= TREE_CHAIN (arg) ? TARGET_CLONES_ATTR_SEPARATOR : '\0';
11411144
str_len_sum += len + 1;
11421145
}
11431146

@@ -1152,12 +1155,12 @@ sorted_attr_string (tree arglist)
11521155
args = XNEWVEC (char *, argnum);
11531156

11541157
i = 0;
1155-
attr = strtok (attr_str, ",");
1158+
attr = strtok (attr_str, separator_str);
11561159
while (attr != NULL)
11571160
{
11581161
args[i] = attr;
11591162
i++;
1160-
attr = strtok (NULL, ",");
1163+
attr = strtok (NULL, separator_str);
11611164
}
11621165

11631166
qsort (args, argnum, sizeof (char *), attr_strcmp);

gcc/config/riscv/riscv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,4 +1298,9 @@ extern void riscv_remove_unneeded_save_restore_calls (void);
12981298
STACK_BOUNDARY / BITS_PER_UNIT) \
12991299
: (crtl->outgoing_args_size + STACK_POINTER_OFFSET))
13001300

1301+
/* According to the RISC-V C API, the arch string may contains ','. To avoid
1302+
the conflict with the default separator, we choose '#' as the separator for
1303+
the target attribute. */
1304+
#define TARGET_CLONES_ATTR_SEPARATOR '#'
1305+
13011306
#endif /* ! GCC_RISCV_H */

gcc/defaults.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
874874
#define TARGET_HAS_FMV_TARGET_ATTRIBUTE 1
875875
#endif
876876

877+
/* Select a attribute separator for function multiversioning. */
878+
#ifndef TARGET_CLONES_ATTR_SEPARATOR
879+
#define TARGET_CLONES_ATTR_SEPARATOR ','
880+
#endif
877881

878882
/* Select a format to encode pointers in exception handling data. We
879883
prefer those that result in fewer dynamic relocations. Assume no

gcc/doc/tm.texi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10822,6 +10822,13 @@ the function declaration to hold a pointer to a target-specific
1082210822
@code{struct cl_target_option} structure.
1082310823
@end deftypefn
1082410824

10825+
@defmac TARGET_CLONES_ATTR_SEPARATOR
10826+
Define this char-typed macro to select a character that separates each
10827+
target specific attributes from the @code{attribute(target_clones("..."))}
10828+
attribute string. This macro should be carefully chosen to avoid conflicts
10829+
with the target specific attributes. The default value is @code{','}.
10830+
@end defmac
10831+
1082510832
@deftypefn {Target Hook} void TARGET_OPTION_SAVE (struct cl_target_option *@var{ptr}, struct gcc_options *@var{opts}, struct gcc_options *@var{opts_set})
1082610833
This hook is called to save any additional target-specific information
1082710834
in the @code{struct cl_target_option} structure for function-specific

gcc/doc/tm.texi.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7101,6 +7101,13 @@ on this implementation detail.
71017101

71027102
@hook TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P
71037103

7104+
@defmac TARGET_CLONES_ATTR_SEPARATOR
7105+
Define this char-typed macro to select a character that separates each
7106+
target specific attributes from the @code{attribute(target_clones("..."))}
7107+
attribute string. This macro should be carefully chosen to avoid conflicts
7108+
with the target specific attributes. The default value is @code{','}.
7109+
@end defmac
7110+
71047111
@hook TARGET_OPTION_SAVE
71057112

71067113
@hook TARGET_OPTION_RESTORE

gcc/multiple_target.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ create_dispatcher_calls (struct cgraph_node *node)
181181
}
182182
}
183183

184-
/* Create string with attributes separated by comma.
184+
/* Create string with attributes separated by TARGET_CLONES_ATTR_SEPARATOR.
185185
Return number of attributes. */
186186

187187
static int
@@ -195,17 +195,21 @@ get_attr_str (tree arglist, char *attr_str)
195195
{
196196
const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
197197
size_t len = strlen (str);
198-
for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
198+
for (const char *p = strchr (str, TARGET_CLONES_ATTR_SEPARATOR);
199+
p;
200+
p = strchr (p + 1, TARGET_CLONES_ATTR_SEPARATOR))
199201
argnum++;
200202
memcpy (attr_str + str_len_sum, str, len);
201-
attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
203+
attr_str[str_len_sum + len]
204+
= TREE_CHAIN (arg) ? TARGET_CLONES_ATTR_SEPARATOR : '\0';
202205
str_len_sum += len + 1;
203206
argnum++;
204207
}
205208
return argnum;
206209
}
207210

208-
/* Return number of attributes separated by comma and put them into ARGS.
211+
/* Return number of attributes separated by TARGET_CLONES_ATTR_SEPARATOR
212+
and put them into ARGS.
209213
If there is no DEFAULT attribute return -1.
210214
If there is an empty string in attribute return -2.
211215
If there are multiple DEFAULT attributes return -3.
@@ -216,9 +220,10 @@ separate_attrs (char *attr_str, char **attrs, int attrnum)
216220
{
217221
int i = 0;
218222
int default_count = 0;
223+
static const char separator_str[] = { TARGET_CLONES_ATTR_SEPARATOR, 0 };
219224

220-
for (char *attr = strtok (attr_str, ",");
221-
attr != NULL; attr = strtok (NULL, ","))
225+
for (char *attr = strtok (attr_str, separator_str);
226+
attr != NULL; attr = strtok (NULL, separator_str))
222227
{
223228
if (strcmp (attr, "default") == 0)
224229
{
@@ -306,7 +311,7 @@ static bool
306311
expand_target_clones (struct cgraph_node *node, bool definition)
307312
{
308313
int i;
309-
/* Parsing target attributes separated by comma. */
314+
/* Parsing target attributes separated by TARGET_CLONES_ATTR_SEPARATOR. */
310315
tree attr_target = lookup_attribute ("target_clones",
311316
DECL_ATTRIBUTES (node->decl));
312317
/* No targets specified. */

gcc/tree.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15229,7 +15229,9 @@ get_target_clone_attr_len (tree arglist)
1522915229
const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1523015230
size_t len = strlen (str);
1523115231
str_len_sum += len + 1;
15232-
for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
15232+
for (const char *p = strchr (str, TARGET_CLONES_ATTR_SEPARATOR);
15233+
p;
15234+
p = strchr (p + 1, TARGET_CLONES_ATTR_SEPARATOR))
1523315235
argnum++;
1523415236
argnum++;
1523515237
}

0 commit comments

Comments
 (0)