MLIR  16.0.0git
SPIRVBinaryUtils.cpp
Go to the documentation of this file.
1 //===- SPIRVBinaryUtils.cpp - MLIR SPIR-V Binary Module Utilities ---------===//
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 defines common utilities for SPIR-V binary module.
10 //
11 //===----------------------------------------------------------------------===//
12 
15 
16 using namespace mlir;
17 
19  spirv::Version version, uint32_t idBound) {
20  uint32_t majorVersion = 1;
21  uint32_t minorVersion = 0;
22  switch (version) {
23 #define MIN_VERSION_CASE(v) \
24  case spirv::Version::V_1_##v: \
25  minorVersion = v; \
26  break
27 
35 #undef MIN_VERSION_CASE
36  }
37 
38  // See "2.3. Physical Layout of a SPIR-V Module and Instruction" in the SPIR-V
39  // spec for the definition of the binary module header.
40  //
41  // The first five words of a SPIR-V module must be:
42  // +-------------------------------------------------------------------------+
43  // | Magic number |
44  // +-------------------------------------------------------------------------+
45  // | Version number (bytes: 0 | major number | minor number | 0) |
46  // +-------------------------------------------------------------------------+
47  // | Generator magic number |
48  // +-------------------------------------------------------------------------+
49  // | Bound (all result <id>s in the module guaranteed to be less than it) |
50  // +-------------------------------------------------------------------------+
51  // | 0 (reserved for instruction schema) |
52  // +-------------------------------------------------------------------------+
53  header.push_back(spirv::kMagicNumber);
54  header.push_back((majorVersion << 16) | (minorVersion << 8));
55  header.push_back(kGeneratorNumber);
56  header.push_back(idBound); // <id> bound
57  header.push_back(0); // Schema (reserved word)
58 }
59 
60 /// Returns the word-count-prefixed opcode for an SPIR-V instruction.
61 uint32_t spirv::getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode) {
62  assert(((wordCount >> 16) == 0) && "word count out of range!");
63  return (wordCount << 16) | static_cast<uint32_t>(opcode);
64 }
65 
67  StringRef literal) {
68  // We need to encode the literal and the null termination.
69  auto encodingSize = literal.size() / 4 + 1;
70  auto bufferStartSize = binary.size();
71  binary.resize(bufferStartSize + encodingSize, 0);
72  std::memcpy(binary.data() + bufferStartSize, literal.data(), literal.size());
73 }
Include the generated interface declarations.
#define MIN_VERSION_CASE(v)
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.
uint32_t getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode)
Returns the word-count-prefixed opcode for an SPIR-V instruction.
constexpr uint32_t kMagicNumber
SPIR-V magic number.
constexpr uint32_t kGeneratorNumber
The serializer tool ID registered to the Khronos Group.
void encodeStringLiteralInto(SmallVectorImpl< uint32_t > &binary, StringRef literal)
Encodes an SPIR-V literal string into the given binary vector.