26 if (Src < Buffer.
begin() || Src +
sizeof(
T) > Buffer.
end())
27 return parseFailed(
"Reading structure out of file bounds");
29 memcpy(&
Struct, Src,
sizeof(
T));
38 Twine Str =
"structure") {
39 static_assert(std::is_integral_v<T>,
40 "Cannot call readInteger on non-integral type.");
42 if (Src < Buffer.
begin() || Src +
sizeof(
T) > Buffer.
end())
48 if (
reinterpret_cast<uintptr_t
>(Src) %
alignof(
T) != 0)
49 memcpy(
reinterpret_cast<char *
>(&Val), Src,
sizeof(
T));
51 Val = *
reinterpret_cast<const T *
>(Src);
60Error DXContainer::parseHeader() {
61 return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header);
64Error DXContainer::parseDXILHeader(StringRef Part) {
66 return parseFailed(
"More than one DXIL part is present in the file");
67 const char *Current = Part.
begin();
68 dxbc::ProgramHeader Header;
71 Current +=
offsetof(dxbc::ProgramHeader,
Bitcode) + Header.Bitcode.Offset;
72 DXIL.emplace(std::make_pair(Header, Current));
76Error DXContainer::parseShaderFeatureFlags(StringRef Part) {
77 if (ShaderFeatureFlags)
78 return parseFailed(
"More than one SFI0 part is present in the file");
79 uint64_t FlagValue = 0;
82 ShaderFeatureFlags = FlagValue;
86Error DXContainer::parseHash(StringRef Part) {
88 return parseFailed(
"More than one HASH part is present in the file");
89 dxbc::ShaderHash ReadHash;
96Error DXContainer::parseRootSignature(StringRef Part) {
98 return parseFailed(
"More than one RTS0 part is present in the file");
99 RootSignature = DirectX::RootSignature(Part);
100 if (
Error Err = RootSignature->parse())
105Error DXContainer::parsePSVInfo(StringRef Part) {
107 return parseFailed(
"More than one PSV0 part is present in the file");
108 PSVInfo = DirectX::PSVRuntimeInfo(Part);
121 return parseFailed(
"Signature parameters extend beyond the part boundary");
128 for (
const auto &Param : Parameters) {
129 if (Param.NameOffset < StringTableOffset)
130 return parseFailed(
"Invalid parameter name offset: name starts before "
131 "the first name offset");
132 if (Param.NameOffset - StringTableOffset > StringTable.size())
133 return parseFailed(
"Invalid parameter name offset: name starts after the "
134 "end of the part data");
139Error DXContainer::parsePartOffsets() {
143 for (
uint32_t Part = 0; Part < Header.PartCount; ++Part) {
147 if (PartOffset < LastOffset)
150 "Part offset for part {0} begins before the previous part ends",
154 if (PartOffset >=
Data.getBufferSize())
155 return parseFailed(
"Part offset points beyond boundary of the file");
162 return parseFailed(
"File not large enough to read part name");
163 PartOffsets.push_back(PartOffset);
170 Data.getBufferStart() + PartOffset + 4,
171 PartSize,
"part size"))
173 StringRef PartData =
Data.getBuffer().substr(PartDataStart, PartSize);
174 LastOffset = PartOffset + PartSize;
176 case dxbc::PartType::DXIL:
177 if (
Error Err = parseDXILHeader(PartData))
180 case dxbc::PartType::SFI0:
181 if (
Error Err = parseShaderFeatureFlags(PartData))
184 case dxbc::PartType::HASH:
185 if (
Error Err = parseHash(PartData))
188 case dxbc::PartType::PSV0:
189 if (
Error Err = parsePSVInfo(PartData))
192 case dxbc::PartType::ISG1:
193 if (
Error Err = InputSignature.initialize(PartData))
196 case dxbc::PartType::OSG1:
197 if (
Error Err = OutputSignature.initialize(PartData))
200 case dxbc::PartType::PSG1:
201 if (
Error Err = PatchConstantSignature.initialize(PartData))
206 case dxbc::PartType::RTS0:
207 if (
Error Err = parseRootSignature(PartData))
217 return parseFailed(
"Cannot fully parse pipeline state validation "
218 "information without DXIL part.");
219 if (
Error Err = PSVInfo->parse(DXIL->first.ShaderKind))
226 DXContainer Container(Object);
227 if (
Error Err = Container.parseHeader())
228 return std::move(Err);
229 if (
Error Err = Container.parsePartOffsets())
230 return std::move(Err);
234void DXContainer::PartIterator::updateIteratorImpl(
const uint32_t Offset) {
235 StringRef Buffer = Container.Data.getBuffer();
242 IteratorState.Offset =
Offset;
246 const char *Current = PartData.begin();
249 if (PartData.size() < 6 *
sizeof(
uint32_t))
251 "Invalid root signature, insufficient space for header.");
260 RootParametersOffset =
268 StaticSamplersOffset =
275 ParametersHeaders.Data = PartData.substr(
276 RootParametersOffset,
280 StaticSamplers.Data = PartData.substr(
281 StaticSamplersOffset,
290 const char *Current = Data.begin();
297 if (PSVInfoData.
size() < Size)
299 "Pipeline state data extends beyond the bounds of the part");
306 if (PSVVersion == 3) {
307 v3::RuntimeInfo Info;
311 Info.swapBytes(ShaderStage);
313 }
else if (PSVVersion == 2) {
314 v2::RuntimeInfo Info;
318 Info.swapBytes(ShaderStage);
320 }
else if (PSVVersion == 1) {
321 v1::RuntimeInfo Info;
325 Info.swapBytes(ShaderStage);
327 }
else if (PSVVersion == 0) {
328 v0::RuntimeInfo Info;
332 Info.swapBytes(ShaderStage);
336 "Cannot read PSV Runtime Info, unsupported PSV version.");
345 if (ResourceCount > 0) {
350 size_t BindingDataSize = Resources.Stride * ResourceCount;
351 Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
353 if (Resources.Data.size() < BindingDataSize)
355 "Resource binding data extends beyond the bounds of the part");
357 Current += BindingDataSize;
359 Resources.Stride =
sizeof(v2::ResourceBindInfo);
366 Current =
reinterpret_cast<const char *
>(
367 alignTo<4>(
reinterpret_cast<uintptr_t
>(Current)));
372 if (StringTableSize % 4 != 0)
375 StringTable =
StringRef(Current, StringTableSize);
377 Current += StringTableSize;
379 uint32_t SemanticIndexTableSize = 0;
384 SemanticIndexTable.reserve(SemanticIndexTableSize);
385 for (
uint32_t I = 0;
I < SemanticIndexTableSize; ++
I) {
390 SemanticIndexTable.push_back(Index);
404 SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
405 SigInputElements.Stride;
407 if (Data.end() - Current <
410 "Signature elements extend beyond the size of the part");
412 size_t InputSize = SigInputElements.Stride * InputCount;
413 SigInputElements.Data = Data.substr(Current - Data.begin(), InputSize);
414 Current += InputSize;
416 size_t OutputSize = SigOutputElements.Stride * OutputCount;
417 SigOutputElements.Data = Data.substr(Current - Data.begin(), OutputSize);
418 Current += OutputSize;
420 size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
421 SigPatchOrPrimElements.Data = Data.substr(Current - Data.begin(), PSize);
434 return maskDwordSize(
Y) *
X * 4;
443 maskDwordSize(
static_cast<uint32_t>(OutputVectorCounts[
I]));
444 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
445 OutputVectorMasks[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
449 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0) {
450 uint32_t NumDwords = maskDwordSize(PatchConstOrPrimVectorCount);
451 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
452 PatchOrPrimMasks.Data = Data.substr(Current - Data.begin(), NumBytes);
459 if (InputVectorCount == 0 || OutputVectorCounts[
I] == 0)
461 uint32_t NumDwords = mapTableSize(InputVectorCount, OutputVectorCounts[
I]);
462 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
463 InputOutputMap[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
468 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0 &&
469 InputVectorCount > 0) {
471 mapTableSize(InputVectorCount, PatchConstOrPrimVectorCount);
472 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
473 InputPatchMap.Data = Data.substr(Current - Data.begin(), NumBytes);
478 if (ShaderStage ==
Triple::Domain && PatchConstOrPrimVectorCount > 0 &&
479 OutputVectorCounts[0] > 0) {
481 mapTableSize(PatchConstOrPrimVectorCount, OutputVectorCounts[0]);
482 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
483 PatchOutputMap.Data = Data.substr(Current - Data.begin(), NumBytes);
491 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
492 return P->SigInputElements;
493 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
494 return P->SigInputElements;
495 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
496 return P->SigInputElements;
501 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
502 return P->SigOutputElements;
503 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
504 return P->SigOutputElements;
505 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
506 return P->SigOutputElements;
511 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
512 return P->SigPatchOrPrimElements;
513 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
514 return P->SigPatchOrPrimElements;
515 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
516 return P->SigPatchOrPrimElements;
527 OS <<
"DXContainer does not support " << FeatureString;
540Expected<section_iterator>
568 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
569 if (It == Parts.end())
573 Sec.
p =
reinterpret_cast<uintptr_t
>(It);
578 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
583 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
588 return (Sec.
p -
reinterpret_cast<uintptr_t
>(Parts.begin())) /
589 sizeof(PartIterator);
593 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
594 return It->Data.size();
598 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
660 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.begin());
665 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.end());
672 return "DirectX Container";
695 return ExC.takeError();
697 return std::move(Obj);
#define offsetof(TYPE, MEMBER)
static Error parseFailed(const Twine &Msg)
static Error readStruct(StringRef Buffer, const char *Src, T &Struct)
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")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - 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...
StringRef - Represent a constant reference to a string, i.e.
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
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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)
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)
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
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.