[AArch64] Fix SDNode type mismatches between *.td files and ISel (#116523)
[llvm-project.git] / llvm / unittests / Object / OffloadingTest.cpp
blob18c9efaceed06f1fe6a9492f1058c70829b65e7f
1 #include "llvm/Object/OffloadBinary.h"
3 #include "llvm/Testing/Support/Error.h"
4 #include "gtest/gtest.h"
5 #include <random>
7 using namespace llvm;
8 using namespace llvm::object;
10 TEST(OffloadingTest, checkOffloadingBinary) {
11 // Create random data to fill the image.
12 std::mt19937 Rng(std::random_device{}());
13 std::uniform_int_distribution<uint64_t> SizeDist(0, 256);
14 std::uniform_int_distribution<uint16_t> KindDist(0);
15 std::uniform_int_distribution<uint16_t> BinaryDist(
16 std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
17 std::uniform_int_distribution<int16_t> StringDist('!', '~');
18 std::vector<uint8_t> Image(SizeDist(Rng));
19 std::generate(Image.begin(), Image.end(), [&]() { return BinaryDist(Rng); });
20 std::vector<std::pair<std::string, std::string>> Strings(SizeDist(Rng));
21 for (auto &KeyAndValue : Strings) {
22 std::string Key(SizeDist(Rng), '\0');
23 std::string Value(SizeDist(Rng), '\0');
25 std::generate(Key.begin(), Key.end(), [&]() { return StringDist(Rng); });
26 std::generate(Value.begin(), Value.end(),
27 [&]() { return StringDist(Rng); });
29 KeyAndValue = std::make_pair(Key, Value);
32 // Create the image.
33 MapVector<StringRef, StringRef> StringData;
34 for (auto &KeyAndValue : Strings)
35 StringData[KeyAndValue.first] = KeyAndValue.second;
36 std::unique_ptr<MemoryBuffer> ImageData = MemoryBuffer::getMemBuffer(
37 {reinterpret_cast<char *>(Image.data()), Image.size()}, "", false);
39 OffloadBinary::OffloadingImage Data;
40 Data.TheImageKind = static_cast<ImageKind>(KindDist(Rng));
41 Data.TheOffloadKind = static_cast<OffloadKind>(KindDist(Rng));
42 Data.Flags = KindDist(Rng);
43 Data.StringData = StringData;
44 Data.Image = std::move(ImageData);
46 auto BinaryBuffer =
47 MemoryBuffer::getMemBufferCopy(OffloadBinary::write(Data));
48 auto BinaryOrErr = OffloadBinary::create(*BinaryBuffer);
49 if (!BinaryOrErr)
50 FAIL();
52 // Make sure we get the same data out.
53 auto &Binary = **BinaryOrErr;
54 ASSERT_EQ(Data.TheImageKind, Binary.getImageKind());
55 ASSERT_EQ(Data.TheOffloadKind, Binary.getOffloadKind());
56 ASSERT_EQ(Data.Flags, Binary.getFlags());
58 for (auto &KeyAndValue : Strings)
59 ASSERT_TRUE(StringData[KeyAndValue.first] ==
60 Binary.getString(KeyAndValue.first));
62 EXPECT_TRUE(Data.Image->getBuffer() == Binary.getImage());
64 // Ensure the size and alignment of the data is correct.
65 EXPECT_TRUE(Binary.getSize() % OffloadBinary::getAlignment() == 0);
66 EXPECT_TRUE(Binary.getSize() == BinaryBuffer->getBuffer().size());