From 890de04332ba367c5b5a7f2eb9bd2786ab181a4f Mon Sep 17 00:00:00 2001 From: Andrea Faulds Date: Tue, 10 Dec 2024 17:17:59 +0100 Subject: [PATCH] [mlir][mlir-vulkan-runner] Move part of device pass pipeline to mlir-opt Adds a new mlir-opt test-only pass, -test-vulkan-runner-pipeline, which runs a set of passes needed for mlir-vulkan-runner, and removes them from the runner. The tests are changed to invoke mlir-opt with this flag before invoking the runner. The passes moved are ones concerned with lowering of the device code prior to serialization to SPIR-V. This is an incremental step towards moving the entire pipeline to mlir-opt, to align with other runners (see #73457). --- mlir/test/lib/Pass/CMakeLists.txt | 1 + .../lib/Pass/TestVulkanRunnerPipeline.cpp | 47 +++++++++++++++++++ mlir/test/mlir-vulkan-runner/addf.mlir | 3 +- mlir/test/mlir-vulkan-runner/addf_if.mlir | 3 +- mlir/test/mlir-vulkan-runner/addi.mlir | 3 +- mlir/test/mlir-vulkan-runner/addi8.mlir | 3 +- .../mlir-vulkan-runner/addui_extended.mlir | 14 +++--- mlir/test/mlir-vulkan-runner/mulf.mlir | 3 +- .../mlir-vulkan-runner/smul_extended.mlir | 14 +++--- mlir/test/mlir-vulkan-runner/subf.mlir | 3 +- mlir/test/mlir-vulkan-runner/time.mlir | 3 +- .../mlir-vulkan-runner/umul_extended.mlir | 14 +++--- .../vector-deinterleave.mlir | 7 +-- .../mlir-vulkan-runner/vector-interleave.mlir | 7 +-- .../mlir-vulkan-runner/vector-shuffle.mlir | 7 +-- mlir/tools/mlir-opt/mlir-opt.cpp | 2 + .../mlir-vulkan-runner/mlir-vulkan-runner.cpp | 38 +-------------- 17 files changed, 102 insertions(+), 70 deletions(-) create mode 100644 mlir/test/lib/Pass/TestVulkanRunnerPipeline.cpp diff --git a/mlir/test/lib/Pass/CMakeLists.txt b/mlir/test/lib/Pass/CMakeLists.txt index f489b7e51e503..b474c1863f335 100644 --- a/mlir/test/lib/Pass/CMakeLists.txt +++ b/mlir/test/lib/Pass/CMakeLists.txt @@ -4,6 +4,7 @@ add_mlir_library(MLIRTestPass TestDynamicPipeline.cpp TestPassManager.cpp TestSPIRVCPURunnerPipeline.cpp + TestVulkanRunnerPipeline.cpp EXCLUDE_FROM_LIBMLIR diff --git a/mlir/test/lib/Pass/TestVulkanRunnerPipeline.cpp b/mlir/test/lib/Pass/TestVulkanRunnerPipeline.cpp new file mode 100644 index 0000000000000..eda9aa9f9efef --- /dev/null +++ b/mlir/test/lib/Pass/TestVulkanRunnerPipeline.cpp @@ -0,0 +1,47 @@ +//===------------------ TestVulkanRunnerPipeline.cpp --------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Implements a pipeline for use by mlir-vulkan-runner tests. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Conversion/ConvertToSPIRV/ConvertToSPIRVPass.h" +#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h" +#include "mlir/Dialect/GPU/Transforms/Passes.h" +#include "mlir/Dialect/MemRef/Transforms/Passes.h" +#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" +#include "mlir/Dialect/SPIRV/Transforms/Passes.h" +#include "mlir/Pass/PassManager.h" + +using namespace mlir; + +namespace { + +void buildTestVulkanRunnerPipeline(OpPassManager &passManager) { + passManager.addPass(createGpuKernelOutliningPass()); + passManager.addPass(memref::createFoldMemRefAliasOpsPass()); + + ConvertToSPIRVPassOptions convertToSPIRVOptions{}; + convertToSPIRVOptions.convertGPUModules = true; + passManager.addPass(createConvertToSPIRVPass(convertToSPIRVOptions)); + OpPassManager &modulePM = passManager.nest(); + modulePM.addPass(spirv::createSPIRVLowerABIAttributesPass()); + modulePM.addPass(spirv::createSPIRVUpdateVCEPass()); +} + +} // namespace + +namespace mlir::test { +void registerTestVulkanRunnerPipeline() { + PassPipelineRegistration<>( + "test-vulkan-runner-pipeline", + "Runs a series of passes for lowering GPU-dialect MLIR to " + "SPIR-V-dialect MLIR intended for mlir-vulkan-runner.", + buildTestVulkanRunnerPipeline); +} +} // namespace mlir::test diff --git a/mlir/test/mlir-vulkan-runner/addf.mlir b/mlir/test/mlir-vulkan-runner/addf.mlir index f6162d1441e46..d435f75a28805 100644 --- a/mlir/test/mlir-vulkan-runner/addf.mlir +++ b/mlir/test/mlir-vulkan-runner/addf.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK: [3.3, 3.3, 3.3, 3.3, 3.3, 3.3, 3.3, 3.3] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/addf_if.mlir b/mlir/test/mlir-vulkan-runner/addf_if.mlir index 5638bd44682de..8ae995c65e7e8 100644 --- a/mlir/test/mlir-vulkan-runner/addf_if.mlir +++ b/mlir/test/mlir-vulkan-runner/addf_if.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK: [3.3, 3.3, 3.3, 3.3, 0, 0, 0, 0] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/addi.mlir b/mlir/test/mlir-vulkan-runner/addi.mlir index 79fefe0ab03f4..7e212a4fb179c 100644 --- a/mlir/test/mlir-vulkan-runner/addi.mlir +++ b/mlir/test/mlir-vulkan-runner/addi.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK-COUNT-64: [3, 3, 3, 3, 3, 3, 3, 3] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/addi8.mlir b/mlir/test/mlir-vulkan-runner/addi8.mlir index 0672b48eed6a2..e0b1a8e8875c0 100644 --- a/mlir/test/mlir-vulkan-runner/addi8.mlir +++ b/mlir/test/mlir-vulkan-runner/addi8.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK-COUNT-64: [3, 3, 3, 3, 3, 3, 3, 3] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/addui_extended.mlir b/mlir/test/mlir-vulkan-runner/addui_extended.mlir index 9b1f1964b3f95..158541f326be7 100644 --- a/mlir/test/mlir-vulkan-runner/addui_extended.mlir +++ b/mlir/test/mlir-vulkan-runner/addui_extended.mlir @@ -1,13 +1,15 @@ // Make sure that addition with carry produces expected results // with and without expansion to primitive add/cmp ops for WebGPU. -// RUN: mlir-vulkan-runner %s \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s -// RUN: mlir-vulkan-runner %s --vulkan-runner-spirv-webgpu-prepare \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline -spirv-webgpu-prepare \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s // CHECK: [0, 42, 0, 42] // CHECK: [1, 0, 1, 1] diff --git a/mlir/test/mlir-vulkan-runner/mulf.mlir b/mlir/test/mlir-vulkan-runner/mulf.mlir index 1399e30d38f57..22fa034a9b455 100644 --- a/mlir/test/mlir-vulkan-runner/mulf.mlir +++ b/mlir/test/mlir-vulkan-runner/mulf.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK-COUNT-4: [6, 6, 6, 6] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/smul_extended.mlir b/mlir/test/mlir-vulkan-runner/smul_extended.mlir index ccad49ecac6c4..2dd31d2ebb9a0 100644 --- a/mlir/test/mlir-vulkan-runner/smul_extended.mlir +++ b/mlir/test/mlir-vulkan-runner/smul_extended.mlir @@ -1,13 +1,15 @@ // Make sure that signed extended multiplication produces expected results // with and without expansion to primitive mul/add ops for WebGPU. -// RUN: mlir-vulkan-runner %s \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s -// RUN: mlir-vulkan-runner %s --vulkan-runner-spirv-webgpu-prepare \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline -spirv-webgpu-prepare \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s // CHECK: [0, 1, -2, 1, 1048560, -87620295, -131071, 560969770] // CHECK: [0, 0, -1, 0, 0, -1, 0, -499807318] diff --git a/mlir/test/mlir-vulkan-runner/subf.mlir b/mlir/test/mlir-vulkan-runner/subf.mlir index f19607bbed9bf..23496ef3abc00 100644 --- a/mlir/test/mlir-vulkan-runner/subf.mlir +++ b/mlir/test/mlir-vulkan-runner/subf.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK-COUNT-32: [2.2, 2.2, 2.2, 2.2] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/time.mlir b/mlir/test/mlir-vulkan-runner/time.mlir index 1e86f66877edf..6a0bfef36793b 100644 --- a/mlir/test/mlir-vulkan-runner/time.mlir +++ b/mlir/test/mlir-vulkan-runner/time.mlir @@ -1,4 +1,5 @@ -// RUN: mlir-vulkan-runner %s --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils --entry-point-result=void | FileCheck %s // CHECK: Compute shader execution time // CHECK: Command buffer submit time diff --git a/mlir/test/mlir-vulkan-runner/umul_extended.mlir b/mlir/test/mlir-vulkan-runner/umul_extended.mlir index 23c71be1c0325..78300d2fd81dd 100644 --- a/mlir/test/mlir-vulkan-runner/umul_extended.mlir +++ b/mlir/test/mlir-vulkan-runner/umul_extended.mlir @@ -1,13 +1,15 @@ // Make sure that unsigned extended multiplication produces expected results // with and without expansion to primitive mul/add ops for WebGPU. -// RUN: mlir-vulkan-runner %s \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s -// RUN: mlir-vulkan-runner %s --vulkan-runner-spirv-webgpu-prepare \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline -spirv-webgpu-prepare \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s // CHECK: [0, 1, -2, 1, 1048560, -87620295, -131071, -49] // CHECK: [0, 0, 1, -2, 0, 65534, -131070, 6] diff --git a/mlir/test/mlir-vulkan-runner/vector-deinterleave.mlir b/mlir/test/mlir-vulkan-runner/vector-deinterleave.mlir index f2214ebc10262..097f3905949d8 100644 --- a/mlir/test/mlir-vulkan-runner/vector-deinterleave.mlir +++ b/mlir/test/mlir-vulkan-runner/vector-deinterleave.mlir @@ -1,6 +1,7 @@ -// RUN: mlir-vulkan-runner %s \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s // CHECK: [0, 2] // CHECK: [1, 3] diff --git a/mlir/test/mlir-vulkan-runner/vector-interleave.mlir b/mlir/test/mlir-vulkan-runner/vector-interleave.mlir index 0846d52a45b11..5dd4abbd1fb19 100644 --- a/mlir/test/mlir-vulkan-runner/vector-interleave.mlir +++ b/mlir/test/mlir-vulkan-runner/vector-interleave.mlir @@ -1,6 +1,7 @@ -// RUN: mlir-vulkan-runner %s \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s // CHECK: [0, 2, 1, 3] module attributes { diff --git a/mlir/test/mlir-vulkan-runner/vector-shuffle.mlir b/mlir/test/mlir-vulkan-runner/vector-shuffle.mlir index 7cf53b54590bc..be97b48b1812e 100644 --- a/mlir/test/mlir-vulkan-runner/vector-shuffle.mlir +++ b/mlir/test/mlir-vulkan-runner/vector-shuffle.mlir @@ -1,6 +1,7 @@ -// RUN: mlir-vulkan-runner %s \ -// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ -// RUN: --entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -test-vulkan-runner-pipeline \ +// RUN: | mlir-vulkan-runner - \ +// RUN: --shared-libs=%vulkan-runtime-wrappers,%mlir_runner_utils \ +// RUN: --entry-point-result=void | FileCheck %s // CHECK: [2, 1, 3, 3] module attributes { diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp index 94bc67a1e9609..960f7037a1b61 100644 --- a/mlir/tools/mlir-opt/mlir-opt.cpp +++ b/mlir/tools/mlir-opt/mlir-opt.cpp @@ -153,6 +153,7 @@ void registerTestTransformDialectEraseSchedulePass(); void registerTestPassStateExtensionCommunication(); void registerTestVectorLowerings(); void registerTestVectorReductionToSPIRVDotProd(); +void registerTestVulkanRunnerPipeline(); void registerTestWrittenToPass(); #if MLIR_ENABLE_PDL_IN_PATTERNMATCH void registerTestDialectConversionPasses(); @@ -291,6 +292,7 @@ void registerTestPasses() { mlir::test::registerTestPassStateExtensionCommunication(); mlir::test::registerTestVectorLowerings(); mlir::test::registerTestVectorReductionToSPIRVDotProd(); + mlir::test::registerTestVulkanRunnerPipeline(); mlir::test::registerTestWrittenToPass(); #if MLIR_ENABLE_PDL_IN_PATTERNMATCH mlir::test::registerTestDialectConversionPasses(); diff --git a/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp b/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp index 2dd539ef83481..eab64c97e89e4 100644 --- a/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp +++ b/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp @@ -12,9 +12,7 @@ // //===----------------------------------------------------------------------===// -#include "mlir/Conversion/ConvertToSPIRV/ConvertToSPIRVPass.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h" -#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h" #include "mlir/Conversion/GPUToVulkan/ConvertGPUToVulkanPass.h" #include "mlir/Conversion/LLVMCommon/LoweringOptions.h" #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" @@ -30,8 +28,6 @@ #include "mlir/Dialect/MemRef/Transforms/Passes.h" #include "mlir/Dialect/SCF/IR/SCF.h" #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" -#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" -#include "mlir/Dialect/SPIRV/Transforms/Passes.h" #include "mlir/Dialect/Vector/IR/VectorOps.h" #include "mlir/ExecutionEngine/JitRunner.h" #include "mlir/Pass/Pass.h" @@ -43,18 +39,7 @@ using namespace mlir; -namespace { -struct VulkanRunnerOptions { - llvm::cl::OptionCategory category{"mlir-vulkan-runner options"}; - llvm::cl::opt spirvWebGPUPrepare{ - "vulkan-runner-spirv-webgpu-prepare", - llvm::cl::desc("Run MLIR transforms used when targetting WebGPU"), - llvm::cl::cat(category)}; -}; -} // namespace - -static LogicalResult runMLIRPasses(Operation *op, - VulkanRunnerOptions &options) { +static LogicalResult runMLIRPasses(Operation *op, JitRunnerOptions &) { auto module = dyn_cast(op); if (!module) return op->emitOpError("expected a 'builtin.module' op"); @@ -62,18 +47,6 @@ static LogicalResult runMLIRPasses(Operation *op, if (failed(applyPassManagerCLOptions(passManager))) return failure(); - passManager.addPass(createGpuKernelOutliningPass()); - passManager.addPass(memref::createFoldMemRefAliasOpsPass()); - - ConvertToSPIRVPassOptions convertToSPIRVOptions{}; - convertToSPIRVOptions.convertGPUModules = true; - passManager.addPass(createConvertToSPIRVPass(convertToSPIRVOptions)); - OpPassManager &modulePM = passManager.nest(); - modulePM.addPass(spirv::createSPIRVLowerABIAttributesPass()); - modulePM.addPass(spirv::createSPIRVUpdateVCEPass()); - if (options.spirvWebGPUPrepare) - modulePM.addPass(spirv::createSPIRVWebGPUPreparePass()); - passManager.addPass(createConvertGpuLaunchFuncToVulkanLaunchFuncPass()); passManager.addPass(createFinalizeMemRefToLLVMConversionPass()); passManager.addPass(createConvertVectorToLLVMPass()); @@ -96,15 +69,8 @@ int main(int argc, char **argv) { llvm::InitializeNativeTarget(); llvm::InitializeNativeTargetAsmPrinter(); - // Initialize runner-specific CLI options. These will be parsed and - // initialzied in `JitRunnerMain`. - VulkanRunnerOptions options; - auto runPassesWithOptions = [&options](Operation *op, JitRunnerOptions &) { - return runMLIRPasses(op, options); - }; - mlir::JitRunnerConfig jitRunnerConfig; - jitRunnerConfig.mlirTransformer = runPassesWithOptions; + jitRunnerConfig.mlirTransformer = runMLIRPasses; mlir::DialectRegistry registry; registry.insert