| 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 | |
| 16 | #include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h" |
| 17 | #include "mlir/Support/LLVM.h" |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <optional> |
| 21 | |
| 22 | namespace mlir { |
| 23 | namespace spirv { |
| 24 | |
| 25 | /// SPIR-V binary header word count |
| 26 | constexpr unsigned kHeaderWordCount = 5; |
| 27 | |
| 28 | /// SPIR-V magic number |
| 29 | constexpr uint32_t kMagicNumber = 0x07230203; |
| 30 | |
| 31 | /// The serializer tool ID registered to the Khronos Group |
| 32 | constexpr uint32_t kGeneratorNumber = 22; |
| 33 | |
| 34 | /// Max number of words |
| 35 | /// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_universal_limits |
| 36 | constexpr uint32_t kMaxWordCount = 65535; |
| 37 | |
| 38 | /// Max number of words for literal |
| 39 | constexpr uint32_t kMaxLiteralWordCount = kMaxWordCount - 3; |
| 40 | |
| 41 | /// Appends a SPRI-V module header to `header` with the given `version` and |
| 42 | /// `idBound`. |
| 43 | void appendModuleHeader(SmallVectorImpl<uint32_t> &header, |
| 44 | spirv::Version version, uint32_t idBound); |
| 45 | |
| 46 | /// Returns the word-count-prefixed opcode for an SPIR-V instruction. |
| 47 | uint32_t getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode); |
| 48 | |
| 49 | /// Encodes an SPIR-V `literal` string into the given `binary` vector. |
| 50 | void encodeStringLiteralInto(SmallVectorImpl<uint32_t> &binary, |
| 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. |
| 55 | inline StringRef decodeStringLiteral(ArrayRef<uint32_t> words, |
| 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. |
| 65 | inline std::optional<spirv::Opcode> |
| 66 | getContinuationOpcode(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 |
| 85 |