Skip to content

Commit 6655681

Browse files
authored
[llvm-offload-wrapper] Fix Triple and OpenMP handling (#167580)
Summary: The OpenMP handling using an offload binary should be optional, it's only used for extra metadata for llvm-objdump. Also the triple was completely wrong, it didn't let anyone correctly choose between ELF and COFF handling.
1 parent ea10026 commit 6655681

File tree

4 files changed

+99
-64
lines changed

4 files changed

+99
-64
lines changed

llvm/lib/Frontend/Offloading/OffloadWrapper.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,27 @@ GlobalVariable *createBinDesc(Module &M, ArrayRef<ArrayRef<char>> Bufs,
147147
Image->setAlignment(Align(object::OffloadBinary::getAlignment()));
148148

149149
StringRef Binary(Buf.data(), Buf.size());
150-
assert(identify_magic(Binary) == file_magic::offload_binary &&
151-
"Invalid binary format");
152150

151+
uint64_t BeginOffset = 0;
152+
uint64_t EndOffset = Binary.size();
153+
154+
// Optionally use an offload binary for its offload dumping support.
153155
// The device image struct contains the pointer to the beginning and end of
154156
// the image stored inside of the offload binary. There should only be one
155157
// of these for each buffer so we parse it out manually.
156-
const auto *Header =
157-
reinterpret_cast<const object::OffloadBinary::Header *>(
158-
Binary.bytes_begin());
159-
const auto *Entry = reinterpret_cast<const object::OffloadBinary::Entry *>(
160-
Binary.bytes_begin() + Header->EntryOffset);
161-
162-
auto *Begin = ConstantInt::get(getSizeTTy(M), Entry->ImageOffset);
163-
auto *Size =
164-
ConstantInt::get(getSizeTTy(M), Entry->ImageOffset + Entry->ImageSize);
158+
if (identify_magic(Binary) == file_magic::offload_binary) {
159+
const auto *Header =
160+
reinterpret_cast<const object::OffloadBinary::Header *>(
161+
Binary.bytes_begin());
162+
const auto *Entry =
163+
reinterpret_cast<const object::OffloadBinary::Entry *>(
164+
Binary.bytes_begin() + Header->EntryOffset);
165+
BeginOffset = Entry->ImageOffset;
166+
EndOffset = Entry->ImageOffset + Entry->ImageSize;
167+
}
168+
169+
auto *Begin = ConstantInt::get(getSizeTTy(M), BeginOffset);
170+
auto *Size = ConstantInt::get(getSizeTTy(M), EndOffset);
165171
Constant *ZeroBegin[] = {Zero, Begin};
166172
Constant *ZeroSize[] = {Zero, Size};
167173

llvm/test/Other/offload-wrapper.ll

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
; RUN: llvm-offload-wrapper --triple=x86_64-unknown-linux-gnu -kind=openmp %s -o %t.bc
2+
; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefix=OMP
3+
4+
; OMP: @__start_llvm_offload_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
5+
; OMP-NEXT: @__stop_llvm_offload_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
6+
; OMP-NEXT: @__dummy.llvm_offload_entries = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "llvm_offload_entries", align 8
7+
; OMP-NEXT: @llvm.compiler.used = appending global [1 x ptr] [ptr @__dummy.llvm_offload_entries], section "llvm.metadata"
8+
; OMP-NEXT: @.omp_offloading.device_image = internal unnamed_addr constant [[[SIZE:[0-9]+]] x i8] c"{{.*}}", section ".llvm.offloading", align 8
9+
; OMP-NEXT: @.omp_offloading.device_images = internal unnamed_addr constant [1 x %__tgt_device_image] [%__tgt_device_image { ptr @.omp_offloading.device_image, ptr getelementptr ([[[SIZE]] x i8], ptr @.omp_offloading.device_image, i64 0, i64 [[SIZE]]), ptr @__start_llvm_offload_entries, ptr @__stop_llvm_offload_entries }]
10+
; OMP-NEXT: @.omp_offloading.descriptor = internal constant %__tgt_bin_desc { i32 1, ptr @.omp_offloading.device_images, ptr @__start_llvm_offload_entries, ptr @__stop_llvm_offload_entries }
11+
; OMP-NEXT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 101, ptr @.omp_offloading.descriptor_reg, ptr null }]
12+
13+
; OMP: define internal void @.omp_offloading.descriptor_reg() section ".text.startup" {
14+
; OMP-NEXT: entry:
15+
; OMP-NEXT: call void @__tgt_register_lib(ptr @.omp_offloading.descriptor)
16+
; OMP-NEXT: %0 = call i32 @atexit(ptr @.omp_offloading.descriptor_unreg)
17+
; OMP-NEXT: ret void
18+
; OMP-NEXT: }
19+
20+
; OMP: define internal void @.omp_offloading.descriptor_unreg() section ".text.startup" {
21+
; OMP-NEXT: entry:
22+
; OMP-NEXT: call void @__tgt_unregister_lib(ptr @.omp_offloading.descriptor)
23+
; OMP-NEXT: ret void
24+
; OMP-NEXT: }
25+
26+
; RUN: llvm-offload-wrapper --triple=x86_64-unknown-linux-gnu -kind=hip %s -o %t.bc
27+
; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefix=HIP
28+
29+
; HIP: @__start_llvm_offload_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
30+
; HIP-NEXT: @__stop_llvm_offload_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
31+
; HIP-NEXT: @__dummy.llvm_offload_entries = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "llvm_offload_entries", align 8
32+
; HIP-NEXT: @llvm.compiler.used = appending global [1 x ptr] [ptr @__dummy.llvm_offload_entries], section "llvm.metadata"
33+
; HIP-NEXT: @.fatbin_image = internal constant {{.*}}, section ".hip_fatbin"
34+
; HIP-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1212764230, i32 1, ptr @.fatbin_image, ptr null }, section ".hipFatBinSegment", align 8
35+
; HIP-NEXT: @.hip.binary_handle = internal global ptr null
36+
; HIP-NEXT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 101, ptr @.hip.fatbin_reg, ptr null }]
37+
38+
; HIP: define internal void @.hip.fatbin_reg() section ".text.startup" {
39+
; HIP-NEXT: entry:
40+
; HIP-NEXT: %0 = call ptr @__hipRegisterFatBinary(ptr @.fatbin_wrapper)
41+
; HIP-NEXT: store ptr %0, ptr @.hip.binary_handle, align 8
42+
; HIP-NEXT: call void @.hip.globals_reg(ptr %0)
43+
; HIP-NEXT: %1 = call i32 @atexit(ptr @.hip.fatbin_unreg)
44+
; HIP-NEXT: ret void
45+
; HIP-NEXT: }
46+
47+
; HIP: define internal void @.hip.fatbin_unreg() section ".text.startup" {
48+
; HIP-NEXT: entry:
49+
; HIP-NEXT: %0 = load ptr, ptr @.hip.binary_handle, align 8
50+
; HIP-NEXT: call void @__hipUnregisterFatBinary(ptr %0)
51+
; HIP-NEXT: ret void
52+
; HIP-NEXT: }
53+
54+
; RUN: llvm-offload-wrapper --triple=x86_64-unknown-linux-gnu -kind=cuda %s -o %t.bc
55+
; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefix=CUDA
56+
57+
; CUDA: @__start_llvm_offload_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
58+
; CUDA-NEXT: @__stop_llvm_offload_entries = external hidden constant [0 x %struct.__tgt_offload_entry]
59+
; CUDA-NEXT: @__dummy.llvm_offload_entries = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer, section "llvm_offload_entries", align 8
60+
; CUDA-NEXT: @llvm.compiler.used = appending global [1 x ptr] [ptr @__dummy.llvm_offload_entries], section "llvm.metadata"
61+
; CUDA-NEXT: @.fatbin_image = internal constant {{.*}}, section ".nv_fatbin"
62+
; CUDA-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1180844977, i32 1, ptr @.fatbin_image, ptr null }, section ".nvFatBinSegment", align 8
63+
; CUDA-NEXT: @.cuda.binary_handle = internal global ptr null
64+
; CUDA-NEXT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 101, ptr @.cuda.fatbin_reg, ptr null }]
65+
66+
; CUDA: define internal void @.cuda.fatbin_reg() section ".text.startup" {
67+
; CUDA-NEXT: entry:
68+
; CUDA-NEXT: %0 = call ptr @__cudaRegisterFatBinary(ptr @.fatbin_wrapper)
69+
; CUDA-NEXT: store ptr %0, ptr @.cuda.binary_handle, align 8
70+
; CUDA-NEXT: call void @.cuda.globals_reg(ptr %0)
71+
; CUDA-NEXT: call void @__cudaRegisterFatBinaryEnd(ptr %0)
72+
; CUDA-NEXT: %1 = call i32 @atexit(ptr @.cuda.fatbin_unreg)
73+
; CUDA-NEXT: ret void
74+
; CUDA-NEXT: }
75+
76+
; CUDA: define internal void @.cuda.fatbin_unreg() section ".text.startup" {
77+
; CUDA-NEXT: entry:
78+
; CUDA-NEXT: %0 = load ptr, ptr @.cuda.binary_handle, align 8
79+
; CUDA-NEXT: call void @__cudaUnregisterFatBinary(ptr %0)
80+
; CUDA-NEXT: ret void
81+
; CUDA-NEXT: }

llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static Error wrapImages(ArrayRef<ArrayRef<char>> BuffersToWrap) {
6464

6565
LLVMContext Context;
6666
Module M("offload.wrapper.module", Context);
67-
M.setTargetTriple(Triple());
67+
M.setTargetTriple(llvm::Triple(TheTriple));
6868

6969
switch (Kind) {
7070
case llvm::object::OFK_OpenMP:

0 commit comments

Comments
 (0)