LLVM 17.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/MCAsmLayout.h"
12#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCSection.h"
15#include "llvm/MC/MCValue.h"
18
19using namespace llvm;
20
22
23namespace {
24class DXContainerObjectWriter : public MCObjectWriter {
26
27 /// The target specific DXContainer writer instance.
28 std::unique_ptr<MCDXContainerTargetWriter> TargetObjectWriter;
29
30public:
31 DXContainerObjectWriter(std::unique_ptr<MCDXContainerTargetWriter> MOTW,
33 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
34
35 ~DXContainerObjectWriter() override {}
36
37private:
38 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
39 const MCFragment *Fragment, const MCFixup &Fixup,
40 MCValue Target, uint64_t &FixedValue) override {}
41
42 void executePostLayoutBinding(MCAssembler &Asm,
43 const MCAsmLayout &Layout) override {}
44
45 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
46};
47} // namespace
48
49uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm,
50 const MCAsmLayout &Layout) {
51 // Start the file size as the header plus the size of the part offsets.
52 // Presently DXContainer files usually contain 7-10 parts. Reserving space for
53 // 16 part offsets gives us a little room for growth.
55 uint64_t PartOffset = 0;
56 for (const MCSection &Sec : Asm) {
58 // Skip empty sections.
59 if (SectionSize == 0)
60 continue;
61
62 assert(SectionSize < std::numeric_limits<uint32_t>::max() &&
63 "Section size too large for DXContainer");
64
65 PartOffsets.push_back(PartOffset);
66 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
67 PartOffset = alignTo(PartOffset, Align(4ul));
68 // The DXIL part also writes a program header, so we need to include its
69 // size when computing the offset for a part after the DXIL part.
70 if (Sec.getName() == "DXIL")
71 PartOffset += sizeof(dxbc::ProgramHeader);
72 }
73 assert(PartOffset < std::numeric_limits<uint32_t>::max() &&
74 "Part data too large for DXContainer");
75
76 uint64_t PartStart =
77 sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
78 uint64_t FileSize = PartStart + PartOffset;
79 assert(FileSize < std::numeric_limits<uint32_t>::max() &&
80 "File size too large for DXContainer");
81
82 // Write the header.
83 W.write<char>({'D', 'X', 'B', 'C'});
84 // Write 16-bytes of 0's for the hash.
85 W.OS.write_zeros(16);
86 // Write 1.0 for file format version.
87 W.write<uint16_t>(1u);
88 W.write<uint16_t>(0u);
89 // Write the file size.
90 W.write<uint32_t>(static_cast<uint32_t>(FileSize));
91 // Write the number of parts.
92 W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
93 // Write the offsets for the part headers for each part.
94 for (uint64_t Offset : PartOffsets)
95 W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
96
97 for (const MCSection &Sec : Asm) {
99 // Skip empty sections.
100 if (SectionSize == 0)
101 continue;
102
103 unsigned Start = W.OS.tell();
104 // Write section header.
105 W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));
106
107 uint64_t PartSize = SectionSize;
108
109 if (Sec.getName() == "DXIL")
110 PartSize += sizeof(dxbc::ProgramHeader);
111 // DXContainer parts should be 4-byte aligned.
112 PartSize = alignTo(PartSize, Align(4));
113 W.write<uint32_t>(static_cast<uint32_t>(PartSize));
114 if (Sec.getName() == "DXIL") {
115 dxbc::ProgramHeader Header;
116 memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
117
118 const Triple &TT = Asm.getContext().getTargetTriple();
119 VersionTuple Version = TT.getOSVersion();
120 Header.MajorVersion = static_cast<uint8_t>(Version.getMajor());
121 if (Version.getMinor())
122 Header.MinorVersion = static_cast<uint8_t>(*Version.getMinor());
123 if (TT.hasEnvironment())
124 Header.ShaderKind =
125 static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
126
127 // The program header's size field is in 32-bit words.
128 Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
129 memcpy(Header.Bitcode.Magic, "DXIL", 4);
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, Layout);
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())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:198
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:39
This represents an "assembler immediate".
Definition: MCValue.h:36
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
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:31
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:428
@ SectionSize
Definition: COFF.h:60
const uint64_t Version
Definition: InstrProf.h:1058
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:64
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
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:1862
Definition: BitVector.h:851
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:92
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52