Skip to content

Commit 0e39b13

Browse files
authored
[mlir] Remove the mlir-spirv-cpu-runner (move to mlir-cpu-runner) (#114563)
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 5fbe9b9 commit 0e39b13

File tree

14 files changed

+93
-155
lines changed

14 files changed

+93
-155
lines changed

mlir/docs/SPIRVToLLVMDialectConversion.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ The SPIR-V to LLVM conversion does not involve modelling of workgroups. Hence,
501501
we say that only current invocation is in conversion's scope. This means that
502502
global variables with pointers of `Input`, `Output`, and `Private` storage
503503
classes are supported. Also, `StorageBuffer` storage class is allowed for
504-
executing [`mlir-spirv-cpu-runner`](#mlir-spirv-cpu-runner).
504+
executing [SPIR-V CPU Runner tests](#spir-v-cpu-runner-tests).
505505

506506
Moreover, `bind` that specifies the descriptor set and the binding number and
507507
`built_in` that specifies SPIR-V `BuiltIn` decoration have no conversion into
@@ -815,14 +815,15 @@ Module in SPIR-V has one region that contains one block. It is defined via
815815
`spirv.module` is converted into `ModuleOp`. This plays a role of enclosing scope
816816
to LLVM ops. At the moment, SPIR-V module attributes are ignored.
817817

818-
## `mlir-spirv-cpu-runner`
818+
## SPIR-V CPU Runner Tests
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
822-
supported.
820+
The `mlir-cpu-runner` has support for executing a `gpu` dialect kernel on the
821+
CPU via SPIR-V to LLVM dialect conversion. This is referred to as the "SPIR-V
822+
CPU Runner". The `--link-nested-modules` flag needs to be passed for this.
823+
Currently, only single-threaded kernels are supported.
823824

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

827828
### Pipeline
828829

@@ -846,7 +847,7 @@ executed using `ExecutionEngine`.
846847
### Walk-through
847848

848849
This section gives a detailed overview of the IR changes while running
849-
`mlir-spirv-cpu-runner`. First, consider that we have the following IR. (For
850+
SPIR-V CPU Runner tests. First, consider that we have the following IR. (For
850851
simplicity some type annotations and function implementations have been
851852
omitted).
852853

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/lib/Pass/TestSPIRVCPURunnerPipeline.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Implements a pipeline for use by mlir-spirv-cpu-runner tests.
9+
// Implements a pipeline for use by SPIR-V CPU Runner tests.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -40,7 +40,7 @@ void registerTestSPIRVCPURunnerPipeline() {
4040
PassPipelineRegistration<>(
4141
"test-spirv-cpu-runner-pipeline",
4242
"Runs a series of passes for lowering SPIR-V-dialect MLIR to "
43-
"LLVM-dialect MLIR intended for mlir-spirv-cpu-runner.",
43+
"LLVM-dialect MLIR intended for SPIR-V CPU Runner tests.",
4444
buildTestSPIRVCPURunnerPipeline);
4545
}
4646
} // namespace test

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: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,68 @@
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+
// TODO: Consider removing this linking functionality from the SPIR-V CPU Runner
33+
// flow in favour of a more proper host/device split like other runners.
34+
// https://github.com/llvm/llvm-project/issues/115348
35+
llvm::cl::opt<bool> LinkNestedModules(
36+
"link-nested-modules",
37+
llvm::cl::desc("Link two nested MLIR modules into a single LLVM IR module. "
38+
"Useful if both the host and device code can be run on the "
39+
"same CPU, as in SPIR-V CPU Runner tests."));
40+
41+
/// A utility function that builds llvm::Module from two nested MLIR modules.
42+
///
43+
/// module @main {
44+
/// module @kernel {
45+
/// // Some ops
46+
/// }
47+
/// // Some other ops
48+
/// }
49+
///
50+
/// Each of these two modules is translated to LLVM IR module, then they are
51+
/// linked together and returned.
52+
static std::unique_ptr<llvm::Module>
53+
convertMLIRModule(Operation *op, llvm::LLVMContext &context) {
54+
auto module = dyn_cast<ModuleOp>(op);
55+
if (!module)
56+
return op->emitError("op must be a 'builtin.module"), nullptr;
57+
58+
std::unique_ptr<llvm::Module> kernelModule;
59+
if (LinkNestedModules) {
60+
// Verify that there is only one nested module.
61+
auto modules = module.getOps<ModuleOp>();
62+
if (!llvm::hasSingleElement(modules)) {
63+
module.emitError("The module must contain exactly one nested module");
64+
return nullptr;
65+
}
66+
67+
// Translate nested module and erase it.
68+
ModuleOp nested = *modules.begin();
69+
kernelModule = translateModuleToLLVMIR(nested, context);
70+
nested.erase();
71+
}
72+
73+
std::unique_ptr<llvm::Module> mainModule =
74+
translateModuleToLLVMIR(module, context);
75+
76+
if (LinkNestedModules)
77+
llvm::Linker::linkModules(*mainModule, std::move(kernelModule));
78+
79+
return mainModule;
80+
}
81+
2482
int main(int argc, char **argv) {
2583
llvm::InitLLVM y(argc, argv);
2684
llvm::InitializeNativeTarget();
@@ -30,5 +88,7 @@ int main(int argc, char **argv) {
3088
mlir::DialectRegistry registry;
3189
mlir::registerAllToLLVMIRTranslations(registry);
3290

33-
return mlir::JitRunnerMain(argc, argv, registry);
91+
mlir::JitRunnerConfig jitRunnerConfig;
92+
jitRunnerConfig.llvmModuleBuilder = convertMLIRModule;
93+
return mlir::JitRunnerMain(argc, argv, registry, jitRunnerConfig);
3494
}

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)