Skip to content

Commit 2e7e1e9

Browse files
committed
[mlir] Remove the mlir-spirv-cpu-runner (move to mlir-cpu-runner)
This commit builds on and completes the work done in 9f6c632 to eliminate the need for a separate mlir-spirv-cpu-runner binary. Since the MLIR processing is already done outside this runner, the only real difference between it and the mlir-cpu-runner is the final linking step between the nested LLVM IR modules. By moving this step into mlir-cpu-runner behind a new command-line flag (`--link-nested-modules`), this commit is able to completely remvoe the runner component of the mlir-spirv-cpu-runner. The runtime libraries and associated build infrastructure are left undisturbed.
1 parent 97fb21a commit 2e7e1e9

File tree

8 files changed

+66
-135
lines changed

8 files changed

+66
-135
lines changed

mlir/docs/SPIRVToLLVMDialectConversion.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,14 @@ to LLVM ops. At the moment, SPIR-V module attributes are ignored.
817817

818818
## `mlir-spirv-cpu-runner`
819819

820-
`mlir-spirv-cpu-runner` allows to execute `gpu` dialect kernel on the CPU via
821-
SPIR-V to LLVM dialect conversion. Currently, only single-threaded kernel is
820+
`mlir-spirv-cpu-runner` is the name of a formerly separate tool that allows to
821+
execute `gpu` dialect kernel on the CPU via SPIR-V to LLVM dialect conversion.
822+
This runner is now merged with the `mlir-cpu-runner`; pass the
823+
`--link-nested-modules` flag. Currently, only single-threaded kernels are
822824
supported.
823825

824-
To build the runner, add the following option to `cmake`: `bash
825-
-DMLIR_ENABLE_SPIRV_CPU_RUNNER=1`
826+
To build the required runtime libaries, add the following option to `cmake`:
827+
`-DMLIR_ENABLE_SPIRV_CPU_RUNNER=1`
826828

827829
### Pipeline
828830

mlir/test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ endif()
199199
if(MLIR_ENABLE_SPIRV_CPU_RUNNER)
200200
add_subdirectory(mlir-spirv-cpu-runner)
201201
list(APPEND MLIR_TEST_DEPENDS
202-
mlir-spirv-cpu-runner
203202
mlir_test_spirv_cpu_runner_c_wrappers
204203
)
205204
endif()

mlir/test/mlir-spirv-cpu-runner/double.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: mlir-opt %s -test-spirv-cpu-runner-pipeline \
2-
// RUN: | mlir-spirv-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_test_spirv_cpu_runner_c_wrappers \
2+
// RUN: | mlir-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_test_spirv_cpu_runner_c_wrappers --link-nested-modules \
33
// RUN: | FileCheck %s
44

55
// CHECK: [8, 8, 8, 8, 8, 8]

mlir/test/mlir-spirv-cpu-runner/simple_add.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: mlir-opt %s -test-spirv-cpu-runner-pipeline \
2-
// RUN: | mlir-spirv-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_test_spirv_cpu_runner_c_wrappers \
2+
// RUN: | mlir-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_test_spirv_cpu_runner_c_wrappers --link-nested-modules \
33
// RUN: | FileCheck %s
44

55
// CHECK: data =

mlir/tools/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_subdirectory(mlir-query)
66
add_subdirectory(mlir-reduce)
77
add_subdirectory(mlir-rewrite)
88
add_subdirectory(mlir-shlib)
9-
add_subdirectory(mlir-spirv-cpu-runner)
109
add_subdirectory(mlir-translate)
1110
add_subdirectory(mlir-vulkan-runner)
1211
add_subdirectory(tblgen-lsp-server)

mlir/tools/mlir-cpu-runner/mlir-cpu-runner.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,65 @@
1717
#include "mlir/ExecutionEngine/OptUtils.h"
1818
#include "mlir/IR/Dialect.h"
1919
#include "mlir/Target/LLVMIR/Dialect/All.h"
20+
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
21+
#include "mlir/Target/LLVMIR/Export.h"
2022

23+
#include "llvm/IR/LLVMContext.h"
24+
#include "llvm/IR/Module.h"
25+
#include "llvm/Linker/Linker.h"
26+
#include "llvm/Support/CommandLine.h"
2127
#include "llvm/Support/InitLLVM.h"
2228
#include "llvm/Support/TargetSelect.h"
2329

30+
using namespace mlir;
31+
32+
llvm::cl::opt<bool> LinkNestedModules(
33+
"link-nested-modules",
34+
llvm::cl::desc("Link two nested MLIR modules into a single LLVM IR module. "
35+
"Useful if both the host and device code can be run on the "
36+
"same CPU, as in mlir-spirv-cpu-runner tests."));
37+
38+
/// A utility function that builds llvm::Module from two nested MLIR modules.
39+
///
40+
/// module @main {
41+
/// module @kernel {
42+
/// // Some ops
43+
/// }
44+
/// // Some other ops
45+
/// }
46+
///
47+
/// Each of these two modules is translated to LLVM IR module, then they are
48+
/// linked together and returned.
49+
static std::unique_ptr<llvm::Module>
50+
convertMLIRModule(Operation *op, llvm::LLVMContext &context) {
51+
auto module = dyn_cast<ModuleOp>(op);
52+
if (!module)
53+
return op->emitError("op must be a 'builtin.module"), nullptr;
54+
55+
std::unique_ptr<llvm::Module> kernelModule;
56+
if (LinkNestedModules) {
57+
// Verify that there is only one nested module.
58+
auto modules = module.getOps<ModuleOp>();
59+
if (!llvm::hasSingleElement(modules)) {
60+
module.emitError("The module must contain exactly one nested module");
61+
return nullptr;
62+
}
63+
64+
// Translate nested module and erase it.
65+
ModuleOp nested = *modules.begin();
66+
kernelModule = translateModuleToLLVMIR(nested, context);
67+
nested.erase();
68+
}
69+
70+
std::unique_ptr<llvm::Module> mainModule =
71+
translateModuleToLLVMIR(module, context);
72+
73+
if (LinkNestedModules)
74+
llvm::Linker::linkModules(*mainModule, std::move(kernelModule));
75+
76+
return mainModule;
77+
}
78+
2479
int main(int argc, char **argv) {
2580
llvm::InitLLVM y(argc, argv);
2681
llvm::InitializeNativeTarget();
@@ -30,5 +85,7 @@ int main(int argc, char **argv) {
3085
mlir::DialectRegistry registry;
3186
mlir::registerAllToLLVMIRTranslations(registry);
3287

33-
return mlir::JitRunnerMain(argc, argv, registry);
88+
mlir::JitRunnerConfig jitRunnerConfig;
89+
jitRunnerConfig.llvmModuleBuilder = convertMLIRModule;
90+
return mlir::JitRunnerMain(argc, argv, registry, jitRunnerConfig);
3491
}

mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt

Lines changed: 0 additions & 36 deletions
This file was deleted.

mlir/tools/mlir-spirv-cpu-runner/mlir-spirv-cpu-runner.cpp

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)