1 | //===-- DeviceImage.cpp - Representation of the device code/image ---------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | //===----------------------------------------------------------------------===// |
10 | |
11 | #include "DeviceImage.h" |
12 | |
13 | #include "OffloadEntry.h" |
14 | #include "Shared/APITypes.h" |
15 | #include "Shared/Debug.h" |
16 | #include "Shared/Utils.h" |
17 | |
18 | #include "llvm/ADT/iterator_range.h" |
19 | #include "llvm/Support/Error.h" |
20 | #include <memory> |
21 | |
22 | __tgt_bin_desc *OffloadEntryTy::getBinaryDescription() const { |
23 | return &DeviceImage.getBinaryDesc(); |
24 | } |
25 | |
26 | DeviceImageTy::DeviceImageTy(__tgt_bin_desc &BinaryDesc, |
27 | __tgt_device_image &TgtDeviceImage) |
28 | : BinaryDesc(&BinaryDesc), Image(TgtDeviceImage) { |
29 | |
30 | llvm::StringRef ImageStr( |
31 | static_cast<char *>(Image.ImageStart), |
32 | llvm::omp::target::getPtrDiff(Image.ImageEnd, Image.ImageStart)); |
33 | |
34 | auto BinaryOrErr = |
35 | llvm::object::OffloadBinary::create(llvm::MemoryBufferRef(ImageStr, "" )); |
36 | |
37 | if (!BinaryOrErr) { |
38 | consumeError(BinaryOrErr.takeError()); |
39 | return; |
40 | } |
41 | |
42 | Binary = std::move(*BinaryOrErr); |
43 | void *Begin = const_cast<void *>( |
44 | static_cast<const void *>(Binary->getImage().bytes_begin())); |
45 | void *End = const_cast<void *>( |
46 | static_cast<const void *>(Binary->getImage().bytes_end())); |
47 | |
48 | Image = __tgt_device_image{Begin, End, Image.EntriesBegin, Image.EntriesEnd}; |
49 | } |
50 | |