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