LLVM 23.0.0git
MCDXContainerWriter.cpp
Go to the documentation of this file.
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
11#include "llvm/MC/MCAssembler.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCSection.h"
14#include "llvm/MC/MCValue.h"
16
17using namespace llvm;
18
20
22
25
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.
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(PartOffset);
39 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
40 PartOffset = alignTo(PartOffset, 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(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>({'D', 'X', 'B', 'C'});
57 // Write 16-bytes of 0's for the hash.
58 W.OS.write_zeros(16);
59 // Write 1.0 for file format version.
60 W.write<uint16_t>(1u);
61 W.write<uint16_t>(0u);
62 // Write the file size.
63 W.write<uint32_t>(static_cast<uint32_t>(FileSize));
64 // Write the number of parts.
65 W.write<uint32_t>(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>(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>(ArrayRef<char>(Part.Name.data(), 4));
75
76 uint64_t PartSize = SectionSize;
77
78 if (dxbc::isProgramPart(Part.Name))
79 PartSize += sizeof(dxbc::ProgramHeader);
80 // DXContainer parts should be 4-byte aligned.
81 PartSize = alignTo(PartSize, Align(4));
82 W.write<uint32_t>(static_cast<uint32_t>(PartSize));
83 if (dxbc::isProgramPart(Part.Name)) {
85 memset(reinterpret_cast<void *>(&Header), 0, 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(0));
91 Header.Version =
92 dxbc::ProgramHeader::getVersion(MajorVersion, 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(Header.Bitcode.Magic, "DXIL", 4);
100 VersionTuple DXILVersion = TT.getDXILVersion();
101 Header.Bitcode.MajorVersion = DXILVersion.getMajor();
102 Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
103 Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
104 Header.Bitcode.Size = SectionSize;
106 Header.swapBytes();
107 W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
108 sizeof(dxbc::ProgramHeader)));
109 }
110 W.write<char>(Part.Data);
111 unsigned Size = W.OS.tell() - Start;
112 W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
113 }
114}
115
116void DXContainerObjectWriter::clearParts() {
117 Parts.clear();
118 SectionBuffers.clear();
119}
120
122 clearParts();
123 for (const MCSection &Sec : *Asm) {
124 if (shouldSkipSection(Sec.getName(), Asm->getSectionAddressSize(Sec)))
125 continue;
126
127 SectionBuffers.emplace_back();
128 raw_svector_ostream OS(SectionBuffers.back());
129 Asm->writeSectionData(OS, &Sec);
130 Parts.push_back({Sec.getName(), StringRef(SectionBuffers.back())});
131 }
132 return Parts;
133}
134
136 write(W.OS, getContext().getTargetTriple());
137 clearParts();
138
139 return 0;
140}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
uint64_t writeObject() override
Write the object file and returns the number of bytes written.
ArrayRef< MCDXContainerPart > collectParts() override
void write(raw_ostream &OS, const Triple &TT)
virtual ArrayRef< MCDXContainerPart > collectParts()
virtual bool shouldSkipSection(StringRef SectionName, size_t SectionSize)
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
Represents a version number in the form major[.minor[.subminor[.build]]].
unsigned getMajor() const
Retrieve the major version number.
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
LLVM_ABI bool isProgramPart(StringRef PartName)
constexpr bool IsBigEndianHost
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:334
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:186
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Use this type to describe the size and type of a DXIL container part.
static uint8_t getVersion(uint8_t Major, uint8_t Minor)
Adapter to write values to a stream in a particular byte order.