2022-04-14 01:10:08 +03:00
|
|
|
//===- llvm/MC/MCSPIRVObjectWriter.cpp - SPIR-V Object Writer ----*- C++ *-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCSPIRVObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/EndianStream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) {
|
|
|
|
constexpr uint32_t MagicNumber = 0x07230203;
|
2024-04-22 10:47:46 -07:00
|
|
|
constexpr uint32_t GeneratorID = 43;
|
|
|
|
constexpr uint32_t GeneratorMagicNumber =
|
|
|
|
(GeneratorID << 16) | (LLVM_VERSION_MAJOR);
|
2022-04-14 01:10:08 +03:00
|
|
|
constexpr uint32_t Schema = 0;
|
2024-03-18 11:42:44 +01:00
|
|
|
|
2022-04-14 01:10:08 +03:00
|
|
|
W.write<uint32_t>(MagicNumber);
|
2024-07-21 12:48:09 -07:00
|
|
|
W.write<uint32_t>((VersionInfo.Major << 16) | (VersionInfo.Minor << 8));
|
2022-04-14 01:10:08 +03:00
|
|
|
W.write<uint32_t>(GeneratorMagicNumber);
|
2024-07-21 12:48:09 -07:00
|
|
|
W.write<uint32_t>(VersionInfo.Bound);
|
2022-04-14 01:10:08 +03:00
|
|
|
W.write<uint32_t>(Schema);
|
|
|
|
}
|
|
|
|
|
2024-07-21 12:48:09 -07:00
|
|
|
void SPIRVObjectWriter::setBuildVersion(unsigned Major, unsigned Minor,
|
|
|
|
unsigned Bound) {
|
|
|
|
VersionInfo.Major = Major;
|
|
|
|
VersionInfo.Minor = Minor;
|
|
|
|
VersionInfo.Bound = Bound;
|
|
|
|
}
|
|
|
|
|
2024-07-01 10:04:58 -07:00
|
|
|
uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm) {
|
2022-04-14 01:10:08 +03:00
|
|
|
uint64_t StartOffset = W.OS.tell();
|
|
|
|
writeHeader(Asm);
|
|
|
|
for (const MCSection &S : Asm)
|
2024-07-01 10:04:58 -07:00
|
|
|
Asm.writeSectionData(W.OS, &S);
|
2022-04-14 01:10:08 +03:00
|
|
|
return W.OS.tell() - StartOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MCObjectWriter>
|
|
|
|
llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS) {
|
|
|
|
return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS);
|
|
|
|
}
|