diff mbox

Forwarding -foffload=[...] from the driver (compile-time) to libgomp (run-time)

Message ID alpine.DEB.2.10.1508202251220.2630@digraph.polyomino.org.uk
State New
Headers show

Commit Message

Joseph Myers Aug. 20, 2015, 10:52 p.m. UTC
On Tue, 18 Aug 2015, Thomas Schwinge wrote:

> So, back to modifying the driver; here is my current messy WIP patch with
> still a lot of TODOs in it -- but it appears to work at last.  :-)
> 
> Maybe somebody else is able to continue with that task while I'm out of
> office.  This has been developed on top of gomp-4_0-branch r226832.  I'm
> also attaching a tarball of the even more messy indivdual patches,
> foffload.tar.bz2, in case there's anything to salvage in there, or if
> that helps to understand the development options/history.  Earlier
> messages in this thread should give enough context what this is about,
> <http://news.gmane.org/find-root.php?message_id=%3C87egjopgh0.fsf%40kepler.schwinge.homeip.net%3E>.

This is what I've committed to gomp-4_0-branch, with the driver changes 
substantially cleaned up and smaller changes to the other bits of the 
patch.

gcc:
2015-08-20  Thomas Schwinge  <thomas@codesourcery.com>
	    Joseph Myers  <joseph@codesourcery.com>

	* doc/invoke.texi (-ffixed-@var{reg}): Document conflict with
	Fortran options.
	* gcc.c (offload_targets): Update comment.
	(add_omp_infile_spec_func, spec_lang_mask_accept): New.
	(driver_self_specs) [ENABLE_OFFLOADING]: Add spec to use
	%:add-omp-infile().
	(static_spec_functions): Add add-omp-infile.
	(struct switchstr): Add lang_mask field.  Expand comment.
	(struct infile): Add lang_mask field.
	(add_infile, save_switch, do_spec): Add lang_mask argument.
	(driver_unknown_option_callback, driver_wrong_lang_callback)
	(driver_handle_option, process_command, do_self_spec)
	(driver::do_spec_on_infiles): All callers changed.
	(give_switch): Check languages of switch against
	spec_lang_mask_accept.
	(driver::maybe_putenv_OFFLOAD_TARGETS): Do not use intermediate
	targets variable.
	* gcc.h (do_spec): Update prototype.

fortran:
2015-08-20  Joseph Myers  <joseph@codesourcery.com>

	* gfortranspec.c (lang_specific_pre_link): Update call to do_spec.

java:
2015-08-20  Joseph Myers  <joseph@codesourcery.com>

	* jvspec.c (lang_specific_pre_link): Update call to do_spec.

libgomp:
2015-08-20  Thomas Schwinge  <thomas@codesourcery.com>
	    Joseph Myers  <joseph@codesourcery.com>

	* plugin/configfrag.ac (fnmatch.h): Check for header.
	(fnmatch): Check for function.
	(tgt_name): Do not set.
	(offload_targets): Separate with colons not commas.
	* config.h.in, configure: Regenerate.
	* env.c (initialize_env): Make static.  Remove TODO.
	* libgomp.h (gomp_offload_target_available_p): New prototype.
	* libgomp.map (GOACC_2.0.GOMP_4_BRANCH): Add
	GOMP_set_offload_targets.
	(INTERNAL): Remove.
	* libgomp_g.h (GOMP_set_offload_targets): New prototype.
	* oacc-init.c (resolve_device): Do not handle acc_device_host.
	Add comments.
	* target.c: Include <fnmatch.h>.
	(resolve_device): Use host fallback when offload data not
	available.
	(gomp_offload_target_available_p, offload_target_to_plugin_name)
	(gomp_offload_targets, gomp_offload_targets_init)
	(GOMP_set_offload_targets, gomp_plugin_prefix)
	(gomp_plugin_suffix): New.
	(gomp_load_plugin_for_device): Add gomp_debug call.
	(gomp_target_init): Usegomp_offload_targets instead of
	OFFLOAD_TARGETS.  Handle and rewrie colon-separated string.
	* testsuite/lib/libgomp.exp: Expect offload targets to be
	colon-separated.  Adjust matching of offload targets.  Don't
	generate constructor here.
	(libgomp_target_compile): Use GCC_UNDER_TEST.
	(check_effective_target_openacc_nvidia_accel_supported)
	(check_effective_target_openacc_host_selected): Adjust checks of
	offload target names.
	* testsuite/libgomp.c++/c++.exp: Do not set
	HAVE_SET_GXX_UNDER_TEST or GXX_UNDER_TEST.
	* testsuite/libgomp.c/c.exp: Do not append to
	libgomp_compile_options,
	* testsuite/libgomp.fortran/fortran.exp: Do not set
	GFORTRAN_UNDER_TEST or libgomp_compile_options.
	* testsuite/libgomp.graphite/graphite.exp: Do not append to
	libgomp_compile_options.
	* testsuite/libgomp.oacc-c++/c++.exp: Set SAVE_GCC_UNDER_TEST and
	GCC_UNDER_TEST.  Do not set HAVE_SET_GXX_UNDER_TEST and
	GXX_UNDER_TEST.  Do not append to ALWAYS_CFLAGS.  Adjust set of
	offload targets.  Use -foffload=.
	* testsuite/libgomp.oacc-c/c.exp: Do not append to
	libgomp_compile_options or ALWAYS_CFLAGS.  Adjust set of offload
	targets.  Use -foffload=.
	* testsuite/libgomp.oacc-fortran/fortran.exp: Do not set
	GFORTRAN_UNDER_TEST or append to libgomp_compile_options.  Do not
	append to ALWAYS_CFLAGS.  Adjust set of offload targets.  Use
	-foffload=.

Comments

Nathan Sidwell Aug. 21, 2015, 4:06 p.m. UTC | #1
On 08/20/15 18:52, Joseph Myers wrote:
> On Tue, 18 Aug 2015, Thomas Schwinge wrote:

> This is what I've committed to gomp-4_0-branch, with the driver changes
> substantially cleaned up and smaller changes to the other bits of the
> patch.
>
> gcc:
> 2015-08-20  Thomas Schwinge  <thomas@codesourcery.com>
> 	    Joseph Myers  <joseph@codesourcery.com>
>
> 	* doc/invoke.texi (-ffixed-@var{reg}): Document conflict with
> 	Fortran options.
> 	* gcc.c (offload_targets): Update comment.
> 	(add_omp_infile_spec_func, spec_lang_mask_accept): New.
> 	(driver_self_specs) [ENABLE_OFFLOADING]: Add spec to use
> 	%:add-omp-infile().
> 	(static_spec_functions): Add add-omp-infile.
> 	(struct switchstr): Add lang_mask field.  Expand comment.
> 	(struct infile): Add lang_mask field.
> 	(add_infile, save_switch, do_spec): Add lang_mask argument.
> 	(driver_unknown_option_callback, driver_wrong_lang_callback)
> 	(driver_handle_option, process_command, do_self_spec)
> 	(driver::do_spec_on_infiles): All callers changed.
> 	(give_switch): Check languages of switch against
> 	spec_lang_mask_accept.
> 	(driver::maybe_putenv_OFFLOAD_TARGETS): Do not use intermediate
> 	targets variable.
> 	* gcc.h (do_spec): Update prototype.

this appears to cause an ICE in add_omp_infile_spec_func at;
   gcc_assert (offload_targets != NULL);

when you use something like -foffload='-save-temps -v -fdump-rtl-all 
-fdump-tree-all -fno-verbose-asm'

Is that use ill-formed?

nathan
Joseph Myers Aug. 21, 2015, 4:21 p.m. UTC | #2
On Fri, 21 Aug 2015, Nathan Sidwell wrote:

> this appears to cause an ICE in add_omp_infile_spec_func at;
>   gcc_assert (offload_targets != NULL);
> 
> when you use something like -foffload='-save-temps -v -fdump-rtl-all
> -fdump-tree-all -fno-verbose-asm'
> 
> Is that use ill-formed?

I'll need to reverse-engineer the question of what's a well-formed 
-foffload= option (bug 67300 filed yesterday for the lack of any 
documentation of that option).
diff mbox

Patch

Index: libgomp/plugin/configfrag.ac
===================================================================
--- libgomp/plugin/configfrag.ac	(revision 226979)
+++ libgomp/plugin/configfrag.ac	(working copy)
@@ -29,6 +29,8 @@ 
 offload_targets=
 AC_SUBST(offload_targets)
 plugin_support=yes
+AC_CHECK_HEADERS([fnmatch.h], , [plugin_support=no])
+AC_CHECK_FUNCS([fnmatch], , [plugin_support=no])
 AC_CHECK_LIB(dl, dlsym, , [plugin_support=no])
 if test x"$plugin_support" = xyes; then
   AC_DEFINE(PLUGIN_SUPPORT, 1,
@@ -92,10 +94,8 @@  if test x"$enable_offload_targets" != x; then
     tgt=`echo $tgt | sed 's/=.*//'`
     case $tgt in
       *-intelmic-* | *-intelmicemul-*)
-	tgt_name=intelmic
 	;;
       nvptx*)
-        tgt_name=nvptx
 	PLUGIN_NVPTX=$tgt
 	PLUGIN_NVPTX_CPPFLAGS=$CUDA_DRIVER_CPPFLAGS
 	PLUGIN_NVPTX_LDFLAGS=$CUDA_DRIVER_LDFLAGS
@@ -127,9 +127,9 @@  if test x"$enable_offload_targets" != x; then
 	;;
     esac
     if test x"$offload_targets" = x; then
-      offload_targets=$tgt_name
+      offload_targets=$tgt
     else
-      offload_targets=$offload_targets,$tgt_name
+      offload_targets=$offload_targets:$tgt
     fi
     if test x"$tgt_dir" != x; then
       offload_additional_options="$offload_additional_options -B$tgt_dir/libexec/gcc/\$(target_alias)/\$(gcc_version) -B$tgt_dir/bin"
@@ -141,7 +141,7 @@  if test x"$enable_offload_targets" != x; then
   done
 fi
 AC_DEFINE_UNQUOTED(OFFLOAD_TARGETS, "$offload_targets",
-  [Define to hold the list of target names suitable for offloading.])
+  [Define to hold the list of offload targets, separated by colons.])
 AM_CONDITIONAL([PLUGIN_NVPTX], [test $PLUGIN_NVPTX = 1])
 AC_DEFINE_UNQUOTED([PLUGIN_NVPTX], [$PLUGIN_NVPTX],
   [Define to 1 if the NVIDIA plugin is built, 0 if not.])
Index: libgomp/config.h.in
===================================================================
--- libgomp/config.h.in	(revision 226979)
+++ libgomp/config.h.in	(working copy)
@@ -24,6 +24,12 @@ 
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #undef HAVE_DLFCN_H
 
+/* Define to 1 if you have the `fnmatch' function. */
+#undef HAVE_FNMATCH
+
+/* Define to 1 if you have the <fnmatch.h> header file. */
+#undef HAVE_FNMATCH_H
+
 /* Define to 1 if you have the `getloadavg' function. */
 #undef HAVE_GETLOADAVG
 
@@ -95,7 +101,7 @@ 
    */
 #undef LT_OBJDIR
 
-/* Define to hold the list of target names suitable for offloading. */
+/* Define to hold the list of offload targets, separated by colons. */
 #undef OFFLOAD_TARGETS
 
 /* Name of package */
Index: libgomp/configure
===================================================================
--- libgomp/configure	(revision 226979)
+++ libgomp/configure	(working copy)
@@ -15119,6 +15119,33 @@  esac
 offload_targets=
 
 plugin_support=yes
+for ac_header in fnmatch.h
+do :
+  ac_fn_c_check_header_mongrel "$LINENO" "fnmatch.h" "ac_cv_header_fnmatch_h" "$ac_includes_default"
+if test "x$ac_cv_header_fnmatch_h" = x""yes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_FNMATCH_H 1
+_ACEOF
+
+else
+  plugin_support=no
+fi
+
+done
+
+for ac_func in fnmatch
+do :
+  ac_fn_c_check_func "$LINENO" "fnmatch" "ac_cv_func_fnmatch"
+if test "x$ac_cv_func_fnmatch" = x""yes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_FNMATCH 1
+_ACEOF
+
+else
+  plugin_support=no
+fi
+done
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dlsym in -ldl" >&5
 $as_echo_n "checking for dlsym in -ldl... " >&6; }
 if test "${ac_cv_lib_dl_dlsym+set}" = set; then :
@@ -15236,10 +15263,8 @@  if test x"$enable_offload_targets" != x; then
     tgt=`echo $tgt | sed 's/=.*//'`
     case $tgt in
       *-intelmic-* | *-intelmicemul-*)
-	tgt_name=intelmic
 	;;
       nvptx*)
-        tgt_name=nvptx
 	PLUGIN_NVPTX=$tgt
 	PLUGIN_NVPTX_CPPFLAGS=$CUDA_DRIVER_CPPFLAGS
 	PLUGIN_NVPTX_LDFLAGS=$CUDA_DRIVER_LDFLAGS
@@ -15282,9 +15307,9 @@  rm -f core conftest.err conftest.$ac_objext \
 	;;
     esac
     if test x"$offload_targets" = x; then
-      offload_targets=$tgt_name
+      offload_targets=$tgt
     else
-      offload_targets=$offload_targets,$tgt_name
+      offload_targets=$offload_targets:$tgt
     fi
     if test x"$tgt_dir" != x; then
       offload_additional_options="$offload_additional_options -B$tgt_dir/libexec/gcc/\$(target_alias)/\$(gcc_version) -B$tgt_dir/bin"
Index: libgomp/libgomp_g.h
===================================================================
--- libgomp/libgomp_g.h	(revision 226979)
+++ libgomp/libgomp_g.h	(working copy)
@@ -206,6 +206,7 @@  extern void GOMP_single_copy_end (void *);
 
 /* target.c */
 
+extern void GOMP_set_offload_targets (const char *);
 extern void GOMP_target (int, void (*) (void *), const void *,
 			 size_t, void **, size_t *, unsigned char *);
 extern void GOMP_target_data (int, const void *,
Index: libgomp/oacc-init.c
===================================================================
--- libgomp/oacc-init.c	(revision 226979)
+++ libgomp/oacc-init.c	(working copy)
@@ -122,7 +122,9 @@  resolve_device (acc_device_t d, bool fail_is_error
       {
 	if (goacc_device_type)
 	  {
-	    /* Lookup the named device.  */
+	    /* Lookup the device that has been explicitly named, so do not pay
+	       attention to gomp_offload_target_available_p.  (That is, hard
+	       error if not actually available.)  */
 	    while (++d != _ACC_device_hwm)
 	      if (dispatchers[d]
 		  && !strcasecmp (goacc_device_type,
@@ -148,8 +150,14 @@  resolve_device (acc_device_t d, bool fail_is_error
     case acc_device_not_host:
       /* Find the first available device after acc_device_not_host.  */
       while (++d != _ACC_device_hwm)
-	if (dispatchers[d] && dispatchers[d]->get_num_devices_func () > 0)
+	if (dispatchers[d]
+	    && dispatchers[d]->get_num_devices_func () > 0
+	    /* No device has been explicitly named, so pay attention to
+	       gomp_offload_target_available_p, to not decide on an offload
+	       target that we don't have offload data available for.  */
+	    && gomp_offload_target_available_p (dispatchers[d]->type))
 	  goto found;
+      /* No non-host device found.  */
       if (d_arg == acc_device_default)
 	{
 	  d = acc_device_host;
@@ -164,9 +172,6 @@  resolve_device (acc_device_t d, bool fail_is_error
         return NULL;
       break;
 
-    case acc_device_host:
-      break;
-
     default:
       if (d > _ACC_device_hwm)
 	{
@@ -181,7 +186,8 @@  resolve_device (acc_device_t d, bool fail_is_error
 
   assert (d != acc_device_none
 	  && d != acc_device_default
-	  && d != acc_device_not_host);
+	  && d != acc_device_not_host
+	  && d < _ACC_device_hwm);
 
   if (dispatchers[d] == NULL && fail_is_error)
     {
Index: libgomp/libgomp.map
===================================================================
--- libgomp/libgomp.map	(revision 226979)
+++ libgomp/libgomp.map	(working copy)
@@ -339,6 +339,7 @@  GOACC_2.0.GOMP_4_BRANCH {
 	GOACC_get_ganglocal_ptr;
 	GOACC_parallel_keyed;
 	GOACC_register_static;
+	GOMP_set_offload_targets;
 } GOACC_2.0;
 
 GOMP_PLUGIN_1.0 {
@@ -352,9 +353,3 @@  GOMP_PLUGIN_1.0 {
 	GOMP_PLUGIN_async_unmap_vars;
 	GOMP_PLUGIN_acc_thread;
 };
-
-# TODO.  See testsuite/lib/libgomp.exp:libgomp_init.
-INTERNAL {
-  global:
-	initialize_env;
-};
Index: libgomp/target.c
===================================================================
--- libgomp/target.c	(revision 226979)
+++ libgomp/target.c	(working copy)
@@ -41,6 +41,7 @@ 
 
 #ifdef PLUGIN_SUPPORT
 #include <dlfcn.h>
+#include <fnmatch.h>
 #include "plugin-suffix.h"
 #endif
 
@@ -122,17 +123,26 @@  gomp_get_num_devices (void)
 }
 
 static struct gomp_device_descr *
-resolve_device (int device_id)
+resolve_device (int device)
 {
-  if (device_id == GOMP_DEVICE_ICV)
+  int device_id;
+  if (device == GOMP_DEVICE_ICV)
     {
       struct gomp_task_icv *icv = gomp_icv (false);
       device_id = icv->default_device_var;
     }
+  else
+    device_id = device;
 
   if (device_id < 0 || device_id >= gomp_get_num_devices ())
     return NULL;
 
+  /* If the device-var ICV does not actually have offload data available, don't
+     try use it (which will fail), and use host fallback instead.  */
+  if (device == GOMP_DEVICE_ICV
+      && !gomp_offload_target_available_p (devices[device_id].type))
+    return NULL;
+
   return &devices[device_id];
 }
 
@@ -947,6 +957,48 @@  gomp_fini_device (struct gomp_device_descr *device
   devicep->is_initialized = false;
 }
 
+/* Do we have offload data available for the given offload target type?
+   Instead of verifying that *all* offload data is available that could
+   possibly be required, we instead just look for *any*.  If we later find any
+   offload data missing, that's user error.  */
+
+attribute_hidden bool
+gomp_offload_target_available_p (int type)
+{
+  bool available = false;
+
+  /* Has the offload target already been initialized?  */
+  for (int i = 0; !available && i < num_devices; i++)
+    {
+      struct gomp_device_descr *devicep = &devices[i];
+      gomp_mutex_lock (&devicep->lock);
+      if (devicep->type == type && devicep->is_initialized)
+	available = true;
+      gomp_mutex_unlock (&devicep->lock);
+    }
+
+  if (!available)
+    {
+      gomp_mutex_lock (&register_lock);
+
+      /* If there is no offload data available at all, we cannot later fail to
+	 find any of it for a specific offload target.  This is the case where
+	 there are no offloaded code regions in user code, but there can still
+	 be executable directives used, or runtime library calls made.  */
+      if (num_offload_images == 0)
+	available = true;
+
+      /* Can the offload target be initialized?  */
+      for (int i = 0; !available && i < num_offload_images; i++)
+	if (offload_images[i].type == type)
+	  available = true;
+
+      gomp_mutex_unlock (&register_lock);
+    }
+
+  return available;
+}
+
 /* Called when encountering a target directive.  If DEVICE
    is GOMP_DEVICE_ICV, it means use device-var ICV.  If it is
    GOMP_DEVICE_HOST_FALLBACK (or any value
@@ -1116,6 +1168,8 @@  static bool
 gomp_load_plugin_for_device (struct gomp_device_descr *device,
 			     const char *plugin_name)
 {
+  gomp_debug (0, "%s (\"%s\")\n", __FUNCTION__, plugin_name);
+
   const char *err = NULL, *last_missing = NULL;
 
   void *plugin_handle = dlopen (plugin_name, RTLD_LAZY);
@@ -1212,6 +1266,39 @@  gomp_load_plugin_for_device (struct gomp_device_de
   return 0;
 }
 
+/* Helper, to translate from an offload target to the corresponding plugin name.  */
+
+static const char *
+offload_target_to_plugin_name (const char *offload_target)
+{
+  if (fnmatch ("*-intelmic*", offload_target, 0) == 0)
+    return "intelmic";
+  if (fnmatch ("nvptx*", offload_target, 0) == 0)
+    return "nvptx";
+  gomp_fatal ("Unknown offload target: %s", offload_target);
+}
+
+/* List of offload targets, separated by colon.  Defaults to the list
+   determined when configuring libgomp.  */
+static const char *gomp_offload_targets = OFFLOAD_TARGETS;
+static bool gomp_offload_targets_init = false;
+
+/* Override the list of offload targets.  This must be called early, and only
+   once.  */
+
+void
+GOMP_set_offload_targets (const char *offload_targets)
+{
+  gomp_debug (0, "%s (\"%s\")\n", __FUNCTION__, offload_targets);
+
+  /* Make sure this gets called early.  */
+  assert (gomp_is_initialized == PTHREAD_ONCE_INIT);
+  /* Make sure this only gets called once.  */
+  assert (!gomp_offload_targets_init);
+  gomp_offload_targets_init = true;
+  gomp_offload_targets = offload_targets;
+}
+
 /* This function initializes the runtime needed for offloading.
    It parses the list of offload targets and tries to load the plugins for
    these targets.  On return, the variables NUM_DEVICES and NUM_DEVICES_OPENMP
@@ -1219,11 +1306,12 @@  gomp_load_plugin_for_device (struct gomp_device_de
    corresponding devices, first the GOMP_OFFLOAD_CAP_OPENMP_400 ones, follows
    by the others.  */
 
+static const char *gomp_plugin_prefix ="libgomp-plugin-";
+static const char *gomp_plugin_suffix = SONAME_SUFFIX (1);
+
 static void
 gomp_target_init (void)
 {
-  const char *prefix ="libgomp-plugin-";
-  const char *suffix = SONAME_SUFFIX (1);
   const char *cur, *next;
   char *plugin_name;
   int i, new_num_devices;
@@ -1231,48 +1319,60 @@  gomp_target_init (void)
   num_devices = 0;
   devices = NULL;
 
-  cur = OFFLOAD_TARGETS;
+  cur = gomp_offload_targets;
   if (*cur)
     do
       {
-	struct gomp_device_descr current_device;
+	next = strchr (cur, ':');
+	/* If no other offload target following...  */
+	if (next == NULL)
+	  /* ..., point to the terminating NUL character.  */
+	  next = cur + strlen (cur);
 
-	next = strchr (cur, ',');
+	size_t gomp_plugin_prefix_len = strlen (gomp_plugin_prefix);
+	size_t cur_len = next - cur;
+	size_t gomp_plugin_suffix_len = strlen (gomp_plugin_suffix);
+	plugin_name = gomp_malloc (gomp_plugin_prefix_len
+				   + cur_len
+				   + gomp_plugin_suffix_len
+				   + 1);
+	memcpy (plugin_name, gomp_plugin_prefix, gomp_plugin_prefix_len);
+	memcpy (plugin_name + gomp_plugin_prefix_len, cur, cur_len);
+	/* NUL-terminate the string here...  */
+	plugin_name[gomp_plugin_prefix_len + cur_len] = '\0';
+	/* ..., so that we can then use it to translate the offload target to
+	   the plugin name...  */
+	const char *cur_plugin_name
+	  = offload_target_to_plugin_name (plugin_name
+					   + gomp_plugin_prefix_len);
+	size_t cur_plugin_name_len = strlen (cur_plugin_name);
+	assert (cur_plugin_name_len <= cur_len);
+	/* ..., and then rewrite it.  */
+	memcpy (plugin_name + gomp_plugin_prefix_len,
+		cur_plugin_name, cur_plugin_name_len);
+	memcpy (plugin_name + gomp_plugin_prefix_len + cur_plugin_name_len,
+		gomp_plugin_suffix, gomp_plugin_suffix_len);
+	plugin_name[gomp_plugin_prefix_len
+		    + cur_plugin_name_len
+		    + gomp_plugin_suffix_len] = '\0';
 
-	plugin_name = (char *) malloc (1 + (next ? next - cur : strlen (cur))
-				       + strlen (prefix) + strlen (suffix));
-	if (!plugin_name)
-	  {
-	    num_devices = 0;
-	    break;
-	  }
-
-	strcpy (plugin_name, prefix);
-	strncat (plugin_name, cur, next ? next - cur : strlen (cur));
-	strcat (plugin_name, suffix);
-
+	struct gomp_device_descr current_device;
 	if (gomp_load_plugin_for_device (&current_device, plugin_name))
 	  {
 	    new_num_devices = current_device.get_num_devices_func ();
 	    if (new_num_devices >= 1)
 	      {
-		/* Augment DEVICES and NUM_DEVICES.  */
-
-		devices = realloc (devices, (num_devices + new_num_devices)
-				   * sizeof (struct gomp_device_descr));
-		if (!devices)
-		  {
-		    num_devices = 0;
-		    free (plugin_name);
-		    break;
-		  }
-
 		current_device.name = current_device.get_name_func ();
 		/* current_device.capabilities has already been set.  */
 		current_device.type = current_device.get_type_func ();
 		current_device.mem_map.root = NULL;
 		current_device.is_initialized = false;
 		current_device.openacc.data_environ = NULL;
+
+		/* Augment DEVICES and NUM_DEVICES.  */
+		devices = gomp_realloc (devices,
+					((num_devices + new_num_devices)
+					 * sizeof (struct gomp_device_descr)));
 		for (i = 0; i < new_num_devices; i++)
 		  {
 		    current_device.target_id = i;
@@ -1286,18 +1386,12 @@  gomp_target_init (void)
 	free (plugin_name);
 	cur = next + 1;
       }
-    while (next);
+    while (*next);
 
   /* In DEVICES, sort the GOMP_OFFLOAD_CAP_OPENMP_400 ones first, and set
      NUM_DEVICES_OPENMP.  */
   struct gomp_device_descr *devices_s
-    = malloc (num_devices * sizeof (struct gomp_device_descr));
-  if (!devices_s)
-    {
-      num_devices = 0;
-      free (devices);
-      devices = NULL;
-    }
+    = gomp_malloc (num_devices * sizeof (struct gomp_device_descr));
   num_devices_openmp = 0;
   for (i = 0; i < num_devices; i++)
     if (devices[i].capabilities & GOMP_OFFLOAD_CAP_OPENMP_400)
Index: libgomp/env.c
===================================================================
--- libgomp/env.c	(revision 226979)
+++ libgomp/env.c	(working copy)
@@ -1175,11 +1175,7 @@  handle_omp_display_env (unsigned long stacksize, i
 }
 
 
-/* TODO.  See testsuite/lib/libgomp.exp:libgomp_init.  */
-#if 0
-static
-#endif
-void __attribute__((constructor))
+static void __attribute__((constructor))
 initialize_env (void)
 {
   unsigned long thread_limit_var, stacksize;
Index: libgomp/libgomp.h
===================================================================
--- libgomp/libgomp.h	(revision 226979)
+++ libgomp/libgomp.h	(working copy)
@@ -785,6 +785,7 @@  extern void gomp_unmap_vars (struct target_mem_des
 extern void gomp_init_device (struct gomp_device_descr *);
 extern void gomp_fini_device (struct gomp_device_descr *);
 extern void gomp_unload_device (struct gomp_device_descr *);
+extern bool gomp_offload_target_available_p (int);
 
 /* work.c */
 
Index: libgomp/testsuite/libgomp.oacc-c/c.exp
===================================================================
--- libgomp/testsuite/libgomp.oacc-c/c.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.oacc-c/c.exp	(working copy)
@@ -32,8 +32,6 @@  dg-init
 # Turn on OpenACC.
 lappend ALWAYS_CFLAGS "additional_flags=-fopenacc"
 
-lappend libgomp_compile_options "compiler=$GCC_UNDER_TEST"
-
 # Gather a list of all tests.
 set tests [lsort [concat \
 		      [find $srcdir/$subdir *.c] \
@@ -62,17 +60,14 @@  set_ld_library_path_env_vars
 set SAVE_ALWAYS_CFLAGS "$ALWAYS_CFLAGS"
 foreach offload_target_openacc $offload_targets_s_openacc {
     set ALWAYS_CFLAGS "$SAVE_ALWAYS_CFLAGS"
-    set tagopt "-DACC_DEVICE_TYPE_$offload_target_openacc=1"
-    # Set $ACC_DEVICE_TYPE.  See the comments in
-    # ../lib/libgomp.exp:libgomp_init.
-    lappend ALWAYS_CFLAGS "ldflags=constructor-setenv-ACC_DEVICE_TYPE-$offload_target_openacc.o"
 
     # Todo: Determine shared memory or not using run-time test.
-    switch $offload_target_openacc {
-	host {
+    switch -glob $offload_target_openacc {
+	disable {
 	    set acc_mem_shared 1
+	    set tagopt "-DACC_DEVICE_TYPE_host=1"
 	}
-	nvidia {
+	nvptx* {
 	    if { ![check_effective_target_openacc_nvidia_accel_present] } {
 		# Don't bother; execution testing is going to FAIL.
 		untested "$subdir $offload_target_openacc offloading"
@@ -86,12 +81,14 @@  foreach offload_target_openacc $offload_targets_s_
 	    lappend ALWAYS_CFLAGS "additional_flags=-I${srcdir}/libgomp.oacc-c-c++-common"
 
 	    set acc_mem_shared 0
+	    set tagopt "-DACC_DEVICE_TYPE_nvidia=1"
 	}
 	default {
 	    set acc_mem_shared 0
+	    #TODO error
 	}
     }
-    set tagopt "$tagopt -DACC_MEM_SHARED=$acc_mem_shared"
+    set tagopt "$tagopt -DACC_MEM_SHARED=$acc_mem_shared -foffload=$offload_target_openacc"
 
     dg-runtest $tests "$tagopt" $DEFAULT_CFLAGS
     gcc-dg-runtest $ttests "$tagopt" ""
Index: libgomp/testsuite/libgomp.c++/c++.exp
===================================================================
--- libgomp/testsuite/libgomp.c++/c++.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.c++/c++.exp	(working copy)
@@ -4,7 +4,6 @@  load_gcc_lib gcc-dg.exp
 global shlib_ext
 
 set shlib_ext [get_shlib_extension]
-#TODO
 set lang_link_flags "-lstdc++"
 set lang_test_file_found 0
 set lang_library_path "../libstdc++-v3/src/.libs"
@@ -47,13 +46,6 @@  if { $blddir != "" } {
 }
 
 if { $lang_test_file_found } {
-    if ![info exists GXX_UNDER_TEST] then {
-	# TODO.  See libgomp.oacc-c++/c++.exp.
-	set HAVE_SET_GXX_UNDER_TEST ""
-	set GXX_UNDER_TEST "$GCC_UNDER_TEST"
-    }
-    lappend libgomp_compile_options "compiler=$GXX_UNDER_TEST"
-
     # Gather a list of all tests.
     set tests [lsort [find $srcdir/$subdir *.C]]
 
@@ -76,10 +68,5 @@  if { $lang_test_file_found } {
     dg-runtest $tests "" "$libstdcxx_includes $DEFAULT_CFLAGS"
 }
 
-# TODO.  See above.
-if { [info exists HAVE_SET_GXX_UNDER_TEST] } {
-    unset GXX_UNDER_TEST
-}
-
 # All done.
 dg-finish
Index: libgomp/testsuite/libgomp.fortran/fortran.exp
===================================================================
--- libgomp/testsuite/libgomp.fortran/fortran.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.fortran/fortran.exp	(working copy)
@@ -47,11 +47,6 @@  if { $blddir != "" } {
 }
 
 if { $lang_test_file_found } {
-    if ![info exists GFORTRAN_UNDER_TEST] then {
-	set GFORTRAN_UNDER_TEST $GCC_UNDER_TEST
-    }
-    lappend libgomp_compile_options "compiler=$GFORTRAN_UNDER_TEST"
-
     # Gather a list of all tests.
     set tests [lsort [find $srcdir/$subdir *.\[fF\]{,90,95,03,08}]]
 
Index: libgomp/testsuite/libgomp.oacc-c++/c++.exp
===================================================================
--- libgomp/testsuite/libgomp.oacc-c++/c++.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.oacc-c++/c++.exp	(working copy)
@@ -13,7 +13,6 @@  load_gcc_lib gcc-dg.exp
 global shlib_ext
 
 set shlib_ext [get_shlib_extension]
-#TODO
 set lang_link_flags "-lstdc++"
 set lang_test_file_found 0
 set lang_library_path "../libstdc++-v3/src/.libs"
@@ -32,6 +31,11 @@  dg-init
 # Turn on OpenACC.
 lappend ALWAYS_CFLAGS "additional_flags=-fopenacc"
 
+# Switch into C++ mode.  Otherwise, the libgomp.oacc-c-c++-common/*.c
+# files would be compiled as C files.
+set SAVE_GCC_UNDER_TEST "$GCC_UNDER_TEST"
+set GCC_UNDER_TEST "$GCC_UNDER_TEST -x c++"
+
 set blddir [lookfor_file [get_multilibs] libgomp]
 
 
@@ -56,14 +60,6 @@  if { $blddir != "" } {
 }
 
 if { $lang_test_file_found } {
-    if ![info exists GXX_UNDER_TEST] then {
-	# Use GCC_UNDER_TEST, but switch into C++ mode, as otherwise the
-	# libgomp.oacc-c-c++-common/*.c files would be compiled as C files.
-	set HAVE_SET_GXX_UNDER_TEST ""
-	set GXX_UNDER_TEST "$GCC_UNDER_TEST -x c++"
-    }
-    lappend libgomp_compile_options "compiler=$GXX_UNDER_TEST"
-
     # Gather a list of all tests.
     set tests [lsort [concat \
 			  [find $srcdir/$subdir *.C] \
@@ -104,17 +100,14 @@  if { $lang_test_file_found } {
     set SAVE_ALWAYS_CFLAGS "$ALWAYS_CFLAGS"
     foreach offload_target_openacc $offload_targets_s_openacc {
 	set ALWAYS_CFLAGS "$SAVE_ALWAYS_CFLAGS"
-	set tagopt "-DACC_DEVICE_TYPE_$offload_target_openacc=1"
-	# Set $ACC_DEVICE_TYPE.  See the comments in
-	# ../lib/libgomp.exp:libgomp_init.
-	lappend ALWAYS_CFLAGS "ldflags=constructor-setenv-ACC_DEVICE_TYPE-$offload_target_openacc.o"
 
 	# Todo: Determine shared memory or not using run-time test.
-	switch $offload_target_openacc {
-	    host {
+	switch -glob $offload_target_openacc {
+	    disable {
 		set acc_mem_shared 1
+		set tagopt "-DACC_DEVICE_TYPE_host=1"
 	    }
-	    nvidia {
+	    nvptx* {
 		if { ![check_effective_target_openacc_nvidia_accel_present] } {
 		    # Don't bother; execution testing is going to FAIL.
 		    untested "$subdir $offload_target_openacc offloading"
@@ -128,12 +121,14 @@  if { $lang_test_file_found } {
 		lappend ALWAYS_CFLAGS "additional_flags=-I${srcdir}/libgomp.oacc-c-c++-common"
 
 		set acc_mem_shared 0
+		set tagopt "-DACC_DEVICE_TYPE_nvidia=1"
 	    }
 	    default {
 		set acc_mem_shared 0
+		#TODO error
 	    }
 	}
-	set tagopt "$tagopt -DACC_MEM_SHARED=$acc_mem_shared"
+	set tagopt "$tagopt -DACC_MEM_SHARED=$acc_mem_shared -foffload=$offload_target_openacc"
 
 	dg-runtest $tests "$tagopt" "$libstdcxx_includes $DEFAULT_CFLAGS"
 	gcc-dg-runtest $ttests "$tagopt" "$libstdcxx_includes"
@@ -141,9 +136,7 @@  if { $lang_test_file_found } {
 }
 
 # See above.
-if { [info exists HAVE_SET_GXX_UNDER_TEST] } {
-    unset GXX_UNDER_TEST
-}
+set GCC_UNDER_TEST "$SAVE_GCC_UNDER_TEST"
 
 unset TORTURE_OPTIONS
 
Index: libgomp/testsuite/lib/libgomp.exp
===================================================================
--- libgomp/testsuite/lib/libgomp.exp	(revision 226979)
+++ libgomp/testsuite/lib/libgomp.exp	(working copy)
@@ -36,24 +36,21 @@  load_gcc_lib fortran-modules.exp
 load_file libgomp-test-support.exp
 
 # Populate offload_targets_s (offloading targets separated by a space), and
-# offload_targets_s_openacc (the same, but with OpenACC names; OpenACC spells
-# some of them a little differently).
-set offload_targets_s [split $offload_targets ","]
+# offload_targets_s_openacc (those suitable for OpenACC).
+set offload_targets_s [split $offload_targets ":"]
 set offload_targets_s_openacc {}
 foreach offload_target_openacc $offload_targets_s {
-    switch $offload_target_openacc {
-	intelmic {
+    switch -glob $offload_target_openacc {
+	*-intelmic* {
 	    # TODO.  Skip; will all FAIL because of missing
 	    # GOMP_OFFLOAD_CAP_OPENACC_200.
 	    continue
 	}
-	nvptx {
-	    set offload_target_openacc "nvidia"
-	}
     }
     lappend offload_targets_s_openacc "$offload_target_openacc"
 }
-lappend offload_targets_s_openacc "host"
+# Host fallback.
+lappend offload_targets_s_openacc "disable"
 
 set dg-do-what-default run
 
@@ -134,7 +131,7 @@  proc libgomp_init { args } {
     # Add liboffloadmic build directory in LD_LIBRARY_PATH to support
     # non-fallback testing for Intel MIC targets
     global offload_targets
-    if { [string match "*,intelmic,*" ",$offload_targets,"] } {
+    if { [string match "*:*-intelmic*:*" ":$offload_targets:"] } {
 	append always_ld_library_path ":${blddir}/../liboffloadmic/.libs"
 	append always_ld_library_path ":${blddir}/../liboffloadmic/plugin/.libs"
 	# libstdc++ is required by liboffloadmic
@@ -235,56 +232,6 @@  proc libgomp_init { args } {
     if { $offload_additional_options != "" } {
 	lappend ALWAYS_CFLAGS "additional_flags=${offload_additional_options}"
     }
-
-    # TODO.  Evil hack.  DejaGnu doesn't have a mechanism for setting
-    # environment variables on remote boards.  Thus, we have to fake it, using
-    # GCC's constructor attributes to create object files that install the
-    # desired environment variables.
-    set e_list [list \
-		    [list defaults DUMMY=dummy ] \
-		    [list ACC_DEVICE_TYPE-host ACC_DEVICE_TYPE=host ] \
-		    [list ACC_DEVICE_TYPE-nvidia ACC_DEVICE_TYPE=nvidia ] ]
-    foreach e $e_list {
-	set v [lindex $e 0]
-	set s [lindex $e 1]
-	verbose "creating constructor-setenv: $v: $s"
-	set src constructor-setenv-$v.c
-	set obj constructor-setenv-$v.o
-	set f_src [open $src "w"]
-	puts $f_src "static void __attribute__((constructor(1000)))"
-	puts $f_src "init_env(void) {"
-	puts $f_src "  int putenv(char *);"
-	puts $f_src "  putenv(\"$s\");"
-	puts $f_src "}"
-	if { $v == "defaults" } {
-	    # TODO.  We want libgomp to initialize after the putenv calls.
-	    # But: shared libraries' constructors (and thus
-	    # env.c:initialize_env) will be called before the executable's
-	    # (init_env functions created above), so it will already have been
-	    # initialized (and has to be, in case we're not linking in this
-	    # gunk).  Assuming no execution of other libgomp functionality in
-	    # between (which we're not doing during initialization),
-	    # initialize_env's effects are idempotent when calling it again, so
-	    # we'll do that now, after the putenv calls have been executed.
-	    puts $f_src "static void __attribute__((constructor(1001)))"
-	    puts $f_src "init_libgomp(void) {"
-	    # Some test cases specify -fno-openmp, so libgomp isn't linked in.
-	    puts $f_src "  void initialize_env(void) __attribute__((weak));"
-	    puts $f_src "  if (initialize_env)"
-	    puts $f_src "    initialize_env();"
-	    puts $f_src "}"
-	}
-	close $f_src
-	# TODO.  Using whichever compiler is currently configured...  At least
-	# switch it into C mode.
-	set lines [libgomp_target_compile $src $obj object "additional_flags=-xc"]
-	# TODO.  Error checking.
-	file delete $src
-    }
-    # When adding constructor-setenv-*.o files, make sure to cancel any -x flag
-    # that may have been set before.
-    lappend ALWAYS_CFLAGS "ldflags=-x none"
-    lappend ALWAYS_CFLAGS "ldflags=constructor-setenv-defaults.o"
 }
 
 #
@@ -296,6 +243,7 @@  proc libgomp_target_compile { source dest type opt
     global libgomp_compile_options
     global gluefile wrap_flags
     global ALWAYS_CFLAGS
+    global GCC_UNDER_TEST
     global lang_test_file
     global lang_library_path
     global lang_link_flags
@@ -323,6 +271,7 @@  proc libgomp_target_compile { source dest type opt
 
     lappend options "additional_flags=[libio_include_flags]"
     lappend options "timeout=[timeout_value]"
+    lappend options "compiler=$GCC_UNDER_TEST"
 
     set options [concat $libgomp_compile_options $options]
 
@@ -370,7 +319,7 @@  proc check_effective_target_offload_device { } {
 
 proc check_effective_target_openacc_nvidia_accel_supported { } {
     global offload_targets_s_openacc
-    set res [lsearch $offload_targets_s_openacc "nvidia" ]
+    set res [lsearch -glob $offload_targets_s_openacc "nvptx*" ]
     if { $res != -1 } {
 	return 1;
     }
@@ -396,7 +345,7 @@  proc check_effective_target_openacc_nvidia_accel_s
 	return 0;
     }
     global offload_target_openacc
-    if { $offload_target_openacc == "nvidia" } {
+    if { [string match "nvptx*" $offload_target_openacc] } {
         return 1;
     }
     return 0;
@@ -406,7 +355,7 @@  proc check_effective_target_openacc_nvidia_accel_s
 
 proc check_effective_target_openacc_host_selected { } {
     global offload_target_openacc
-    if { $offload_target_openacc == "host" } {
+    if { $offload_target_openacc == "disable" } {
         return 1;
     }
     return 0;
Index: libgomp/testsuite/libgomp.oacc-fortran/fortran.exp
===================================================================
--- libgomp/testsuite/libgomp.oacc-fortran/fortran.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.oacc-fortran/fortran.exp	(working copy)
@@ -49,11 +49,6 @@  if { $blddir != "" } {
 }
 
 if { $lang_test_file_found } {
-    if ![info exists GFORTRAN_UNDER_TEST] then {
-	set GFORTRAN_UNDER_TEST $GCC_UNDER_TEST
-    }
-    lappend libgomp_compile_options "compiler=$GFORTRAN_UNDER_TEST"
-
     # Gather a list of all tests.
     set tests [lsort [find $srcdir/$subdir *.\[fF\]{,90,95,03,08}]]
 
@@ -74,20 +69,14 @@  if { $lang_test_file_found } {
     set_ld_library_path_env_vars
 
     # Test OpenACC with available accelerators.
-    set SAVE_ALWAYS_CFLAGS "$ALWAYS_CFLAGS"
     foreach offload_target_openacc $offload_targets_s_openacc {
-	set ALWAYS_CFLAGS "$SAVE_ALWAYS_CFLAGS"
-	set tagopt "-DACC_DEVICE_TYPE_$offload_target_openacc=1"
-	# Set $ACC_DEVICE_TYPE.  See the comments in
-	# ../lib/libgomp.exp:libgomp_init.
-	lappend ALWAYS_CFLAGS "ldflags=constructor-setenv-ACC_DEVICE_TYPE-$offload_target_openacc.o"
-
 	# Todo: Determine shared memory or not using run-time test.
-	switch $offload_target_openacc {
-	    host {
+	switch -glob $offload_target_openacc {
+	    disable {
 		set acc_mem_shared 1
+		set tagopt "-DACC_DEVICE_TYPE_host=1"
 	    }
-	    nvidia {
+	    nvptx* {
 		if { ![check_effective_target_openacc_nvidia_accel_present] } {
 		    # Don't bother; execution testing is going to FAIL.
 		    untested "$subdir $offload_target_openacc offloading"
@@ -95,12 +84,14 @@  if { $lang_test_file_found } {
 		}
 
 		set acc_mem_shared 0
+		set tagopt "-DACC_DEVICE_TYPE_nvidia=1"
 	    }
 	    default {
 		set acc_mem_shared 0
+		#TODO error
 	    }
 	}
-	set tagopt "$tagopt -DACC_MEM_SHARED=$acc_mem_shared"
+	set tagopt "$tagopt -DACC_MEM_SHARED=$acc_mem_shared -foffload=$offload_target_openacc"
 
 	# For Fortran we're doing torture testing, as Fortran has far more tests
 	# with arrays etc. that testing just -O0 or -O2 is insufficient, that is
Index: libgomp/testsuite/libgomp.c/c.exp
===================================================================
--- libgomp/testsuite/libgomp.c/c.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.c/c.exp	(working copy)
@@ -23,8 +23,6 @@  dg-init
 # Turn on OpenMP.
 lappend ALWAYS_CFLAGS "additional_flags=-fopenmp"
 
-lappend libgomp_compile_options "compiler=$GCC_UNDER_TEST"
-
 # Gather a list of all tests.
 set tests [lsort [find $srcdir/$subdir *.c]]
 
Index: libgomp/testsuite/libgomp.graphite/graphite.exp
===================================================================
--- libgomp/testsuite/libgomp.graphite/graphite.exp	(revision 226979)
+++ libgomp/testsuite/libgomp.graphite/graphite.exp	(working copy)
@@ -48,8 +48,6 @@  dg-init
 # Turn on OpenMP.
 lappend ALWAYS_CFLAGS "additional_flags=-fopenmp"
 
-lappend libgomp_compile_options "compiler=$GCC_UNDER_TEST"
-
 # Gather a list of all tests.
 set tests [lsort [find $srcdir/$subdir *.c]]
 
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 226979)
+++ gcc/doc/invoke.texi	(working copy)
@@ -24036,6 +24036,10 @@  macro in the machine description macro file.
 This flag does not have a negative form, because it specifies a
 three-way choice.
 
+Note that this flag may conflict with the @option{-ffixed-form} as
+well as @option{-ffixed-line-length-none} and
+@option{-ffixed-line-length-<n>} options of the Fortran front end.
+
 @item -fcall-used-@var{reg}
 @opindex fcall-used
 Treat the register named @var{reg} as an allocable register that is
Index: gcc/gcc.c
===================================================================
--- gcc/gcc.c	(revision 226979)
+++ gcc/gcc.c	(working copy)
@@ -158,7 +158,7 @@  static const char *const spec_version = DEFAULT_TA
 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
 static const char *spec_host_machine = DEFAULT_REAL_TARGET_MACHINE;
 
-/* List of offload targets.  */
+/* List of offload targets.  Empty string for -foffload=disable.  */
 
 static char *offload_targets = NULL;
 
@@ -275,6 +275,8 @@  static const char *compare_debug_auxbase_opt_spec_
 static const char *pass_through_libs_spec_func (int, const char **);
 static const char *replace_extension_spec_func (int, const char **);
 static const char *greater_than_spec_func (int, const char **);
+static const char *add_omp_infile_spec_func (int, const char **);
+
 static char *convert_white_space (char *);
 
 /* The Specs Language
@@ -1061,6 +1063,14 @@  static const char *const multilib_defaults_raw[] =
 
 static const char *const driver_self_specs[] = {
   "%{fdump-final-insns:-fdump-final-insns=.} %<fdump-final-insns",
+#ifdef ENABLE_OFFLOADING
+  /* If the user didn't specify any, default to all configured offload
+     targets.  */
+  "%{!foffload=*:-foffload=" OFFLOAD_TARGETS "}",
+  /* If linking against libgomp, add a setup file.  */
+  "%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*} 1):" \
+  "%:add-omp-infile()}",
+#endif /* ENABLE_OFFLOADING */
   DRIVER_SELF_SPECS, CONFIGURE_SPECS, GOMP_SELF_SPECS, GTM_SELF_SPECS,
   CILK_SELF_SPECS
 };
@@ -1491,6 +1501,7 @@  static const struct spec_function static_spec_func
   { "pass-through-libs",	pass_through_libs_spec_func },
   { "replace-extension",	replace_extension_spec_func },
   { "gt",			greater_than_spec_func },
+  { "add-omp-infile",		add_omp_infile_spec_func },
 #ifdef EXTRA_SPEC_FUNCTIONS
   EXTRA_SPEC_FUNCTIONS
 #endif
@@ -3073,10 +3084,16 @@  execute (void)
    SWITCH_LIVE to indicate this switch is true in a conditional spec.
    SWITCH_FALSE to indicate this switch is overridden by a later switch.
    SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
-   SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored
+   SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored.
+   SWITCH_KEEP_FOR_GCC to indicate that this switch, otherwise ignored,
+   should be included in COLLECT_GCC_OPTIONS.
    in all do_spec calls afterwards.  Used for %<S from self specs.
-   The `validated' field is nonzero if any spec has looked at this switch;
-   if it remains zero at the end of the run, it must be meaningless.  */
+   The `known' field describes whether this is an internal switch.
+   The `validated' field describes whether any spec has looked at this switch;
+   if it remains false at the end of the run, the switch must be meaningless.
+   The `ordering' field is used to temporarily mark switches that have to be
+   kept in a specific order.
+   The `lang_mask' field stores the flags associated with this option.  */
 
 #define SWITCH_LIVE    			(1 << 0)
 #define SWITCH_FALSE   			(1 << 1)
@@ -3092,6 +3109,7 @@  struct switchstr
   bool known;
   bool validated;
   bool ordering;
+  unsigned int lang_mask;
 };
 
 static struct switchstr *switches;
@@ -3100,6 +3118,10 @@  static int n_switches;
 
 static int n_switches_alloc;
 
+/* If nonzero, do not pass through switches for languages not matching
+   this mask.  */
+static unsigned int spec_lang_mask_accept;
+
 /* Set to zero if -fcompare-debug is disabled, positive if it's
    enabled and we're running the first compilation, negative if it's
    enabled and we're running the second compilation.  For most of the
@@ -3137,6 +3159,7 @@  struct infile
   const char *name;
   const char *language;
   struct compiler *incompiler;
+  unsigned int lang_mask;
   bool compiled;
   bool preprocessed;
 };
@@ -3337,15 +3360,16 @@  alloc_infile (void)
     }
 }
 
-/* Store an input file with the given NAME and LANGUAGE in
+/* Store an input file with the given NAME and LANGUAGE and LANG_MASK in
    infiles.  */
 
 static void
-add_infile (const char *name, const char *language)
+add_infile (const char *name, const char *language, unsigned int lang_mask)
 {
   alloc_infile ();
   infiles[n_infiles].name = name;
-  infiles[n_infiles++].language = language;
+  infiles[n_infiles].language = language;
+  infiles[n_infiles++].lang_mask = lang_mask;
 }
 
 /* Allocate space for a switch in switches.  */
@@ -3366,11 +3390,12 @@  alloc_switch (void)
 }
 
 /* Save an option OPT with N_ARGS arguments in array ARGS, marking it
-   as validated if VALIDATED and KNOWN if it is an internal switch.  */
+   as validated if VALIDATED and KNOWN if it is an internal switch.
+   LANG_MASK is the flags associated with this option.  */
 
 static void
 save_switch (const char *opt, size_t n_args, const char *const *args,
-	     bool validated, bool known)
+	     bool validated, bool known, unsigned int lang_mask)
 {
   alloc_switch ();
   switches[n_switches].part1 = opt + 1;
@@ -3387,6 +3412,7 @@  save_switch (const char *opt, size_t n_args, const
   switches[n_switches].validated = validated;
   switches[n_switches].known = known;
   switches[n_switches].ordering = 0;
+  switches[n_switches].lang_mask = lang_mask;
   n_switches++;
 }
 
@@ -3404,7 +3430,8 @@  driver_unknown_option_callback (const struct cl_de
 	 diagnosed only if there are warnings.  */
       save_switch (decoded->canonical_option[0],
 		   decoded->canonical_option_num_elements - 1,
-		   &decoded->canonical_option[1], false, true);
+		   &decoded->canonical_option[1], false, true,
+		   cl_options[decoded->opt_index].flags);
       return false;
     }
   if (decoded->opt_index == OPT_SPECIAL_unknown)
@@ -3412,7 +3439,8 @@  driver_unknown_option_callback (const struct cl_de
       /* Give it a chance to define it a spec file.  */
       save_switch (decoded->canonical_option[0],
 		   decoded->canonical_option_num_elements - 1,
-		   &decoded->canonical_option[1], false, false);
+		   &decoded->canonical_option[1], false, false,
+		   cl_options[decoded->opt_index].flags);
       return false;
     }
   else
@@ -3439,7 +3467,8 @@  driver_wrong_lang_callback (const struct cl_decode
   else
     save_switch (decoded->canonical_option[0],
 		 decoded->canonical_option_num_elements - 1,
-		 &decoded->canonical_option[1], false, true);
+		 &decoded->canonical_option[1], false, true,
+		 option->flags);
 }
 
 static const char *spec_lang = 0;
@@ -3689,7 +3718,8 @@  driver_handle_option (struct gcc_options *opts,
 	compare_debug_opt = NULL;
       else
 	compare_debug_opt = arg;
-      save_switch (compare_debug_replacement_opt, 0, NULL, validated, true);
+      save_switch (compare_debug_replacement_opt, 0, NULL, validated, true,
+		   cl_options[opt_index].flags);
       return true;
 
     case OPT_fdiagnostics_color_:
@@ -3744,17 +3774,17 @@  driver_handle_option (struct gcc_options *opts,
 	for (j = 0; arg[j]; j++)
 	  if (arg[j] == ',')
 	    {
-	      add_infile (save_string (arg + prev, j - prev), "*");
+	      add_infile (save_string (arg + prev, j - prev), "*", 0);
 	      prev = j + 1;
 	    }
 	/* Record the part after the last comma.  */
-	add_infile (arg + prev, "*");
+	add_infile (arg + prev, "*", 0);
       }
       do_save = false;
       break;
 
     case OPT_Xlinker:
-      add_infile (arg, "*");
+      add_infile (arg, "*", 0);
       do_save = false;
       break;
 
@@ -3776,19 +3806,21 @@  driver_handle_option (struct gcc_options *opts,
     case OPT_l:
       /* POSIX allows separation of -l and the lib arg; canonicalize
 	 by concatenating -l with its arg */
-      add_infile (concat ("-l", arg, NULL), "*");
+      add_infile (concat ("-l", arg, NULL), "*", 0);
       do_save = false;
       break;
 
     case OPT_L:
       /* Similarly, canonicalize -L for linkers that may not accept
 	 separate arguments.  */
-      save_switch (concat ("-L", arg, NULL), 0, NULL, validated, true);
+      save_switch (concat ("-L", arg, NULL), 0, NULL, validated, true,
+		   cl_options[opt_index].flags);
       return true;
 
     case OPT_F:
       /* Likewise -F.  */
-      save_switch (concat ("-F", arg, NULL), 0, NULL, validated, true);
+      save_switch (concat ("-F", arg, NULL), 0, NULL, validated, true,
+		   cl_options[opt_index].flags);
       return true;
 
     case OPT_save_temps:
@@ -3911,7 +3943,8 @@  driver_handle_option (struct gcc_options *opts,
       save_temps_prefix = xstrdup (arg);
       /* On some systems, ld cannot handle "-o" without a space.  So
 	 split the option from its argument.  */
-      save_switch ("-o", 1, &arg, validated, true);
+      save_switch ("-o", 1, &arg, validated, true,
+		   cl_options[opt_index].flags);
       return true;
 
 #ifdef ENABLE_DEFAULT_PIE
@@ -3945,9 +3978,12 @@  driver_handle_option (struct gcc_options *opts,
     }
 
   if (do_save)
+    {
     save_switch (decoded->canonical_option[0],
 		 decoded->canonical_option_num_elements - 1,
-		 &decoded->canonical_option[1], validated, true);
+		 &decoded->canonical_option[1], validated, true,
+		 cl_options[opt_index].flags);
+    }
   return true;
 }
 
@@ -4244,7 +4280,7 @@  process_command (unsigned int decoded_options_coun
           if (strcmp (fname, "-") != 0 && access (fname, F_OK) < 0)
 	    perror_with_name (fname);
           else
-	    add_infile (arg, spec_lang);
+	    add_infile (arg, spec_lang, 0);
 
           free (fname);
 	  continue;
@@ -4386,7 +4422,8 @@  process_command (unsigned int decoded_options_coun
   if (compare_debug == 2 || compare_debug == 3)
     {
       const char *opt = concat ("-fcompare-debug=", compare_debug_opt, NULL);
-      save_switch (opt, 0, NULL, false, true);
+      save_switch (opt, 0, NULL, false, true,
+		   cl_options[OPT_fcompare_debug_].flags);
       compare_debug = 1;
     }
 
@@ -4397,7 +4434,7 @@  process_command (unsigned int decoded_options_coun
 
       /* Create a dummy input file, so that we can pass
 	 the help option on to the various sub-processes.  */
-      add_infile ("help-dummy", "c");
+      add_infile ("help-dummy", "c", 0);
     }
 
   alloc_switch ();
@@ -4615,13 +4652,15 @@  insert_wrapper (const char *wrapper)
 }
 
 /* Process the spec SPEC and run the commands specified therein.
+   If LANG_MASK is nonzero, switches for other languages are discarded.
    Returns 0 if the spec is successfully processed; -1 if failed.  */
 
 int
-do_spec (const char *spec)
+do_spec (const char *spec, unsigned int lang_mask)
 {
   int value;
 
+  spec_lang_mask_accept = lang_mask;
   value = do_spec_2 (spec);
 
   /* Force out any unfinished command.
@@ -4779,7 +4818,8 @@  do_self_spec (const char *spec)
 	      save_switch (decoded_options[j].canonical_option[0],
 			   (decoded_options[j].canonical_option_num_elements
 			    - 1),
-			   &decoded_options[j].canonical_option[1], false, true);
+			   &decoded_options[j].canonical_option[1], false, true,
+			   cl_options[decoded_options[j].opt_index].flags);
 	      break;
 
 	    default:
@@ -6368,6 +6408,14 @@  check_live_switch (int switchnum, int prefix_lengt
 static void
 give_switch (int switchnum, int omit_first_word)
 {
+  int lang_mask = switches[switchnum].lang_mask & ((1U << cl_lang_count) - 1);
+  unsigned int lang_mask_accept = (1U << cl_lang_count) - 1;
+  if (spec_lang_mask_accept != 0)
+    lang_mask_accept = spec_lang_mask_accept;
+  /* Drop switches specific to a language not in the given mask.  */
+  if (lang_mask != 0 && !(lang_mask & lang_mask_accept))
+    return;
+
   if ((switches[switchnum].live_cond & SWITCH_IGNORE) != 0)
     return;
 
@@ -7448,22 +7496,14 @@  driver::maybe_putenv_COLLECT_LTO_WRAPPER () const
 void
 driver::maybe_putenv_OFFLOAD_TARGETS () const
 {
-  const char *targets = offload_targets;
-
-  /* If no targets specified by -foffload, use all available targets.  */
-  if (!targets)
-    targets = OFFLOAD_TARGETS;
-
-  if (strlen (targets) > 0)
+  if (offload_targets && offload_targets[0] != '\0')
     {
       obstack_grow (&collect_obstack, "OFFLOAD_TARGET_NAMES=",
 		    sizeof ("OFFLOAD_TARGET_NAMES=") - 1);
-      obstack_grow (&collect_obstack, targets,
-		    strlen (targets) + 1);
+      obstack_grow (&collect_obstack, offload_targets,
+		    strlen (offload_targets) + 1);
       xputenv (XOBFINISH (&collect_obstack, char *));
     }
-
-  free (offload_targets);
 }
 
 /* Reject switches that no pass was interested in.  */
@@ -7767,7 +7807,8 @@  driver::do_spec_on_infiles () const
 		  debug_check_temp_file[1] = NULL;
 		}
 
-	      value = do_spec (input_file_compiler->spec);
+	      value = do_spec (input_file_compiler->spec,
+			       infiles[i].lang_mask);
 	      infiles[i].compiled = true;
 	      if (value < 0)
 		this_file_error = 1;
@@ -7781,7 +7822,8 @@  driver::do_spec_on_infiles () const
 		  n_switches_alloc = n_switches_alloc_debug_check[1];
 		  switches = switches_debug_check[1];
 
-		  value = do_spec (input_file_compiler->spec);
+		  value = do_spec (input_file_compiler->spec,
+				   infiles[i].lang_mask);
 
 		  compare_debug = -compare_debug;
 		  n_switches = n_switches_debug_check[0];
@@ -7936,7 +7978,7 @@  driver::maybe_run_linker (const char *argv0) const
 		    " to the linker.\n\n"));
 	  fflush (stdout);
 	}
-      int value = do_spec (link_command_spec);
+      int value = do_spec (link_command_spec, 0);
       if (value < 0)
 	errorcount = 1;
       linker_was_run = (tmp != execution_count);
@@ -9507,6 +9549,50 @@  greater_than_spec_func (int argc, const char **arg
   return NULL;
 }
 
+/* If applicable, generate a C source file containing a constructor call to
+   GOMP_set_offload_targets, to inform libgomp which offload targets have
+   actually been requested (-foffload=[...]), and adds that as an infile.  */
+
+static const char *
+add_omp_infile_spec_func (int argc, const char **)
+{
+  gcc_assert (argc == 0);
+  gcc_assert (offload_targets != NULL);
+
+  /* Nothing to do if we're not actually linking.  */
+  if (have_c)
+    return NULL;
+
+  int err;
+  const char *tmp_filename;
+  tmp_filename = make_temp_file (".c");
+  record_temp_file (tmp_filename, !save_temps_flag, 0);
+  FILE *f = fopen (tmp_filename, "w");
+  if (f == NULL)
+    fatal_error (input_location,
+		 "could not open temporary file %s", tmp_filename);
+  /* As libgomp uses constructors internally, and this code is only added when
+     linking against libgomp, it is fine to use a constructor here.  */
+  err = fprintf (f,
+		 "extern void GOMP_set_offload_targets (const char *);\n"
+		 "static __attribute__ ((constructor)) void\n"
+		 "init (void)\n"
+		 "{\n"
+		 "  GOMP_set_offload_targets (\"%s\");\n"
+		 "}\n",
+		 offload_targets);
+  if (err < 0)
+    fatal_error (input_location,
+		 "could not write to temporary file %s", tmp_filename);
+  err = fclose (f);
+  if (err == EOF)
+    fatal_error (input_location,
+		 "could not close temporary file %s", tmp_filename);
+
+  add_infile (tmp_filename, "cpp-output", CL_C);
+  return NULL;
+}
+
 /* Insert backslash before spaces in ORIG (usually a file path), to 
    avoid being broken by spec parser.
 
Index: gcc/gcc.h
===================================================================
--- gcc/gcc.h	(revision 226979)
+++ gcc/gcc.h	(working copy)
@@ -65,7 +65,7 @@  struct spec_function
 };
 
 /* These are exported by gcc.c.  */
-extern int do_spec (const char *);
+extern int do_spec (const char *, unsigned int);
 extern void record_temp_file (const char *, int, int);
 extern void pfatal_with_name (const char *) ATTRIBUTE_NORETURN;
 extern void set_input (const char *);
Index: gcc/fortran/gfortranspec.c
===================================================================
--- gcc/fortran/gfortranspec.c	(revision 226979)
+++ gcc/fortran/gfortranspec.c	(working copy)
@@ -441,7 +441,7 @@  int
 lang_specific_pre_link (void)
 {
   if (library)
-    do_spec ("%:include(libgfortran.spec)");
+    do_spec ("%:include(libgfortran.spec)", 0);
 
   return 0;
 }
Index: gcc/java/jvspec.c
===================================================================
--- gcc/java/jvspec.c	(revision 226979)
+++ gcc/java/jvspec.c	(working copy)
@@ -629,7 +629,7 @@  lang_specific_pre_link (void)
      class name.  Append dummy `.c' that can be stripped by set_input so %b
      is correct.  */ 
   set_input (concat (main_class_name, "main.c", NULL));
-  err = do_spec (jvgenmain_spec);
+  err = do_spec (jvgenmain_spec, 0);
   if (err == 0)
     {
       /* Shift the outfiles array so the generated main comes first.