Path: blob/main/contrib/llvm-project/llvm/lib/MC/MCDXContainerWriter.cpp
35233 views
//===- llvm/MC/MCDXContainerWriter.cpp - DXContainer Writer -----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/MC/MCDXContainerWriter.h"9#include "llvm/BinaryFormat/DXContainer.h"10#include "llvm/MC/MCAssembler.h"11#include "llvm/MC/MCContext.h"12#include "llvm/MC/MCSection.h"13#include "llvm/MC/MCValue.h"14#include "llvm/Support/Alignment.h"15#include "llvm/Support/EndianStream.h"1617using namespace llvm;1819MCDXContainerTargetWriter::~MCDXContainerTargetWriter() {}2021namespace {22class DXContainerObjectWriter : public MCObjectWriter {23::support::endian::Writer W;2425/// The target specific DXContainer writer instance.26std::unique_ptr<MCDXContainerTargetWriter> TargetObjectWriter;2728public:29DXContainerObjectWriter(std::unique_ptr<MCDXContainerTargetWriter> MOTW,30raw_pwrite_stream &OS)31: W(OS, llvm::endianness::little), TargetObjectWriter(std::move(MOTW)) {}3233~DXContainerObjectWriter() override {}3435private:36void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,37const MCFixup &Fixup, MCValue Target,38uint64_t &FixedValue) override {}3940uint64_t writeObject(MCAssembler &Asm) override;41};42} // namespace4344uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm) {45// Start the file size as the header plus the size of the part offsets.46// Presently DXContainer files usually contain 7-10 parts. Reserving space for47// 16 part offsets gives us a little room for growth.48llvm::SmallVector<uint64_t, 16> PartOffsets;49uint64_t PartOffset = 0;50for (const MCSection &Sec : Asm) {51uint64_t SectionSize = Asm.getSectionAddressSize(Sec);52// Skip empty sections.53if (SectionSize == 0)54continue;5556assert(SectionSize < std::numeric_limits<uint32_t>::max() &&57"Section size too large for DXContainer");5859PartOffsets.push_back(PartOffset);60PartOffset += sizeof(dxbc::PartHeader) + SectionSize;61PartOffset = alignTo(PartOffset, Align(4ul));62// The DXIL part also writes a program header, so we need to include its63// size when computing the offset for a part after the DXIL part.64if (Sec.getName() == "DXIL")65PartOffset += sizeof(dxbc::ProgramHeader);66}67assert(PartOffset < std::numeric_limits<uint32_t>::max() &&68"Part data too large for DXContainer");6970uint64_t PartStart =71sizeof(dxbc::Header) + (PartOffsets.size() * sizeof(uint32_t));72uint64_t FileSize = PartStart + PartOffset;73assert(FileSize < std::numeric_limits<uint32_t>::max() &&74"File size too large for DXContainer");7576// Write the header.77W.write<char>({'D', 'X', 'B', 'C'});78// Write 16-bytes of 0's for the hash.79W.OS.write_zeros(16);80// Write 1.0 for file format version.81W.write<uint16_t>(1u);82W.write<uint16_t>(0u);83// Write the file size.84W.write<uint32_t>(static_cast<uint32_t>(FileSize));85// Write the number of parts.86W.write<uint32_t>(static_cast<uint32_t>(PartOffsets.size()));87// Write the offsets for the part headers for each part.88for (uint64_t Offset : PartOffsets)89W.write<uint32_t>(static_cast<uint32_t>(PartStart + Offset));9091for (const MCSection &Sec : Asm) {92uint64_t SectionSize = Asm.getSectionAddressSize(Sec);93// Skip empty sections.94if (SectionSize == 0)95continue;9697unsigned Start = W.OS.tell();98// Write section header.99W.write<char>(ArrayRef<char>(Sec.getName().data(), 4));100101uint64_t PartSize = SectionSize;102103if (Sec.getName() == "DXIL")104PartSize += sizeof(dxbc::ProgramHeader);105// DXContainer parts should be 4-byte aligned.106PartSize = alignTo(PartSize, Align(4));107W.write<uint32_t>(static_cast<uint32_t>(PartSize));108if (Sec.getName() == "DXIL") {109dxbc::ProgramHeader Header;110memset(reinterpret_cast<void *>(&Header), 0, sizeof(dxbc::ProgramHeader));111112const Triple &TT = Asm.getContext().getTargetTriple();113VersionTuple Version = TT.getOSVersion();114uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());115uint8_t MinorVersion =116static_cast<uint8_t>(Version.getMinor().value_or(0));117Header.Version =118dxbc::ProgramHeader::getVersion(MajorVersion, MinorVersion);119if (TT.hasEnvironment())120Header.ShaderKind =121static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);122123// The program header's size field is in 32-bit words.124Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;125memcpy(Header.Bitcode.Magic, "DXIL", 4);126VersionTuple DXILVersion = TT.getDXILVersion();127Header.Bitcode.MajorVersion = DXILVersion.getMajor();128Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);129Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);130Header.Bitcode.Size = SectionSize;131if (sys::IsBigEndianHost)132Header.swapBytes();133W.write<char>(ArrayRef<char>(reinterpret_cast<char *>(&Header),134sizeof(dxbc::ProgramHeader)));135}136Asm.writeSectionData(W.OS, &Sec);137unsigned Size = W.OS.tell() - Start;138W.OS.write_zeros(offsetToAlignment(Size, Align(4)));139}140return 0;141}142143std::unique_ptr<MCObjectWriter> llvm::createDXContainerObjectWriter(144std::unique_ptr<MCDXContainerTargetWriter> MOTW, raw_pwrite_stream &OS) {145return std::make_unique<DXContainerObjectWriter>(std::move(MOTW), OS);146}147148149