LLVM 17.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, support::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
47 // TODO: set the version on a min-necessary basis (just like the translator
48 // does) requires some refactoring of MCAssembler::VersionInfoType.
49 constexpr uint32_t Major = 1;
50 constexpr uint32_t Minor = 0;
51 constexpr uint32_t VersionNumber = 0 | (Major << 16) | (Minor << 8);
52 // TODO: check if we could use anything other than 0 (spec allows).
53 constexpr uint32_t GeneratorMagicNumber = 0;
54 // TODO: do not hardcode this as well.
55 constexpr uint32_t Bound = 900;
56 constexpr uint32_t Schema = 0;
57
58 W.write<uint32_t>(MagicNumber);
59 W.write<uint32_t>(VersionNumber);
60 W.write<uint32_t>(GeneratorMagicNumber);
61 W.write<uint32_t>(Bound);
62 W.write<uint32_t>(Schema);
63}
64
65uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm,
66 const MCAsmLayout &Layout) {
67 uint64_t StartOffset = W.OS.tell();
68 writeHeader(Asm);
69 for (const MCSection &S : Asm)
70 Asm.writeSectionData(W.OS, &S, Layout);
71 return W.OS.tell() - StartOffset;
72}
73
74std::unique_ptr<MCObjectWriter>
75llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
77 return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);
78}
PowerPC TLS Dynamic Call Fixup
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
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:428
MagicNumber
Definition: XCOFF.h:47
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:1862
Definition: BitVector.h:851
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52