llvm-project/llvm/lib/MC/SPIRVObjectWriter.cpp
Ilia Diachkov 6c69427e88 [SPIR-V](3/6) Add MC layer, object file support, and InstPrinter
The patch adds SPIRV-specific MC layer implementation, SPIRV object
file support and SPIRVInstPrinter.

Differential Revision: https://reviews.llvm.org/D116462

Authors: Aleksandr Bezzubikov, Lewis Crawford, Ilia Diachkov,
Michal Paszkowski, Andrey Tretyakov, Konrad Trifunovic

Co-authored-by: Aleksandr Bezzubikov <zuban32s@gmail.com>
Co-authored-by: Ilia Diachkov <iliya.diyachkov@intel.com>
Co-authored-by: Michal Paszkowski <michal.paszkowski@outlook.com>
Co-authored-by: Andrey Tretyakov <andrey1.tretyakov@intel.com>
Co-authored-by: Konrad Trifunovic <konrad.trifunovic@intel.com>
2022-04-20 01:10:25 +02:00

77 lines
2.7 KiB
C++

//===- 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;
class SPIRVObjectWriter : public MCObjectWriter {
::support::endian::Writer W;
/// The target specific SPIR-V writer instance.
std::unique_ptr<MCSPIRVObjectTargetWriter> TargetObjectWriter;
public:
SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,
raw_pwrite_stream &OS)
: W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
~SPIRVObjectWriter() override {}
private:
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue) override {}
void executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override {}
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
void writeHeader(const MCAssembler &Asm);
};
void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) {
constexpr uint32_t MagicNumber = 0x07230203;
// TODO: set the version on a min-necessary basis (just like the translator
// does) requires some refactoring of MCAssembler::VersionInfoType.
constexpr uint32_t Major = 1;
constexpr uint32_t Minor = 0;
constexpr uint32_t VersionNumber = 0 | (Major << 16) | (Minor << 8);
// TODO: check if we could use anything other than 0 (spec allows).
constexpr uint32_t GeneratorMagicNumber = 0;
// TODO: do not hardcode this as well.
constexpr uint32_t Bound = 900;
constexpr uint32_t Schema = 0;
W.write<uint32_t>(MagicNumber);
W.write<uint32_t>(VersionNumber);
W.write<uint32_t>(GeneratorMagicNumber);
W.write<uint32_t>(Bound);
W.write<uint32_t>(Schema);
}
uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
uint64_t StartOffset = W.OS.tell();
writeHeader(Asm);
for (const MCSection &S : Asm)
Asm.writeSectionData(W.OS, &S, Layout);
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);
}