aboutsummaryrefslogtreecommitdiff
path: root/mlir/tools/mlir-runner/mlir-runner.cpp
blob: 44ad660e346faedf082f3ec563bb2869e1300c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//===- mlir-runner.cpp - MLIR CPU Execution Driver ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Main entry point to a command line utility that executes an MLIR file on the
// CPU by  translating MLIR to LLVM IR before JIT-compiling and executing the
// latter.
//
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/ExecutionEngine/JitRunner.h"
#include "mlir/ExecutionEngine/OptUtils.h"
#include "mlir/IR/Dialect.h"
#include "mlir/Target/LLVMIR/Dialect/All.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"

#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"

using namespace mlir;

// TODO: Consider removing this linking functionality from the SPIR-V CPU Runner
//       flow in favour of a more proper host/device split like other runners.
//       https://github.com/llvm/llvm-project/issues/115348
static llvm::cl::opt<bool> linkNestedModules(
    "link-nested-modules",
    llvm::cl::desc("Link two nested MLIR modules into a single LLVM IR module. "
                   "Useful if both the host and device code can be run on the "
                   "same CPU, as in SPIR-V CPU Runner tests."));

/// A utility function that builds llvm::Module from two nested MLIR modules.
///
/// module @main {
///   module @kernel {
///     // Some ops
///   }
///   // Some other ops
/// }
///
/// Each of these two modules is translated to LLVM IR module, then they are
/// linked together and returned.
static std::unique_ptr<llvm::Module>
convertMLIRModule(Operation *op, llvm::LLVMContext &context) {
  auto module = dyn_cast<ModuleOp>(op);
  if (!module)
    return op->emitError("op must be a 'builtin.module"), nullptr;

  std::unique_ptr<llvm::Module> kernelModule;
  if (linkNestedModules) {
    // Verify that there is only one nested module.
    auto modules = module.getOps<ModuleOp>();
    if (!llvm::hasSingleElement(modules)) {
      module.emitError("The module must contain exactly one nested module");
      return nullptr;
    }

    // Translate nested module and erase it.
    ModuleOp nested = *modules.begin();
    kernelModule = translateModuleToLLVMIR(nested, context);
    nested.erase();
  }

  std::unique_ptr<llvm::Module> mainModule =
      translateModuleToLLVMIR(module, context);

  if (linkNestedModules)
    llvm::Linker::linkModules(*mainModule, std::move(kernelModule));

  return mainModule;
}

int main(int argc, char **argv) {
  llvm::InitLLVM y(argc, argv);
  llvm::InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmPrinter();
  llvm::InitializeNativeTargetAsmParser();

  mlir::DialectRegistry registry;
  mlir::registerAllToLLVMIRTranslations(registry);

  mlir::JitRunnerConfig jitRunnerConfig;
  jitRunnerConfig.llvmModuleBuilder = convertMLIRModule;
  return mlir::JitRunnerMain(argc, argv, registry, jitRunnerConfig);
}