dxcontainer2yaml.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===------ dxcontainer2yaml.cpp - obj2yaml conversion tool -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "obj2yaml.h"
  9. #include "llvm/Object/DXContainer.h"
  10. #include "llvm/ObjectYAML/DXContainerYAML.h"
  11. #include "llvm/Support/Error.h"
  12. #include <algorithm>
  13. using namespace llvm;
  14. using namespace llvm::object;
  15. static Expected<DXContainerYAML::Object *>
  16. dumpDXContainer(MemoryBufferRef Source) {
  17. assert(file_magic::dxcontainer_object == identify_magic(Source.getBuffer()));
  18. Expected<DXContainer> ExDXC = DXContainer::create(Source);
  19. if (!ExDXC)
  20. return ExDXC.takeError();
  21. DXContainer Container = *ExDXC;
  22. std::unique_ptr<DXContainerYAML::Object> Obj =
  23. std::make_unique<DXContainerYAML::Object>();
  24. for (uint8_t Byte : Container.getHeader().FileHash.Digest)
  25. Obj->Header.Hash.push_back(Byte);
  26. Obj->Header.Version.Major = Container.getHeader().Version.Major;
  27. Obj->Header.Version.Minor = Container.getHeader().Version.Minor;
  28. Obj->Header.FileSize = Container.getHeader().FileSize;
  29. Obj->Header.PartCount = Container.getHeader().PartCount;
  30. Obj->Header.PartOffsets = std::vector<uint32_t>();
  31. for (const auto P : Container) {
  32. Obj->Header.PartOffsets->push_back(P.Offset);
  33. Obj->Parts.push_back(
  34. DXContainerYAML::Part(P.Part.getName().str(), P.Part.Size));
  35. DXContainerYAML::Part &NewPart = Obj->Parts.back();
  36. dxbc::PartType PT = dxbc::parsePartType(P.Part.getName());
  37. switch (PT) {
  38. case dxbc::PartType::DXIL: {
  39. std::optional<DXContainer::DXILData> DXIL = Container.getDXIL();
  40. assert(DXIL && "Since we are iterating and found a DXIL part, "
  41. "this should never not have a value");
  42. NewPart.Program = DXContainerYAML::DXILProgram{
  43. DXIL->first.MajorVersion,
  44. DXIL->first.MinorVersion,
  45. DXIL->first.ShaderKind,
  46. DXIL->first.Size,
  47. DXIL->first.Bitcode.MajorVersion,
  48. DXIL->first.Bitcode.MinorVersion,
  49. DXIL->first.Bitcode.Offset,
  50. DXIL->first.Bitcode.Size,
  51. std::vector<llvm::yaml::Hex8>(
  52. DXIL->second, DXIL->second + DXIL->first.Bitcode.Size)};
  53. break;
  54. }
  55. case dxbc::PartType::SFI0: {
  56. std::optional<uint64_t> Flags = Container.getShaderFlags();
  57. // Omit the flags in the YAML if they are missing or zero.
  58. if (Flags && *Flags > 0)
  59. NewPart.Flags = DXContainerYAML::ShaderFlags(*Flags);
  60. break;
  61. }
  62. case dxbc::PartType::HASH: {
  63. std::optional<dxbc::ShaderHash> Hash = Container.getShaderHash();
  64. if (Hash && Hash->isPopulated())
  65. NewPart.Hash = DXContainerYAML::ShaderHash(*Hash);
  66. break;
  67. }
  68. case dxbc::PartType::Unknown:
  69. break;
  70. }
  71. }
  72. return Obj.release();
  73. }
  74. llvm::Error dxcontainer2yaml(llvm::raw_ostream &Out,
  75. llvm::MemoryBufferRef Source) {
  76. Expected<DXContainerYAML::Object *> YAMLOrErr = dumpDXContainer(Source);
  77. if (!YAMLOrErr)
  78. return YAMLOrErr.takeError();
  79. std::unique_ptr<DXContainerYAML::Object> YAML(YAMLOrErr.get());
  80. yaml::Output Yout(Out);
  81. Yout << *YAML;
  82. return Error::success();
  83. }