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
17void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) {
18 constexpr uint32_t MagicNumber = 0x07230203;
19 constexpr uint32_t GeneratorID = 43;
20 constexpr uint32_t GeneratorMagicNumber =
21 (GeneratorID << 16) | (LLVM_VERSION_MAJOR);
22 constexpr uint32_t Schema = 0;
23
25 W.write<uint32_t>((VersionInfo.Major << 16) | (VersionInfo.Minor << 8));
26 W.write<uint32_t>(GeneratorMagicNumber);
27 W.write<uint32_t>(VersionInfo.Bound);
28 W.write<uint32_t>(Schema);
29}
30
31void SPIRVObjectWriter::setBuildVersion(unsigned Major, unsigned Minor,
32 unsigned Bound) {
33 VersionInfo.Major = Major;
34 VersionInfo.Minor = Minor;
35 VersionInfo.Bound = Bound;
36}
37
38uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm) {
39 uint64_t StartOffset = W.OS.tell();
40 writeHeader(Asm);
41 for (const MCSection &S : Asm)
42 Asm.writeSectionData(W.OS, &S);
43 return W.OS.tell() - StartOffset;
44}
45
46std::unique_ptr<MCObjectWriter>
47llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
49 return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);
50}
raw_pwrite_stream & OS
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
void setBuildVersion(unsigned Major, unsigned Minor, unsigned Bound)
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:147
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:434
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.
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:71