1 //===-- llvm/MC/MCSPIRVObjectWriter.h - 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 #ifndef LLVM_MC_MCSPIRVOBJECTWRITER_H 10 #define LLVM_MC_MCSPIRVOBJECTWRITER_H 11 12 #include "llvm/MC/MCObjectWriter.h" 13 #include "llvm/MC/MCValue.h" 14 #include "llvm/Support/EndianStream.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include <memory> 17 18 namespace llvm { 19 20 class MCSPIRVObjectTargetWriter : public MCObjectTargetWriter { 21 public: getFormat()22 Triple::ObjectFormatType getFormat() const override { return Triple::SPIRV; } classof(const MCObjectTargetWriter * W)23 static bool classof(const MCObjectTargetWriter *W) { 24 return W->getFormat() == Triple::SPIRV; 25 } 26 }; 27 28 class SPIRVObjectWriter final : public MCObjectWriter { 29 support::endian::Writer W; 30 std::unique_ptr<MCSPIRVObjectTargetWriter> TargetObjectWriter; 31 32 struct VersionInfoType { 33 unsigned Major = 0; 34 unsigned Minor = 0; 35 unsigned Bound = 0; 36 } VersionInfo; 37 38 public: SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,raw_pwrite_stream & OS)39 SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW, 40 raw_pwrite_stream &OS) 41 : W(OS, llvm::endianness::little), TargetObjectWriter(std::move(MOTW)) {} 42 43 void setBuildVersion(unsigned Major, unsigned Minor, unsigned Bound); 44 45 private: 46 uint64_t writeObject() override; 47 void writeHeader(const MCAssembler &Asm); 48 }; 49 50 /// Construct a new SPIR-V writer instance. 51 /// 52 /// \param MOTW - The target specific SPIR-V writer subclass. 53 /// \param OS - The stream to write to. 54 /// \returns The constructed object writer. 55 std::unique_ptr<MCObjectWriter> 56 createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW, 57 raw_pwrite_stream &OS); 58 59 } // namespace llvm 60 61 #endif 62