| 1 | //===- llvm/MC/MCDXContainerWriter.cpp - DXContainer Writer -----*- 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 | |
| 9 | #include "llvm/MC/MCDXContainerWriter.h" |
| 10 | #include "llvm/BinaryFormat/DXContainer.h" |
| 11 | #include "llvm/MC/MCAssembler.h" |
| 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCSection.h" |
| 14 | #include "llvm/MC/MCValue.h" |
| 15 | #include "llvm/Support/Alignment.h" |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | cl::opt<bool> EmbedDebug("dx-embed-debug" , |
| 21 | cl::desc("Embed PDB in shader container" )); |
| 22 | cl::opt<bool> |
| 23 | StripDebug("dx-strip-debug" , |
| 24 | cl::desc("Strip debug information from shader bytecode" )); |
| 25 | cl::opt<bool> SlimDebug("dx-slim-debug" , |
| 26 | cl::desc("Generate slim PDB without ILDB part" )); |
| 27 | |
| 28 | MCDXContainerTargetWriter::~MCDXContainerTargetWriter() = default; |
| 29 | |
| 30 | MCDXContainerBaseWriter::~MCDXContainerBaseWriter() = default; |
| 31 | |
| 32 | bool MCDXContainerBaseWriter::shouldSkipSection(StringRef SectionName, |
| 33 | size_t SectionSize) { |
| 34 | // Skip empty and auxiliary sections. |
| 35 | if (SectionSize == 0 || SectionName == PdbFileNameSectionName || |
| 36 | SectionName == ModuleHashSectionName) |
| 37 | return true; |
| 38 | // Slim debug omits ILDB from all DXContainer outputs. |
| 39 | return SlimDebug && SectionName == "ILDB" ; |
| 40 | } |
| 41 | |
| 42 | void MCDXContainerBaseWriter::write(raw_ostream &OS, const Triple &TT) { |
| 43 | ArrayRef<MCDXContainerPart> Parts = collectParts(); |
| 44 | |
| 45 | support::endian::Writer W(OS, llvm::endianness::little); |
| 46 | |
| 47 | // Start the file size as the header plus the size of the part offsets. |
| 48 | // Presently DXContainer files usually contain 7-10 parts. Reserving space for |
| 49 | // 16 part offsets gives us a little room for growth. |
| 50 | llvm::SmallVector<uint64_t, 16> PartOffsets; |
| 51 | uint64_t PartOffset = 0; |
| 52 | bool HasPrivate = false; |
| 53 | for (const MCDXContainerPart &Part : Parts) { |
| 54 | if (HasPrivate) |
| 55 | reportFatalInternalError( |
| 56 | reason: "PRIV must be the last section in a DXContainer" ); |
| 57 | if (Part.Name == "PRIV" ) |
| 58 | HasPrivate = true; |
| 59 | |
| 60 | uint64_t SectionSize = Part.Data.size(); |
| 61 | assert(SectionSize < std::numeric_limits<uint32_t>::max() && |
| 62 | "Section size too large for DXContainer" ); |
| 63 | |
| 64 | PartOffsets.push_back(Elt: PartOffset); |
| 65 | PartOffset += sizeof(dxbc::PartHeader) + SectionSize; |
| 66 | if (!HasPrivate) |
| 67 | PartOffset = alignTo(Size: PartOffset, A: Align(4ul)); |
| 68 | // The DXIL part also writes a program header, so we need to include its |
| 69 | // size when computing the offset for a part after the DXIL part. |
| 70 | if (dxbc::isProgramPart(PartName: Part.Name)) |
| 71 | PartOffset += sizeof(dxbc::ProgramHeader); |
| 72 | } |
| 73 | assert(PartOffset < std::numeric_limits<uint32_t>::max() && |
| 74 | "Part data too large for DXContainer" ); |
| 75 | |
| 76 | uint64_t PartStart = |
| 77 | sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t)); |
| 78 | uint64_t FileSize = PartStart + PartOffset; |
| 79 | assert(FileSize < std::numeric_limits<uint32_t>::max() && |
| 80 | "File size too large for DXContainer" ); |
| 81 | |
| 82 | // Write the header. |
| 83 | W.write<char>(Val: {'D', 'X', 'B', 'C'}); |
| 84 | // Write 16-bytes of 0's for the hash. |
| 85 | W.OS.write_zeros(NumZeros: 16); |
| 86 | // Write 1.0 for file format version. |
| 87 | W.write<uint16_t>(Val: 1u); |
| 88 | W.write<uint16_t>(Val: 0u); |
| 89 | // Write the file size. |
| 90 | W.write<uint32_t>(Val: static_cast<uint32_t>(FileSize)); |
| 91 | // Write the number of parts. |
| 92 | W.write<uint32_t>(Val: static_cast<uint32_t>(PartOffsets.size())); |
| 93 | // Write the offsets for the part headers for each part. |
| 94 | for (uint64_t Offset : PartOffsets) |
| 95 | W.write<uint32_t>(Val: static_cast<uint32_t>(PartStart + Offset)); |
| 96 | |
| 97 | for (const MCDXContainerPart &Part : Parts) { |
| 98 | uint64_t SectionSize = Part.Data.size(); |
| 99 | unsigned Start = W.OS.tell(); |
| 100 | // Write section header. |
| 101 | W.write<char>(Val: ArrayRef<char>(Part.Name.data(), 4)); |
| 102 | |
| 103 | uint64_t PartSize = SectionSize; |
| 104 | |
| 105 | if (dxbc::isProgramPart(PartName: Part.Name)) |
| 106 | PartSize += sizeof(dxbc::ProgramHeader); |
| 107 | // DXContainer part should be 4-byte aligned, unless it is PRIV part. |
| 108 | bool IsPrivate = Part.Name == "PRIV" ; |
| 109 | if (!IsPrivate) |
| 110 | PartSize = alignTo(Size: PartSize, A: Align(4)); |
| 111 | W.write<uint32_t>(Val: static_cast<uint32_t>(PartSize)); |
| 112 | if (dxbc::isProgramPart(PartName: Part.Name)) { |
| 113 | dxbc::ProgramHeader ; |
| 114 | memset(s: reinterpret_cast<void *>(&Header), c: 0, n: sizeof(dxbc::ProgramHeader)); |
| 115 | |
| 116 | VersionTuple Version = TT.getOSVersion(); |
| 117 | uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor()); |
| 118 | uint8_t MinorVersion = |
| 119 | static_cast<uint8_t>(Version.getMinor().value_or(u: 0)); |
| 120 | Header.Version = |
| 121 | dxbc::ProgramHeader::getVersion(Major: MajorVersion, Minor: MinorVersion); |
| 122 | if (TT.hasEnvironment()) |
| 123 | Header.ShaderKind = |
| 124 | static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel); |
| 125 | |
| 126 | // The program header's size field is in 32-bit words. |
| 127 | Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4; |
| 128 | memcpy(dest: Header.Bitcode.Magic, src: "DXIL" , n: 4); |
| 129 | VersionTuple DXILVersion = TT.getDXILVersion(); |
| 130 | Header.Bitcode.MajorVersion = DXILVersion.getMajor(); |
| 131 | Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(u: 0); |
| 132 | Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader); |
| 133 | Header.Bitcode.Size = SectionSize; |
| 134 | if (sys::IsBigEndianHost) |
| 135 | Header.swapBytes(); |
| 136 | W.write<char>(Val: ArrayRef<char>(reinterpret_cast<char *>(&Header), |
| 137 | sizeof(dxbc::ProgramHeader))); |
| 138 | } |
| 139 | W.write<char>(Val: Part.Data); |
| 140 | if (!IsPrivate) { |
| 141 | unsigned Size = W.OS.tell() - Start; |
| 142 | W.OS.write_zeros(NumZeros: offsetToAlignment(Value: Size, Alignment: Align(4))); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void DXContainerObjectWriter::clearParts() { |
| 148 | Parts.clear(); |
| 149 | SectionBuffers.clear(); |
| 150 | } |
| 151 | |
| 152 | ArrayRef<MCDXContainerPart> DXContainerObjectWriter::collectParts() { |
| 153 | clearParts(); |
| 154 | for (const MCSection &Sec : *Asm) { |
| 155 | if (shouldSkipSection(SectionName: Sec.getName(), SectionSize: Asm->getSectionAddressSize(Sec))) |
| 156 | continue; |
| 157 | |
| 158 | SectionBuffers.emplace_back(); |
| 159 | raw_svector_ostream OS(SectionBuffers.back()); |
| 160 | Asm->writeSectionData(OS, Section: &Sec); |
| 161 | Parts.push_back(Elt: {.Name: Sec.getName(), .Data: StringRef(SectionBuffers.back())}); |
| 162 | } |
| 163 | return Parts; |
| 164 | } |
| 165 | |
| 166 | bool DXContainerObjectWriter::shouldSkipSection(StringRef SectionName, |
| 167 | size_t SectionSize) { |
| 168 | // Do not write ILDB part if we're not embedding it. |
| 169 | if (SectionName == "ILDB" && (!EmbedDebug || StripDebug)) |
| 170 | return true; |
| 171 | if (SectionName == "SRCI" ) |
| 172 | return true; |
| 173 | return MCDXContainerBaseWriter::shouldSkipSection(SectionName, SectionSize); |
| 174 | } |
| 175 | |
| 176 | uint64_t DXContainerObjectWriter::writeObject() { |
| 177 | write(OS&: W.OS, TT: getContext().getTargetTriple()); |
| 178 | clearParts(); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |