aboutsummaryrefslogtreecommitdiff
path: root/gcc/cgraph.h
diff options
context:
space:
mode:
authorSriraman Tallam <tmsriram@google.com>2012-11-06 02:35:17 +0000
committerSriraman Tallam <tmsriram@google.com>2012-11-06 02:35:17 +0000
commit3d74ed35d627531fe30b365556b7d7ebfa67f72e (patch)
tree9f75f10875e97a88f4cbb481f476857a29e3bbb2 /gcc/cgraph.h
parentbd4187d6d468065a9794b35d4cea4a7c1bb6de89 (diff)
Function Multiversioning
======================== Sriraman Tallam, tmsriram@google.com Overview of the patch which adds support to specify function versions. This is only enabled for target i386. Example: int foo (); /* Default version */ int foo () __attribute__ ((target("avx,popcnt")));/*Specialized for avx and popcnt */ int foo () __attribute__ ((target("arch=core2,ssse3")));/*Specialized for core2 and ssse3*/ int main () { int (*p)() = &foo; return foo () + (*p)(); } int foo () { return 0; } int __attribute__ ((target("avx,popcnt"))) foo () { return 0; } int __attribute__ ((target("arch=core2,ssse3"))) foo () { return 0; } The above example has foo defined 3 times, but all 3 definitions of foo are different versions of the same function. The call to foo in main, directly and via a pointer, are calls to the multi-versioned function foo which is dispatched to the right foo at run-time. Front-end changes: The front-end changes are calls at appropriate places to target hooks that determine the following: * Determine if two function decls with the same signature are versions. * Determine the assembler name of a function version. * Generate the dispatcher function for a set of function versions. * Compare versions to see if one has a higher priority over the other. All the implementation happens in the target-specific config/i386/i386.c. What does the patch do? * Tracking decls that correspond to function versions of function name, say "foo": When the front-end sees more than one decl for "foo", it calls a target hook to determine if they are versions. To prevent duplicate definition errors with other versions of "foo", "decls_match" function in cp/decl.c is made to return false when 2 decls have are deemed versions by the target. This will make all function versions of "foo" to be added to the overload list of "foo". * Change the assembler names of the function versions. For i386, the target changes the assembler names of the function versions by suffixing the sorted list of args to "target" to the function name of "foo". For example, the assembler name of "void foo () __attribute__ ((target ("sse4")))" will become _Z3foov.sse4. The target hook mangle_decl_assembler_name is used for this. * Overload resolution: Function "build_over_call" in cp/call.c sees a call to function "foo", which is multi-versioned. The overload resolution happens in function "joust" in "cp/call.c". Here, the call to "foo" has all possible versions of "foo" as candidates. All the candidates of "foo" are stored in the cgraph side data structure. Each version of foo is chained in a doubly-linked list with the default function as the first element. This allows any pass to access all the semantically identical versions. A call to a multi-versioned function will be replaced by a call to a dispatcher function, determined by a target hook, to execute the right function version at run-time. Optimization to directly call a version when possible: Also, in joust, where overload resolution happens, a multiversioned function resolution is made to return the most specialized version. This is the version that will be checked for dispatching first and is determined by the target. Now, if the caller can inline this function version then a direct call is made to this function version rather than go through the dispatcher. When a direct call cannot be made, a call to the dispatcher function is created. * Creating the dispatcher body. The dispatcher body, called the resolver is made only when there is a call to a multiversioned function dispatcher or the address of a function is taken. This is generated during cgraph_analyze_function. This is done by another target hook. * Dispatch ordering. The order in which the function versions are checked during dispatch is based on a priority value assigned for the ISA that is catered. More specialized versions are checked for dispatching first. This is to mitigate the ambiguity that can arise when more than one function version is valid for execution on a particular platform. This is not a perfect solution, and in future the user should be allowed to assign a dispatching priority value to each version. Function MV in the Intel compiler: The intel compiler supports function multiversioning and the syntax is similar to the patch proposed here. Here is an example of how to generate multiple function versions with the intel compiler. /* Create a stub function to specify the various versions of function that will be created, using declspec attribute cpu_dispatch. */ __declspec (cpu_dispatch (core_i7_sse4_2, atom, generic)) void foo () {}; /* Bodies of each function version. */ /* Intel Corei7 processor + SSE4.2 version. */ __declspec (cpu_specific(core_i7_sse4_2)) void foo () { printf ("corei7 + sse4.2"); } /* Atom processor. */ __declspec (cpu_specific(atom)) void foo () { printf ("atom"); } /* The generic or the default version. */ __declspec (cpu_specific(generic)) void foo () { printf ("This is generic"); } A new function version is generated by defining a new function with the same signature but with a different cpu_specific declspec attribute string. The set of cpu_specific strings that are allowed is the following: "core_2nd_gen_avx" "core_aes_pclmulqdq" "core_i7_sse4_2" "core_2_duo_sse4_1" "core_2_duo_ssse3" "atom" "pentium_4_sse3" "pentium_4" "pentium_m" "pentium_iii" "generic" Comparison with the GCC MV implementation in this patch: * Version creation syntax: The implementation in this patch also has a similar syntax to specify function versions. The first stub function is not needed. Here is the code to generate the function versions with this patch: /* Intel Corei7 processor + SSE4.2 version. */ __attribute__ ((target ("arch=corei7, sse4.2"))) void foo () { printf ("corei7 + sse4.2"); } /* Atom processor. */ __attribute__ ((target ("arch=atom"))) void foo () { printf ("atom"); } void foo () { } The target attribute can have one of the following arch names: "amd" "intel" "atom" "core2" "corei7" "nehalem" "westmere" "sandybridge" "amdfam10h" "barcelona" "shanghai" "istanbul" "amdfam15h" "bdver1" "bdver2" and any number of the following ISA names: "cmov" "mmx" "popcnt" "sse" "sse2" "sse3" "ssse3" "sse4.1" "sse4.2" "avx" "avx2" * doc/tm.texi.in (TARGET_OPTION_FUNCTION_VERSIONS): New hook description. * (TARGET_COMPARE_VERSION_PRIORITY): New hook description. * (TARGET_GET_FUNCTION_VERSIONS_DISPATCHER): New hook description. * (TARGET_GENERATE_VERSION_DISPATCHER_BODY): New hook description. * doc/tm.texi: Regenerate. * target.def (compare_version_priority): New target hook. * (generate_version_dispatcher_body): New target hook. * (get_function_versions_dispatcher): New target hook. * (function_versions): New target hook. * cgraph.c (cgraph_fnver_htab): New htab. (cgraph_fn_ver_htab_hash): New function. (cgraph_fn_ver_htab_eq): New function. (version_info_node): New pointer. (insert_new_cgraph_node_version): New function. (get_cgraph_node_version): New function. (delete_function_version): New function. (record_function_versions): New function. * cgraph.h (cgraph_node): New bitfield dispatcher_function. (cgraph_function_version_info): New struct. (get_cgraph_node_version): New function. (insert_new_cgraph_node_version): New function. (record_function_versions): New function. (delete_function_version): New function. (init_lowered_empty_function): Expose function. * tree.h (DECL_FUNCTION_VERSIONED): New macro. (tree_function_decl): New bit-field versioned_function. * cgraphunit.c (cgraph_analyze_function): Generate body of multiversion function dispatcher. (cgraph_analyze_functions): Analyze dispatcher function. (init_lowered_empty_function): Make non-static. New parameter in_ssa. (assemble_thunk): Add parameter to call to init_lowered_empty_function. * config/i386/i386.c (add_condition_to_bb): New function. (get_builtin_code_for_version): New function. (ix86_compare_version_priority): New function. (feature_compare): New function. (dispatch_function_versions): New function. (ix86_function_versions): New function. (attr_strcmp): New function. (ix86_mangle_function_version_assembler_name): New function. (ix86_mangle_decl_assembler_name): New function. (make_name): New function. (make_dispatcher_decl): New function. (is_function_default_version): New function. (ix86_get_function_versions_dispatcher): New function. (make_attribute): New function. (make_resolver_func): New function. (ix86_generate_version_dispatcher_body): New function. (fold_builtin_cpu): Return integer for cpu builtins. (TARGET_MANGLE_DECL_ASSEMBLER_NAME): New macro. (TARGET_COMPARE_VERSION_PRIORITY): New macro. (TARGET_GENERATE_VERSION_DISPATCHER_BODY): New macro. (TARGET_GET_FUNCTION_VERSIONS_DISPATCHER): New macro. (TARGET_OPTION_FUNCTION_VERSIONS): New macro. * class.c (add_method): Change assembler names of function versions. (mark_versions_used): New static function. (resolve_address_of_overloaded_function): Create dispatcher decl and return address of dispatcher instead. * decl.c (decls_match): Make decls unmatched for versioned functions. (duplicate_decls): Remove ambiguity for versioned functions. Delete versioned function data for merged decls. * decl2.c (check_classfn): Check attributes of versioned functions for match. * call.c (get_function_version_dispatcher): New function. (mark_versions_used): New static function. (build_over_call): Make calls to multiversioned functions to call the dispatcher. (joust): For calls to multi-versioned functions, make the most specialized function version win. * testsuite/g++.dg/mv1.C: New test. * testsuite/g++.dg/mv2.C: New test. * testsuite/g++.dg/mv3.C: New test. * testsuite/g++.dg/mv4.C: New test. * testsuite/g++.dg/mv5.C: New test. * testsuite/g++.dg/mv6.C: New test. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@193204 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/cgraph.h')
-rw-r--r--gcc/cgraph.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/gcc/cgraph.h b/gcc/cgraph.h
index f276512df31..25c1f33eb57 100644
--- a/gcc/cgraph.h
+++ b/gcc/cgraph.h
@@ -280,6 +280,8 @@ struct GTY(()) cgraph_node {
/* ?? We should be able to remove this. We have enough bits in
cgraph to calculate it. */
unsigned tm_clone : 1;
+ /* True if this decl is a dispatcher for function versions. */
+ unsigned dispatcher_function : 1;
};
DEF_VEC_P(symtab_node);
@@ -292,6 +294,47 @@ DEF_VEC_P(cgraph_node_ptr);
DEF_VEC_ALLOC_P(cgraph_node_ptr,heap);
DEF_VEC_ALLOC_P(cgraph_node_ptr,gc);
+/* Function Multiversioning info. */
+struct GTY(()) cgraph_function_version_info {
+ /* The cgraph_node for which the function version info is stored. */
+ struct cgraph_node *this_node;
+ /* Chains all the semantically identical function versions. The
+ first function in this chain is the version_info node of the
+ default function. */
+ struct cgraph_function_version_info *prev;
+ /* If this version node corresponds to a dispatcher for function
+ versions, this points to the version info node of the default
+ function, the first node in the chain. */
+ struct cgraph_function_version_info *next;
+ /* If this node corresponds to a function version, this points
+ to the dispatcher function decl, which is the function that must
+ be called to execute the right function version at run-time.
+
+ If this cgraph node is a dispatcher (if dispatcher_function is
+ true, in the cgraph_node struct) for function versions, this
+ points to resolver function, which holds the function body of the
+ dispatcher. The dispatcher decl is an alias to the resolver
+ function decl. */
+ tree dispatcher_resolver;
+};
+
+/* Get the cgraph_function_version_info node corresponding to node. */
+struct cgraph_function_version_info *
+ get_cgraph_node_version (struct cgraph_node *node);
+
+/* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
+ corresponding to cgraph_node NODE. */
+struct cgraph_function_version_info *
+ insert_new_cgraph_node_version (struct cgraph_node *node);
+
+/* Record that DECL1 and DECL2 are semantically identical function
+ versions. */
+void record_function_versions (tree decl1, tree decl2);
+
+/* Remove the cgraph_function_version_info and cgraph_node for DECL. This
+ DECL is a duplicate declaration. */
+void delete_function_version (tree decl);
+
/* A cgraph node set is a collection of cgraph nodes. A cgraph node
can appear in multiple sets. */
struct cgraph_node_set_def
@@ -638,6 +681,9 @@ void init_cgraph (void);
bool cgraph_process_new_functions (void);
void cgraph_process_same_body_aliases (void);
void fixup_same_cpp_alias_visibility (symtab_node node, symtab_node target, tree alias);
+/* Initialize datastructures so DECL is a function in lowered gimple form.
+ IN_SSA is true if the gimple is in SSA. */
+basic_block init_lowered_empty_function (tree decl, bool in_ssa);
/* In cgraphclones.c */