This commit is a further incremental step toward moving the whole mlir-vulkan-runner MLIR pass pipeline into mlir-opt (see #73457). The previous step was b225b3adf7b78387c9fcb97a3ff0e0a1e26eafe2, which moved all device passes prior to SPIR-V serialization into a new mlir-opt test pass, `-test-vulkan-runner-pipeline`. This commit changes how SPIR-V serialization is accomplished for Vulkan runner tests. Until now, this was done by the Vulkan-specific ConvertGpuLaunchFuncToVulkanLaunchFunc pass. With this commit, this responsibility is removed from that pass, and is instead done with the existing generic GpuModuleToBinaryPass. In addition, the SPIR-V serialization step is no longer done inside mlir-vulkan-runner, but rather inside mlir-opt (in the `-test-vulkan-runner-pipeline` pass). Both of these changes represent a greater alignment between mlir-vulkan-runner and the other GPU integration tests. Notably, the IR shapes produced by the mlir-opt pipelines for the Vulkan and SYCL runners are now much more similar, with both using a gpu.binary op for the serialized SPIR-V kernel. In order to enable this, this commit includes these supporting changes: - ConvertToSPIRVPass is enhanced to support producing the IR shape where a spirv.module is nested inside a gpu.module, since this is what GpuModuleToBinaryPass expects. - ConvertGPULaunchFuncToVulkanLaunchFunc is changed to remove its SPIR-V serialization functionality, and instead now extracts the SPIR-V from a gpu.binary operation (as produced by ConvertToSPIRVPass). - `-test-vulkan-runner-pipeline` now attaches SPIR-V target information required by GpuModuleToBinaryPass. - The WebGPU pass option, which had been removed from mlir-vulkan-runner in the previous commit in this series, is restored as an option to `-test-vulkan-runner-pipeline` instead, so that the WebGPU pass continues being inserted into the pipeline just before SPIR-V serialization.
137 lines
5.6 KiB
C++
137 lines
5.6 KiB
C++
//===- ConvertToSPIRVPass.cpp - MLIR SPIR-V Conversion --------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Conversion/ConvertToSPIRV/ConvertToSPIRVPass.h"
|
|
#include "mlir/Conversion/ArithToSPIRV/ArithToSPIRV.h"
|
|
#include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"
|
|
#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRV.h"
|
|
#include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
|
|
#include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h"
|
|
#include "mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h"
|
|
#include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h"
|
|
#include "mlir/Conversion/VectorToSPIRV/VectorToSPIRV.h"
|
|
#include "mlir/Dialect/Arith/Transforms/Passes.h"
|
|
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
|
|
#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
|
|
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
|
|
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
|
|
#include "mlir/Dialect/Vector/IR/VectorOps.h"
|
|
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h"
|
|
#include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h"
|
|
#include "mlir/IR/PatternMatch.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
#include <memory>
|
|
|
|
#define DEBUG_TYPE "convert-to-spirv"
|
|
|
|
namespace mlir {
|
|
#define GEN_PASS_DEF_CONVERTTOSPIRVPASS
|
|
#include "mlir/Conversion/Passes.h.inc"
|
|
} // namespace mlir
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
|
|
/// Map memRef memory space to SPIR-V storage class.
|
|
void mapToMemRef(Operation *op, spirv::TargetEnvAttr &targetAttr) {
|
|
spirv::TargetEnv targetEnv(targetAttr);
|
|
bool targetEnvSupportsKernelCapability =
|
|
targetEnv.allows(spirv::Capability::Kernel);
|
|
spirv::MemorySpaceToStorageClassMap memorySpaceMap =
|
|
targetEnvSupportsKernelCapability
|
|
? spirv::mapMemorySpaceToOpenCLStorageClass
|
|
: spirv::mapMemorySpaceToVulkanStorageClass;
|
|
spirv::MemorySpaceToStorageClassConverter converter(memorySpaceMap);
|
|
spirv::convertMemRefTypesAndAttrs(op, converter);
|
|
}
|
|
|
|
/// Populate patterns for each dialect.
|
|
void populateConvertToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,
|
|
ScfToSPIRVContext &scfToSPIRVContext,
|
|
RewritePatternSet &patterns) {
|
|
arith::populateCeilFloorDivExpandOpsPatterns(patterns);
|
|
arith::populateArithToSPIRVPatterns(typeConverter, patterns);
|
|
populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns);
|
|
populateFuncToSPIRVPatterns(typeConverter, patterns);
|
|
populateGPUToSPIRVPatterns(typeConverter, patterns);
|
|
index::populateIndexToSPIRVPatterns(typeConverter, patterns);
|
|
populateMemRefToSPIRVPatterns(typeConverter, patterns);
|
|
populateVectorToSPIRVPatterns(typeConverter, patterns);
|
|
populateSCFToSPIRVPatterns(typeConverter, scfToSPIRVContext, patterns);
|
|
ub::populateUBToSPIRVConversionPatterns(typeConverter, patterns);
|
|
}
|
|
|
|
/// A pass to perform the SPIR-V conversion.
|
|
struct ConvertToSPIRVPass final
|
|
: impl::ConvertToSPIRVPassBase<ConvertToSPIRVPass> {
|
|
using ConvertToSPIRVPassBase::ConvertToSPIRVPassBase;
|
|
|
|
void runOnOperation() override {
|
|
Operation *op = getOperation();
|
|
MLIRContext *context = &getContext();
|
|
|
|
// Unroll vectors in function signatures to native size.
|
|
if (runSignatureConversion && failed(spirv::unrollVectorsInSignatures(op)))
|
|
return signalPassFailure();
|
|
|
|
// Unroll vectors in function bodies to native size.
|
|
if (runVectorUnrolling && failed(spirv::unrollVectorsInFuncBodies(op)))
|
|
return signalPassFailure();
|
|
|
|
// Generic conversion.
|
|
if (!convertGPUModules) {
|
|
spirv::TargetEnvAttr targetAttr = spirv::lookupTargetEnvOrDefault(op);
|
|
std::unique_ptr<ConversionTarget> target =
|
|
SPIRVConversionTarget::get(targetAttr);
|
|
SPIRVTypeConverter typeConverter(targetAttr);
|
|
RewritePatternSet patterns(context);
|
|
ScfToSPIRVContext scfToSPIRVContext;
|
|
mapToMemRef(op, targetAttr);
|
|
populateConvertToSPIRVPatterns(typeConverter, scfToSPIRVContext,
|
|
patterns);
|
|
if (failed(applyPartialConversion(op, *target, std::move(patterns))))
|
|
return signalPassFailure();
|
|
return;
|
|
}
|
|
|
|
// Clone each GPU kernel module for conversion, given that the GPU
|
|
// launch op still needs the original GPU kernel module.
|
|
SmallVector<Operation *, 1> gpuModules;
|
|
OpBuilder builder(context);
|
|
op->walk([&](gpu::GPUModuleOp gpuModule) {
|
|
if (nestInGPUModule)
|
|
builder.setInsertionPointToStart(gpuModule.getBody());
|
|
else
|
|
builder.setInsertionPoint(gpuModule);
|
|
gpuModules.push_back(builder.clone(*gpuModule));
|
|
});
|
|
// Run conversion for each module independently as they can have
|
|
// different TargetEnv attributes.
|
|
for (Operation *gpuModule : gpuModules) {
|
|
spirv::TargetEnvAttr targetAttr =
|
|
spirv::lookupTargetEnvOrDefault(gpuModule);
|
|
std::unique_ptr<ConversionTarget> target =
|
|
SPIRVConversionTarget::get(targetAttr);
|
|
SPIRVTypeConverter typeConverter(targetAttr);
|
|
RewritePatternSet patterns(context);
|
|
ScfToSPIRVContext scfToSPIRVContext;
|
|
mapToMemRef(gpuModule, targetAttr);
|
|
populateConvertToSPIRVPatterns(typeConverter, scfToSPIRVContext,
|
|
patterns);
|
|
if (failed(applyFullConversion(gpuModule, *target, std::move(patterns))))
|
|
return signalPassFailure();
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace
|