public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [Patch] lto-wrapper.cc: Delete offload_names temp files in case of error [PR106686]
Date: Fri, 19 Aug 2022 18:53:49 +0200	[thread overview]
Message-ID: <0ab0cff7-01b7-a637-b8f4-9e6d6417f324@codesourcery.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 790 bytes --]

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

[-- Attachment #2: lto-wrap.diff --]
[-- Type: text/x-patch, Size: 5068 bytes --]

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<cl_decoded_option> &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<cl_decoded_option> compiler_opts,
-		       vec<cl_decoded_option> linker_opts)
+		       vec<cl_decoded_option> 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;
 	}
     }
 

             reply	other threads:[~2022-08-19 16:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-19 16:53 Tobias Burnus [this message]
2022-08-22  7:44 ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0ab0cff7-01b7-a637-b8f4-9e6d6417f324@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).