24 return !Src ||
Size >
static_cast<size_t>(Buffer.
end() - Src);
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);
71 Buffer = Buffer.
substr(Src - Buffer.
data(), MaxSize);
83Error DXContainer::parseHeader() {
84 if (
Error Err =
readStruct(Data.getBuffer(), Data.getBuffer().data(), Header))
86 if (StringRef(
reinterpret_cast<char *
>(Header.Magic), 4) !=
"DXBC")
93 std::optional<DXILData> &DXIL = IsDebug ? this->DebugDXIL : this->DXIL;
98 const char *Current = Part.
begin();
99 dxbc::ProgramHeader Header;
102 Current +=
offsetof(dxbc::ProgramHeader,
Bitcode) + Header.Bitcode.Offset;
103 DXIL.emplace(std::make_pair(Header, Current));
107Error DXContainer::parseDebugName(StringRef Part) {
109 return parseFailed(
"more than one ILDN part is present in the file");
110 const char *Current = Part.
begin();
111 dxbc::DebugNameHeader Header;
114 Current +=
sizeof(Header);
120 if (
Name.size() != Header.NameLength)
121 return parseFailed(
"debug file name length mismatch");
122 DebugName.emplace(Header,
Name.data());
127Error DXContainer::parseShaderFeatureFlags(StringRef Part) {
128 if (ShaderFeatureFlags)
129 return parseFailed(
"More than one SFI0 part is present in the file");
130 uint64_t FlagValue = 0;
133 ShaderFeatureFlags = FlagValue;
137Error DXContainer::parseHash(StringRef Part) {
139 return parseFailed(
"More than one HASH part is present in the file");
140 dxbc::ShaderHash ReadHash;
147Error DXContainer::parseRootSignature(StringRef Part) {
149 return parseFailed(
"More than one RTS0 part is present in the file");
150 RootSignature = DirectX::RootSignature(Part);
151 if (
Error Err = RootSignature->parse())
156Error DXContainer::parsePSVInfo(StringRef Part) {
158 return parseFailed(
"More than one PSV0 part is present in the file");
159 PSVInfo = DirectX::PSVRuntimeInfo(Part);
172 return parseFailed(
"Signature parameters extend beyond the part boundary");
179 for (
const auto &Param : Parameters) {
180 if (Param.NameOffset < StringTableOffset)
181 return parseFailed(
"Invalid parameter name offset: name starts before "
182 "the first name offset");
183 if (Param.NameOffset - StringTableOffset > StringTable.size())
184 return parseFailed(
"Invalid parameter name offset: name starts after the "
185 "end of the part data");
192 return parseFailed(
"more than one VERS part is present in the file");
193 const char *Current = Part.
begin();
197 Current +=
sizeof(Header);
200 return parseFailed(
"Incorrect shader compiler version flags combination");
203 const char *Prev = Current;
205 CommitSha,
"CommitSha"))
209 Header.ContentSizeInBytes - (Current - Prev),
210 CustomVersionString,
"CustomVersionString"))
213 VersionInfo.emplace();
214 VersionInfo->Parameters = Header;
215 VersionInfo->CommitSha = CommitSha;
216 VersionInfo->CustomVersionString = CustomVersionString;
220Error DXContainer::parsePartOffsets() {
224 for (
uint32_t Part = 0; Part < Header.PartCount; ++Part) {
228 if (PartOffset < LastOffset)
231 "Part offset for part {0} begins before the previous part ends",
235 if (PartOffset >=
Data.getBufferSize())
236 return parseFailed(
"Part offset points beyond boundary of the file");
243 return parseFailed(
"File not large enough to read part name");
244 PartOffsets.push_back(PartOffset);
251 Data.getBufferStart() + PartOffset + 4,
252 PartSize,
"part size"))
254 StringRef PartData =
Data.getBuffer().substr(PartDataStart, PartSize);
255 LastOffset = PartOffset + PartSize;
257 case dxbc::PartType::DXIL:
258 case dxbc::PartType::ILDB:
259 if (
Error Err = parseDXILHeader(PT, PartData))
262 case dxbc::PartType::ILDN:
263 if (
Error Err = parseDebugName(PartData))
266 case dxbc::PartType::SFI0:
267 if (
Error Err = parseShaderFeatureFlags(PartData))
270 case dxbc::PartType::HASH:
271 if (
Error Err = parseHash(PartData))
274 case dxbc::PartType::PSV0:
275 if (
Error Err = parsePSVInfo(PartData))
278 case dxbc::PartType::ISG1:
279 if (
Error Err = InputSignature.initialize(PartData))
282 case dxbc::PartType::OSG1:
283 if (
Error Err = OutputSignature.initialize(PartData))
286 case dxbc::PartType::PSG1:
287 if (
Error Err = PatchConstantSignature.initialize(PartData))
292 case dxbc::PartType::RTS0:
293 if (
Error Err = parseRootSignature(PartData))
296 case dxbc::PartType::VERS:
297 if (
Error Err = parseCompilerVersionInfo(PartData))
303 if (DXIL && DebugDXIL &&
304 DXIL->first.ShaderKind != DebugDXIL->first.ShaderKind)
306 "ILDB part shader kind does not match DXIL part shader kind");
311 std::optional<uint16_t> ShaderKind = getShaderKind();
313 return parseFailed(
"cannot fully parse pipeline state validation "
314 "information without DXIL or ILDB part");
315 if (
Error Err = PSVInfo->parse(*ShaderKind))
322 DXContainer Container(Object);
323 if (
Error Err = Container.parseHeader())
324 return std::move(Err);
325 if (
Error Err = Container.parsePartOffsets())
326 return std::move(Err);
330void DXContainer::PartIterator::updateIteratorImpl(
const uint32_t Offset) {
331 StringRef Buffer = Container.Data.getBuffer();
338 IteratorState.Offset =
Offset;
342 const char *Current = PartData.begin();
345 if (PartData.size() < 6 *
sizeof(
uint32_t))
347 "Invalid root signature, insufficient space for header.");
356 RootParametersOffset =
364 StaticSamplersOffset =
371 ParametersHeaders.Data = PartData.substr(
372 RootParametersOffset,
375 StaticSamplers.Stride = (Version <= 2)
379 StaticSamplers.Data = PartData.substr(StaticSamplersOffset,
380 static_cast<size_t>(NumStaticSamplers) *
381 StaticSamplers.Stride);
389 const char *Current = Data.begin();
396 if (PSVInfoData.
size() < Size)
398 "Pipeline state data extends beyond the bounds of the part");
405 if (PSVVersion == 3) {
406 v3::RuntimeInfo Info;
410 Info.swapBytes(ShaderStage);
412 }
else if (PSVVersion == 2) {
413 v2::RuntimeInfo Info;
417 Info.swapBytes(ShaderStage);
419 }
else if (PSVVersion == 1) {
420 v1::RuntimeInfo Info;
424 Info.swapBytes(ShaderStage);
426 }
else if (PSVVersion == 0) {
427 v0::RuntimeInfo Info;
431 Info.swapBytes(ShaderStage);
435 "Cannot read PSV Runtime Info, unsupported PSV version.");
444 if (ResourceCount > 0) {
449 size_t BindingDataSize = Resources.Stride * ResourceCount;
450 Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
452 if (Resources.Data.size() < BindingDataSize)
454 "Resource binding data extends beyond the bounds of the part");
456 Current += BindingDataSize;
458 Resources.Stride =
sizeof(v2::ResourceBindInfo);
465 Current =
reinterpret_cast<const char *
>(
466 alignTo<4>(
reinterpret_cast<uintptr_t
>(Current)));
471 if (StringTableSize % 4 != 0)
474 StringTable =
StringRef(Current, StringTableSize);
476 Current += StringTableSize;
478 uint32_t SemanticIndexTableSize = 0;
483 SemanticIndexTable.reserve(SemanticIndexTableSize);
484 for (
uint32_t I = 0;
I < SemanticIndexTableSize; ++
I) {
489 SemanticIndexTable.push_back(Index);
503 SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
504 SigInputElements.Stride;
506 if (Data.end() - Current <
509 "Signature elements extend beyond the size of the part");
511 size_t InputSize = SigInputElements.Stride * InputCount;
512 SigInputElements.Data = Data.substr(Current - Data.begin(), InputSize);
513 Current += InputSize;
515 size_t OutputSize = SigOutputElements.Stride * OutputCount;
516 SigOutputElements.Data = Data.substr(Current - Data.begin(), OutputSize);
517 Current += OutputSize;
519 size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
520 SigPatchOrPrimElements.Data = Data.substr(Current - Data.begin(), PSize);
533 return maskDwordSize(
Y) *
X * 4;
542 maskDwordSize(
static_cast<uint32_t>(OutputVectorCounts[
I]));
543 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
544 OutputVectorMasks[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
548 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0) {
549 uint32_t NumDwords = maskDwordSize(PatchConstOrPrimVectorCount);
550 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
551 PatchOrPrimMasks.Data = Data.substr(Current - Data.begin(), NumBytes);
558 if (InputVectorCount == 0 || OutputVectorCounts[
I] == 0)
560 uint32_t NumDwords = mapTableSize(InputVectorCount, OutputVectorCounts[
I]);
561 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
562 InputOutputMap[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
567 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0 &&
568 InputVectorCount > 0) {
570 mapTableSize(InputVectorCount, PatchConstOrPrimVectorCount);
571 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
572 InputPatchMap.Data = Data.substr(Current - Data.begin(), NumBytes);
577 if (ShaderStage ==
Triple::Domain && PatchConstOrPrimVectorCount > 0 &&
578 OutputVectorCounts[0] > 0) {
580 mapTableSize(PatchConstOrPrimVectorCount, OutputVectorCounts[0]);
581 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
582 PatchOutputMap.Data = Data.substr(Current - Data.begin(), NumBytes);
590 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
591 return P->SigInputElements;
592 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
593 return P->SigInputElements;
594 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
595 return P->SigInputElements;
600 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
601 return P->SigOutputElements;
602 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
603 return P->SigOutputElements;
604 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
605 return P->SigOutputElements;
610 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
611 return P->SigPatchOrPrimElements;
612 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
613 return P->SigPatchOrPrimElements;
614 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
615 return P->SigPatchOrPrimElements;
626 OS <<
"DXContainer does not support " << FeatureString;
639Expected<section_iterator>
667 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
668 if (It == Parts.end())
672 Sec.
p =
reinterpret_cast<uintptr_t
>(It);
677 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
682 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
687 return (Sec.
p -
reinterpret_cast<uintptr_t
>(Parts.begin())) /
688 sizeof(PartIterator);
692 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
693 return It->Data.size();
697 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
759 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.begin());
764 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.end());
771 return "DirectX Container";
794 return ExC.takeError();
796 return std::move(Obj);
#define offsetof(TYPE, MEMBER)
static Error readString(StringRef Buffer, const char *&Src, size_t MaxSize, StringRef &Val, Twine Desc)
Read a null-terminated string at the position Src from Buffer, with maximum byte size of MaxSize (inc...
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)
bool isValidCompilerVersionFlags(uint32_t V)
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.
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
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