From patchwork Wed Mar 14 20:56:31 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sriraman Tallam X-Patchwork-Id: 146718 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 221E2B6EEE for ; Thu, 15 Mar 2012 07:56:56 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1332363417; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Received:Received:To:Subject:Message-Id:Date: From:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=vjsafjF SS8zQ8HJxvh84JTg87oE=; b=EmIqSRVAC8dBBJYmAL0LxQbhJ40EpE9iwGbO4zV AGl2ygvHKNlhqtW9A1eLLFhk8rYgzxWWVW9v+EurfkK+1EUy0yD4hgAt1UYbQOQv OW4dg/rHEBTatu5ncwXRUTef2SCeTp9KPETuRJ2DpxVaWs/L0QQx7oR5e5iDPnxo DT1Y= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:Received:Received:To:Subject:Message-Id:Date:From:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=FFD+UWL9VyMARwxUozOEqdrvSRP8I+IMWQdxm8+K/lJprMftqaGDi0I/YJ33ox TKxAl1snZ7j0gwu6EY3ISPLf21QZ8z5PbcZGoozFwev3wBESNw5pwS8JDBRhpqoo oDVDBQrNfPnmkF4vOXnXSv5FffzNTnF43QpDV4U9nK7ms=; Received: (qmail 30182 invoked by alias); 14 Mar 2012 20:56:52 -0000 Received: (qmail 30173 invoked by uid 22791); 14 Mar 2012 20:56:49 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_LOW, TW_CP, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-gx0-f201.google.com (HELO mail-gx0-f201.google.com) (209.85.161.201) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 14 Mar 2012 20:56:32 +0000 Received: by ggki24 with SMTP id i24so388149ggk.2 for ; Wed, 14 Mar 2012 13:56:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=to:subject:message-id:date:from:x-gm-message-state; bh=GV69cSI2pbL1WQwdIsvUS/wTMsOusUF4IPmYn/z0KnY=; b=PFxGvfVHQoF8EyAv1W5+e3kZoJ3a6Fq82pVfWklBbbR+A2xwVAW0xMQrthQUuSO+69 ApkVFtR+aqnE481z+bWqvc++fQD+gXHA95luvBez5YdqGeAvGlLOD5QmitAsH6HaWFQW sM16ItQd1QnZsCR39rzglKvyyXMDHg75/1txb3D+QT17JA48jaVKdiEvq21EL0b9/PQU dFY5KLgamw3oAmaxDCNIj/rSnXl5DW3SX2AOdYW8bo/AdDknULmZqTnGfFE25aEVZSg5 QR3naWdf8pLgw2E2Aw7fuYet0MdTFKw9SL7VAdsFhDiX3GldVxiee3wsqjnoTFIX5M2Z VUqA== Received: by 10.236.165.99 with SMTP id d63mr5731561yhl.7.1331758591805; Wed, 14 Mar 2012 13:56:31 -0700 (PDT) Received: by 10.236.165.99 with SMTP id d63mr5731548yhl.7.1331758591734; Wed, 14 Mar 2012 13:56:31 -0700 (PDT) Received: from wpzn4.hot.corp.google.com (216-239-44-65.google.com [216.239.44.65]) by gmr-mx.google.com with ESMTPS id g49si2823505yhe.6.2012.03.14.13.56.31 (version=TLSv1/SSLv3 cipher=AES128-SHA); Wed, 14 Mar 2012 13:56:31 -0700 (PDT) Received: from azwildcat.mtv.corp.google.com (azwildcat.mtv.corp.google.com [172.18.110.231]) by wpzn4.hot.corp.google.com (Postfix) with ESMTP id 8093E1E005A; Wed, 14 Mar 2012 13:56:31 -0700 (PDT) Received: by azwildcat.mtv.corp.google.com (Postfix, from userid 69128) id 29C02B21D3; Wed, 14 Mar 2012 13:56:31 -0700 (PDT) To: reply@codereview.appspotmail.com,gcc-patches@gcc.gnu.org Subject: [google][4.6]Make option -freorder-functions= invoke function reordering linker plugin (issue5825054) Message-Id: <20120314205631.29C02B21D3@azwildcat.mtv.corp.google.com> Date: Wed, 14 Mar 2012 13:56:31 -0700 (PDT) From: tmsriram@google.com (Sriraman Tallam) X-Gm-Message-State: ALoCoQklXlJbbbtlwtPCjbCm/xk/4V1c1lcCglamdv7c9xqtVH9zM5Oobta9JuE4m1Pdac4XM3iqG+7QHTVicbb8nQmahAqW2mM3j/6j128C7BZOXvZgaf912l0qGMkHxNrozcqYYxZ3HpMBeTnktv1J7JmThuvzFZHo7syPbb/OxNkXFDh/bro= X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch overloads GCC option -freorder-functions with -freoder-functions=* which will invoke the linker plugin libfunction_reordering_plugin.so. For now, the only accepted option is -freoder-functions=cgedge, where the functions which are connected by hot callgraph edges are placed closer. When -freorder-functions is specified without the '=', the original behaviour is maintained. This patch also removes the -fcallgraph-profiles-sections option. For context, the function_reordering_plugin is only available in gcc-4_6 branch. I am working on porting this to trunk and will send out a patch soon. * cgraphbuild.c (remove_cgraph_callee_edges): Replace flag_callgraph_profiles_sections with flag_reorder_functions. * final.c (rest_of_handle_final): Ditto. * configure: Regenerate. * config.in: undef FRPLUGINSONAME. * congifure.ac: Define FRPLUGINSONAME. * config.host: Set host_function_reordering_plugin_soname. * gcc.c: Invoke function_reordering linker plugin with -freorder-functions=* option. (set_func_reorder_linker_plugin_spec): New function. (main): Call set_func_reorder_linker_plugin_spec when -freorder-functions=* option is seen. * common.opt (fcallgraph-profiles-sections): Remove. (freorder-functions=): New option. * function_reordering_plugin.c (register_claim_file_hook): New function pointer. (register_all_symbols_read_hook): Ditto. (no_op): New global. (out_file): Make static. (is_api_exist): Make static. (process_option): New function. (onload): Return if no_op is set. Do not register handlers until necessary. --- This patch is available for review at http://codereview.appspot.com/5825054 Index: function_reordering_plugin/function_reordering_plugin.c =================================================================== --- function_reordering_plugin/function_reordering_plugin.c (revision 185372) +++ function_reordering_plugin/function_reordering_plugin.c (working copy) @@ -64,6 +64,9 @@ enum ld_plugin_status claim_file_hook (const struc int *claimed); enum ld_plugin_status all_symbols_read_hook (); +static ld_plugin_register_claim_file register_claim_file_hook = NULL; +static ld_plugin_register_all_symbols_read + register_all_symbols_read_hook = NULL; static ld_plugin_get_input_section_count get_input_section_count = NULL; static ld_plugin_get_input_section_type get_input_section_type = NULL; static ld_plugin_get_input_section_name get_input_section_name = NULL; @@ -73,12 +76,16 @@ static ld_plugin_allow_section_ordering allow_sect /* The file where the final function order will be stored. It is "./final_layout.txt". It can be changed by passing - new name to --plugin-opt */ + new name to --plugin-opt as --plugin-opt "file=". + To dump to stderr, say --plugin-opt "file=stderr". */ -char *out_file = "./final_layout.txt"; +static char *out_file = "./final_layout.txt"; -int is_api_exist = 0; +static int is_api_exist = 0; +/* The plugin does nothing when no-op is 1. */ +static int no_op = 0; + /* Copies new output file name out_file */ void get_filename (const char *name) { @@ -91,6 +98,39 @@ void get_filename (const char *name) strcpy (out_file, name); } +/* Process options to plugin. Options with prefix "group=" are special. + They specify the type of grouping. The option "group=none" makes the + plugin do nothing. Options with prefix "file=" set the output file + where the final function order must be stored. */ +void +process_option (const char *name) +{ + const char *option_group = "group="; + const char *option_file = "file="; + + /* Check if option is "group=" */ + if (strncmp (name, option_group, strlen (option_group)) == 0) + { + if (strcmp (name + strlen (option_group), "none") == 0) + no_op = 1; + else + no_op = 0; + return; + } + + /* Check if option is "file=" */ + if (strncmp (name, option_file, strlen (option_file)) == 0) + { + get_filename (name + strlen (option_file)); + return; + } + + /* Unknown option, set no_op to 1. */ + no_op = 1; + fprintf (stderr, "Unknown option to function reordering plugin :%s\n", + name); +} + /* Plugin entry point. */ enum ld_plugin_status onload (struct ld_plugin_tv *tv) @@ -105,14 +145,16 @@ onload (struct ld_plugin_tv *tv) case LDPT_GOLD_VERSION: break; case LDPT_OPTION: - get_filename (entry->tv_u.tv_string); + process_option (entry->tv_u.tv_string); + /* If no_op is set, do not do anything else. */ + if (no_op) return LDPS_OK; break; case LDPT_REGISTER_CLAIM_FILE_HOOK: - assert ((*entry->tv_u.tv_register_claim_file) (claim_file_hook) == LDPS_OK); + register_claim_file_hook = *entry->tv_u.tv_register_claim_file; break; case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: - assert ((*entry->tv_u.tv_register_all_symbols_read) (all_symbols_read_hook) - == LDPS_OK); + register_all_symbols_read_hook + = *entry->tv_u.tv_register_all_symbols_read; break; case LDPT_GET_INPUT_SECTION_COUNT: get_input_section_count = *entry->tv_u.tv_get_input_section_count; @@ -137,14 +179,25 @@ onload (struct ld_plugin_tv *tv) } } - if (get_input_section_count != NULL + assert (!no_op); + + if (register_all_symbols_read_hook != NULL + && register_claim_file_hook != NULL + && get_input_section_count != NULL && get_input_section_type != NULL && get_input_section_name != NULL && get_input_section_contents != NULL && update_section_order != NULL && allow_section_ordering != NULL) is_api_exist = 1; + else + return LDPS_OK; + /* Register handlers. */ + assert ((*register_all_symbols_read_hook) (all_symbols_read_hook) + == LDPS_OK); + assert ((*register_claim_file_hook) (claim_file_hook) + == LDPS_OK); return LDPS_OK; } @@ -161,9 +214,8 @@ claim_file_hook (const struct ld_plugin_input_file (void) claimed; - /* Silently return if the plugin APIs are not supported. */ - if (!is_api_exist) - return LDPS_OK; + /* Plugin APIs are supported if this is called. */ + assert (is_api_exist); if (is_ordering_specified == 0) { @@ -221,9 +273,8 @@ all_symbols_read_hook (void) unsigned int *shndx; FILE *fp; - /* Silently return if the plugin APIs are not supported. */ - if (!is_api_exist) - return LDPS_OK; + /* Plugin APIs are supported if this is called. */ + assert (is_api_exist); if (is_callgraph_empty ()) return LDPS_OK; Index: function_reordering_plugin/ChangeLog.google-4_6 =================================================================== --- function_reordering_plugin/ChangeLog.google-4_6 (revision 185372) +++ function_reordering_plugin/ChangeLog.google-4_6 (working copy) @@ -1,3 +1,15 @@ +2012-03-14 Sriraman Tallam + + * function_reordering_plugin.c (register_claim_file_hook): New function + pointer. + (register_all_symbols_read_hook): Ditto. + (no_op): New global. + (out_file): Make static. + (is_api_exist): Make static. + (process_option): New function. + (onload): Return if no_op is set. Do not register handlers until + necessary. + 2012-02-25 Sriraman Tallam * function_reordering_plugin.c: Check for presence of elf.h. Index: gcc/cgraphbuild.c =================================================================== --- gcc/cgraphbuild.c (revision 185372) +++ gcc/cgraphbuild.c (working copy) @@ -703,10 +703,10 @@ extern bool cgraph_callee_edges_final_cleanup; static unsigned int remove_cgraph_callee_edges (void) { - /* The -fcallgraph-profiles-sections flag needs the call-graph preserved + /* The -freorder-functions=* flag needs the call-graph preserved till pass_final. */ if (cgraph_callee_edges_final_cleanup - && flag_callgraph_profiles_sections) + && flag_reorder_functions > 1) return 0; cgraph_node_remove_callees (cgraph_node (current_function_decl)); Index: gcc/configure =================================================================== --- gcc/configure (revision 185372) +++ gcc/configure (working copy) @@ -4861,7 +4861,7 @@ fi { $as_echo "$as_me:${as_lineno-$LINENO}: result: $acx_cv_cc_gcc_supports_ada" >&5 $as_echo "$acx_cv_cc_gcc_supports_ada" >&6; } -if test x$GNATBIND != xno && test x$GNATMAKE != xno && test x$acx_cv_cc_gcc_supports_ada != xno; then +if test "x$GNATBIND" != xno && test "x$GNATMAKE" != xno && test x$acx_cv_cc_gcc_supports_ada != xno; then have_gnat=yes else have_gnat=no @@ -11480,6 +11480,12 @@ esac cat >>confdefs.h <<_ACEOF +#define FRPLUGINSONAME "${host_function_reordering_plugin_soname}" +_ACEOF + + + +cat >>confdefs.h <<_ACEOF #define LTOPLUGINSONAME "${host_lto_plugin_soname}" _ACEOF @@ -17572,7 +17578,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 17575 "configure" +#line 17581 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -17678,7 +17684,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 17681 "configure" +#line 17687 "configure" #include "confdefs.h" #if HAVE_DLFCN_H Index: gcc/final.c =================================================================== --- gcc/final.c (revision 185372) +++ gcc/final.c (working copy) @@ -4421,9 +4421,9 @@ rest_of_handle_final (void) decl_fini_priority_lookup (current_function_decl)); - /* With -fcallgraph-profiles-sections, add ".gnu.callgraph.text" section + /* With -freorder-functions=, add ".gnu.callgraph.text" section for storing profiling information. */ - if (flag_callgraph_profiles_sections + if (flag_reorder_functions > 1 && flag_profile_use && cgraph_node (current_function_decl) != NULL && (cgraph_node (current_function_decl))->callees != NULL) Index: gcc/gcc.c =================================================================== --- gcc/gcc.c (revision 185372) +++ gcc/gcc.c (working copy) @@ -663,6 +663,9 @@ proper position among the other output files. */ -plugin-opt=-fresolution=%u.res \ %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}} \ }"PLUGIN_COND_CLOSE" \ + %{freorder-functions=*: \ + -plugin %(func_reorder_linker_plugin_file) \ + -plugin-opt=%(func_reorder_linker_plugin_opt)} \ %{flto|flto=*:% + + * cgraphbuild.c (remove_cgraph_callee_edges): Replace + flag_callgraph_profiles_sections with flag_reorder_functions. + * final.c (rest_of_handle_final): Ditto. + * configure: Regenerate. + * config.in: undef FRPLUGINSONAME. + * congifure.ac: Define FRPLUGINSONAME. + * config.host: Set host_function_reordering_plugin_soname. + * gcc.c: Invoke function_reordering linker plugin with + -freorder-functions=* option. + (set_func_reorder_linker_plugin_spec): New function. + (main): Call set_func_reorder_linker_plugin_spec when + -freorder-functions=* option is seen. + * common.opt: Add new option -freorder-functions=. Remove + -fcallgraph-profiles-sections. + + 2012-03-12 Sterling Augustine Cary Coutant Index: gcc/common.opt =================================================================== --- gcc/common.opt (revision 185372) +++ gcc/common.opt (working copy) @@ -920,10 +920,6 @@ fcaller-saves Common Report Var(flag_caller_saves) Optimization Save registers around function calls -fcallgraph-profiles-sections -Common Report Var(flag_callgraph_profiles_sections) Init(0) -Generate .note.callgraph.text sections listing callees and edge counts. - fcheck-data-deps Common Report Var(flag_check_data_deps) Compare the results of several data dependence analyzers. @@ -1758,9 +1754,19 @@ Common Report Var(flag_reorder_blocks_and_partitio Reorder basic blocks and partition into hot and cold sections freorder-functions -Common Report Var(flag_reorder_functions) Optimization +Common Report Var(flag_reorder_functions,1) Init(0) Optimization Reorder functions to improve code placement +freorder-functions= +Common Joined RejectNegative Enum(function_reorder) Var(flag_reorder_functions) Init(0) Optimization +-freorder-functions=[cgedge] Select the scheme for function reordering. This invokes a linker plugin. Generate .note.callgraph.text sections listing callees and edge counts. + +Enum +Name(function_reorder) Type(int) UnknownError(unrecognized function reorder value %qs) + +EnumValue +Enum(function_reorder) String(cgedge) Value(2) + frerun-cse-after-loop Common Report Var(flag_rerun_cse_after_loop) Optimization Add a common subexpression elimination pass after loop optimizations Index: gcc/config.host =================================================================== --- gcc/config.host (revision 185372) +++ gcc/config.host (working copy) @@ -76,6 +76,7 @@ out_host_hook_obj=host-default.o host_can_use_collect2=yes use_long_long_for_widest_fast_int=no host_lto_plugin_soname=liblto_plugin.so +host_function_reordering_plugin_soname=libfunction_reordering_plugin.so # Unsupported hosts list. Generally, only include hosts known to fail here, # since we allow hosts not listed to be supported generically.