This is an archive of the discontinued LLVM Phabricator instance.

[Clang][Bundler] Use llvm-objcopy for creating fat object files
ClosedPublic

Authored by sdmitriev on Aug 20 2019, 10:16 AM.

Details

Summary

clang-offload-bundler currently uses partial linking for creating fat object files, but such technique cannot be used on Windows due to the absence of partial linking support in the linker. This patch changes implementation to use llvm-objcopy for merging device and host objects instead of doing partial linking. This is one step forward towards enabling OpenMP offload on Windows.

Diff Detail

Event Timeline

sdmitriev created this revision.Aug 20 2019, 10:16 AM

Any feedback?

ABataev added inline comments.Aug 26 2019, 9:00 AM
clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
485

As I understand, the resulting object file has the same structure as before? And we still can get an access to the host object file using standard programs, like readelf, objdump, etc.?

sdmitriev marked an inline comment as done.Aug 26 2019, 9:47 AM
sdmitriev added inline comments.
clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
485

Yes, the structure is the same. The only difference is in the section flags that contain device objects - such sections used to have ALLOC attribute, but they do not have it now which I believe is a good think.

That is what we had earlier:

bash-4.2$ cat y.c
#pragma omp declare target
void foo() {}
#pragma omp end declare target
bash-4.2$ clang -fopenmp -fopenmp-targets=x86_64-pc-linux-gnu -c y.c
bash-4.2$ objdump -h y.o 

y.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000008  0000000000000000  0000000000000000  00000040  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .text.startup 00000010  0000000000000000  0000000000000000  00000050  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  2 __CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu 000003f8  0000000000000000  0000000000000000  00000060  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 __CLANG_OFFLOAD_BUNDLE__host-x86_64-unknown-linux-gnu 00000630  0000000000000000  0000000000000000  00000460  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .eh_frame     00000058  0000000000000000  0000000000000000  00000a90  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
  5 .init_array.0 00000008  0000000000000000  0000000000000000  00000ae8  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, DATA
  6 .comment      0000006a  0000000000000000  0000000000000000  00000af0  2**0
                  CONTENTS, READONLY
  7 .note.GNU-stack 00000000  0000000000000000  0000000000000000  00000b5a  2**0
                  CONTENTS, READONLY
  8 .llvm_addrsig 00000002  0000000000000000  0000000000000000  00000b5a  2**0
                  CONTENTS, READONLY, EXCLUDE
bash-4.2$

And here is what we have with this patch:

bash-4.2$ clang -fopenmp -fopenmp-targets=x86_64-pc-linux-gnu -c y.c
bash-4.2$ objdump -h y.o 

y.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000006  0000000000000000  0000000000000000  00000040  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .text.startup 00000010  0000000000000000  0000000000000000  00000050  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  2 .init_array.0 00000008  0000000000000000  0000000000000000  00000060  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, DATA
  3 .comment      0000006a  0000000000000000  0000000000000000  00000068  2**0
                  CONTENTS, READONLY
  4 .note.GNU-stack 00000000  0000000000000000  0000000000000000  000000d2  2**0
                  CONTENTS, READONLY
  5 .eh_frame     00000058  0000000000000000  0000000000000000  000000d8  2**3
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
  6 .llvm_addrsig 00000002  0000000000000000  0000000000000000  00000238  2**0
                  CONTENTS, READONLY, EXCLUDE
  7 __CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu 000003f8  0000000000000000  0000000000000000  00000353  2**0
                  CONTENTS, READONLY
  8 __CLANG_OFFLOAD_BUNDLE__host-x86_64-unknown-linux-gnu 00000630  0000000000000000  0000000000000000  0000074b  2**0
                  CONTENTS, READONLY
bash-4.2$
ABataev added inline comments.Aug 26 2019, 9:55 AM
clang/test/Driver/clang-offload-bundler.c
233

Why does FileCheck have all these strange options?

sdmitriev marked an inline comment as done.Aug 26 2019, 10:11 AM
sdmitriev added inline comments.
clang/test/Driver/clang-offload-bundler.c
233

Well, -DVAR=VALUE is a standard FileCheck option which allows to define variable that can be used in the check pattern (please see http://www.llvm.org/docs/CommandGuide/FileCheck.html)

-D<VAR=VALUE>
Sets a filecheck pattern variable VAR with value VALUE that can be used in CHECK: lines.

I am using these options for defining variables which contain input and output object names as well as the host triple.

This revision is now accepted and ready to land.Aug 26 2019, 10:15 AM
This revision was automatically updated to reflect the committed changes.
mgorny added inline comments.Oct 13 2019, 10:34 AM
clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
467

This is broken for running tests in clang standalone builds. llvm-copy is installed in the system, unlike clang-offload-bundler that is present in build directory. Not to mention it's simply wrong to assume both executables must be in the same directory.