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