From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id A0B29385780A for ; Mon, 23 Nov 2020 12:18:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A0B29385780A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ucw.cz Authentication-Results: sourceware.org; spf=none smtp.mailfrom=hubicka@kam.mff.cuni.cz Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id E75AD28276C; Mon, 23 Nov 2020 13:18:52 +0100 (CET) Date: Mon, 23 Nov 2020 13:18:52 +0100 From: Jan Hubicka To: gcc-patches@gcc.gnu.org, rguenther@suse.de, mliska@suse.cz Subject: Do not leak memory when streaming in ssa names Message-ID: <20201123121852.GB37604@kam.mff.cuni.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-15.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Nov 2020 12:18:57 -0000 Hi, tree-streamer-in currently calls init_tree_ssa that calls init_ssanames that allocates space in ssa_names array for 50 names. Later it streams in the count and calls init_tree_ssa again, this time with correct count, which is rounded up to 50 and allocated again leaking the first array. This patch fixes it by adding extra argument to init_tree_ssa specifing whether ssa names should be initialized. There are few places where we initialize ssa and only lto streaming is special, so i think extra arg works well here. I am not quite sure about the 50MB default. I think it was made before ggc_free was implemented (so resizing vectors was expensive) and when we had only one function in SSA form at a time. Most of functions in C++ will probably need fewer than 50 SSA names until inlining. This saves 21MB of WPA linking libxul. Bootstrapping/regtesting x86_64-linux, OK if it suceeds? * tree-ssa.c (init_tree_ssa): Add argument INIT_SSANAMES_P. * tree-ssa.h (init_tree_ssa): Update prototype. * tree-ssanames.c (init_ssanames): Fix handling of explicit size. * tree-streamer-in.c (init_function): Pass false to init_tree_ssa. diff --git a/gcc/tree-ssa.c b/gcc/tree-ssa.c index b44361f8244..8b009d91830 100644 --- a/gcc/tree-ssa.c +++ b/gcc/tree-ssa.c @@ -1218,15 +1218,18 @@ err: # pragma GCC diagnostic pop #endif -/* Initialize global DFA and SSA structures. */ +/* Initialize global DFA and SSA structures. + If INIT_SSANAMES_P is false, do not initialize SSA names. During LTO + streaming we do that later once we know number of names. */ void -init_tree_ssa (struct function *fn) +init_tree_ssa (struct function *fn, bool init_ssanames_p) { fn->gimple_df = ggc_cleared_alloc (); fn->gimple_df->default_defs = hash_table::create_ggc (20); pt_solution_reset (&fn->gimple_df->escaped); - init_ssanames (fn, 0); + if (init_ssanames_p) + init_ssanames (fn, 0); } /* Deallocate memory associated with SSA data structures for FNDECL. */ diff --git a/gcc/tree-ssa.h b/gcc/tree-ssa.h index 79f2b13fd6a..9a4ff008300 100644 --- a/gcc/tree-ssa.h +++ b/gcc/tree-ssa.h @@ -45,7 +45,7 @@ extern void insert_debug_temps_for_defs (gimple_stmt_iterator *); extern void reset_debug_uses (gimple *); extern void release_defs_bitset (bitmap toremove); extern void verify_ssa (bool, bool); -extern void init_tree_ssa (function *); +extern void init_tree_ssa (function *, bool init_ssanames = true); extern void delete_tree_ssa (function *); extern bool tree_ssa_useless_type_conversion (tree); extern tree tree_ssa_strip_useless_type_conversions (tree); diff --git a/gcc/tree-ssanames.c b/gcc/tree-ssanames.c index 6ac97fe39c4..ec4681f85cb 100644 --- a/gcc/tree-ssanames.c +++ b/gcc/tree-ssanames.c @@ -77,10 +77,10 @@ unsigned int ssa_name_nodes_created; void init_ssanames (struct function *fn, int size) { - if (size < 50) - size = 50; - - vec_alloc (SSANAMES (fn), size); + if (!size) + vec_alloc (SSANAMES (fn), 50); + else + vec_safe_reserve (SSANAMES (fn), size, true); /* Version 0 is special, so reserve the first slot in the table. Though currently unused, we may use version 0 in alias analysis as part of diff --git a/gcc/lto-streamer-in.c b/gcc/lto-streamer-in.c index 64037f74ad3..3150c489401 100644 --- a/gcc/lto-streamer-in.c +++ b/gcc/lto-streamer-in.c @@ -1384,7 +1384,7 @@ input_function (tree fn_decl, class data_in *data_in, push_struct_function (fn_decl); fn = DECL_STRUCT_FUNCTION (fn_decl); - init_tree_ssa (fn); + init_tree_ssa (fn, false); /* We input IL in SSA form. */ cfun->gimple_df->in_ssa_p = true;