LLVM 24.0.0git
MCDXContainerWriter.cpp
Go to the documentation of this file.
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
11#include "llvm/MC/MCAssembler.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCSection.h"
14#include "llvm/MC/MCValue.h"
17
18using namespace llvm;
19
20cl::opt<bool> EmbedDebug("dx-embed-debug",
21 cl::desc("Embed PDB in shader container"));
23 StripDebug("dx-strip-debug",
24 cl::desc("Strip debug information from shader bytecode"));
25cl::opt<bool> SlimDebug("dx-slim-debug",
26 cl::desc("Generate slim PDB without ILDB part"));
27
29
31
33 size_t SectionSize) {
34 // Skip empty and auxiliary sections.
35 if (SectionSize == 0 || SectionName == PdbFileNameSectionName ||
37 return true;
38 // Slim debug omits ILDB from all DXContainer outputs.
39 return SlimDebug && SectionName == "ILDB";
40}
41
44
46
47 // Start the file size as the header plus the size of the part offsets.
48 // Presently DXContainer files usually contain 7-10 parts. Reserving space for
49 // 16 part offsets gives us a little room for growth.
51 uint64_t PartOffset = 0;
52 bool HasPrivate = false;
53 for (const MCDXContainerPart &Part : Parts) {
54 if (HasPrivate)
56 "PRIV must be the last section in a DXContainer");
57 if (Part.Name == "PRIV")
58 HasPrivate = true;
59
60 uint64_t SectionSize = Part.Data.size();
61 assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
62 "Section size too large for DXContainer");
63
64 PartOffsets.push_back(PartOffset);
65 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
66 if (!HasPrivate)
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 (dxbc::isProgramPart(Part.Name))
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 MCDXContainerPart &Part : Parts) {
98 uint64_t SectionSize = Part.Data.size();
99 unsigned Start = W.OS.tell();
100 // Write section header.
101 W.write<char>(ArrayRef<char>(Part.Name.data(), 4));
102
103 uint64_t PartSize = SectionSize;
104
105 if (dxbc::isProgramPart(Part.Name))
106 PartSize += sizeof(dxbc::ProgramHeader);
107 // DXContainer part should be 4-byte aligned, unless it is PRIV part.
108 bool IsPrivate = Part.Name == "PRIV";
109 if (!IsPrivate)
110 PartSize = alignTo(PartSize, Align(4));
111 W.write<uint32_t>(static_cast<uint32_t>(PartSize));
112 if (dxbc::isProgramPart(Part.Name)) {
113 dxbc::ProgramHeader Header;
114 memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
115
116 VersionTuple Version = TT.getOSVersion();
117 uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
118 uint8_t MinorVersion =
119 static_cast<uint8_t>(Version.getMinor().value_or(0));
120 Header.Version =
121 dxbc::ProgramHeader::getVersion(MajorVersion, MinorVersion);
122 if (TT.hasEnvironment())
123 Header.ShaderKind =
124 static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
125
126 // The program header's size field is in 32-bit words.
127 Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
128 memcpy(Header.Bitcode.Magic, "DXIL", 4);
129 VersionTuple DXILVersion = TT.getDXILVersion();
130 Header.Bitcode.MajorVersion = DXILVersion.getMajor();
131 Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
132 Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
133 Header.Bitcode.Size = SectionSize;
135 Header.swapBytes();
136 W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
137 sizeof(dxbc::ProgramHeader)));
138 }
139 W.write<char>(Part.Data);
140 if (!IsPrivate) {
141 unsigned Size = W.OS.tell() - Start;
142 W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
143 }
144 }
145}
146
147void DXContainerObjectWriter::clearParts() {
148 Parts.clear();
149 SectionBuffers.clear();
150}
151
153 clearParts();
154 for (const MCSection &Sec : *Asm) {
155 if (shouldSkipSection(Sec.getName(), Asm->getSectionAddressSize(Sec)))
156 continue;
157
158 SectionBuffers.emplace_back();
159 raw_svector_ostream OS(SectionBuffers.back());
160 Asm->writeSectionData(OS, &Sec);
161 Parts.push_back({Sec.getName(), StringRef(SectionBuffers.back())});
162 }
163 return Parts;
164}
165
167 size_t SectionSize) {
168 // Do not write ILDB part if we're not embedding it.
169 if (SectionName == "ILDB" && (!EmbedDebug || StripDebug))
170 return true;
171 if (SectionName == "SRCI")
172 return true;
174}
175
177 write(W.OS, getContext().getTargetTriple());
178 clearParts();
179
180 return 0;
181}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
cl::opt< bool > EmbedDebug("dx-embed-debug", cl::desc("Embed PDB in shader container"))
cl::opt< bool > StripDebug("dx-strip-debug", cl::desc("Strip debug information from shader bytecode"))
cl::opt< bool > SlimDebug("dx-slim-debug", cl::desc("Generate slim PDB without ILDB part"))
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
uint64_t writeObject() override
Write the object file and returns the number of bytes written.
bool shouldSkipSection(StringRef SectionName, size_t SectionSize) override
ArrayRef< MCDXContainerPart > collectParts() override
void write(raw_ostream &OS, const Triple &TT)
virtual ArrayRef< MCDXContainerPart > collectParts()
virtual bool shouldSkipSection(StringRef SectionName, size_t SectionSize)
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Represents a version number in the form major[.minor[.subminor[.build]]].
unsigned getMajor() const
Retrieve the major version number.
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
LLVM_ABI bool isProgramPart(StringRef PartName)
constexpr bool IsBigEndianHost
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
static constexpr StringLiteral ModuleHashSectionName
Contains module hash.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:186
static constexpr StringLiteral PdbFileNameSectionName
Contains PDB output file name.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Use this type to describe the size and type of a DXIL container part.
static uint8_t getVersion(uint8_t Major, uint8_t Minor)
Adapter to write values to a stream in a particular byte order.