LLVM 20.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"
17
18using namespace llvm;
19
21
22namespace {
23class DXContainerObjectWriter : public MCObjectWriter {
25
26 /// The target specific DXContainer writer instance.
27 std::unique_ptr<MCDXContainerTargetWriter> TargetObjectWriter;
28
29public:
30 DXContainerObjectWriter(std::unique_ptr<MCDXContainerTargetWriter> MOTW,
32 : W(OS, llvm::endianness::little), TargetObjectWriter(std::move(MOTW)) {}
33
34 ~DXContainerObjectWriter() override {}
35
36private:
37 void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
39 uint64_t &FixedValue) override {}
40
41 uint64_t writeObject(MCAssembler &Asm) override;
42};
43} // namespace
44
45uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm) {
46 // Start the file size as the header plus the size of the part offsets.
47 // Presently DXContainer files usually contain 7-10 parts. Reserving space for
48 // 16 part offsets gives us a little room for growth.
50 uint64_t PartOffset = 0;
51 for (const MCSection &Sec : Asm) {
52 uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
53 // Skip empty sections.
54 if (SectionSize == 0)
55 continue;
56
57 assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
58 "Section size too large for DXContainer");
59
60 PartOffsets.push_back(PartOffset);
61 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
62 PartOffset = alignTo(PartOffset, Align(4ul));
63 // The DXIL part also writes a program header, so we need to include its
64 // size when computing the offset for a part after the DXIL part.
65 if (Sec.getName() == "DXIL")
66 PartOffset += sizeof(dxbc::ProgramHeader);
67 }
68 assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
69 "Part data too large for DXContainer");
70
71 uint64_t PartStart =
72 sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
73 uint64_t FileSize = PartStart + PartOffset;
74 assert(FileSize < std::numeric_limits<uint32_t>::max() &&
75 "File size too large for DXContainer");
76
77 // Write the header.
78 W.write<char>({'D', 'X', 'B', 'C'});
79 // Write 16-bytes of 0's for the hash.
80 W.OS.write_zeros(16);
81 // Write 1.0 for file format version.
82 W.write<uint16_t>(1u);
83 W.write<uint16_t>(0u);
84 // Write the file size.
85 W.write<uint32_t>(static_cast<uint32_t>(FileSize));
86 // Write the number of parts.
87 W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
88 // Write the offsets for the part headers for each part.
89 for (uint64_t Offset : PartOffsets)
90 W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
91
92 for (const MCSection &Sec : Asm) {
93 uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
94 // Skip empty sections.
95 if (SectionSize == 0)
96 continue;
97
98 unsigned Start = W.OS.tell();
99 // Write section header.
100 W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));
101
102 uint64_t PartSize = SectionSize;
103
104 if (Sec.getName() == "DXIL")
105 PartSize += sizeof(dxbc::ProgramHeader);
106 // DXContainer parts should be 4-byte aligned.
107 PartSize = alignTo(PartSize, Align(4));
108 W.write<uint32_t>(static_cast<uint32_t>(PartSize));
109 if (Sec.getName() == "DXIL") {
110 dxbc::ProgramHeader Header;
111 memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
112
113 const Triple &TT = Asm.getContext().getTargetTriple();
114 VersionTuple Version = TT.getOSVersion();
115 uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
116 uint8_t MinorVersion =
117 static_cast<uint8_t>(Version.getMinor().value_or(0));
118 Header.Version =
119 dxbc::ProgramHeader::getVersion(MajorVersion, MinorVersion);
120 if (TT.hasEnvironment())
121 Header.ShaderKind =
122 static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
123
124 // The program header's size field is in 32-bit words.
125 Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
126 memcpy(Header.Bitcode.Magic, "DXIL", 4);
127 VersionTuple DXILVersion = TT.getDXILVersion();
128 Header.Bitcode.MajorVersion = DXILVersion.getMajor();
129 Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
130 Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
131 Header.Bitcode.Size = SectionSize;
133 Header.swapBytes();
134 W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
135 sizeof(dxbc::ProgramHeader)));
136 }
137 Asm.writeSectionData(W.OS, &Sec);
138 unsigned Size = W.OS.tell() - Start;
139 W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
140 }
141 return 0;
142}
143
144std::unique_ptr<MCObjectWriter> llvm::createDXContainerObjectWriter(
145 std::unique_ptr<MCDXContainerTargetWriter> MOTW, raw_pwrite_stream &OS) {
146 return std::make_unique<DXContainerObjectWriter>(std::move(MOTW), OS);
147}
uint64_t Size
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
This represents an "assembler immediate".
Definition: MCValue.h:36
size_t size() const
Definition: SmallVector.h:92
void push_back(const T &Elt)
Definition: SmallVector.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:71
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:74
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:434
@ SectionSize
Definition: COFF.h:60
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:26
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::unique_ptr< MCObjectWriter > createDXContainerObjectWriter(std::unique_ptr< MCDXContainerTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new DXContainer writer instance.
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:197
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
endianness
Definition: bit.h:70
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
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.
Definition: DXContainer.h:93
static uint8_t getVersion(uint8_t Major, uint8_t Minor)
Definition: DXContainer.h:135
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:67