MLIR 22.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
21namespace mlir {
22namespace spirv {
23
24/// SPIR-V binary header word count
25constexpr unsigned kHeaderWordCount = 5;
26
27/// SPIR-V magic number
28constexpr uint32_t kMagicNumber = 0x07230203;
29
30/// The serializer tool ID registered to the Khronos Group
31constexpr uint32_t kGeneratorNumber = 22;
32
33/// Max number of words
34/// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_universal_limits
35constexpr uint32_t kMaxWordCount = 65535;
36
37/// Max number of words for literal
38constexpr uint32_t kMaxLiteralWordCount = kMaxWordCount - 3;
39
40/// Appends a SPRI-V module header to `header` with the given `version` and
41/// `idBound`.
43 spirv::Version version, uint32_t idBound);
44
45/// Returns the word-count-prefixed opcode for an SPIR-V instruction.
46uint32_t getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode);
47
48/// Encodes an SPIR-V `literal` string into the given `binary` vector.
50 StringRef literal);
51
52/// Decodes a string literal in `words` starting at `wordIndex`. Update the
53/// latter to point to the position in words after the string literal.
55 unsigned &wordIndex) {
56 StringRef str(reinterpret_cast<const char *>(words.data() + wordIndex));
57 wordIndex += str.size() / 4 + 1;
58 return str;
59}
60
61} // namespace spirv
62} // namespace mlir
63
64#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 kMaxLiteralWordCount
Max number of words for literal.
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.
constexpr uint32_t kMaxWordCount
Max number of words https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_universal_limits.
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.