LLVM 19.0.0git
SPIRVObjectWriter.cpp
Go to the documentation of this file.
1//===- llvm/MC/MCSPIRVObjectWriter.cpp - SPIR-V Object 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/MCSection.h"
12#include "llvm/MC/MCValue.h"
14
15using namespace llvm;
16
17namespace {
18class SPIRVObjectWriter : public MCObjectWriter {
20
21 /// The target specific SPIR-V writer instance.
22 std::unique_ptr<MCSPIRVObjectTargetWriter> TargetObjectWriter;
23
24public:
25 SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
27 : W(OS, llvm::endianness::little), TargetObjectWriter(std::move(MOTW)) {}
28
29 ~SPIRVObjectWriter() override {}
30
31private:
32 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
33 const MCFragment *Fragment, const MCFixup &Fixup,
34 MCValue Target, uint64_t &FixedValue) override {}
35
37 const MCAsmLayout &Layout) override {}
38
39 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
40 void writeHeader(const MCAssembler &Asm);
41};
42} // namespace
43
44void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) {
45 constexpr uint32_t MagicNumber = 0x07230203;
46 constexpr uint32_t GeneratorMagicNumber = 0;
47 constexpr uint32_t Schema = 0;
48
49 // Construct SPIR-V version and Bound
50 const MCAssembler::VersionInfoType &VIT = Asm.getVersionInfo();
51 uint32_t VersionNumber = 0 | (VIT.Major << 16) | (VIT.Minor << 8);
52 uint32_t Bound = VIT.Update;
53
54 W.write<uint32_t>(MagicNumber);
55 W.write<uint32_t>(VersionNumber);
56 W.write<uint32_t>(GeneratorMagicNumber);
57 W.write<uint32_t>(Bound);
58 W.write<uint32_t>(Schema);
59}
60
61uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm,
62 const MCAsmLayout &Layout) {
63 uint64_t StartOffset = W.OS.tell();
64 writeHeader(Asm);
65 for (const MCSection &S : Asm)
66 Asm.writeSectionData(W.OS, &S, Layout);
67 return W.OS.tell() - StartOffset;
68}
69
70std::unique_ptr<MCObjectWriter>
71llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
73 return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);
74}
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
{ bool EmitBuildVersion VersionInfoType
MachO specific deployment target version info.
Definition: MCAssembler.h:102
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...
virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Write the object file and returns the number of bytes written.
virtual void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
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
Target - Wrapper for Target specific information.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:444
MagicNumber
Definition: XCOFF.h:48
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectWriter > createSPIRVObjectWriter(std::unique_ptr< MCSPIRVObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new SPIR-V writer instance.
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:1858
endianness
Definition: bit.h:70
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:67