Incremental LTO linking part 2: lto-plugin support

Jan Hubicka hubicka@ucw.cz
Tue May 8 15:15:00 GMT 2018


Hi,
with lto, incremental linking can be meaninfuly done in three ways:
 1) read LTO file and produce non-LTO .o file
    this is current behaviour of gcc -r or ld -r with plugin
 2) read LTO files and merge section for later LTO
    this is current behaviour of ld -r w/o plugin
 3) read LTO files into the compiler, link them and produce
    incrementaly linked LTO object.

3 makes most sense and I am maing it new default for gcc -r. For testing purposes
and perhaps in order to have tool to turn LTO object into real object, we want
to have 1) available as well.  GCC currently have -flinker-output option that
decides between modes that is decided by linker plugin and can be overwritten
by user (I have forgot to document this).

I am targeting for -flinker-output=rel to be incremental linking into LTO
and adding -flinker-output=nolto-rel for 1).

The main limitation of 2 and 3 is that you can not link LTO and non-LTO
object files theger.  For 2 HJ's binutils patchset has support and I think
it can be extended to handle 3 as well. But with default binutils we want
to warn users.  This patch implements the warning (and prevents linker plugin
to add redundat linker-ouptut options.

Bootstrapped/regtested x86_64-linux with rest of the inclink patchset. OK?

	* lto-plugin.c: (non_claimed_files): New static var.
	(linker_ouput_known): New static var.
	(all_symbols_read_handler): When user specifies linker output do not
	imply it; output warning when nonlto-rel mode is forced.
	(claim_file_header): Record number of nonclaimed files.
	(process_option): Remember if linker output is known

Index: lto-plugin.c
===================================================================
--- lto-plugin.c	(revision 260042)
+++ lto-plugin.c	(working copy)
@@ -27,10 +27,13 @@
    More information at http://gcc.gnu.org/wiki/whopr/driver.
 
    This plugin should be passed the lto-wrapper options and will forward them.
-   It also has 2 options of its own:
+   It also has options at his own:
    -debug: Print the command line used to run lto-wrapper.
    -nop: Instead of running lto-wrapper, pass the original to the plugin. This
-   only works if the input files are hybrid.  */
+   only works if the input files are hybrid. 
+   -linker-output-known: Do not determine linker output
+   -sym-style={none,win32,underscore|uscore}
+   -pass-through  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -159,6 +162,7 @@
 
 static struct plugin_file_info *claimed_files = NULL;
 static unsigned int num_claimed_files = 0;
+static unsigned int non_claimed_files = 0;
 
 /* List of files with offloading.  */
 static struct plugin_offload_file *offload_files;
@@ -185,6 +189,7 @@
 static char *resolution_file = NULL;
 static enum ld_plugin_output_file_type linker_output;
 static int linker_output_set;
+static int linker_output_known;
 
 /* The version of gold being used, or -1 if not gold.  The number is
    MAJOR * 100 + MINOR.  */
@@ -637,7 +642,8 @@
 all_symbols_read_handler (void)
 {
   unsigned i;
-  unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 3;
+  unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 2
+    	   + !linker_output_known;
   char **lto_argv;
   const char *linker_output_str = NULL;
   const char **lto_arg_ptr;
@@ -661,26 +667,37 @@
   for (i = 0; i < lto_wrapper_num_args; i++)
     *lto_arg_ptr++ = lto_wrapper_argv[i];
 
-  assert (linker_output_set);
-  switch (linker_output)
+  if (!linker_output_known)
     {
-    case LDPO_REL:
-      linker_output_str = "-flinker-output=rel";
-      break;
-    case LDPO_DYN:
-      linker_output_str = "-flinker-output=dyn";
-      break;
-    case LDPO_PIE:
-      linker_output_str = "-flinker-output=pie";
-      break;
-    case LDPO_EXEC:
-      linker_output_str = "-flinker-output=exec";
-      break;
-    default:
-      message (LDPL_FATAL, "unsupported linker output %i", linker_output);
-      break;
+      assert (linker_output_set);
+      switch (linker_output)
+	{
+	case LDPO_REL:
+	  if (non_claimed_files)
+	    {
+	      message (LDPL_WARNING, "incremental linking of LTO and non-LTO "
+		       "objects; using -flinker-output=nolto-rel which will "
+		       "bypass whole program optimization");
+	      linker_output_str = "-flinker-output=nolto-rel";
+	    }
+	  else
+	    linker_output_str = "-flinker-output=rel";
+	  break;
+	case LDPO_DYN:
+	  linker_output_str = "-flinker-output=dyn";
+	  break;
+	case LDPO_PIE:
+	  linker_output_str = "-flinker-output=pie";
+	  break;
+	case LDPO_EXEC:
+	  linker_output_str = "-flinker-output=exec";
+	  break;
+	default:
+	  message (LDPL_FATAL, "unsupported linker output %i", linker_output);
+	  break;
+	}
+      *lto_arg_ptr++ = xstrdup (linker_output_str);
     }
-  *lto_arg_ptr++ = xstrdup (linker_output_str);
 
   if (num_offload_files > 0)
     {
@@ -1108,6 +1125,7 @@
   goto cleanup;
 
  err:
+  non_claimed_files++;
   free (lto_file.name);
 
  cleanup:
@@ -1122,6 +1140,8 @@
 static void
 process_option (const char *option)
 {
+  if (strcmp (option, "-linker-output-known") == 0)
+    linker_output_known = 1;
   if (strcmp (option, "-debug") == 0)
     debug = 1;
   else if (strcmp (option, "-nop") == 0)



More information about the Gcc-patches mailing list