lto-wrapper.cc: Delete offload_names temp files in case of error [PR106686]

Message ID 0ab0cff7-01b7-a637-b8f4-9e6d6417f324@codesourcery.com
State Committed
Commit e228683b244c6afbe3bdfef7ba607fbda813bd0f
Headers
Series lto-wrapper.cc: Delete offload_names temp files in case of error [PR106686] |

Commit Message

Tobias Burnus Aug. 19, 2022, 4:53 p.m. UTC
  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
  

Comments

Richard Biener Aug. 22, 2022, 7:44 a.m. UTC | #1
On Fri, Aug 19, 2022 at 6:54 PM Tobias Burnus <tobias@codesourcery.com> wrote:
>
> 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?

OK.

> 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
  

Patch

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;
 	}
     }