DXContainer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DXContainer.h - DXContainer file implementation ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the DXContainerFile class, which implements the ObjectFile
  15. // interface for DXContainer files.
  16. //
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_OBJECT_DXCONTAINER_H
  20. #define LLVM_OBJECT_DXCONTAINER_H
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/BinaryFormat/DXContainer.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/MemoryBufferRef.h"
  26. namespace llvm {
  27. namespace object {
  28. class DXContainer {
  29. public:
  30. using DXILData = std::pair<dxbc::ProgramHeader, const char *>;
  31. private:
  32. DXContainer(MemoryBufferRef O);
  33. MemoryBufferRef Data;
  34. dxbc::Header Header;
  35. SmallVector<uint32_t, 4> PartOffsets;
  36. std::optional<DXILData> DXIL;
  37. std::optional<uint64_t> ShaderFlags;
  38. std::optional<dxbc::ShaderHash> Hash;
  39. Error parseHeader();
  40. Error parsePartOffsets();
  41. Error parseDXILHeader(StringRef Part);
  42. Error parseShaderFlags(StringRef Part);
  43. Error parseHash(StringRef Part);
  44. friend class PartIterator;
  45. public:
  46. // The PartIterator is a wrapper around the iterator for the PartOffsets
  47. // member of the DXContainer. It contains a refernce to the container, and the
  48. // current iterator value, as well as storage for a parsed part header.
  49. class PartIterator {
  50. const DXContainer &Container;
  51. SmallVectorImpl<uint32_t>::const_iterator OffsetIt;
  52. struct PartData {
  53. dxbc::PartHeader Part;
  54. uint32_t Offset;
  55. StringRef Data;
  56. } IteratorState;
  57. friend class DXContainer;
  58. PartIterator(const DXContainer &C,
  59. SmallVectorImpl<uint32_t>::const_iterator It)
  60. : Container(C), OffsetIt(It) {
  61. if (OffsetIt == Container.PartOffsets.end())
  62. updateIteratorImpl(Container.PartOffsets.back());
  63. else
  64. updateIterator();
  65. }
  66. // Updates the iterator's state data. This results in copying the part
  67. // header into the iterator and handling any required byte swapping. This is
  68. // called when incrementing or decrementing the iterator.
  69. void updateIterator() {
  70. if (OffsetIt != Container.PartOffsets.end())
  71. updateIteratorImpl(*OffsetIt);
  72. }
  73. // Implementation for updating the iterator state based on a specified
  74. // offest.
  75. void updateIteratorImpl(const uint32_t Offset);
  76. public:
  77. PartIterator &operator++() {
  78. if (OffsetIt == Container.PartOffsets.end())
  79. return *this;
  80. ++OffsetIt;
  81. updateIterator();
  82. return *this;
  83. }
  84. PartIterator operator++(int) {
  85. PartIterator Tmp = *this;
  86. ++(*this);
  87. return Tmp;
  88. }
  89. bool operator==(const PartIterator &RHS) const {
  90. return OffsetIt == RHS.OffsetIt;
  91. }
  92. bool operator!=(const PartIterator &RHS) const {
  93. return OffsetIt != RHS.OffsetIt;
  94. }
  95. const PartData &operator*() { return IteratorState; }
  96. const PartData *operator->() { return &IteratorState; }
  97. };
  98. PartIterator begin() const {
  99. return PartIterator(*this, PartOffsets.begin());
  100. }
  101. PartIterator end() const { return PartIterator(*this, PartOffsets.end()); }
  102. StringRef getData() const { return Data.getBuffer(); }
  103. static Expected<DXContainer> create(MemoryBufferRef Object);
  104. const dxbc::Header &getHeader() const { return Header; }
  105. std::optional<DXILData> getDXIL() const { return DXIL; }
  106. std::optional<uint64_t> getShaderFlags() const { return ShaderFlags; }
  107. std::optional<dxbc::ShaderHash> getShaderHash() const { return Hash; }
  108. };
  109. } // namespace object
  110. } // namespace llvm
  111. #endif // LLVM_OBJECT_DXCONTAINER_H
  112. #ifdef __GNUC__
  113. #pragma GCC diagnostic pop
  114. #endif