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/CommandLine.h"
17
18using namespace llvm;
19
20cl::opt<bool> EmbedDebug("dx-embed-debug",
21 cl::desc("Embed PDB in shader container"));
22cl::opt<bool>
23 StripDebug("dx-strip-debug",
24 cl::desc("Strip debug information from shader bytecode"));
25
26MCDXContainerTargetWriter::~MCDXContainerTargetWriter() = default;
27
28MCDXContainerBaseWriter::~MCDXContainerBaseWriter() = default;
29
30void MCDXContainerBaseWriter::write(raw_ostream &OS, const Triple &TT) {
31 ArrayRef<MCDXContainerPart> Parts = collectParts();
32
33 support::endian::Writer W(OS, llvm::endianness::little);
34
35 // Start the file size as the header plus the size of the part offsets.
36 // Presently DXContainer files usually contain 7-10 parts. Reserving space for
37 // 16 part offsets gives us a little room for growth.
38 llvm::SmallVector<uint64_t, 16> PartOffsets;
39 uint64_t PartOffset = 0;
40 for (const MCDXContainerPart &Part : Parts) {
41 uint64_t SectionSize = Part.Data.size();
42 assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
43 "Section size too large for DXContainer");
44
45 PartOffsets.push_back(Elt: PartOffset);
46 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
47 PartOffset = alignTo(Size: PartOffset, A: Align(4ul));
48 // The DXIL part also writes a program header, so we need to include its
49 // size when computing the offset for a part after the DXIL part.
50 if (dxbc::isProgramPart(PartName: Part.Name))
51 PartOffset += sizeof(dxbc::ProgramHeader);
52 }
53 assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
54 "Part data too large for DXContainer");
55
56 uint64_t PartStart =
57 sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
58 uint64_t FileSize = PartStart + PartOffset;
59 assert(FileSize < std::numeric_limits<uint32_t>::max() &&
60 "File size too large for DXContainer");
61
62 // Write the header.
63 W.write<char>(Val: {'D', 'X', 'B', 'C'});
64 // Write 16-bytes of 0's for the hash.
65 W.OS.write_zeros(NumZeros: 16);
66 // Write 1.0 for file format version.
67 W.write<uint16_t>(Val: 1u);
68 W.write<uint16_t>(Val: 0u);
69 // Write the file size.
70 W.write<uint32_t>(Val: static_cast<uint32_t>(FileSize));
71 // Write the number of parts.
72 W.write<uint32_t>(Val: static_cast<uint32_t>(PartOffsets.size()));
73 // Write the offsets for the part headers for each part.
74 for (uint64_t Offset : PartOffsets)
75 W.write<uint32_t>(Val: static_cast<uint32_t>(PartStart + Offset));
76
77 for (const MCDXContainerPart &Part : Parts) {
78 uint64_t SectionSize = Part.Data.size();
79 unsigned Start = W.OS.tell();
80 // Write section header.
81 W.write<char>(Val: ArrayRef<char>(Part.Name.data(), 4));
82
83 uint64_t PartSize = SectionSize;
84
85 if (dxbc::isProgramPart(PartName: Part.Name))
86 PartSize += sizeof(dxbc::ProgramHeader);
87 // DXContainer parts should be 4-byte aligned.
88 PartSize = alignTo(Size: PartSize, A: Align(4));
89 W.write<uint32_t>(Val: static_cast<uint32_t>(PartSize));
90 if (dxbc::isProgramPart(PartName: Part.Name)) {
91 dxbc::ProgramHeader Header;
92 memset(s: reinterpret_cast<void *>(&Header), c: 0, n: sizeof(dxbc::ProgramHeader));
93
94 VersionTuple Version = TT.getOSVersion();
95 uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
96 uint8_t MinorVersion =
97 static_cast<uint8_t>(Version.getMinor().value_or(u: 0));
98 Header.Version =
99 dxbc::ProgramHeader::getVersion(Major: MajorVersion, Minor: MinorVersion);
100 if (TT.hasEnvironment())
101 Header.ShaderKind =
102 static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
103
104 // The program header's size field is in 32-bit words.
105 Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
106 memcpy(dest: Header.Bitcode.Magic, src: "DXIL", n: 4);
107 VersionTuple DXILVersion = TT.getDXILVersion();
108 Header.Bitcode.MajorVersion = DXILVersion.getMajor();
109 Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(u: 0);
110 Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
111 Header.Bitcode.Size = SectionSize;
112 if (sys::IsBigEndianHost)
113 Header.swapBytes();
114 W.write<char>(Val: ArrayRef<char>(reinterpret_cast<char *>(&Header),
115 sizeof(dxbc::ProgramHeader)));
116 }
117 W.write<char>(Val: Part.Data);
118 unsigned Size = W.OS.tell() - Start;
119 W.OS.write_zeros(NumZeros: offsetToAlignment(Value: Size, Alignment: Align(4)));
120 }
121}
122
123void DXContainerObjectWriter::clearParts() {
124 Parts.clear();
125 SectionBuffers.clear();
126}
127
128ArrayRef<MCDXContainerPart> DXContainerObjectWriter::collectParts() {
129 clearParts();
130 for (const MCSection &Sec : *Asm) {
131 if (shouldSkipSection(SectionName: Sec.getName(), SectionSize: Asm->getSectionAddressSize(Sec)))
132 continue;
133
134 SectionBuffers.emplace_back();
135 raw_svector_ostream OS(SectionBuffers.back());
136 Asm->writeSectionData(OS, Section: &Sec);
137 Parts.push_back(Elt: {.Name: Sec.getName(), .Data: StringRef(SectionBuffers.back())});
138 }
139 return Parts;
140}
141
142bool DXContainerObjectWriter::shouldSkipSection(StringRef SectionName,
143 size_t SectionSize) {
144 // Do not write ILDB part if we're not embedding it.
145 if (SectionName == "ILDB" && (!EmbedDebug || StripDebug))
146 return true;
147 if (SectionName == "SRCI")
148 return true;
149 return MCDXContainerBaseWriter::shouldSkipSection(SectionName, SectionSize);
150}
151
152uint64_t DXContainerObjectWriter::writeObject() {
153 write(OS&: W.OS, TT: getContext().getTargetTriple());
154 clearParts();
155
156 return 0;
157}
158