54 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/MC/MCAssembler.h"10#include "llvm/MC/MCContext.h"11#include "llvm/MC/MCSPIRVObjectWriter.h"12#include "llvm/MC/MCSection.h"13#include "llvm/MC/MCValue.h"14#include "llvm/Support/EndianStream.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::AMD23 ? UINT16_MAX24 : ((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 41uint64_t SPIRVObjectWriter::writeObject() {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,51 raw_pwrite_stream &OS) {52 return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);53}54