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 | |
13 | #include "mlir/Target/SPIRV/SPIRVBinaryUtils.h" |
14 | #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h" |
15 | #include "llvm/Config/llvm-config.h" // for LLVM_VERSION_MAJOR |
16 | #include "llvm/Support/Debug.h" |
17 | |
18 | #define DEBUG_TYPE "spirv-binary-utils" |
19 | |
20 | using namespace mlir; |
21 | |
22 | void spirv::(SmallVectorImpl<uint32_t> &, |
23 | spirv::Version version, uint32_t idBound) { |
24 | uint32_t majorVersion = 1; |
25 | uint32_t minorVersion = 0; |
26 | switch (version) { |
27 | #define MIN_VERSION_CASE(v) \ |
28 | case spirv::Version::V_1_##v: \ |
29 | minorVersion = v; \ |
30 | break |
31 | |
32 | MIN_VERSION_CASE(0); |
33 | MIN_VERSION_CASE(1); |
34 | MIN_VERSION_CASE(2); |
35 | MIN_VERSION_CASE(3); |
36 | MIN_VERSION_CASE(4); |
37 | MIN_VERSION_CASE(5); |
38 | MIN_VERSION_CASE(6); |
39 | #undef MIN_VERSION_CASE |
40 | } |
41 | |
42 | // See "2.3. Physical Layout of a SPIR-V Module and Instruction" in the SPIR-V |
43 | // spec for the definition of the binary module header. |
44 | // |
45 | // The first five words of a SPIR-V module must be: |
46 | // +-------------------------------------------------------------------------+ |
47 | // | Magic number | |
48 | // +-------------------------------------------------------------------------+ |
49 | // | Version number (bytes: 0 | major number | minor number | 0) | |
50 | // +-------------------------------------------------------------------------+ |
51 | // | Generator magic number | |
52 | // +-------------------------------------------------------------------------+ |
53 | // | Bound (all result <id>s in the module guaranteed to be less than it) | |
54 | // +-------------------------------------------------------------------------+ |
55 | // | 0 (reserved for instruction schema) | |
56 | // +-------------------------------------------------------------------------+ |
57 | header.push_back(Elt: spirv::kMagicNumber); |
58 | header.push_back(Elt: (majorVersion << 16) | (minorVersion << 8)); |
59 | header.push_back(Elt: (kGeneratorNumber << 16) | LLVM_VERSION_MAJOR); |
60 | header.push_back(Elt: idBound); // <id> bound |
61 | header.push_back(Elt: 0); // Schema (reserved word) |
62 | } |
63 | |
64 | /// Returns the word-count-prefixed opcode for an SPIR-V instruction. |
65 | uint32_t spirv::getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode) { |
66 | assert(((wordCount >> 16) == 0) && "word count out of range!" ); |
67 | return (wordCount << 16) | static_cast<uint32_t>(opcode); |
68 | } |
69 | |
70 | void spirv::encodeStringLiteralInto(SmallVectorImpl<uint32_t> &binary, |
71 | StringRef literal) { |
72 | // We need to encode the literal and the null termination. |
73 | size_t encodingSize = literal.size() / 4 + 1; |
74 | size_t sizeOfDataToCopy = literal.size(); |
75 | if (encodingSize >= kMaxLiteralWordCount) { |
76 | // Reserve one word for the null termination. |
77 | encodingSize = kMaxLiteralWordCount - 1; |
78 | // Do not override the last word (null termination) when copying. |
79 | sizeOfDataToCopy = (encodingSize - 1) * 4; |
80 | LLVM_DEBUG(llvm::dbgs() |
81 | << "Truncating string literal to max size (" |
82 | << (kMaxLiteralWordCount - 1) << "): " << literal << "\n" ); |
83 | } |
84 | size_t bufferStartSize = binary.size(); |
85 | binary.resize(N: bufferStartSize + encodingSize, NV: 0); |
86 | std::memcpy(dest: binary.data() + bufferStartSize, src: literal.data(), |
87 | n: sizeOfDataToCopy); |
88 | } |
89 | |