xref: /llvm-project/offload/src/DeviceImage.cpp (revision 08533a3ee8f3a09a59cf6ac3be59198b26b7f739)
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(static_cast<char *>(Image.ImageStart),
31                            utils::getPtrDiff(Image.ImageEnd, Image.ImageStart));
32 
33   auto BinaryOrErr =
34       llvm::object::OffloadBinary::create(llvm::MemoryBufferRef(ImageStr, ""));
35 
36   if (!BinaryOrErr) {
37     consumeError(BinaryOrErr.takeError());
38     return;
39   }
40 
41   Binary = std::move(*BinaryOrErr);
42   void *Begin = const_cast<void *>(
43       static_cast<const void *>(Binary->getImage().bytes_begin()));
44   void *End = const_cast<void *>(
45       static_cast<const void *>(Binary->getImage().bytes_end()));
46 
47   Image = __tgt_device_image{Begin, End, Image.EntriesBegin, Image.EntriesEnd};
48 }
49