[AMDGPU] prevent shrinking udiv/urem if either operand is in (SignedMax,UnsignedMax...
[llvm-project.git] / llvm / lib / ObjectYAML / OffloadEmitter.cpp
blob131da68d77506d6e2d9f1a8a33b8f5184b7588cb
1 //===- OffloadEmitter.cpp -------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Object/OffloadBinary.h"
10 #include "llvm/ObjectYAML/OffloadYAML.h"
11 #include "llvm/ObjectYAML/yaml2obj.h"
12 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15 using namespace OffloadYAML;
17 namespace llvm {
18 namespace yaml {
20 bool yaml2offload(Binary &Doc, raw_ostream &Out, ErrorHandler EH) {
21 for (const auto &Member : Doc.Members) {
22 object::OffloadBinary::OffloadingImage Image{};
23 if (Member.ImageKind)
24 Image.TheImageKind = *Member.ImageKind;
25 if (Member.OffloadKind)
26 Image.TheOffloadKind = *Member.OffloadKind;
27 if (Member.Flags)
28 Image.Flags = *Member.Flags;
30 if (Member.StringEntries)
31 for (const auto &Entry : *Member.StringEntries)
32 Image.StringData[Entry.Key] = Entry.Value;
34 SmallVector<char, 1024> Data;
35 raw_svector_ostream OS(Data);
36 if (Member.Content)
37 Member.Content->writeAsBinary(OS);
38 Image.Image = MemoryBuffer::getMemBufferCopy(OS.str());
40 // Copy the data to a new buffer so we can modify the bytes directly.
41 auto Buffer = object::OffloadBinary::write(Image);
42 auto *TheHeader =
43 reinterpret_cast<object::OffloadBinary::Header *>(&Buffer[0]);
44 if (Doc.Version)
45 TheHeader->Version = *Doc.Version;
46 if (Doc.Size)
47 TheHeader->Size = *Doc.Size;
48 if (Doc.EntryOffset)
49 TheHeader->EntryOffset = *Doc.EntryOffset;
50 if (Doc.EntrySize)
51 TheHeader->EntrySize = *Doc.EntrySize;
53 Out.write(Buffer.begin(), Buffer.size());
56 return true;
59 } // namespace yaml
60 } // namespace llvm