From patchwork Fri Aug 19 16:53:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 56889 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B5DF38582A0 for ; Fri, 19 Aug 2022 16:54:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa4.mentor.iphmx.com (esa4.mentor.iphmx.com [68.232.137.252]) by sourceware.org (Postfix) with ESMTPS id A274F3858D28 for ; Fri, 19 Aug 2022 16:54:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A274F3858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.93,248,1654588800"; d="diff'?scan'208,217";a="81590350" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa4.mentor.iphmx.com with ESMTP; 19 Aug 2022 08:53:55 -0800 IronPort-SDR: 8+DJiWH6LlzNvh2O57tgwEzhkVoNEPzxunSgVdFrPxOHalWJSypLoXTDkyyXu15pzKxsJJpt1G 8T+XzeuxQof8bAXBMRMsYKVvlXPC8hgJdV4DnkNy3OlVQomSLzqJzq6Hh0pPDUGOKfrw/bafHO CPmpMzPKOlBIqsOm0G3BghVNfesAkxfbEOOdqcadbD6RV36LQVdEXBvoORzIwyaPZzPWG7Lxd+ sa9XduXUHLp09VP7aekVZEF8iSx+pszaaU8eqtP1OCXAy4aC5/z0WY9lsXdAd/6u0rWeEipW9L Fus= Message-ID: <0ab0cff7-01b7-a637-b8f4-9e6d6417f324@codesourcery.com> Date: Fri, 19 Aug 2022 18:53:49 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Content-Language: en-US To: gcc-patches From: Tobias Burnus Subject: [Patch] lto-wrapper.cc: Delete offload_names temp files in case of error [PR106686] X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-09.mgc.mentorg.com (139.181.222.9) To svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, HTML_MESSAGE, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" I saw that files such as /tmp/ccxFKYeS.target.o kept accumulating. Not a high number, but still several. I turned out that when compiling an offloading program successfully, they were removed. But when it failed, those remained. (Example: See PR.) This patch fixes this by storing the file name earlier and process them during cleanup, unless they have been taken care of before. (Usual way; as they are printf'ed to stdout, I assume the caller takes care of the tmp files.) OK for mainline? Tobias ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 lto-wrapper.cc: Delete offload_names temp files in case of error [PR106686] Usually, the caller takes care of the .o files for the offload compilers (suffix: ".target.o"). However, if an error occurs during processing (e.g. fatal error by lto1), they were not deleted. gcc/ChangeLog: PR lto/106686 * lto-wrapper.cc (free_array_of_ptrs): Move before tool_cleanup. (tool_cleanup): Unlink offload_names. (compile_offload_image): Take filename argument to set it early. (compile_images_for_offload_targets): Update call; set offload_names to NULL after freeing the array. diff --git a/gcc/lto-wrapper.cc b/gcc/lto-wrapper.cc index 1e8eba1..9a76470 100644 --- a/gcc/lto-wrapper.cc +++ b/gcc/lto-wrapper.cc @@ -89,6 +89,25 @@ static bool xassembler_options_error = false; const char tool_name[] = "lto-wrapper"; +/* Auxiliary function that frees elements of PTR and PTR itself. + N is number of elements to be freed. If PTR is NULL, nothing is freed. + If an element is NULL, subsequent elements are not freed. */ + +static void ** +free_array_of_ptrs (void **ptr, unsigned n) +{ + if (!ptr) + return NULL; + for (unsigned i = 0; i < n; i++) + { + if (!ptr[i]) + break; + free (ptr[i]); + } + free (ptr); + return NULL; +} + /* Delete tempfiles. Called from utils_cleanup. */ void @@ -114,6 +133,12 @@ tool_cleanup (bool) if (output_names[i]) maybe_unlink (output_names[i]); } + if (offload_names) + { + for (i = 0; offload_names[i]; i++) + maybe_unlink (offload_names[i]); + free_array_of_ptrs ((void **) offload_names, i); + } } static void @@ -626,25 +651,6 @@ merge_and_complain (vec &decoded_options, } } -/* Auxiliary function that frees elements of PTR and PTR itself. - N is number of elements to be freed. If PTR is NULL, nothing is freed. - If an element is NULL, subsequent elements are not freed. */ - -static void ** -free_array_of_ptrs (void **ptr, unsigned n) -{ - if (!ptr) - return NULL; - for (unsigned i = 0; i < n; i++) - { - if (!ptr[i]) - break; - free (ptr[i]); - } - free (ptr); - return NULL; -} - /* Parse STR, saving found tokens into PVALUES and return their number. Tokens are assumed to be delimited by ':'. If APPEND is non-null, append it to every token we find. */ @@ -908,13 +914,13 @@ access_check (const char *name, int mode) /* Prepare a target image for offload TARGET, using mkoffload tool from COMPILER_PATH. Return the name of the resultant object file. */ -static char * +static const char * compile_offload_image (const char *target, const char *compiler_path, unsigned in_argc, char *in_argv[], vec compiler_opts, - vec linker_opts) + vec linker_opts, + char **filename) { - char *filename = NULL; char *dumpbase; char **argv; char *suffix @@ -922,6 +928,7 @@ compile_offload_image (const char *target, const char *compiler_path, strcpy (suffix, "/accel/"); strcat (suffix, target); strcat (suffix, "/mkoffload"); + *filename = NULL; char **paths = NULL; unsigned n_paths = parse_env_var (compiler_path, &paths, suffix); @@ -950,9 +957,9 @@ compile_offload_image (const char *target, const char *compiler_path, /* Generate temporary output file name. */ if (save_temps) - filename = concat (dumpbase, ".o", NULL); + *filename = concat (dumpbase, ".o", NULL); else - filename = make_temp_file (".target.o"); + *filename = make_temp_file (".target.o"); struct obstack argv_obstack; obstack_init (&argv_obstack); @@ -962,7 +969,7 @@ compile_offload_image (const char *target, const char *compiler_path, if (verbose) obstack_ptr_grow (&argv_obstack, "-v"); obstack_ptr_grow (&argv_obstack, "-o"); - obstack_ptr_grow (&argv_obstack, filename); + obstack_ptr_grow (&argv_obstack, *filename); /* Append names of input object files. */ for (unsigned i = 0; i < in_argc; i++) @@ -986,7 +993,7 @@ compile_offload_image (const char *target, const char *compiler_path, obstack_free (&argv_obstack, NULL); free_array_of_ptrs ((void **) paths, n_paths); - return filename; + return *filename; } @@ -1016,10 +1023,9 @@ compile_images_for_offload_targets (unsigned in_argc, char *in_argv[], offload_names = XCNEWVEC (char *, num_targets + 1); for (unsigned i = 0; i < num_targets; i++) { - offload_names[next_name_entry] - = compile_offload_image (names[i], compiler_path, in_argc, in_argv, - compiler_opts, linker_opts); - if (!offload_names[next_name_entry]) + if (!compile_offload_image (names[i], compiler_path, in_argc, in_argv, + compiler_opts, linker_opts, + &offload_names[next_name_entry])) #if OFFLOAD_DEFAULTED continue; #else @@ -1778,6 +1784,7 @@ cont1: for (i = 0; offload_names[i]; i++) printf ("%s\n", offload_names[i]); free_array_of_ptrs ((void **) offload_names, i); + offload_names = NULL; } }