| 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" |
| 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 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | void 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>(Val: MagicNumber); |
| 28 | W.write<uint32_t>(Val: (VersionInfo.Major << 16) | (VersionInfo.Minor << 8)); |
| 29 | W.write<uint32_t>(Val: GeneratorMagicNumber); |
| 30 | W.write<uint32_t>(Val: VersionInfo.Bound); |
| 31 | W.write<uint32_t>(Val: Schema); |
| 32 | } |
| 33 | |
| 34 | void SPIRVObjectWriter::setBuildVersion(unsigned Major, unsigned Minor, |
| 35 | unsigned Bound) { |
| 36 | VersionInfo.Major = Major; |
| 37 | VersionInfo.Minor = Minor; |
| 38 | VersionInfo.Bound = Bound; |
| 39 | } |
| 40 | |
| 41 | uint64_t SPIRVObjectWriter::writeObject() { |
| 42 | uint64_t StartOffset = W.OS.tell(); |
| 43 | writeHeader(Asm: *Asm); |
| 44 | for (const MCSection &S : *Asm) |
| 45 | Asm->writeSectionData(OS&: W.OS, Section: &S); |
| 46 | return W.OS.tell() - StartOffset; |
| 47 | } |
| 48 | |
| 49 | std::unique_ptr<MCObjectWriter> |
| 50 | llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW, |
| 51 | raw_pwrite_stream &OS) { |
| 52 | return std::make_unique<SPIRVObjectWriter>(args: std::move(MOTW), args&: OS); |
| 53 | } |
| 54 |