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"
16
17using namespace llvm;
18
20
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.
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") {
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;
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;
116 }
117 return 0;
118}
uint64_t Size
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
uint64_t writeObject(MCAssembler &Asm) override
Write the object file and returns the number of bytes written.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
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
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:147
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
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:93
static uint8_t getVersion(uint8_t Major, uint8_t Minor)
Definition: DXContainer.h:135
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:71