1 //===- llvm/MC/MCSPIRVObjectWriter.cpp - SPIR-V Object Writer ----*- C++ *-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCAssembler.h"
10 #include "llvm/MC/MCSPIRVObjectWriter.h"
11 #include "llvm/MC/MCSection.h"
12 #include "llvm/MC/MCValue.h"
13 #include "llvm/Support/EndianStream.h"
18 class SPIRVObjectWriter
: public MCObjectWriter
{
19 ::support::endian::Writer W
;
21 /// The target specific SPIR-V writer instance.
22 std::unique_ptr
<MCSPIRVObjectTargetWriter
> TargetObjectWriter
;
25 SPIRVObjectWriter(std::unique_ptr
<MCSPIRVObjectTargetWriter
> MOTW
,
26 raw_pwrite_stream
&OS
)
27 : W(OS
, llvm::endianness::little
), TargetObjectWriter(std::move(MOTW
)) {}
29 ~SPIRVObjectWriter() override
{}
32 void recordRelocation(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
33 const MCFragment
*Fragment
, const MCFixup
&Fixup
,
34 MCValue Target
, uint64_t &FixedValue
) override
{}
36 void executePostLayoutBinding(MCAssembler
&Asm
,
37 const MCAsmLayout
&Layout
) override
{}
39 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
) override
;
40 void writeHeader(const MCAssembler
&Asm
);
44 void SPIRVObjectWriter::writeHeader(const MCAssembler
&Asm
) {
45 constexpr uint32_t MagicNumber
= 0x07230203;
47 // TODO: set the version on a min-necessary basis (just like the translator
48 // does) requires some refactoring of MCAssembler::VersionInfoType.
49 constexpr uint32_t Major
= 1;
50 constexpr uint32_t Minor
= 0;
51 constexpr uint32_t VersionNumber
= 0 | (Major
<< 16) | (Minor
<< 8);
52 // TODO: check if we could use anything other than 0 (spec allows).
53 constexpr uint32_t GeneratorMagicNumber
= 0;
54 // TODO: do not hardcode this as well.
55 constexpr uint32_t Bound
= 900;
56 constexpr uint32_t Schema
= 0;
58 W
.write
<uint32_t>(MagicNumber
);
59 W
.write
<uint32_t>(VersionNumber
);
60 W
.write
<uint32_t>(GeneratorMagicNumber
);
61 W
.write
<uint32_t>(Bound
);
62 W
.write
<uint32_t>(Schema
);
65 uint64_t SPIRVObjectWriter::writeObject(MCAssembler
&Asm
,
66 const MCAsmLayout
&Layout
) {
67 uint64_t StartOffset
= W
.OS
.tell();
69 for (const MCSection
&S
: Asm
)
70 Asm
.writeSectionData(W
.OS
, &S
, Layout
);
71 return W
.OS
.tell() - StartOffset
;
74 std::unique_ptr
<MCObjectWriter
>
75 llvm::createSPIRVObjectWriter(std::unique_ptr
<MCSPIRVObjectTargetWriter
> MOTW
,
76 raw_pwrite_stream
&OS
) {
77 return std::make_unique
<SPIRVObjectWriter
>(std::move(MOTW
), OS
);