LLVM 22.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 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.
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(PartOffset);
38 PartOffset += sizeof(dxbc::PartHeader) + SectionSize;
39 PartOffset = alignTo(PartOffset, 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>({'D', 'X', 'B', 'C'});
56 // Write 16-bytes of 0's for the hash.
57 W.OS.write_zeros(16);
58 // Write 1.0 for file format version.
59 W.write<uint16_t>(1u);
60 W.write<uint16_t>(0u);
61 // Write the file size.
62 W.write<uint32_t>(static_cast<uint32_t>(FileSize));
63 // Write the number of parts.
64 W.write<uint32_t>(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>(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>(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(PartSize, Align(4));
85 W.write<uint32_t>(static_cast<uint32_t>(PartSize));
86 if (Sec.getName() == "DXIL") {
88 memset(reinterpret_cast<void *>(&Header), 0, 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(0));
95 Header.Version =
96 dxbc::ProgramHeader::getVersion(MajorVersion, 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(Header.Bitcode.Magic, "DXIL", 4);
104 VersionTuple DXILVersion = TT.getDXILVersion();
105 Header.Bitcode.MajorVersion = DXILVersion.getMajor();
106 Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
107 Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
108 Header.Bitcode.Size = SectionSize;
110 Header.swapBytes();
111 W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
112 sizeof(dxbc::ProgramHeader)));
113 }
114 Asm.writeSectionData(W.OS, &Sec);
115 unsigned Size = W.OS.tell() - Start;
117 }
118 return 0;
119}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Size
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
uint64_t writeObject() override
Write the object file and returns the number of bytes written.
LLVM_ABI uint64_t getSectionAddressSize(const MCSection &Sec) const
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
const Triple & getTargetTriple() const
Definition: MCContext.h:400
MCContext & getContext() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
size_t size() const
Definition: SmallVector.h:79
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:30
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:72
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:75
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:148
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:477
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
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:99
static uint8_t getVersion(uint8_t Major, uint8_t Minor)
Definition: DXContainer.h:141
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:71