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);
71 Buffer = Buffer.
substr(Src - Buffer.
data(), MaxSize);
83Error DXContainer::parseHeader() {
84 return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header);
89 std::optional<DXILData> &DXIL = IsDebug ? this->DebugDXIL : this->DXIL;
94 const char *Current = Part.
begin();
95 dxbc::ProgramHeader Header;
98 Current +=
offsetof(dxbc::ProgramHeader,
Bitcode) + Header.Bitcode.Offset;
99 DXIL.emplace(std::make_pair(Header, Current));
103Error DXContainer::parseDebugName(StringRef Part) {
105 return parseFailed(
"more than one ILDN part is present in the file");
106 const char *Current = Part.
begin();
107 dxbc::DebugNameHeader Header;
110 Current +=
sizeof(Header);
116 if (
Name.size() != Header.NameLength)
117 return parseFailed(
"debug file name length mismatch");
118 DebugName.emplace(Header,
Name.data());
123Error DXContainer::parseShaderFeatureFlags(StringRef Part) {
124 if (ShaderFeatureFlags)
125 return parseFailed(
"More than one SFI0 part is present in the file");
126 uint64_t FlagValue = 0;
129 ShaderFeatureFlags = FlagValue;
133Error DXContainer::parseHash(StringRef Part) {
135 return parseFailed(
"More than one HASH part is present in the file");
136 dxbc::ShaderHash ReadHash;
143Error DXContainer::parseRootSignature(StringRef Part) {
145 return parseFailed(
"More than one RTS0 part is present in the file");
146 RootSignature = DirectX::RootSignature(Part);
147 if (
Error Err = RootSignature->parse())
152Error DXContainer::parsePSVInfo(StringRef Part) {
154 return parseFailed(
"More than one PSV0 part is present in the file");
155 PSVInfo = DirectX::PSVRuntimeInfo(Part);
168 return parseFailed(
"Signature parameters extend beyond the part boundary");
175 for (
const auto &Param : Parameters) {
176 if (Param.NameOffset < StringTableOffset)
177 return parseFailed(
"Invalid parameter name offset: name starts before "
178 "the first name offset");
179 if (Param.NameOffset - StringTableOffset > StringTable.size())
180 return parseFailed(
"Invalid parameter name offset: name starts after the "
181 "end of the part data");
188 return parseFailed(
"more than one VERS part is present in the file");
189 const char *Current = Part.
begin();
193 Current +=
sizeof(Header);
196 return parseFailed(
"Incorrect shader compiler version flags combination");
199 const char *Prev = Current;
201 CommitSha,
"CommitSha"))
205 Header.ContentSizeInBytes - (Current - Prev),
206 CustomVersionString,
"CustomVersionString"))
209 VersionInfo.emplace();
210 VersionInfo->Parameters = Header;
211 VersionInfo->CommitSha = CommitSha;
212 VersionInfo->CustomVersionString = CustomVersionString;
216Error DXContainer::parsePartOffsets() {
220 for (
uint32_t Part = 0; Part < Header.PartCount; ++Part) {
224 if (PartOffset < LastOffset)
227 "Part offset for part {0} begins before the previous part ends",
231 if (PartOffset >=
Data.getBufferSize())
232 return parseFailed(
"Part offset points beyond boundary of the file");
239 return parseFailed(
"File not large enough to read part name");
240 PartOffsets.push_back(PartOffset);
247 Data.getBufferStart() + PartOffset + 4,
248 PartSize,
"part size"))
250 StringRef PartData =
Data.getBuffer().substr(PartDataStart, PartSize);
251 LastOffset = PartOffset + PartSize;
253 case dxbc::PartType::DXIL:
254 case dxbc::PartType::ILDB:
255 if (
Error Err = parseDXILHeader(PT, PartData))
258 case dxbc::PartType::ILDN:
259 if (
Error Err = parseDebugName(PartData))
262 case dxbc::PartType::SFI0:
263 if (
Error Err = parseShaderFeatureFlags(PartData))
266 case dxbc::PartType::HASH:
267 if (
Error Err = parseHash(PartData))
270 case dxbc::PartType::PSV0:
271 if (
Error Err = parsePSVInfo(PartData))
274 case dxbc::PartType::ISG1:
275 if (
Error Err = InputSignature.initialize(PartData))
278 case dxbc::PartType::OSG1:
279 if (
Error Err = OutputSignature.initialize(PartData))
282 case dxbc::PartType::PSG1:
283 if (
Error Err = PatchConstantSignature.initialize(PartData))
288 case dxbc::PartType::RTS0:
289 if (
Error Err = parseRootSignature(PartData))
292 case dxbc::PartType::VERS:
293 if (
Error Err = parseCompilerVersionInfo(PartData))
299 if (DXIL && DebugDXIL &&
300 DXIL->first.ShaderKind != DebugDXIL->first.ShaderKind)
302 "ILDB part shader kind does not match DXIL part shader kind");
307 std::optional<uint16_t> ShaderKind = getShaderKind();
309 return parseFailed(
"cannot fully parse pipeline state validation "
310 "information without DXIL or ILDB part");
311 if (
Error Err = PSVInfo->parse(*ShaderKind))
318 DXContainer Container(Object);
319 if (
Error Err = Container.parseHeader())
320 return std::move(Err);
321 if (
Error Err = Container.parsePartOffsets())
322 return std::move(Err);
326void DXContainer::PartIterator::updateIteratorImpl(
const uint32_t Offset) {
327 StringRef Buffer = Container.Data.getBuffer();
334 IteratorState.Offset =
Offset;
338 const char *Current = PartData.begin();
341 if (PartData.size() < 6 *
sizeof(
uint32_t))
343 "Invalid root signature, insufficient space for header.");
352 RootParametersOffset =
360 StaticSamplersOffset =
367 ParametersHeaders.Data = PartData.substr(
368 RootParametersOffset,
371 StaticSamplers.Stride = (Version <= 2)
375 StaticSamplers.Data = PartData.substr(StaticSamplersOffset,
376 static_cast<size_t>(NumStaticSamplers) *
377 StaticSamplers.Stride);
385 const char *Current = Data.begin();
392 if (PSVInfoData.
size() < Size)
394 "Pipeline state data extends beyond the bounds of the part");
401 if (PSVVersion == 3) {
402 v3::RuntimeInfo Info;
406 Info.swapBytes(ShaderStage);
408 }
else if (PSVVersion == 2) {
409 v2::RuntimeInfo Info;
413 Info.swapBytes(ShaderStage);
415 }
else if (PSVVersion == 1) {
416 v1::RuntimeInfo Info;
420 Info.swapBytes(ShaderStage);
422 }
else if (PSVVersion == 0) {
423 v0::RuntimeInfo Info;
427 Info.swapBytes(ShaderStage);
431 "Cannot read PSV Runtime Info, unsupported PSV version.");
440 if (ResourceCount > 0) {
445 size_t BindingDataSize = Resources.Stride * ResourceCount;
446 Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
448 if (Resources.Data.size() < BindingDataSize)
450 "Resource binding data extends beyond the bounds of the part");
452 Current += BindingDataSize;
454 Resources.Stride =
sizeof(v2::ResourceBindInfo);
461 Current =
reinterpret_cast<const char *
>(
462 alignTo<4>(
reinterpret_cast<uintptr_t
>(Current)));
467 if (StringTableSize % 4 != 0)
470 StringTable =
StringRef(Current, StringTableSize);
472 Current += StringTableSize;
474 uint32_t SemanticIndexTableSize = 0;
479 SemanticIndexTable.reserve(SemanticIndexTableSize);
480 for (
uint32_t I = 0;
I < SemanticIndexTableSize; ++
I) {
485 SemanticIndexTable.push_back(Index);
499 SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
500 SigInputElements.Stride;
502 if (Data.end() - Current <
505 "Signature elements extend beyond the size of the part");
507 size_t InputSize = SigInputElements.Stride * InputCount;
508 SigInputElements.Data = Data.substr(Current - Data.begin(), InputSize);
509 Current += InputSize;
511 size_t OutputSize = SigOutputElements.Stride * OutputCount;
512 SigOutputElements.Data = Data.substr(Current - Data.begin(), OutputSize);
513 Current += OutputSize;
515 size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
516 SigPatchOrPrimElements.Data = Data.substr(Current - Data.begin(), PSize);
529 return maskDwordSize(
Y) *
X * 4;
538 maskDwordSize(
static_cast<uint32_t>(OutputVectorCounts[
I]));
539 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
540 OutputVectorMasks[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
544 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0) {
545 uint32_t NumDwords = maskDwordSize(PatchConstOrPrimVectorCount);
546 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
547 PatchOrPrimMasks.Data = Data.substr(Current - Data.begin(), NumBytes);
554 if (InputVectorCount == 0 || OutputVectorCounts[
I] == 0)
556 uint32_t NumDwords = mapTableSize(InputVectorCount, OutputVectorCounts[
I]);
557 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
558 InputOutputMap[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
563 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0 &&
564 InputVectorCount > 0) {
566 mapTableSize(InputVectorCount, PatchConstOrPrimVectorCount);
567 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
568 InputPatchMap.Data = Data.substr(Current - Data.begin(), NumBytes);
573 if (ShaderStage ==
Triple::Domain && PatchConstOrPrimVectorCount > 0 &&
574 OutputVectorCounts[0] > 0) {
576 mapTableSize(PatchConstOrPrimVectorCount, OutputVectorCounts[0]);
577 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
578 PatchOutputMap.Data = Data.substr(Current - Data.begin(), NumBytes);
586 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
587 return P->SigInputElements;
588 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
589 return P->SigInputElements;
590 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
591 return P->SigInputElements;
596 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
597 return P->SigOutputElements;
598 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
599 return P->SigOutputElements;
600 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
601 return P->SigOutputElements;
606 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
607 return P->SigPatchOrPrimElements;
608 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
609 return P->SigPatchOrPrimElements;
610 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
611 return P->SigPatchOrPrimElements;
622 OS <<
"DXContainer does not support " << FeatureString;
635Expected<section_iterator>
663 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
664 if (It == Parts.end())
668 Sec.
p =
reinterpret_cast<uintptr_t
>(It);
673 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
678 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
683 return (Sec.
p -
reinterpret_cast<uintptr_t
>(Parts.begin())) /
684 sizeof(PartIterator);
688 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
689 return It->Data.size();
693 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
755 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.begin());
760 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.end());
767 return "DirectX Container";
790 return ExC.takeError();
792 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