Hi!

On 2020-06-23T17:21:06+0200, Tobias Burnus <tob...@codesourcery.com> wrote:
> If the offloading code is (only) in a library, one can come up
> with the idea to build those parts as shared library – and link
> it to the nonoffloading code.(*)

> (*) Thomas mentioned that this is supposed to work also in more
> complex cases than the one I outlined, although, that is probably
> currently the most common one.

Static linking is another such case that we've seen in the wild -- and
that supposedly does work, mostly.

The "more complex cases" would include dynamically loading/registering
offloading code, and unregistering it again, and such things.  The
interfaces are there (implemented), but testsuite coverage isn't -- so
I'm not going to claim that it actually works.  ;-)

> Currently, this fails as the mkoffload calls the nonoffloading
> compiler without the -fpic/-fPIC flags, even though the compiler
> was originally invoked with those options. – And at some point,
> the linker then complains.
>
> This patch simply adds -fpic/-fPIC to the calls to the nonoffloading
> ("host") compiler, invoked from mkoffload, if they were present before.
>
> For the testcase at hand, this works with both AMDGCN and nvptx
> with the attached patch.
>
> OK for the trunk?

I don't think I can approve, but seems fine if this works (as you've
confirmed) -- it's one incremental step forward!

Or, should this instead be handled in the LTO wrapper (?) options merging
etc. machinery?  I'd have to dig in further.  (Jakub?)

Eventually (not now...), instead of special-casing more and more options
(I somehow doubt that '-fpic', '-fPIC' are the only ones?), shouldn't we
solve this in some more generic way, like re-invoking the host compiler
exactly as invoked before (if that makes sense?), or -- freaky ;-) --
instead of doing the "LTO wrapper thing", pause the host compiler,
generate offload code, inject that code into the host compiler, and
directly proceed there?  (That is, do offload code generation as part of
the host compilation, instead of at link time.)  (Just thinking aloud,
and without and "depth" -- and any such work will be a bigger, separate
task, obviously.)

> PS: I think as mid-/longterm project it would be nice to test this
> in the testsuite, but that's unfortunately a larger task.

ACK.

>  gcc/config/gcn/mkoffload.c   | 15 +++++++++++++--
>  gcc/config/nvptx/mkoffload.c | 15 +++++++++++++--
>  2 files changed, 26 insertions(+), 4 deletions(-)

What about 'gcc/config/i386/intelmic-mkoffload.c'?  I see that one
unconditionally passes '-fPIC' to some things -- is that doing the right
thing for your case, too?


Grüße
 Thomas


> diff --git a/gcc/config/gcn/mkoffload.c b/gcc/config/gcn/mkoffload.c
> index 14f422e..0415d94 100644
> --- a/gcc/config/gcn/mkoffload.c
> +++ b/gcc/config/gcn/mkoffload.c
> @@ -483,7 +483,8 @@ process_obj (FILE *in, FILE *cfile)
>  /* Compile a C file using the host compiler.  */
>
>  static void
> -compile_native (const char *infile, const char *outfile, const char 
> *compiler)
> +compile_native (const char *infile, const char *outfile, const char 
> *compiler,
> +             bool fPIC, bool fpic)
>  {
>    const char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
>    if (!collect_gcc_options)
> @@ -493,6 +494,10 @@ compile_native (const char *infile, const char *outfile, 
> const char *compiler)
>    struct obstack argv_obstack;
>    obstack_init (&argv_obstack);
>    obstack_ptr_grow (&argv_obstack, compiler);
> +  if (fPIC)
> +    obstack_ptr_grow (&argv_obstack, "-fPIC");
> +  if (fpic)
> +    obstack_ptr_grow (&argv_obstack, "-fpic");
>    if (save_temps)
>      obstack_ptr_grow (&argv_obstack, "-save-temps");
>    if (verbose)
> @@ -596,6 +601,8 @@ main (int argc, char **argv)
>    /* Scan the argument vector.  */
>    bool fopenmp = false;
>    bool fopenacc = false;
> +  bool fPIC = false;
> +  bool fpic = false;
>    for (int i = 1; i < argc; i++)
>      {
>  #define STR "-foffload-abi="
> @@ -614,6 +621,10 @@ main (int argc, char **argv)
>       fopenmp = true;
>        else if (strcmp (argv[i], "-fopenacc") == 0)
>       fopenacc = true;
> +      else if (strcmp (argv[i], "-fPIC") == 0)
> +     fPIC = true;
> +      else if (strcmp (argv[i], "-fpic") == 0)
> +     fpic = true;
>        else if (strcmp (argv[i], "-save-temps") == 0)
>       save_temps = true;
>        else if (strcmp (argv[i], "-v") == 0)
> @@ -766,7 +777,7 @@ main (int argc, char **argv)
>    xputenv (concat ("COMPILER_PATH=", cpath, NULL));
>    xputenv (concat ("LIBRARY_PATH=", lpath, NULL));
>
> -  compile_native (gcn_cfile_name, outname, collect_gcc);
> +  compile_native (gcn_cfile_name, outname, collect_gcc, fPIC, fpic);
>
>    return 0;
>  }
> diff --git a/gcc/config/nvptx/mkoffload.c b/gcc/config/nvptx/mkoffload.c
> index efdf9b9..4fecb2b 100644
> --- a/gcc/config/nvptx/mkoffload.c
> +++ b/gcc/config/nvptx/mkoffload.c
> @@ -356,7 +356,8 @@ process (FILE *in, FILE *out)
>  }
>
>  static void
> -compile_native (const char *infile, const char *outfile, const char 
> *compiler)
> +compile_native (const char *infile, const char *outfile, const char 
> *compiler,
> +             bool fPIC, bool fpic)
>  {
>    const char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
>    if (!collect_gcc_options)
> @@ -366,6 +367,10 @@ compile_native (const char *infile, const char *outfile, 
> const char *compiler)
>    struct obstack argv_obstack;
>    obstack_init (&argv_obstack);
>    obstack_ptr_grow (&argv_obstack, compiler);
> +  if (fPIC)
> +    obstack_ptr_grow (&argv_obstack, "-fPIC");
> +  if (fpic)
> +    obstack_ptr_grow (&argv_obstack, "-fpic");
>    if (save_temps)
>      obstack_ptr_grow (&argv_obstack, "-save-temps");
>    if (verbose)
> @@ -471,6 +476,8 @@ main (int argc, char **argv)
>    /* Scan the argument vector.  */
>    bool fopenmp = false;
>    bool fopenacc = false;
> +  bool fPIC = false;
> +  bool fpic = false;
>    for (int i = 1; i < argc; i++)
>      {
>  #define STR "-foffload-abi="
> @@ -489,6 +496,10 @@ main (int argc, char **argv)
>       fopenmp = true;
>        else if (strcmp (argv[i], "-fopenacc") == 0)
>       fopenacc = true;
> +      else if (strcmp (argv[i], "-fPIC") == 0)
> +     fPIC = true;
> +      else if (strcmp (argv[i], "-fpic") == 0)
> +     fpic = true;
>        else if (strcmp (argv[i], "-save-temps") == 0)
>       save_temps = true;
>        else if (strcmp (argv[i], "-v") == 0)
> @@ -587,7 +598,7 @@ main (int argc, char **argv)
>
>    fclose (out);
>
> -  compile_native (ptx_cfile_name, outname, collect_gcc);
> +  compile_native (ptx_cfile_name, outname, collect_gcc, fPIC, fpic);
>
>    return 0;
>  }
-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander 
Walter

Reply via email to