LLVM  15.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 
9 #include "llvm/MC/MCAssembler.h"
11 #include "llvm/MC/MCSection.h"
12 #include "llvm/MC/MCValue.h"
14 
15 using namespace llvm;
16 
19 
20  /// The target specific SPIR-V writer instance.
21  std::unique_ptr<MCSPIRVObjectTargetWriter> TargetObjectWriter;
22 
23 public:
24  SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
26  : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
27 
28  ~SPIRVObjectWriter() override {}
29 
30 private:
31  void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
32  const MCFragment *Fragment, const MCFixup &Fixup,
33  MCValue Target, uint64_t &FixedValue) override {}
34 
35  void executePostLayoutBinding(MCAssembler &Asm,
36  const MCAsmLayout &Layout) override {}
37 
38  uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
39  void writeHeader(const MCAssembler &Asm);
40 };
41 
42 void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) {
43  constexpr uint32_t MagicNumber = 0x07230203;
44 
45  // TODO: set the version on a min-necessary basis (just like the translator
46  // does) requires some refactoring of MCAssembler::VersionInfoType.
47  constexpr uint32_t Major = 1;
48  constexpr uint32_t Minor = 0;
49  constexpr uint32_t VersionNumber = 0 | (Major << 16) | (Minor << 8);
50  // TODO: check if we could use anything other than 0 (spec allows).
51  constexpr uint32_t GeneratorMagicNumber = 0;
52  // TODO: do not hardcode this as well.
53  constexpr uint32_t Bound = 900;
54  constexpr uint32_t Schema = 0;
55 
56  W.write<uint32_t>(MagicNumber);
57  W.write<uint32_t>(VersionNumber);
58  W.write<uint32_t>(GeneratorMagicNumber);
59  W.write<uint32_t>(Bound);
60  W.write<uint32_t>(Schema);
61 }
62 
63 uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm,
64  const MCAsmLayout &Layout) {
65  uint64_t StartOffset = W.OS.tell();
66  writeHeader(Asm);
67  for (const MCSection &S : Asm)
68  Asm.writeSectionData(W.OS, &S, Layout);
69  return W.OS.tell() - StartOffset;
70 }
71 
72 std::unique_ptr<MCObjectWriter>
73 llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
74  raw_pwrite_stream &OS) {
75  return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);
76 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
SPIRVObjectWriter::SPIRVObjectWriter
SPIRVObjectWriter(std::unique_ptr< MCSPIRVObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Definition: SPIRVObjectWriter.cpp:24
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
llvm::RISCVFenceField::W
@ W
Definition: RISCVBaseInfo.h:241
MCAssembler.h
llvm::createSPIRVObjectWriter
std::unique_ptr< MCObjectWriter > createSPIRVObjectWriter(std::unique_ptr< MCSPIRVObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new SPIR-V writer instance.
Definition: SPIRVObjectWriter.cpp:73
MCSPIRVObjectWriter.h
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
SPIRVObjectWriter::~SPIRVObjectWriter
~SPIRVObjectWriter() override
Definition: SPIRVObjectWriter.cpp:28
llvm::support::little
@ little
Definition: Endian.h:27
SPIRVObjectWriter
Definition: SPIRVObjectWriter.cpp:17
llvm::MCAssembler
Definition: MCAssembler.h:73
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::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
MCSection.h
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
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
std
Definition: BitVector.h:851
EndianStream.h
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::XCOFF::MagicNumber
MagicNumber
Definition: XCOFF.h:44
MCValue.h
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::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71