24 return Src < Buffer.
begin() || Src +
Size > Buffer.
end();
31 return parseFailed(
"Reading structure out of file bounds");
33 memcpy(&Struct, Src,
sizeof(
T));
42 Twine Str =
"structure") {
43 static_assert(std::is_integral_v<T>,
44 "Cannot call readInteger on non-integral type.");
52 if (
reinterpret_cast<uintptr_t
>(Src) %
alignof(
T) != 0)
53 memcpy(
reinterpret_cast<char *
>(&Val), Src,
sizeof(
T));
55 Val = *
reinterpret_cast<const T *
>(Src);
68 Buffer = Buffer.
substr(Src - Buffer.
data(), MaxSize);
80Error DXContainer::parseHeader() {
81 return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header);
86 std::optional<DXILData> &DXIL = IsDebug ? this->DebugDXIL : this->DXIL;
91 const char *Current = Part.
begin();
92 dxbc::ProgramHeader Header;
95 Current +=
offsetof(dxbc::ProgramHeader,
Bitcode) + Header.Bitcode.Offset;
96 DXIL.emplace(std::make_pair(Header, Current));
100Error DXContainer::parseDebugName(StringRef Part) {
102 return parseFailed(
"more than one ILDN part is present in the file");
103 const char *Current = Part.
begin();
104 dxbc::DebugNameHeader Header;
107 Current +=
sizeof(Header);
113 if (
Name.size() != Header.NameLength)
114 return parseFailed(
"debug file name length mismatch");
115 DebugName.emplace(Header,
Name.data());
120Error DXContainer::parseShaderFeatureFlags(StringRef Part) {
121 if (ShaderFeatureFlags)
122 return parseFailed(
"More than one SFI0 part is present in the file");
123 uint64_t FlagValue = 0;
126 ShaderFeatureFlags = FlagValue;
130Error DXContainer::parseHash(StringRef Part) {
132 return parseFailed(
"More than one HASH part is present in the file");
133 dxbc::ShaderHash ReadHash;
140Error DXContainer::parseRootSignature(StringRef Part) {
142 return parseFailed(
"More than one RTS0 part is present in the file");
143 RootSignature = DirectX::RootSignature(Part);
144 if (
Error Err = RootSignature->parse())
149Error DXContainer::parsePSVInfo(StringRef Part) {
151 return parseFailed(
"More than one PSV0 part is present in the file");
152 PSVInfo = DirectX::PSVRuntimeInfo(Part);
165 return parseFailed(
"Signature parameters extend beyond the part boundary");
172 for (
const auto &Param : Parameters) {
173 if (Param.NameOffset < StringTableOffset)
174 return parseFailed(
"Invalid parameter name offset: name starts before "
175 "the first name offset");
176 if (Param.NameOffset - StringTableOffset > StringTable.size())
177 return parseFailed(
"Invalid parameter name offset: name starts after the "
178 "end of the part data");
183Error DXContainer::parsePartOffsets() {
187 for (
uint32_t Part = 0; Part < Header.PartCount; ++Part) {
191 if (PartOffset < LastOffset)
194 "Part offset for part {0} begins before the previous part ends",
198 if (PartOffset >=
Data.getBufferSize())
199 return parseFailed(
"Part offset points beyond boundary of the file");
206 return parseFailed(
"File not large enough to read part name");
207 PartOffsets.push_back(PartOffset);
214 Data.getBufferStart() + PartOffset + 4,
215 PartSize,
"part size"))
217 StringRef PartData =
Data.getBuffer().substr(PartDataStart, PartSize);
218 LastOffset = PartOffset + PartSize;
220 case dxbc::PartType::DXIL:
221 case dxbc::PartType::ILDB:
222 if (
Error Err = parseDXILHeader(PT, PartData))
225 case dxbc::PartType::ILDN:
226 if (
Error Err = parseDebugName(PartData))
229 case dxbc::PartType::SFI0:
230 if (
Error Err = parseShaderFeatureFlags(PartData))
233 case dxbc::PartType::HASH:
234 if (
Error Err = parseHash(PartData))
237 case dxbc::PartType::PSV0:
238 if (
Error Err = parsePSVInfo(PartData))
241 case dxbc::PartType::ISG1:
242 if (
Error Err = InputSignature.initialize(PartData))
245 case dxbc::PartType::OSG1:
246 if (
Error Err = OutputSignature.initialize(PartData))
249 case dxbc::PartType::PSG1:
250 if (
Error Err = PatchConstantSignature.initialize(PartData))
255 case dxbc::PartType::RTS0:
256 if (
Error Err = parseRootSignature(PartData))
262 if (DXIL && DebugDXIL &&
263 DXIL->first.ShaderKind != DebugDXIL->first.ShaderKind)
265 "ILDB part shader kind does not match DXIL part shader kind");
270 std::optional<uint16_t> ShaderKind = getShaderKind();
272 return parseFailed(
"cannot fully parse pipeline state validation "
273 "information without DXIL or ILDB part");
274 if (
Error Err = PSVInfo->parse(*ShaderKind))
281 DXContainer Container(Object);
282 if (
Error Err = Container.parseHeader())
283 return std::move(Err);
284 if (
Error Err = Container.parsePartOffsets())
285 return std::move(Err);
289void DXContainer::PartIterator::updateIteratorImpl(
const uint32_t Offset) {
290 StringRef Buffer = Container.Data.getBuffer();
297 IteratorState.Offset =
Offset;
301 const char *Current = PartData.begin();
304 if (PartData.size() < 6 *
sizeof(
uint32_t))
306 "Invalid root signature, insufficient space for header.");
315 RootParametersOffset =
323 StaticSamplersOffset =
330 ParametersHeaders.Data = PartData.substr(
331 RootParametersOffset,
334 StaticSamplers.Stride = (Version <= 2)
338 StaticSamplers.Data = PartData.substr(StaticSamplersOffset,
339 static_cast<size_t>(NumStaticSamplers) *
340 StaticSamplers.Stride);
348 const char *Current = Data.begin();
355 if (PSVInfoData.
size() < Size)
357 "Pipeline state data extends beyond the bounds of the part");
364 if (PSVVersion == 3) {
365 v3::RuntimeInfo Info;
369 Info.swapBytes(ShaderStage);
371 }
else if (PSVVersion == 2) {
372 v2::RuntimeInfo Info;
376 Info.swapBytes(ShaderStage);
378 }
else if (PSVVersion == 1) {
379 v1::RuntimeInfo Info;
383 Info.swapBytes(ShaderStage);
385 }
else if (PSVVersion == 0) {
386 v0::RuntimeInfo Info;
390 Info.swapBytes(ShaderStage);
394 "Cannot read PSV Runtime Info, unsupported PSV version.");
403 if (ResourceCount > 0) {
408 size_t BindingDataSize = Resources.Stride * ResourceCount;
409 Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
411 if (Resources.Data.size() < BindingDataSize)
413 "Resource binding data extends beyond the bounds of the part");
415 Current += BindingDataSize;
417 Resources.Stride =
sizeof(v2::ResourceBindInfo);
424 Current =
reinterpret_cast<const char *
>(
425 alignTo<4>(
reinterpret_cast<uintptr_t
>(Current)));
430 if (StringTableSize % 4 != 0)
433 StringTable =
StringRef(Current, StringTableSize);
435 Current += StringTableSize;
437 uint32_t SemanticIndexTableSize = 0;
442 SemanticIndexTable.reserve(SemanticIndexTableSize);
443 for (
uint32_t I = 0;
I < SemanticIndexTableSize; ++
I) {
448 SemanticIndexTable.push_back(Index);
462 SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
463 SigInputElements.Stride;
465 if (Data.end() - Current <
468 "Signature elements extend beyond the size of the part");
470 size_t InputSize = SigInputElements.Stride * InputCount;
471 SigInputElements.Data = Data.substr(Current - Data.begin(), InputSize);
472 Current += InputSize;
474 size_t OutputSize = SigOutputElements.Stride * OutputCount;
475 SigOutputElements.Data = Data.substr(Current - Data.begin(), OutputSize);
476 Current += OutputSize;
478 size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
479 SigPatchOrPrimElements.Data = Data.substr(Current - Data.begin(), PSize);
492 return maskDwordSize(
Y) *
X * 4;
501 maskDwordSize(
static_cast<uint32_t>(OutputVectorCounts[
I]));
502 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
503 OutputVectorMasks[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
507 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0) {
508 uint32_t NumDwords = maskDwordSize(PatchConstOrPrimVectorCount);
509 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
510 PatchOrPrimMasks.Data = Data.substr(Current - Data.begin(), NumBytes);
517 if (InputVectorCount == 0 || OutputVectorCounts[
I] == 0)
519 uint32_t NumDwords = mapTableSize(InputVectorCount, OutputVectorCounts[
I]);
520 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
521 InputOutputMap[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
526 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0 &&
527 InputVectorCount > 0) {
529 mapTableSize(InputVectorCount, PatchConstOrPrimVectorCount);
530 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
531 InputPatchMap.Data = Data.substr(Current - Data.begin(), NumBytes);
536 if (ShaderStage ==
Triple::Domain && PatchConstOrPrimVectorCount > 0 &&
537 OutputVectorCounts[0] > 0) {
539 mapTableSize(PatchConstOrPrimVectorCount, OutputVectorCounts[0]);
540 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
541 PatchOutputMap.Data = Data.substr(Current - Data.begin(), NumBytes);
549 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
550 return P->SigInputElements;
551 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
552 return P->SigInputElements;
553 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
554 return P->SigInputElements;
559 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
560 return P->SigOutputElements;
561 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
562 return P->SigOutputElements;
563 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
564 return P->SigOutputElements;
569 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
570 return P->SigPatchOrPrimElements;
571 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
572 return P->SigPatchOrPrimElements;
573 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
574 return P->SigPatchOrPrimElements;
585 OS <<
"DXContainer does not support " << FeatureString;
598Expected<section_iterator>
626 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
627 if (It == Parts.end())
631 Sec.
p =
reinterpret_cast<uintptr_t
>(It);
636 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
641 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
646 return (Sec.
p -
reinterpret_cast<uintptr_t
>(Parts.begin())) /
647 sizeof(PartIterator);
651 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
652 return It->Data.size();
656 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
718 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.begin());
723 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.end());
730 return "DirectX Container";
753 return ExC.takeError();
755 return std::move(Obj);
#define offsetof(TYPE, MEMBER)
static Error readString(StringRef Buffer, const char *&Src, size_t MaxSize, StringRef &Val, Twine Desc)
static Error parseFailed(const Twine &Msg)
static Error readStruct(StringRef Buffer, const char *Src, T &Struct)
static bool readIsOutOfBounds(StringRef Buffer, const char *Src, size_t Size)
static Error readInteger(StringRef Buffer, const char *Src, T &Val, Twine Str="structure")
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
DXNotSupportedError(StringRef S)
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Base class for user error types.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
static constexpr size_t npos
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr size_t size() const
Get the string size.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Manages the enabling and disabling of subtarget specific features.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Triple::ArchType getArch() const override
bool isSectionCompressed(DataRefImpl Sec) const override
section_iterator section_end() const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
section_iterator section_begin() const override
bool isSectionVirtual(DataRefImpl Sec) const override
void moveRelocationNext(DataRefImpl &Rel) const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
void moveSectionNext(DataRefImpl &Sec) const override
bool isSectionData(DataRefImpl Sec) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
StringRef getFileFormatName() const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
uint64_t getRelocationType(DataRefImpl Rel) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
bool isSectionBSS(DataRefImpl Sec) const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool isSectionText(DataRefImpl Sec) const override
Expected< SubtargetFeatures > getFeatures() const override
Expected< StringRef > getSymbolName(DataRefImpl) const override
static LLVM_ABI Expected< DXContainer > create(MemoryBufferRef Object)
ArrayRef< uint8_t > getOutputVectorCounts() const
uint8_t getPatchConstOrPrimVectorCount() const
LLVM_ABI uint8_t getSigInputCount() const
LLVM_ABI uint8_t getSigPatchOrPrimCount() const
uint32_t getVersion() const
LLVM_ABI Error parse(uint16_t ShaderKind)
uint8_t getInputVectorCount() const
LLVM_ABI uint8_t getSigOutputCount() const
LLVM_ABI Error initialize(StringRef Part)
static Expected< std::unique_ptr< DXContainerObjectFile > > createDXContainerObjectFile(MemoryBufferRef Object)
friend class RelocationRef
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI PartType parsePartType(StringRef S)
Triple::EnvironmentType getShaderStage(uint32_t Kind)
bool isDebugProgramPart(PartType PT)
const char * getProgramPartName(bool IsDebug)
static Error parseFailed(const Twine &Msg)
content_iterator< SectionRef > section_iterator
content_iterator< RelocationRef > relocation_iterator
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
constexpr bool IsBigEndianHost
void swapByteOrder(T &Value)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
FunctionAddr VTableAddr uintptr_t uintptr_t Data