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"
16
17using namespace llvm;
18
19static bool isSignaturePart(StringRef Name) {
20 return Name == "ISG1" || Name == "OSG1" || Name == "PSG1";
21}
22
24
26
28 size_t SectionSize) {
29 // Skip empty and auxiliary sections.
30 return SectionSize == 0 || SectionName == PdbFileNameSectionName ||
32}
33
36
38
39 // Start the file size as the header plus the size of the part offsets.
40 // Presently DXContainer files usually contain 7-10 parts. Reserving space for
41 // 16 part offsets gives us a little room for growth.
43 uint64_t PartOffset = 0;
44 bool HasPrivate = false;
45 for (const MCDXContainerPart &Part : Parts) {
46 if (HasPrivate)
48 "PRIV must be the last section in a DXContainer");
49 if (Part.Name == "PRIV")
50 HasPrivate = true;
51
52 uint64_t SectionSize = Part.Data.size();
53 assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
54 "Section size too large for DXContainer");
55
56 PartOffsets.push_back(PartOffset);
57 // Signature writers supply their validator-dependent padding. Parts must
58 // remain contiguous even when a legacy signature is not dword-aligned.
59 if (!HasPrivate && !isSignaturePart(Part.Name))
60 SectionSize = alignTo(SectionSize, Align(4));
61 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
62 // The DXIL part also writes a program header, so we need to include its
63 // size when computing the offset for a part after the DXIL part.
64 if (dxbc::isProgramPart(Part.Name))
65 PartOffset += sizeof(dxbc::ProgramHeader);
66 }
67 assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
68 "Part data too large for DXContainer");
69
70 uint64_t PartStart =
71 sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
72 uint64_t FileSize = PartStart + PartOffset;
73 assert(FileSize < std::numeric_limits<uint32_t>::max() &&
74 "File size too large for DXContainer");
75
76 // Write the header.
77 W.write<char>({'D', 'X', 'B', 'C'});
78 // Write 16-bytes of 0's for the hash.
79 W.OS.write_zeros(16);
80 // Write 1.0 for file format version.
81 W.write<uint16_t>(1u);
82 W.write<uint16_t>(0u);
83 // Write the file size.
84 W.write<uint32_t>(static_cast<uint32_t>(FileSize));
85 // Write the number of parts.
86 W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
87 // Write the offsets for the part headers for each part.
88 for (uint64_t Offset : PartOffsets)
89 W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
90
91 for (const MCDXContainerPart &Part : Parts) {
92 uint64_t SectionSize = Part.Data.size();
93 unsigned Start = W.OS.tell();
94 // Write section header.
95 W.write<char>(ArrayRef<char>(Part.Name.data(), 4));
96
97 uint64_t PartSize = SectionSize;
98
99 if (dxbc::isProgramPart(Part.Name))
100 PartSize += sizeof(dxbc::ProgramHeader);
101 // Signature payloads include padding only for validator 1.7 and later.
102 bool IsPrivate = Part.Name == "PRIV";
103 bool IsSignature = isSignaturePart(Part.Name);
104 if (!IsPrivate && !IsSignature)
105 PartSize = alignTo(PartSize, Align(4));
106 W.write<uint32_t>(static_cast<uint32_t>(PartSize));
107 if (dxbc::isProgramPart(Part.Name)) {
108 dxbc::ProgramHeader Header;
109 memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
110
111 VersionTuple Version = TT.getOSVersion();
112 uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
113 uint8_t MinorVersion =
114 static_cast<uint8_t>(Version.getMinor().value_or(0));
115 Header.Version =
116 dxbc::ProgramHeader::getVersion(MajorVersion, MinorVersion);
117 if (TT.hasEnvironment())
118 Header.ShaderKind =
119 static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
120
121 // The program header's size field is in 32-bit words.
122 Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
123 memcpy(Header.Bitcode.Magic, "DXIL", 4);
124 VersionTuple DXILVersion = TT.getDXILVersion();
125 Header.Bitcode.MajorVersion = DXILVersion.getMajor();
126 Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
127 Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
128 Header.Bitcode.Size = SectionSize;
130 Header.swapBytes();
131 W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
132 sizeof(dxbc::ProgramHeader)));
133 }
134 W.write<char>(Part.Data);
135 if (!IsPrivate && !IsSignature) {
136 unsigned Size = W.OS.tell() - Start;
137 W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
138 }
139 }
140}
141
142void DXContainerObjectWriter::clearParts() {
143 Parts.clear();
144 SectionBuffers.clear();
145}
146
148 clearParts();
149 for (const MCSection &Sec : *Asm) {
150 if (shouldSkipSection(Sec.getName(), Asm->getSectionAddressSize(Sec)))
151 continue;
152
153 SectionBuffers.emplace_back();
154 raw_svector_ostream OS(SectionBuffers.back());
155 Asm->writeSectionData(OS, &Sec);
156 Parts.push_back({Sec.getName(), StringRef(SectionBuffers.back())});
157 }
158 return Parts;
159}
160
162 size_t SectionSize) {
163 if (TargetObjectWriter->shouldSkipSection(SectionName))
164 return true;
165 if (SectionName == "SRCI")
166 return true;
168}
169
171 write(W.OS, getContext().getTargetTriple());
172 clearParts();
173
174 return 0;
175}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isSignaturePart(StringRef Name)
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:580
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:577
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:149
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:191
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.