<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi Jonas,</p>
    <p>thanks for the hint, with "--relocation-model=pic" added to opt
      and llc, I can run the bundled object file through clang without
      any errors.</p>
    <p>However, the linking in clang does not seem to work right, at
      runtime the libomptarget-plugin (rtl.cpp) throws an error because
      it is unable to handle the ELF:</p>
    <p>
      <blockquote type="cite">Libomptarget --> Loading RTLs...<br>
        Libomptarget --> Loading library
        'libomptarget.rtl.ppc64.so'...<br>
        Libomptarget --> Unable to load library
        'libomptarget.rtl.ppc64.so': libomptarget.rtl.ppc64.so: cannot
        open shared object file: No such file or directory!<br>
        Libomptarget --> Loading library
        'libomptarget.rtl.x86_64.so'...<br>
        Libomptarget --> Successfully loaded library
        'libomptarget.rtl.x86_64.so'!<br>
        Libomptarget --> Registering RTL libomptarget.rtl.x86_64.so
        supporting 4 devices!<br>
        Libomptarget --> Loading library
        'libomptarget.rtl.cuda.so'...<br>
        Libomptarget --> Unable to load library
        'libomptarget.rtl.cuda.so': libomptarget.rtl.cuda.so: cannot
        open shared object file: No such file or directory!<br>
        Libomptarget --> Loading library
        'libomptarget.rtl.aarch64.so'...<br>
        Libomptarget --> Unable to load library
        'libomptarget.rtl.aarch64.so': libomptarget.rtl.aarch64.so:
        cannot open shared object file: No such file or directory!<br>
        Libomptarget --> RTLs loaded!<br>
        Target x86_64 RTL --> Unable to get ELF handle: invalid
        operand!<br>
        Libomptarget --> Image 0x0000000000000000 is NOT compatible
        with RTL libomptarget.rtl.x86_64.so!<br>
        Libomptarget --> No RTL found for image 0x0000000000000000!<br>
        Libomptarget --> Done registering entries!<br>
        Libomptarget --> Call to omp_get_num_devices returning 0<br>
        Libomptarget --> Default TARGET OFFLOAD policy is now
        disabled (no devices were found)<br>
      </blockquote>
      Do I need to specify additional flags to clang?</p>
    <p>Thanks,</p>
    <p>Lukas<br>
    </p>
    <pre class="moz-signature" cols="72">Lukas Sommer, M.Sc.
TU Darmstadt
Embedded Systems and Applications Group (ESA)
Hochschulstr. 10, 64289 Darmstadt, Germany
Phone: +49 6151 1622429
<a class="moz-txt-link-abbreviated" href="http://www.esa.informatik.tu-darmstadt.de">www.esa.informatik.tu-darmstadt.de</a></pre>
    <div class="moz-cite-prefix">On 11/4/19 5:43 PM, Jonas Hahnfeld
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:f5cefdcc925868b5eac76f8d641bd1ecfa572de2.camel@hahnjo.de">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <div>Am Montag, den 04.11.2019, 17:28 +0100 schrieb Lukas Sommer:</div>
      <blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px
        #729fcf solid;padding-left:1ex">
        <p>Hi Jonas,</p>
        <p>thanks for your help!</p>
        <p>I tried the suggested command, unfortunately it still fails:</p>
        <p> </p>
        <blockquote type="cite" style="margin:0 0 0 .8ex;
          border-left:2px #729fcf solid;padding-left:1ex">clang -fopenmp
          -fopenmp-targets=x86_64-unknown-linux-gnu app.o</blockquote>
        yields:
        <p> </p>
        <blockquote type="cite" style="margin:0 0 0 .8ex;
          border-left:2px #729fcf solid;padding-left:1ex">/usr/bin/ld:
          /tmp/app-c48732.o: relocation R_X86_64_32 against `.data' can
          not be used when making a shared object; recompile with -fPIC<br>
          /usr/bin/ld: final link failed: nonrepresentable section on
          output<br>
          /usr/bin/ld: cannot find /tmp/app-23d932.out inside /<br>
          /usr/bin/ld: cannot find /tmp/app-23d932.out inside /<br>
          clang-9: error: linker command failed with exit code 1 (use -v
          to see invocation)<br>
          clang-9: error: linker command failed with exit code 1 (use -v
          to see invocation)</blockquote>
        I also tried to generate and bundle assembly (.asm) and run that
        through clang (with openmp-flags as above), but that gave the
        same error.<br>
      </blockquote>
      <div><br>
      </div>
      <div>Hmm, the target image is built as a shared library. To make
        this work, Clang adds -shared -fPIC in
        Generic_GCC::TranslateArgs.</div>
      <div>You're replacing this step with a manual invocation of `opt',
        so we need to pass some extra flags. I'm currently looking into
        what's needed, opt --relocation-model=pic might be a good start
        but I'm not sure that's all.</div>
      <div><br>
      </div>
      <div>Jonas</div>
      <div><br>
      </div>
      <blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px
        #729fcf solid;padding-left:1ex">
        <p>Lukas<br>
        </p>
        <pre>Lukas Sommer, M.Sc.</pre>
        <pre>TU Darmstadt</pre>
        <pre>Embedded Systems and Applications Group (ESA)</pre>
        <pre>Hochschulstr. 10, 64289 Darmstadt, Germany</pre>
        <pre>Phone: +49 6151 1622429</pre>
        <a class="moz-txt-link-abbreviated"
          href="http://www.esa.informatik.tu-darmstadt.de"
          moz-do-not-send="true">
          <pre>www.esa.informatik.tu-darmstadt.de</pre>
        </a>
        <div class="moz-cite-prefix">On 11/4/19 5:01 PM, Jonas Hahnfeld
          wrote:<br>
        </div>
        <blockquote type="cite"
          cite="mid:79be0a771d7cf25e2a3444808c5ebcef59856ded.camel@hahnjo.de"
          style="margin:0 0 0 .8ex; border-left:2px #729fcf
          solid;padding-left:1ex">
          <div>Hi Lukas,</div>
          <div><br>
          </div>
          <div>Am Montag, den 28.10.2019, 13:42 +0100 schrieb Lukas
            Sommer via Openmp-dev:</div>
          <blockquote type="cite" style="margin:0 0 0 .8ex;
            border-left:2px #729fcf solid;padding-left:1ex">
            <pre>Hello,</pre>
            <pre>I'm trying to use the clang-offload-bundler to merge the results of the</pre>
            <pre>compilation for device and host for an application using OpenMP target</pre>
            <pre>pragmas.</pre>
            <pre>My goal is to run a custom pass on the device code and then bundle an</pre>
            <pre>application from the modified device code and the host code. For testing</pre>
            <pre>purposes, I use a very simple vector addition with target offloading</pre>
            <pre>pragmas and the "offloading" to x86_64 (LLVM/Clang release 9.0).</pre>
            <pre>I use clang to generate LLVM IR from the application code. Then I use</pre>
            <pre>the clang-offload-bundler to unbundle the LLVM IR for device and host. I</pre>
            <pre>run opt and llc separately on the LLVM IR for host and device.</pre>
            <pre>To this end, on my application code (code.c), I run the following steps:</pre>
            <blockquote type="cite" style="margin:0 0 0 .8ex;
              border-left:2px #729fcf solid;padding-left:1ex">
              <pre>clang -fopenmp -fopenmp-targets=x86_64-unknown-linux-gnu -S -emit-llvm</pre>
              <pre>-O3 -o bundled.ll code.c</pre>
              <pre>clang-offload-bundler --inputs=bundled.ll</pre>
              <pre>--outputs=host.in.ll,device.in.ll</pre>
              <pre>--targets=host-x86_64-unknown-linux-gnu,openmp-x86_64-unknown-linux-gnu</pre>
              <pre>--type=ll --unbundle</pre>
              <pre>opt -o host.opt.ll -O3 host.in.ll</pre>
              <pre>llc -o host.o -filetype=obj host.opt.ll</pre>
              <pre>opt -o device.opt.ll -O3 device.in.ll (custom pass should be added</pre>
              <pre>here later on)</pre>
              <pre>llc -o device.o -filetype=obj device.opt.ll</pre>
            </blockquote>
            <pre>Afterwards, I tried to bundle the object files for host & device:</pre>
            <blockquote type="cite" style="margin:0 0 0 .8ex;
              border-left:2px #729fcf solid;padding-left:1ex">
              <pre>clang-offload-bundler --inputs=host.o,device.o --outputs=app.o</pre>
              <pre>--targets=host-x86_64-unknown-linux-gnu,openmp-x86_64-unknown-linux-gnu</pre>
              <pre>--type=o</pre>
            </blockquote>
            <pre>This command completes without any warning or error. </pre>
            <pre>I tried to invoke gcc on the output to generate the executable from the</pre>
            <pre>object file (app.o):</pre>
            <blockquote type="cite" style="margin:0 0 0 .8ex;
              border-left:2px #729fcf solid;padding-left:1ex">
              <pre>gcc app.o -lomp -lomptarget</pre>
            </blockquote>
            <pre>However, I get the following error:</pre>
            <blockquote type="cite" style="margin:0 0 0 .8ex;
              border-left:2px #729fcf solid;padding-left:1ex">
              <pre>/usr/bin/ld:</pre>
              <pre>app.o:(.rodata..omp_offloading.device_images[.omp_offloading.descriptor_reg.x86_64-unknown-linux-gnu]+0x10):</pre>
              <pre>undefined reference to `.omp_offloading.entries_begin'</pre>
              <pre>/usr/bin/ld:</pre>
              <pre>app.o:(.rodata..omp_offloading.device_images[.omp_offloading.descriptor_reg.x86_64-unknown-linux-gnu]+0x18):</pre>
              <pre>undefined reference to `.omp_offloading.entries_end'</pre>
              <pre>/usr/bin/ld:</pre>
              <pre>app.o:(.rodata..omp_offloading.descriptor[.omp_offloading.descriptor_reg.x86_64-unknown-linux-gnu]+0x10):</pre>
              <pre>undefined reference to `.omp_offloading.entries_begin'</pre>
              <pre>/usr/bin/ld:</pre>
              <pre>app.o:(.rodata..omp_offloading.descriptor[.omp_offloading.descriptor_reg.x86_64-unknown-linux-gnu]+0x18):</pre>
              <pre>undefined reference to `.omp_offloading.entries_end'</pre>
            </blockquote>
            <pre>Do I need to run any additional steps before going through gcc or pass</pre>
            <pre>additional flags? Is there another way of accomplishing the desired</pre>
            <pre>behavior?</pre>
          </blockquote>
          <div><br>
          </div>
          <div>I think you should use clang with the full -fopenmp
            -fopenmp-targets=x86_64-unknown-linux-gnu to build the final
            executable, ie</div>
          <div> $ clang -fopenmp
            -fopenmp-targets=x86_64-unknown-linux-gnu app.o</div>
          <div>If I remember correctly, the above symbols are provided
            by a linker script in Clang 9.0, which gcc has no clue
            about.</div>
          <div><br>
          </div>
          <div>Please note that there have been recent changes in that
            area and trunk now relies on a new tool
            clang-offload-wrapper AFAICS (see <a
href="https://github.com/llvm/llvm-project/commit/5836c356fa6e17d0e10a2f9e0e111b7236dc15fb"
              moz-do-not-send="true">https://github.com/llvm/llvm-project/commit/5836c356fa6e17d0e10a2f9e0e111b7236dc15fb</a>).</div>
          <div>As before this should be called transparently by clang,
            as long as it's correctly invoked with a consistent value
            for -fopenmp-targets.</div>
          <div><br>
          </div>
          <div>I haven't tested above command, but I hope it works.
            Please let me know if it doesn't and I'll take a closer
            look!</div>
          <div>Jonas</div>
          <div><br>
          </div>
          <blockquote type="cite" style="margin:0 0 0 .8ex;
            border-left:2px #729fcf solid;padding-left:1ex">
            <pre>Thanks a lot in advance,</pre>
            <pre>Best regards</pre>
            <pre>Lukas</pre>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </body>
</html>