Skip to content

Commit c79a397

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 remove the runner component of the mlir-spirv-cpu-runner. The runtime libraries and the tests are moved and renamed to fit into the Execution Engine and Integration tests, following the model of the similar migration done for the CUDA Runner in D97463.
1 parent 97fb21a commit c79a397

File tree

13 files changed

+85
-149
lines changed

13 files changed

+85
-149
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/lib/ExecutionEngine/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(LLVM_OPTIONAL_SOURCES
1414
RunnerUtils.cpp
1515
OptUtils.cpp
1616
JitRunner.cpp
17+
SpirvCpuRuntimeWrappers.cpp
1718
SyclRuntimeWrappers.cpp
1819
)
1920

@@ -401,4 +402,17 @@ if(LLVM_ENABLE_PIC)
401402

402403
set_property(TARGET mlir_sycl_runtime APPEND PROPERTY BUILD_RPATH "${LevelZero_LIBRARIES_DIR}" "${SyclRuntime_LIBRARIES_DIR}")
403404
endif()
405+
406+
if(MLIR_ENABLE_SPIRV_CPU_RUNNER)
407+
add_mlir_library(mlir_spirv_cpu_runtime
408+
SHARED
409+
SpirvCpuRuntimeWrappers.cpp
410+
411+
EXCLUDE_FROM_LIBMLIR
412+
)
413+
414+
target_compile_definitions(mlir_spirv_cpu_runtime
415+
PRIVATE
416+
mlir_spirv_cpu_runtime_EXPORTS)
417+
endif()
404418
endif()

mlir/test/mlir-spirv-cpu-runner/mlir_test_spirv_cpu_runner_c_wrappers.cpp renamed to mlir/lib/ExecutionEngine/SpirvCpuRuntimeWrappers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- mlir_test_spirv_cpu_runner_c_wrappers.cpp - Runner testing library -===//
1+
//===- SpirvCpuRuntimeWrappers.cpp - Runner testing library -===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

mlir/test/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ if(LLVM_BUILD_EXAMPLES)
197197
endif()
198198

199199
if(MLIR_ENABLE_SPIRV_CPU_RUNNER)
200-
add_subdirectory(mlir-spirv-cpu-runner)
201200
list(APPEND MLIR_TEST_DEPENDS
202-
mlir-spirv-cpu-runner
203-
mlir_test_spirv_cpu_runner_c_wrappers
201+
mlir_spirv_cpu_runtime
204202
)
205203
endif()
206204

mlir/test/mlir-spirv-cpu-runner/double.mlir renamed to mlir/test/Integration/GPU/SPIRV/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_spirv_cpu_runtime --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 renamed to mlir/test/Integration/GPU/SPIRV/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_spirv_cpu_runtime --link-nested-modules \
33
// RUN: | FileCheck %s
44

55
// CHECK: data =

mlir/test/lit.cfg.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ def add_runtime(name):
125125
"not",
126126
]
127127

128-
if config.enable_spirv_cpu_runner:
129-
tools.extend(
130-
["mlir-spirv-cpu-runner", add_runtime("mlir_test_spirv_cpu_runner_c_wrappers")]
131-
)
132-
133128
if config.enable_vulkan_runner:
134129
tools.extend([add_runtime("vulkan-runtime-wrappers")])
135130

@@ -142,6 +137,9 @@ def add_runtime(name):
142137
if config.enable_sycl_runner:
143138
tools.extend([add_runtime("mlir_sycl_runtime")])
144139

140+
if config.enable_spirv_cpu_runner:
141+
tools.extend([add_runtime("mlir_spirv_cpu_runtime")])
142+
145143
if config.mlir_run_arm_sve_tests or config.mlir_run_arm_sme_tests:
146144
tools.extend([add_runtime("mlir_arm_runner_utils")])
147145

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

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

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)