Skip to content

[mlir] Remove the mlir-spirv-cpu-runner (move to mlir-cpu-runner) #114563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions mlir/docs/SPIRVToLLVMDialectConversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ The SPIR-V to LLVM conversion does not involve modelling of workgroups. Hence,
we say that only current invocation is in conversion's scope. This means that
global variables with pointers of `Input`, `Output`, and `Private` storage
classes are supported. Also, `StorageBuffer` storage class is allowed for
executing [`mlir-spirv-cpu-runner`](#mlir-spirv-cpu-runner).
executing [SPIR-V CPU Runner tests](#spir-v-cpu-runner-tests).

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

## `mlir-spirv-cpu-runner`
## SPIR-V CPU Runner Tests

`mlir-spirv-cpu-runner` allows to execute `gpu` dialect kernel on the CPU via
SPIR-V to LLVM dialect conversion. Currently, only single-threaded kernel is
supported.
The `mlir-cpu-runner` has support for executing a `gpu` dialect kernel on the
CPU via SPIR-V to LLVM dialect conversion. This is referred to as the "SPIR-V
CPU Runner". The `--link-nested-modules` flag needs to be passed for this.
Currently, only single-threaded kernels are supported.

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

### Pipeline

Expand All @@ -846,7 +847,7 @@ executed using `ExecutionEngine`.
### Walk-through

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

Expand Down
14 changes: 14 additions & 0 deletions mlir/lib/ExecutionEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(LLVM_OPTIONAL_SOURCES
RunnerUtils.cpp
OptUtils.cpp
JitRunner.cpp
SpirvCpuRuntimeWrappers.cpp
SyclRuntimeWrappers.cpp
)

Expand Down Expand Up @@ -401,4 +402,17 @@ if(LLVM_ENABLE_PIC)

set_property(TARGET mlir_sycl_runtime APPEND PROPERTY BUILD_RPATH "${LevelZero_LIBRARIES_DIR}" "${SyclRuntime_LIBRARIES_DIR}")
endif()

if(MLIR_ENABLE_SPIRV_CPU_RUNNER)
add_mlir_library(mlir_spirv_cpu_runtime
SHARED
SpirvCpuRuntimeWrappers.cpp

EXCLUDE_FROM_LIBMLIR
)

target_compile_definitions(mlir_spirv_cpu_runtime
PRIVATE
mlir_spirv_cpu_runtime_EXPORTS)
endif()
endif()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===- mlir_test_spirv_cpu_runner_c_wrappers.cpp - Runner testing library -===//
//===- SpirvCpuRuntimeWrappers.cpp - Runner testing library -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
4 changes: 1 addition & 3 deletions mlir/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,8 @@ if(LLVM_BUILD_EXAMPLES)
endif()

if(MLIR_ENABLE_SPIRV_CPU_RUNNER)
add_subdirectory(mlir-spirv-cpu-runner)
list(APPEND MLIR_TEST_DEPENDS
mlir-spirv-cpu-runner
mlir_test_spirv_cpu_runner_c_wrappers
mlir_spirv_cpu_runtime
)
endif()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: mlir-opt %s -test-spirv-cpu-runner-pipeline \
// RUN: | mlir-spirv-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_test_spirv_cpu_runner_c_wrappers \
// RUN: | mlir-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_spirv_cpu_runtime --link-nested-modules \
// RUN: | FileCheck %s

// CHECK: [8, 8, 8, 8, 8, 8]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: mlir-opt %s -test-spirv-cpu-runner-pipeline \
// RUN: | mlir-spirv-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_test_spirv_cpu_runner_c_wrappers \
// RUN: | mlir-cpu-runner - -e main --entry-point-result=void --shared-libs=%mlir_runner_utils,%mlir_spirv_cpu_runtime --link-nested-modules \
// RUN: | FileCheck %s

// CHECK: data =
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/lib/Pass/TestSPIRVCPURunnerPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
//
// Implements a pipeline for use by mlir-spirv-cpu-runner tests.
// Implements a pipeline for use by SPIR-V CPU Runner tests.
//
//===----------------------------------------------------------------------===//

Expand Down Expand Up @@ -40,7 +40,7 @@ void registerTestSPIRVCPURunnerPipeline() {
PassPipelineRegistration<>(
"test-spirv-cpu-runner-pipeline",
"Runs a series of passes for lowering SPIR-V-dialect MLIR to "
"LLVM-dialect MLIR intended for mlir-spirv-cpu-runner.",
"LLVM-dialect MLIR intended for SPIR-V CPU Runner tests.",
buildTestSPIRVCPURunnerPipeline);
}
} // namespace test
Expand Down
8 changes: 3 additions & 5 deletions mlir/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ def add_runtime(name):
"not",
]

if config.enable_spirv_cpu_runner:
tools.extend(
["mlir-spirv-cpu-runner", add_runtime("mlir_test_spirv_cpu_runner_c_wrappers")]
)

if config.enable_vulkan_runner:
tools.extend([add_runtime("vulkan-runtime-wrappers")])

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

if config.enable_spirv_cpu_runner:
tools.extend([add_runtime("mlir_spirv_cpu_runtime")])

if config.mlir_run_arm_sve_tests or config.mlir_run_arm_sme_tests:
tools.extend([add_runtime("mlir_arm_runner_utils")])

Expand Down
6 changes: 0 additions & 6 deletions mlir/test/mlir-spirv-cpu-runner/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion mlir/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_subdirectory(mlir-query)
add_subdirectory(mlir-reduce)
add_subdirectory(mlir-rewrite)
add_subdirectory(mlir-shlib)
add_subdirectory(mlir-spirv-cpu-runner)
add_subdirectory(mlir-translate)
add_subdirectory(mlir-vulkan-runner)
add_subdirectory(tblgen-lsp-server)
Expand Down
62 changes: 61 additions & 1 deletion mlir/tools/mlir-cpu-runner/mlir-cpu-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,68 @@
#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
Comment on lines +32 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the new TODO (#115348)

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();
Expand All @@ -30,5 +88,7 @@ int main(int argc, char **argv) {
mlir::DialectRegistry registry;
mlir::registerAllToLLVMIRTranslations(registry);

return mlir::JitRunnerMain(argc, argv, registry);
mlir::JitRunnerConfig jitRunnerConfig;
jitRunnerConfig.llvmModuleBuilder = convertMLIRModule;
return mlir::JitRunnerMain(argc, argv, registry, jitRunnerConfig);
}
36 changes: 0 additions & 36 deletions mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt

This file was deleted.

90 changes: 0 additions & 90 deletions mlir/tools/mlir-spirv-cpu-runner/mlir-spirv-cpu-runner.cpp

This file was deleted.

Loading