github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Dialect/SPIRV/Serialization/SPIRVBinaryUtils.cpp (about)

     1  //===- SPIRVBinaryUtils.cpp - MLIR SPIR-V Binary Module Utilities ---------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  //
    18  // This file defines common utilities for SPIR-V binary module.
    19  //
    20  //===----------------------------------------------------------------------===//
    21  
    22  #include "mlir/Dialect/SPIRV/SPIRVBinaryUtils.h"
    23  
    24  using namespace mlir;
    25  
    26  void spirv::appendModuleHeader(SmallVectorImpl<uint32_t> &header,
    27                                 uint32_t idBound) {
    28    // The major and minor version number for the generated SPIR-V binary.
    29    // TODO(antiagainst): use target environment to select the version
    30    constexpr uint8_t kMajorVersion = 1;
    31    constexpr uint8_t kMinorVersion = 0;
    32  
    33    // See "2.3. Physical Layout of a SPIR-V Module and Instruction" in the SPIR-V
    34    // spec for the definition of the binary module header.
    35    //
    36    // The first five words of a SPIR-V module must be:
    37    // +-------------------------------------------------------------------------+
    38    // | Magic number                                                            |
    39    // +-------------------------------------------------------------------------+
    40    // | Version number (bytes: 0 | major number | minor number | 0)             |
    41    // +-------------------------------------------------------------------------+
    42    // | Generator magic number                                                  |
    43    // +-------------------------------------------------------------------------+
    44    // | Bound (all result <id>s in the module guaranteed to be less than it)    |
    45    // +-------------------------------------------------------------------------+
    46    // | 0 (reserved for instruction schema)                                     |
    47    // +-------------------------------------------------------------------------+
    48    header.push_back(spirv::kMagicNumber);
    49    header.push_back((kMajorVersion << 16) | (kMinorVersion << 8));
    50    header.push_back(kGeneratorNumber);
    51    header.push_back(idBound); // <id> bound
    52    header.push_back(0);       // Schema (reserved word)
    53  }