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: 632 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:38f:b0:2d5:3c95:9e21 with SMTP id 15csp1049756pxh; Fri, 19 Aug 2022 09:54:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR7pAlEkkVULYN6qizgYWOb050tDx6r09iI1hwsk+eX6EMlIPRG2lMhlVeHIdh+NdKEEl7RY X-Received: by 2002:a05:6402:11c8:b0:440:6513:be2c with SMTP id j8-20020a05640211c800b004406513be2cmr6675969edw.45.1660928070293; Fri, 19 Aug 2022 09:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660928070; cv=none; d=google.com; s=arc-20160816; b=DOvjA+805cDPvQegk0zaCqfl6aNaJt8SAWbh2/qykvjYgiDXOqfGdimO+KXPlXQXEG y4pQXpc3+xzuTNcn7AEukX//vkv2oj9tABzMTWOhONOiiQyx35N3MriF/CCeesTIie3i pKW0Ksdngen0LTd0YM6/7ouxG6ir6x+Sqo1ABHMNu3CCsM5HMcQeNNAaHsY7nMhopkHA eOkbdZvZFRPDavt7H73srgwLojASybXnkmWsUNNrtbDJHuaqMjQm/H1diKE1eNfF4+TD +H3UPLWHw7FIbDmV6mVTq4ET/P2ZpzjGoyjXmrs21B25U0HvmvRhxxid6i/KVC9MPm5i adtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:from:to :content-language:user-agent:mime-version:date:message-id :ironport-sdr:dmarc-filter:delivered-to; bh=4K3p2C6rfuiWCTO8mBNIqoCcl3+iEFB3GtTBL+TSHY4=; b=QQciu9gyqaXGCIya43vWZi7RVNKCuJkCcyA82g1N7kwr7eo1twM7Lsbq1xdYfREhCL MvzekgDUp8wLE6XNRHTOOIVDBdG81hgkF5zQksMproUNNFfar5Eb3vk+hO4stBgJjGJE 0woZrd7UTFHhr7LWEDWfK7syqXhZ2MwdmgOTKWK/OA20LunPDLhnXwrMeZew6hKxpyyn szxwf9sq8aX9beTyqhujBjxcF3dlvtxjlYhRa1vuyjVxdOKc6dide6dVCvGTSjMsj9Jk eEB/ZEjH26VDI2YUInSOdOcpvNfEdXBlglLFhk02EYhH7kTeIFYJHj67b9BzujpdyCNF G/gA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w19-20020a056402269300b0043e57ef5606si3905026edd.9.2022.08.19.09.54.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Aug 2022 09:54:30 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2AD3385800A for ; Fri, 19 Aug 2022 16:54:25 +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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1741609312270286358?= X-GMAIL-MSGID: =?utf-8?q?1741609312270286358?= 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; } }