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