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,
279 StaticSamplers.Stride = (Version <= 2)
283 StaticSamplers.Data = PartData.substr(StaticSamplersOffset,
284 static_cast<size_t>(NumStaticSamplers) *
285 StaticSamplers.Stride);
293 const char *Current = Data.begin();
300 if (PSVInfoData.
size() < Size)
302 "Pipeline state data extends beyond the bounds of the part");
309 if (PSVVersion == 3) {
310 v3::RuntimeInfo Info;
314 Info.swapBytes(ShaderStage);
316 }
else if (PSVVersion == 2) {
317 v2::RuntimeInfo Info;
321 Info.swapBytes(ShaderStage);
323 }
else if (PSVVersion == 1) {
324 v1::RuntimeInfo Info;
328 Info.swapBytes(ShaderStage);
330 }
else if (PSVVersion == 0) {
331 v0::RuntimeInfo Info;
335 Info.swapBytes(ShaderStage);
339 "Cannot read PSV Runtime Info, unsupported PSV version.");
348 if (ResourceCount > 0) {
353 size_t BindingDataSize = Resources.Stride * ResourceCount;
354 Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
356 if (Resources.Data.size() < BindingDataSize)
358 "Resource binding data extends beyond the bounds of the part");
360 Current += BindingDataSize;
362 Resources.Stride =
sizeof(v2::ResourceBindInfo);
369 Current =
reinterpret_cast<const char *
>(
370 alignTo<4>(
reinterpret_cast<uintptr_t
>(Current)));
375 if (StringTableSize % 4 != 0)
378 StringTable =
StringRef(Current, StringTableSize);
380 Current += StringTableSize;
382 uint32_t SemanticIndexTableSize = 0;
387 SemanticIndexTable.reserve(SemanticIndexTableSize);
388 for (
uint32_t I = 0;
I < SemanticIndexTableSize; ++
I) {
393 SemanticIndexTable.push_back(Index);
407 SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
408 SigInputElements.Stride;
410 if (Data.end() - Current <
413 "Signature elements extend beyond the size of the part");
415 size_t InputSize = SigInputElements.Stride * InputCount;
416 SigInputElements.Data = Data.substr(Current - Data.begin(), InputSize);
417 Current += InputSize;
419 size_t OutputSize = SigOutputElements.Stride * OutputCount;
420 SigOutputElements.Data = Data.substr(Current - Data.begin(), OutputSize);
421 Current += OutputSize;
423 size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
424 SigPatchOrPrimElements.Data = Data.substr(Current - Data.begin(), PSize);
437 return maskDwordSize(
Y) *
X * 4;
446 maskDwordSize(
static_cast<uint32_t>(OutputVectorCounts[
I]));
447 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
448 OutputVectorMasks[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
452 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0) {
453 uint32_t NumDwords = maskDwordSize(PatchConstOrPrimVectorCount);
454 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
455 PatchOrPrimMasks.Data = Data.substr(Current - Data.begin(), NumBytes);
462 if (InputVectorCount == 0 || OutputVectorCounts[
I] == 0)
464 uint32_t NumDwords = mapTableSize(InputVectorCount, OutputVectorCounts[
I]);
465 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
466 InputOutputMap[
I].Data = Data.substr(Current - Data.begin(), NumBytes);
471 if (ShaderStage ==
Triple::Hull && PatchConstOrPrimVectorCount > 0 &&
472 InputVectorCount > 0) {
474 mapTableSize(InputVectorCount, PatchConstOrPrimVectorCount);
475 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
476 InputPatchMap.Data = Data.substr(Current - Data.begin(), NumBytes);
481 if (ShaderStage ==
Triple::Domain && PatchConstOrPrimVectorCount > 0 &&
482 OutputVectorCounts[0] > 0) {
484 mapTableSize(PatchConstOrPrimVectorCount, OutputVectorCounts[0]);
485 size_t NumBytes = NumDwords *
sizeof(
uint32_t);
486 PatchOutputMap.Data = Data.substr(Current - Data.begin(), NumBytes);
494 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
495 return P->SigInputElements;
496 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
497 return P->SigInputElements;
498 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
499 return P->SigInputElements;
504 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
505 return P->SigOutputElements;
506 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
507 return P->SigOutputElements;
508 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
509 return P->SigOutputElements;
514 if (
const auto *
P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(&BasicInfo))
515 return P->SigPatchOrPrimElements;
516 if (
const auto *
P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
517 return P->SigPatchOrPrimElements;
518 if (
const auto *
P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
519 return P->SigPatchOrPrimElements;
530 OS <<
"DXContainer does not support " << FeatureString;
543Expected<section_iterator>
571 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
572 if (It == Parts.end())
576 Sec.
p =
reinterpret_cast<uintptr_t
>(It);
581 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
586 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
591 return (Sec.
p -
reinterpret_cast<uintptr_t
>(Parts.begin())) /
592 sizeof(PartIterator);
596 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
597 return It->Data.size();
601 PartIterator It =
reinterpret_cast<PartIterator
>(Sec.
p);
663 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.begin());
668 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.end());
675 return "DirectX Container";
698 return ExC.takeError();
700 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.