LLVM  15.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"
16 #include "llvm/Support/Alignment.h"
18 
19 using namespace llvm;
20 
22 
23 namespace {
24 class DXContainerObjectWriter : public MCObjectWriter {
26 
27  /// The target specific DXContainer writer instance.
28  std::unique_ptr<MCDXContainerTargetWriter> TargetObjectWriter;
29 
30 public:
31  DXContainerObjectWriter(std::unique_ptr<MCDXContainerTargetWriter> MOTW,
33  : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
34 
35  ~DXContainerObjectWriter() override {}
36 
37 private:
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 
49 uint64_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 
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  }
70  "Part data too large for DXContainer");
71 
72  uint64_t PartStart =
73  sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));
74  uint64_t FileSize = PartStart + PartOffset;
76  "File size too large for DXContainer");
77 
78  // Write the header.
79  W.write<char>({'D', 'X', 'B', 'C'});
80  // Write 16-bytes of 0's for the hash.
81  W.OS.write_zeros(16);
82  // Write 1.0 for file format version.
83  W.write<uint16_t>(1u);
84  W.write<uint16_t>(0u);
85  // Write the file size.
86  W.write<uint32_t>(static_cast<uint32_t>(FileSize));
87  // Write the number of parts.
88  W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));
89  // Write the offsets for the part headers for each part.
90  for (uint64_t Offset : PartOffsets)
91  W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));
92 
93  for (const MCSection &Sec : Asm) {
95  // Skip empty sections.
96  if (SectionSize == 0)
97  continue;
98 
99  unsigned Start = W.OS.tell();
100  // Write section header.
101  W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));
102 
103  uint64_t PartSize = SectionSize + sizeof(dxbc::PartHeader);
104 
105  if (Sec.getName() == "DXIL")
106  PartSize += sizeof(dxbc::ProgramHeader);
107  // DXContainer parts should be 4-byte aligned.
108  PartSize = alignTo(PartSize, Align(4));
109  W.write<uint32_t>(static_cast<uint32_t>(PartSize));
110  if (Sec.getName() == "DXIL") {
111  dxbc::ProgramHeader Header;
112  memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));
113 
114  const Triple &TT = Asm.getContext().getTargetTriple();
115  VersionTuple Version = TT.getOSVersion();
116  Header.MajorVersion = static_cast<uint8_t>(Version.getMajor());
117  if (Version.getMinor())
118  Header.MinorVersion = static_cast<uint8_t>(*Version.getMinor());
119  if (TT.hasEnvironment())
120  Header.ShaderKind =
121  static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
122 
123  // The program header's size field is in 32-bit words.
124  Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
125  memcpy(Header.Bitcode.Magic, "DXIL", 4);
126  Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
127  Header.Bitcode.Size = SectionSize;
129  Header.swapBytes();
130  W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),
131  sizeof(dxbc::ProgramHeader)));
132  }
133  Asm.writeSectionData(W.OS, &Sec, Layout);
134  unsigned Size = W.OS.tell() - Start;
135  W.OS.write_zeros(offsetToAlignment(Size, Align(4)));
136  }
137  return 0;
138 }
139 
140 std::unique_ptr<MCObjectWriter> llvm::createDXContainerObjectWriter(
141  std::unique_ptr<MCDXContainerTargetWriter> MOTW, raw_pwrite_stream &OS) {
142  return std::make_unique<DXContainerObjectWriter>(std::move(MOTW), OS);
143 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:77
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::sys::IsBigEndianHost
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:98
llvm::ARM::PredBlockMask::TT
@ TT
llvm::createDXContainerObjectWriter
std::unique_ptr< MCObjectWriter > createDXContainerObjectWriter(std::unique_ptr< MCDXContainerTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new DXContainer writer instance.
Definition: MCDXContainerWriter.cpp:140
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
llvm::SmallVector< uint64_t, 16 >
llvm::RISCVFenceField::W
@ W
Definition: RISCVBaseInfo.h:242
llvm::dxbc::ProgramHeader::Size
uint32_t Size
Definition: DXContainer.h:116
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCAssembler.h
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::dxbc::BitcodeHeader::Magic
uint8_t Magic[4]
Definition: DXContainer.h:95
llvm::support::endian::Writer
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52
llvm::MCFragment
Definition: MCFragment.h:30
llvm::dxbc::PartHeader
Use this type to describe the size and type of a DXIL container part.
Definition: DXContainer.h:83
llvm::support::little
@ little
Definition: Endian.h:27
MCContext.h
llvm::VersionTuple
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:31
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:1027
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
llvm::MCAssembler
Definition: MCAssembler.h:73
llvm::AMDGPU::Hwreg::Offset
Offset
Definition: SIDefines.h:416
uint64_t
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:418
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::COFF::SectionSize
@ SectionSize
Definition: COFF.h:60
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
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:1675
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
DXContainer.h
MCSection.h
llvm::Triple::Pixel
@ Pixel
Definition: Triple.h:251
MCDXContainerWriter.h
llvm::ArrayRef< char >
llvm::MCDXContainerTargetWriter::~MCDXContainerTargetWriter
virtual ~MCDXContainerTargetWriter()
Definition: MCDXContainerWriter.cpp:21
uint32_t
llvm::MCObjectWriter
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Definition: MCObjectWriter.h:34
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::dxbc::ProgramHeader::swapBytes
void swapBytes()
Definition: DXContainer.h:119
llvm::dxbc::ProgramHeader::MinorVersion
uint8_t MinorVersion
Definition: DXContainer.h:112
llvm::dxbc::ProgramHeader::MajorVersion
uint8_t MajorVersion
Definition: DXContainer.h:113
llvm::dxbc::BitcodeHeader::Size
uint32_t Size
Definition: DXContainer.h:100
MCAsmLayout.h
llvm::dxbc::BitcodeHeader
Definition: DXContainer.h:94
Alignment.h
std
Definition: BitVector.h:851
EndianStream.h
uint16_t
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::dxbc::ProgramHeader::ShaderKind
uint16_t ShaderKind
Definition: DXContainer.h:115
llvm::dxbc::BitcodeHeader::Offset
uint32_t Offset
Definition: DXContainer.h:99
MCValue.h
llvm::dxbc::ProgramHeader::Bitcode
BitcodeHeader Bitcode
Definition: DXContainer.h:117
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::dxbc::Header
Definition: DXContainer.h:66
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::MCAsmLayout::getSectionAddressSize
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:198
llvm::dxbc::ProgramHeader
Definition: DXContainer.h:111
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::offsetToAlignment
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:198