MLIR  19.0.0git
SPIRVBinaryUtils.h
Go to the documentation of this file.
1 //===- SPIRVBinaryUtils.cpp - SPIR-V Binary Module Utils --------*- 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 // This file declares common utilities for SPIR-V binary module.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_TARGET_SPIRV_SPIRVBINARYUTILS_H
14 #define MLIR_TARGET_SPIRV_SPIRVBINARYUTILS_H
15 
17 #include "mlir/Support/LLVM.h"
18 
19 #include <cstdint>
20 
21 namespace mlir {
22 namespace spirv {
23 
24 /// SPIR-V binary header word count
25 constexpr unsigned kHeaderWordCount = 5;
26 
27 /// SPIR-V magic number
28 constexpr uint32_t kMagicNumber = 0x07230203;
29 
30 /// The serializer tool ID registered to the Khronos Group
31 constexpr uint32_t kGeneratorNumber = 22;
32 
33 /// Appends a SPRI-V module header to `header` with the given `version` and
34 /// `idBound`.
36  spirv::Version version, uint32_t idBound);
37 
38 /// Returns the word-count-prefixed opcode for an SPIR-V instruction.
39 uint32_t getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode);
40 
41 /// Encodes an SPIR-V `literal` string into the given `binary` vector.
43  StringRef literal);
44 
45 /// Decodes a string literal in `words` starting at `wordIndex`. Update the
46 /// latter to point to the position in words after the string literal.
47 inline StringRef decodeStringLiteral(ArrayRef<uint32_t> words,
48  unsigned &wordIndex) {
49  StringRef str(reinterpret_cast<const char *>(words.data() + wordIndex));
50  wordIndex += str.size() / 4 + 1;
51  return str;
52 }
53 
54 } // namespace spirv
55 } // namespace mlir
56 
57 #endif // MLIR_TARGET_SPIRV_SPIRVBINARYUTILS_H
void encodeStringLiteralInto(SmallVectorImpl< uint32_t > &binary, StringRef literal)
Encodes an SPIR-V literal string into the given binary vector.
constexpr uint32_t kGeneratorNumber
The serializer tool ID registered to the Khronos Group.
constexpr uint32_t kMagicNumber
SPIR-V magic number.
uint32_t getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode)
Returns the word-count-prefixed opcode for an SPIR-V instruction.
void appendModuleHeader(SmallVectorImpl< uint32_t > &header, spirv::Version version, uint32_t idBound)
Appends a SPRI-V module header to header with the given version and idBound.
StringRef decodeStringLiteral(ArrayRef< uint32_t > words, unsigned &wordIndex)
Decodes a string literal in words starting at wordIndex.
constexpr unsigned kHeaderWordCount
SPIR-V binary header word count.
Include the generated interface declarations.