20 return make_error<GenericBinaryError>(Msg.
str(), object_error::parse_failed);
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() {
66 return parseFailed(
"More than one DXIL part is present in the file");
67 const char *Current = Part.
begin();
72 DXIL.emplace(std::make_pair(Header, Current));
77 if (ShaderFeatureFlags)
78 return parseFailed(
"More than one SFI0 part is present in the file");
82 ShaderFeatureFlags = FlagValue;
88 return parseFailed(
"More than one HASH part is present in the file");
98 return parseFailed(
"More than one RTS0 part is present in the file");
100 if (
Error Err = RootSignature->parse())
107 return parseFailed(
"More than one PSV0 part is present in the file");
121 return parseFailed(
"Signature parameters extend beyond the part boundary");
128 for (
const auto &Param : Parameters) {
130 return parseFailed(
"Invalid parameter name offset: name starts before "
131 "the first name offset");
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))
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();
251 "Invalid root signature, insufficient space for header.");
253 Version = support::endian::read<uint32_t, llvm::endianness::little>(Current);
257 support::endian::read<uint32_t, llvm::endianness::little>(Current);
260 RootParametersOffset =
261 support::endian::read<uint32_t, llvm::endianness::little>(Current);
265 support::endian::read<uint32_t, llvm::endianness::little>(Current);
268 StaticSamplersOffset =
269 support::endian::read<uint32_t, llvm::endianness::little>(Current);
272 Flags = support::endian::read<uint32_t, llvm::endianness::little>(Current);
275 ParametersHeaders.Data = PartData.
substr(
276 RootParametersOffset,
280 StaticSamplers.Data = PartData.
substr(
281 StaticSamplersOffset,
290 const char *Current =
Data.begin();
299 "Pipeline state data extends beyond the bounds of the part");
301 using namespace dxbc::PSV;
303 const uint32_t PSVVersion = getVersion();
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)
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);
393 uint8_t InputCount = getSigInputCount();
394 uint8_t OutputCount = getSigOutputCount();
395 uint8_t PatchOrPrimCount = getSigPatchOrPrimCount();
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);
426 uint8_t PatchConstOrPrimVectorCount = getPatchConstOrPrimVectorCount();
427 uint8_t InputVectorCount = getInputVectorCount();
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;
542 return make_error<DXNotSupportedError>(
"Symbol sections");
546 return make_error<DXNotSupportedError>(
"Symbol names");
551 return make_error<DXNotSupportedError>(
"Symbol addresses");
564 return make_error<DXNotSupportedError>(
"Symbol types");
569 if (It == Parts.end())
573 Sec.
p =
reinterpret_cast<uintptr_t
>(It);
588 return (Sec.
p -
reinterpret_cast<uintptr_t
>(Parts.begin())) /
594 return It->Data.size();
660 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.begin());
665 Sec.
p =
reinterpret_cast<uintptr_t
>(Parts.end());
672 return "DirectX Container";
683 return make_error<DXNotSupportedError>(
"Symbol names");
688 return make_error<DXNotSupportedError>(
"Symbol flags");
695 return ExC.takeError();
697 return std::move(Obj);
#define offsetof(TYPE, MEMBER)
Analysis containing CSE Info
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
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 GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
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.
StringRef getBuffer() const
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).
A table of densely packed, null-terminated strings indexed by offset.
constexpr size_t size() const
Returns the byte size of the table.
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)
LLVM_ABI uint8_t getSigInputCount() const
LLVM_ABI uint8_t getSigPatchOrPrimCount() const
LLVM_ABI Error parse(uint16_t ShaderKind)
LLVM_ABI uint8_t getSigOutputCount() const
LLVM_ABI Error initialize(StringRef Part)
static Expected< std::unique_ptr< DXContainerObjectFile > > createDXContainerObjectFile(MemoryBufferRef Object)
This is a value type class that represents a single relocation in the list of relocations in the obje...
This is a value type class that represents a single section in the list of sections in the object fil...
This is a value type class that represents a single symbol in the list of symbols in the object file.
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
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)
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.