1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
|
/* Pass for parsing functions with multiple target attributes.
Contributed by Evgeny Stupachenko <evstupac@gmail.com>
Copyright (C) 2015-2024 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "stringpool.h"
#include "gimple.h"
#include "diagnostic-core.h"
#include "gimple-ssa.h"
#include "cgraph.h"
#include "tree-pass.h"
#include "target.h"
#include "attribs.h"
#include "pretty-print.h"
#include "gimple-iterator.h"
#include "gimple-walk.h"
#include "tree-inline.h"
#include "intl.h"
/* Walker callback that replaces all FUNCTION_DECL of a function that's
going to be versioned. */
static tree
replace_function_decl (tree *op, int *walk_subtrees, void *data)
{
struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
if (TREE_CODE (*op) == FUNCTION_DECL
&& info->this_node->decl == *op)
{
*op = info->dispatcher_resolver;
*walk_subtrees = 0;
}
return NULL;
}
/* If the call in NODE has multiple target attribute with multiple fields,
replace it with dispatcher call and create dispatcher (once). */
static void
create_dispatcher_calls (struct cgraph_node *node)
{
ipa_ref *ref;
if (!targetm.has_ifunc_p ())
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"the call requires %<ifunc%>, which is not"
" supported by this target");
return;
}
else if (!targetm.get_function_versions_dispatcher)
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"target does not support function version dispatcher");
return;
}
tree idecl = targetm.get_function_versions_dispatcher (node->decl);
if (!idecl)
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"default %<target_clones%> attribute was not set");
return;
}
cgraph_node *inode = cgraph_node::get (idecl);
gcc_assert (inode);
tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
/* Update aliases. */
inode->alias = true;
inode->alias_target = resolver_decl;
if (!inode->analyzed)
inode->resolve_alias (cgraph_node::get (resolver_decl));
auto_vec<cgraph_edge *> edges_to_redirect;
/* We need to capture the references by value rather than just pointers to them
and remove them right away, as removing them later would invalidate what
some other reference pointers point to. */
auto_vec<ipa_ref> references_to_redirect;
while (node->iterate_referring (0, ref))
{
references_to_redirect.safe_push (*ref);
ref->remove_reference ();
}
/* We need to remember NEXT_CALLER as it could be modified in the loop. */
for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
edges_to_redirect.safe_push (e);
if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
{
/* Redirect edges. */
unsigned i;
cgraph_edge *e;
FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
{
e->redirect_callee (inode);
cgraph_edge::redirect_call_stmt_to_callee (e);
}
/* Redirect references. */
FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
{
if (ref->use == IPA_REF_ADDR)
{
struct walk_stmt_info wi;
memset (&wi, 0, sizeof (wi));
wi.info = (void *)node->function_version ();
if (dyn_cast<varpool_node *> (ref->referring))
{
hash_set<tree> visited_nodes;
walk_tree (&DECL_INITIAL (ref->referring->decl),
replace_function_decl, &wi, &visited_nodes);
}
else
{
gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
if (ref->referring->decl != resolver_decl)
walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
}
symtab_node *source = ref->referring;
source->create_reference (inode, IPA_REF_ADDR);
}
else if (ref->use == IPA_REF_ALIAS)
{
symtab_node *source = ref->referring;
source->create_reference (inode, IPA_REF_ALIAS);
if (inode->get_comdat_group ())
source->add_to_same_comdat_group (inode);
}
else
gcc_unreachable ();
}
}
tree fname = clone_function_name (node->decl, "default");
symtab->change_decl_assembler_name (node->decl, fname);
if (node->definition)
{
/* FIXME: copy of cgraph_node::make_local that should be cleaned up
in next stage1. */
node->make_decl_local ();
node->set_section (NULL);
node->set_comdat_group (NULL);
node->externally_visible = false;
node->forced_by_abi = false;
DECL_ARTIFICIAL (node->decl) = 1;
node->force_output = true;
}
}
/* Create string with attributes separated by comma.
Return number of attributes. */
static int
get_attr_str (tree arglist, char *attr_str)
{
tree arg;
size_t str_len_sum = 0;
int argnum = 0;
for (arg = arglist; arg; arg = TREE_CHAIN (arg))
{
const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
size_t len = strlen (str);
for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
argnum++;
memcpy (attr_str + str_len_sum, str, len);
attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
str_len_sum += len + 1;
argnum++;
}
return argnum;
}
/* Return number of attributes separated by comma and put them into ARGS.
If there is no DEFAULT attribute return -1.
If there is an empty string in attribute return -2.
If there are multiple DEFAULT attributes return -3.
*/
static int
separate_attrs (char *attr_str, char **attrs, int attrnum)
{
int i = 0;
int default_count = 0;
for (char *attr = strtok (attr_str, ",");
attr != NULL; attr = strtok (NULL, ","))
{
if (strcmp (attr, "default") == 0)
{
default_count++;
continue;
}
attrs[i++] = attr;
}
if (default_count == 0)
return -1;
else if (default_count > 1)
return -3;
else if (i + default_count < attrnum)
return -2;
return i;
}
/* Return true if symbol is valid in assembler name. */
static bool
is_valid_asm_symbol (char c)
{
if ('a' <= c && c <= 'z')
return true;
if ('A' <= c && c <= 'Z')
return true;
if ('0' <= c && c <= '9')
return true;
if (c == '_')
return true;
return false;
}
/* Replace all not valid assembler symbols with '_'. */
static void
create_new_asm_name (char *old_asm_name, char *new_asm_name)
{
int i;
int old_name_len = strlen (old_asm_name);
/* Replace all not valid assembler symbols with '_'. */
for (i = 0; i < old_name_len; i++)
if (!is_valid_asm_symbol (old_asm_name[i]))
new_asm_name[i] = '_';
else
new_asm_name[i] = old_asm_name[i];
new_asm_name[old_name_len] = '\0';
}
/* Creates target clone of NODE. */
static cgraph_node *
create_target_clone (cgraph_node *node, bool definition, char *name,
tree attributes)
{
cgraph_node *new_node;
if (definition)
{
new_node
= node->create_version_clone_with_body (vNULL, NULL, NULL, NULL, NULL,
name, attributes, false);
if (new_node == NULL)
return NULL;
new_node->force_output = true;
}
else
{
tree new_decl = copy_node (node->decl);
new_node = cgraph_node::get_create (new_decl);
DECL_ATTRIBUTES (new_decl) = attributes;
/* Generate a new name for the new version. */
tree fname = clone_function_name (node->decl, name);
symtab->change_decl_assembler_name (new_node->decl, fname);
}
return new_node;
}
/* If the function in NODE has multiple target attributes
create the appropriate clone for each valid target attribute. */
static bool
expand_target_clones (struct cgraph_node *node, bool definition)
{
int i;
/* Parsing target attributes separated by comma. */
tree attr_target = lookup_attribute ("target_clones",
DECL_ATTRIBUTES (node->decl));
/* No targets specified. */
if (!attr_target)
return false;
tree arglist = TREE_VALUE (attr_target);
int attr_len = get_target_clone_attr_len (arglist);
/* No need to clone for 1 target attribute. */
if (attr_len == -1)
{
warning_at (DECL_SOURCE_LOCATION (node->decl),
0, "single %<target_clones%> attribute is ignored");
return false;
}
if (node->definition
&& (node->alias || !tree_versionable_function_p (node->decl)))
{
auto_diagnostic_group d;
error_at (DECL_SOURCE_LOCATION (node->decl),
"clones for %<target_clones%> attribute cannot be created");
const char *reason = NULL;
if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
reason = G_("function %q+F can never be copied "
"because it has %<noclone%> attribute");
else if (node->alias)
reason
= "%<target_clones%> cannot be combined with %<alias%> attribute";
else
reason = copy_forbidden (DECL_STRUCT_FUNCTION (node->decl));
if (reason)
inform (DECL_SOURCE_LOCATION (node->decl), reason, node->decl);
return false;
}
char *attr_str = XNEWVEC (char, attr_len);
int attrnum = get_attr_str (arglist, attr_str);
char **attrs = XNEWVEC (char *, attrnum);
attrnum = separate_attrs (attr_str, attrs, attrnum);
switch (attrnum)
{
case -1:
error_at (DECL_SOURCE_LOCATION (node->decl),
"%<default%> target was not set");
break;
case -2:
error_at (DECL_SOURCE_LOCATION (node->decl),
"an empty string cannot be in %<target_clones%> attribute");
break;
case -3:
error_at (DECL_SOURCE_LOCATION (node->decl),
"multiple %<default%> targets were set");
break;
default:
break;
}
if (attrnum < 0)
{
XDELETEVEC (attrs);
XDELETEVEC (attr_str);
return false;
}
const char *new_attr_name = (TARGET_HAS_FMV_TARGET_ATTRIBUTE
? "target" : "target_version");
cgraph_function_version_info *decl1_v = NULL;
cgraph_function_version_info *decl2_v = NULL;
cgraph_function_version_info *before = NULL;
cgraph_function_version_info *after = NULL;
decl1_v = node->function_version ();
if (decl1_v == NULL)
decl1_v = node->insert_new_function_version ();
before = decl1_v;
DECL_FUNCTION_VERSIONED (node->decl) = 1;
for (i = 0; i < attrnum; i++)
{
char *attr = attrs[i];
/* Create new target clone. */
tree attributes = make_attribute (new_attr_name, attr,
DECL_ATTRIBUTES (node->decl));
char *suffix = XNEWVEC (char, strlen (attr) + 1);
create_new_asm_name (attr, suffix);
cgraph_node *new_node = create_target_clone (node, definition, suffix,
attributes);
XDELETEVEC (suffix);
if (new_node == NULL)
{
XDELETEVEC (attrs);
XDELETEVEC (attr_str);
return false;
}
new_node->local = false;
decl2_v = new_node->function_version ();
if (decl2_v != NULL)
continue;
decl2_v = new_node->insert_new_function_version ();
/* Chain decl2_v and decl1_v. All semantically identical versions
will be chained together. */
after = decl2_v;
while (before->next != NULL)
before = before->next;
while (after->prev != NULL)
after = after->prev;
before->next = after;
after->prev = before;
DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
}
XDELETEVEC (attrs);
XDELETEVEC (attr_str);
/* Setting new attribute to initial function. */
tree attributes = make_attribute (new_attr_name, "default",
DECL_ATTRIBUTES (node->decl));
DECL_ATTRIBUTES (node->decl) = attributes;
node->local = false;
return true;
}
/* When NODE is a target clone, consider all callees and redirect
to a clone with equal target attributes. That prevents multiple
multi-versioning dispatches and a call-chain can be optimized. */
static void
redirect_to_specific_clone (cgraph_node *node)
{
cgraph_function_version_info *fv = node->function_version ();
if (fv == NULL)
return;
tree attr_target = lookup_attribute ("target", DECL_ATTRIBUTES (node->decl));
if (attr_target == NULL_TREE)
return;
/* We need to remember NEXT_CALLER as it could be modified in the loop. */
for (cgraph_edge *e = node->callees; e ; e = e->next_callee)
{
cgraph_function_version_info *fv2 = e->callee->function_version ();
if (!fv2)
continue;
tree attr_target2 = lookup_attribute ("target",
DECL_ATTRIBUTES (e->callee->decl));
/* Function is not calling proper target clone. */
if (attr_target2 == NULL_TREE
|| !attribute_value_equal (attr_target, attr_target2))
{
while (fv2->prev != NULL)
fv2 = fv2->prev;
/* Try to find a clone with equal target attribute. */
for (; fv2 != NULL; fv2 = fv2->next)
{
cgraph_node *callee = fv2->this_node;
attr_target2 = lookup_attribute ("target",
DECL_ATTRIBUTES (callee->decl));
if (attr_target2 != NULL_TREE
&& attribute_value_equal (attr_target, attr_target2))
{
e->redirect_callee (callee);
cgraph_edge::redirect_call_stmt_to_callee (e);
break;
}
}
}
}
}
static unsigned int
ipa_target_clone (void)
{
struct cgraph_node *node;
auto_vec<cgraph_node *> to_dispatch;
FOR_EACH_FUNCTION (node)
if (expand_target_clones (node, node->definition))
to_dispatch.safe_push (node);
for (unsigned i = 0; i < to_dispatch.length (); i++)
create_dispatcher_calls (to_dispatch[i]);
FOR_EACH_FUNCTION (node)
redirect_to_specific_clone (node);
return 0;
}
namespace {
const pass_data pass_data_target_clone =
{
SIMPLE_IPA_PASS, /* type */
"targetclone", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_NONE, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_update_ssa /* todo_flags_finish */
};
class pass_target_clone : public simple_ipa_opt_pass
{
public:
pass_target_clone (gcc::context *ctxt)
: simple_ipa_opt_pass (pass_data_target_clone, ctxt)
{}
/* opt_pass methods: */
bool gate (function *) final override;
unsigned int execute (function *) final override
{
return ipa_target_clone ();
}
};
bool
pass_target_clone::gate (function *)
{
/* If there were any errors avoid pass property verification errors. */
return !seen_error ();
}
} // anon namespace
simple_ipa_opt_pass *
make_pass_target_clone (gcc::context *ctxt)
{
return new pass_target_clone (ctxt);
}
|