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