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     // The DXIL part also writes a program header, so we need to include its
69     // size when computing the offset for a part after the DXIL part.
70     if (Sec.getName() == "DXIL")
71       PartOffset += sizeof(dxbc::ProgramHeader);
72   }
73   assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
74          "Part data too large for DXContainer");
75 
76   uint64_t PartStart =
77       sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
78   uint64_t FileSize = PartStart + PartOffset;
79   assert(FileSize < std::numeric_limits<uint32_t>::max() &&
80          "File size too large for DXContainer");
81 
82   // Write the header.
83   W.write<char>({'D', 'X', 'B', 'C'});
84   // Write 16-bytes of 0's for the hash.
85   W.OS.write_zeros(16);
86   // Write 1.0 for file format version.
87   W.write<uint16_t>(1u);
88   W.write<uint16_t>(0u);
89   // Write the file size.
90   W.write<uint32_t>(static_cast<uint32_t>(FileSize));
91   // Write the number of parts.
92   W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
93   // Write the offsets for the part headers for each part.
94   for (uint64_t Offset : PartOffsets)
95     W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
96 
97   for (const MCSection &Sec : Asm) {
98     uint64_t SectionSize = Layout.getSectionAddressSize(&Sec);
99     // Skip empty sections.
100     if (SectionSize == 0)
101       continue;
102 
103     unsigned Start = W.OS.tell();
104     // Write section header.
105     W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));
106 
107     uint64_t PartSize = SectionSize;
108 
109     if (Sec.getName() == "DXIL")
110       PartSize += sizeof(dxbc::ProgramHeader);
111     // DXContainer parts should be 4-byte aligned.
112     PartSize = alignTo(PartSize, Align(4));
113     W.write<uint32_t>(static_cast<uint32_t>(PartSize));
114     if (Sec.getName() == "DXIL") {
115       dxbc::ProgramHeader Header;
116       memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
117 
118       const Triple &TT = Asm.getContext().getTargetTriple();
119       VersionTuple Version = TT.getOSVersion();
120       Header.MajorVersion = static_cast<uint8_t>(Version.getMajor());
121       if (Version.getMinor())
122         Header.MinorVersion = static_cast<uint8_t>(*Version.getMinor());
123       if (TT.hasEnvironment())
124         Header.ShaderKind =
125             static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
126 
127       // The program header's size field is in 32-bit words.
128       Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
129       memcpy(Header.Bitcode.Magic, "DXIL", 4);
130       Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
131       Header.Bitcode.Size = SectionSize;
132       if (sys::IsBigEndianHost)
133         Header.swapBytes();
134       W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
135                                    sizeof(dxbc::ProgramHeader)));
136     }
137     Asm.writeSectionData(W.OS, &Sec, Layout);
138     unsigned Size = W.OS.tell() - Start;
139     W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
140   }
141   return 0;
142 }
143 
144 std::unique_ptr<MCObjectWriter> llvm::createDXContainerObjectWriter(
145     std::unique_ptr<MCDXContainerTargetWriter> MOTW, raw_pwrite_stream &OS) {
146   return std::make_unique<DXContainerObjectWriter>(std::move(MOTW), OS);
147 }
148