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

     1  //===- ConvertFromBinary.cpp - MLIR SPIR-V binary to module conversion ----===//
     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 implements a translation from SPIR-V binary module to MLIR SPIR-V
    19  // ModuleOp.
    20  //
    21  //===----------------------------------------------------------------------===//
    22  
    23  #include "mlir/Dialect/SPIRV/SPIRVOps.h"
    24  #include "mlir/Dialect/SPIRV/Serialization.h"
    25  #include "mlir/Dialect/StandardOps/Ops.h"
    26  #include "mlir/IR/Builders.h"
    27  #include "mlir/IR/Function.h"
    28  #include "mlir/IR/Module.h"
    29  #include "mlir/Support/FileUtilities.h"
    30  #include "mlir/Translation.h"
    31  #include "llvm/ADT/StringRef.h"
    32  #include "llvm/Support/MemoryBuffer.h"
    33  
    34  using namespace mlir;
    35  
    36  // Deserializes the SPIR-V binary module stored in the file named as
    37  // `inputFilename` and returns a module containing the SPIR-V module.
    38  OwningModuleRef deserializeModule(llvm::StringRef inputFilename,
    39                                    MLIRContext *context) {
    40    Builder builder(context);
    41  
    42    std::string errorMessage;
    43    auto file = openInputFile(inputFilename, &errorMessage);
    44    if (!file) {
    45      emitError(UnknownLoc::get(context), errorMessage);
    46      return {};
    47    }
    48  
    49    // Make sure the input stream can be treated as a stream of SPIR-V words
    50    auto start = file->getBufferStart();
    51    auto size = file->getBufferSize();
    52    if (size % sizeof(uint32_t) != 0) {
    53      emitError(UnknownLoc::get(context))
    54          << "SPIR-V binary module must contain integral number of 32-bit words";
    55      return {};
    56    }
    57  
    58    auto binary = llvm::makeArrayRef(reinterpret_cast<const uint32_t *>(start),
    59                                     size / sizeof(uint32_t));
    60  
    61    auto spirvModule = spirv::deserialize(binary, context);
    62    if (!spirvModule)
    63      return {};
    64  
    65    OwningModuleRef module(ModuleOp::create(
    66        FileLineColLoc::get(inputFilename, /*line=*/0, /*column=*/0, context)));
    67    module->getBody()->push_front(spirvModule->getOperation());
    68  
    69    return module;
    70  }
    71  
    72  static TranslateToMLIRRegistration
    73      registration("deserialize-spirv",
    74                   [](StringRef inputFilename, MLIRContext *context) {
    75                     return deserializeModule(inputFilename, context);
    76                   });