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