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