llvm-project/llvm/lib/MC/SPIRVObjectWriter.cpp
Alex Voicu 0307147105
[NFC][SPIRV] Add AMDGCN SPIR-V specific defaults to the BE (#165815)
AMDGCN flavoured SPIR-V has slightly different defaults from what the BE
adopts: it assumes all extensions are enabled, and expects nonsemantic
debug info to be generated. Furthermore, it is necessary to encode in
the resulting SPIR-V binary that what was generated was AMDGCN
flavoured, which we do by setting the Generator Version to `UINT16_MAX`
(which matches what we expect to see at reverse translation). We will
register this generator version at
<https://github.com/KhronosGroup/SPIRV-Headers>. This is a preliminary
patch out of a series of patches that are needed for adopting the BE for
AMDGCN flavoured SPIR-V generation.
2025-11-04 12:45:53 +00:00

54 lines
1.8 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/MCContext.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;
constexpr uint32_t GeneratorID = 43;
const uint32_t GeneratorMagicNumber =
Asm.getContext().getTargetTriple().getVendor() == Triple::AMD
? UINT16_MAX
: ((GeneratorID << 16) | (LLVM_VERSION_MAJOR));
constexpr uint32_t Schema = 0;
W.write<uint32_t>(MagicNumber);
W.write<uint32_t>((VersionInfo.Major << 16) | (VersionInfo.Minor << 8));
W.write<uint32_t>(GeneratorMagicNumber);
W.write<uint32_t>(VersionInfo.Bound);
W.write<uint32_t>(Schema);
}
void SPIRVObjectWriter::setBuildVersion(unsigned Major, unsigned Minor,
unsigned Bound) {
VersionInfo.Major = Major;
VersionInfo.Minor = Minor;
VersionInfo.Bound = Bound;
}
uint64_t SPIRVObjectWriter::writeObject() {
uint64_t StartOffset = W.OS.tell();
writeHeader(*Asm);
for (const MCSection &S : *Asm)
Asm->writeSectionData(W.OS, &S);
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);
}