xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCDXContainerWriter.cpp (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 //===- llvm/MC/MCDXContainerWriter.cpp - DXContainer Writer -----*- C++ -*-===//
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 #include "llvm/MC/MCDXContainerWriter.h"
10 #include "llvm/BinaryFormat/DXContainer.h"
11 #include "llvm/MC/MCAsmLayout.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSection.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/Alignment.h"
17 #include "llvm/Support/EndianStream.h"
18 
19 using namespace llvm;
20 
21 MCDXContainerTargetWriter::~MCDXContainerTargetWriter() {}
22 
23 namespace {
24 class DXContainerObjectWriter : public MCObjectWriter {
25   ::support::endian::Writer W;
26 
27   /// The target specific DXContainer writer instance.
28   std::unique_ptr<MCDXContainerTargetWriter> TargetObjectWriter;
29 
30 public:
31   DXContainerObjectWriter(std::unique_ptr<MCDXContainerTargetWriter> MOTW,
32                           raw_pwrite_stream &OS)
33       : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
34 
35   ~DXContainerObjectWriter() override {}
36 
37 private:
38   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
39                         const MCFragment *Fragment, const MCFixup &Fixup,
40                         MCValue Target, uint64_t &FixedValue) override {}
41 
42   void executePostLayoutBinding(MCAssembler &Asm,
43                                 const MCAsmLayout &Layout) override {}
44 
45   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
46 };
47 } // namespace
48 
49 uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm,
50                                               const MCAsmLayout &Layout) {
51   // Start the file size as the header plus the size of the part offsets.
52   // Presently DXContainer files usually contain 7-10 parts. Reserving space for
53   // 16 part offsets gives us a little room for growth.
54   llvm::SmallVector<uint64_t, 16> PartOffsets;
55   uint64_t PartOffset = 0;
56   for (const MCSection &Sec : Asm) {
57     uint64_t SectionSize = Layout.getSectionAddressSize(&Sec);
58     // Skip empty sections.
59     if (SectionSize == 0)
60       continue;
61 
62     assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
63            "Section size too large for DXContainer");
64 
65     PartOffsets.push_back(PartOffset);
66     PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
67     PartOffset = alignTo(PartOffset, Align(4ul));
68   }
69   assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
70          "Part data too large for DXContainer");
71 
72   uint64_t PartStart =
73       sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
74   uint64_t FileSize = PartStart + PartOffset;
75   assert(FileSize < std::numeric_limits<uint32_t>::max() &&
76          "File size too large for DXContainer");
77 
78   // Write the header.
79   W.write<char>({'D', 'X', 'B', 'C'});
80   // Write 16-bytes of 0's for the hash.
81   W.OS.write_zeros(16);
82   // Write 1.0 for file format version.
83   W.write<uint16_t>(1u);
84   W.write<uint16_t>(0u);
85   // Write the file size.
86   W.write<uint32_t>(static_cast<uint32_t>(FileSize));
87   // Write the number of parts.
88   W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
89   // Write the offsets for the part headers for each part.
90   for (uint64_t Offset : PartOffsets)
91     W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
92 
93   for (const MCSection &Sec : Asm) {
94     uint64_t SectionSize = Layout.getSectionAddressSize(&Sec);
95     // Skip empty sections.
96     if (SectionSize == 0)
97       continue;
98 
99     unsigned Start = W.OS.tell();
100     // Write section header.
101     W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));
102 
103     uint64_t PartSize = SectionSize + sizeof(dxbc::PartHeader);
104 
105     if (Sec.getName() == "DXIL")
106       PartSize += sizeof(dxbc::ProgramHeader);
107     // DXContainer parts should be 4-byte aligned.
108     PartSize = alignTo(PartSize, Align(4));
109     W.write<uint32_t>(static_cast<uint32_t>(PartSize));
110     if (Sec.getName() == "DXIL") {
111       dxbc::ProgramHeader Header;
112       memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
113 
114       const Triple &TT = Asm.getContext().getTargetTriple();
115       VersionTuple Version = TT.getOSVersion();
116       Header.MajorVersion = static_cast<uint8_t>(Version.getMajor());
117       if (Version.getMinor())
118         Header.MinorVersion = static_cast<uint8_t>(*Version.getMinor());
119       if (TT.hasEnvironment())
120         Header.ShaderKind =
121             static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
122 
123       // The program header's size field is in 32-bit words.
124       Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
125       memcpy(Header.Bitcode.Magic, "DXIL", 4);
126       Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
127       Header.Bitcode.Size = SectionSize;
128       if (sys::IsBigEndianHost)
129         Header.swapBytes();
130       W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
131                                    sizeof(dxbc::ProgramHeader)));
132     }
133     Asm.writeSectionData(W.OS, &Sec, Layout);
134     unsigned Size = W.OS.tell() - Start;
135     W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
136   }
137   return 0;
138 }
139 
140 std::unique_ptr<MCObjectWriter> llvm::createDXContainerObjectWriter(
141     std::unique_ptr<MCDXContainerTargetWriter> MOTW, raw_pwrite_stream &OS) {
142   return std::make_unique<DXContainerObjectWriter>(std::move(MOTW), OS);
143 }
144