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