MLIR 23.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#include <optional>
21
22namespace mlir {
23namespace spirv {
24
25/// SPIR-V binary header word count
26constexpr unsigned kHeaderWordCount = 5;
27
28/// SPIR-V magic number
29constexpr uint32_t kMagicNumber = 0x07230203;
30
31/// The serializer tool ID registered to the Khronos Group
32constexpr uint32_t kGeneratorNumber = 22;
33
34/// Max number of words
35/// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_universal_limits
36constexpr uint32_t kMaxWordCount = 65535;
37
38/// Max number of words for literal
39constexpr uint32_t kMaxLiteralWordCount = kMaxWordCount - 3;
40
41/// Appends a SPRI-V module header to `header` with the given `version` and
42/// `idBound`.
44 spirv::Version version, uint32_t idBound);
45
46/// Returns the word-count-prefixed opcode for an SPIR-V instruction.
47uint32_t getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode);
48
49/// Encodes an SPIR-V `literal` string into the given `binary` vector.
51 StringRef literal);
52
53/// Decodes a string literal in `words` starting at `wordIndex`. Update the
54/// latter to point to the position in words after the string literal.
56 unsigned &wordIndex) {
57 StringRef str(reinterpret_cast<const char *>(words.data() + wordIndex));
58 wordIndex += str.size() / 4 + 1;
59 return str;
60}
61
62/// Returns the SPV_INTEL_long_composites continuation opcode that may follow
63/// `parent`, or std::nullopt if `parent` is not a splittable composite/struct
64/// op.
65inline std::optional<spirv::Opcode>
66getContinuationOpcode(spirv::Opcode parent) {
67 switch (parent) {
68 case spirv::Opcode::OpTypeStruct:
69 return spirv::Opcode::OpTypeStructContinuedINTEL;
70 case spirv::Opcode::OpConstantComposite:
71 return spirv::Opcode::OpConstantCompositeContinuedINTEL;
72 case spirv::Opcode::OpSpecConstantComposite:
73 return spirv::Opcode::OpSpecConstantCompositeContinuedINTEL;
74 case spirv::Opcode::OpCompositeConstruct:
75 return spirv::Opcode::OpCompositeConstructContinuedINTEL;
76 default:
77 return std::nullopt;
78 }
79}
80
81} // namespace spirv
82} // namespace mlir
83
84#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.
std::optional< spirv::Opcode > getContinuationOpcode(spirv::Opcode parent)
Returns the SPV_INTEL_long_composites continuation opcode that may follow parent, or std::nullopt if ...
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.