LLVM 22.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
10#include "llvm/MC/MCContext.h"
12#include "llvm/MC/MCSection.h"
13#include "llvm/MC/MCValue.h"
15
16using namespace llvm;
17
18void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) {
19 constexpr uint32_t MagicNumber = 0x07230203;
20 constexpr uint32_t GeneratorID = 43;
21 const uint32_t GeneratorMagicNumber =
22 Asm.getContext().getTargetTriple().getVendor() == Triple::AMD
23 ? UINT16_MAX
24 : ((GeneratorID << 16) | (LLVM_VERSION_MAJOR));
25 constexpr uint32_t Schema = 0;
26
27 W.write<uint32_t>(MagicNumber);
28 W.write<uint32_t>((VersionInfo.Major << 16) | (VersionInfo.Minor << 8));
29 W.write<uint32_t>(GeneratorMagicNumber);
30 W.write<uint32_t>(VersionInfo.Bound);
31 W.write<uint32_t>(Schema);
32}
33
34void SPIRVObjectWriter::setBuildVersion(unsigned Major, unsigned Minor,
35 unsigned Bound) {
36 VersionInfo.Major = Major;
37 VersionInfo.Minor = Minor;
38 VersionInfo.Bound = Bound;
39}
40
42 uint64_t StartOffset = W.OS.tell();
43 writeHeader(*Asm);
44 for (const MCSection &S : *Asm)
45 Asm->writeSectionData(W.OS, &S);
46 return W.OS.tell() - StartOffset;
47}
48
49std::unique_ptr<MCObjectWriter>
50llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
52 return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);
53}
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
virtual uint64_t writeObject()=0
Write the object file and returns the number of bytes written.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:521
void setBuildVersion(unsigned Major, unsigned Minor, unsigned Bound)
uint64_t tell() const
tell - Return the current offset with the file.
An abstract base class for streams implementations that also support a pwrite operation.
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectWriter > createSPIRVObjectWriter(std::unique_ptr< MCSPIRVObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new SPIR-V writer instance.