Skip to content

[mlir] Prepare convert-gpu-to-spirv for OpenCL support #69941

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 9 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ def ConvertGPUToSPIRV : Pass<"convert-gpu-to-spirv", "ModuleOp"> {
to control the set and binding if wanted.
}];
let constructor = "mlir::createConvertGPUToSPIRVPass()";
let dependentDialects = ["spirv::SPIRVDialect"];
let dependentDialects = [
"func::FuncDialect",
"spirv::SPIRVDialect",
];
let options = [
Option<"use64bitIndex", "use-64bit-index",
"bool", /*default=*/"false",
Expand Down
82 changes: 73 additions & 9 deletions mlir/lib/Conversion/GPUToSPIRV/GPUToSPIRVPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"
#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRV.h"
#include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
Expand Down Expand Up @@ -54,22 +55,67 @@ void GPUToSPIRVPass::runOnOperation() {

SmallVector<Operation *, 1> gpuModules;
OpBuilder builder(context);

auto getTargetEnvFromGPUModuleOp = [*this](gpu::GPUModuleOp moduleOp) {
Operation *gpuModule = moduleOp.getOperation();
auto targetAttr = spirv::lookupTargetEnvOrDefault(gpuModule);
std::unique_ptr<ConversionTarget> target =
SPIRVConversionTarget::get(targetAttr);

SPIRVConversionOptions options;
options.use64bitIndex = this->use64bitIndex;
SPIRVTypeConverter typeConverter(targetAttr, options);
const spirv::TargetEnv &targetEnv = typeConverter.getTargetEnv();
return targetEnv;
};

module.walk([&](gpu::GPUModuleOp moduleOp) {
// Clone each GPU kernel module for conversion, given that the GPU
// launch op still needs the original GPU kernel module.
builder.setInsertionPoint(moduleOp.getOperation());
// SPIRV module insertion point by is after original GPU module.
// This works fine for Vulkan shader that has a dedicated runner.
// But OpenCL kernel needs SPIRV module placed inside original GPU module as
// OpenCL uses GPU compilation pipeline.
const mlir::spirv::TargetEnv &targetEnv =
getTargetEnvFromGPUModuleOp(moduleOp);
FailureOr<spirv::MemoryModel> memoryModel =
spirv::getMemoryModel(targetEnv);
if (failed(memoryModel))
return signalPassFailure();
if (memoryModel == spirv::MemoryModel::OpenCL) {
builder.setInsertionPoint(moduleOp.getBody(),
moduleOp.getBody()->begin());
} else {
builder.setInsertionPoint(moduleOp.getOperation());
}
gpuModules.push_back(builder.clone(*moduleOp.getOperation()));
});

// Run conversion for each module independently as they can have different
// TargetEnv attributes.
for (Operation *gpuModule : gpuModules) {
mlir::spirv::TargetEnvAttr targetAttr =
Copy link
Member

Choose a reason for hiding this comment

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

Can we use the lambda function in the above? This is duplicating the logic there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now the lambda returns boolean and cannot be used. Also that line is from the original code. I just moved it up a bit since I needed access to targetAttr before memory space mapping.

spirv::lookupTargetEnvOrDefault(gpuModule);
std::unique_ptr<ConversionTarget> target =
SPIRVConversionTarget::get(targetAttr);

SPIRVConversionOptions options;
options.use64bitIndex = this->use64bitIndex;
SPIRVTypeConverter typeConverter(targetAttr, options);
const spirv::TargetEnv &targetEnv = typeConverter.getTargetEnv();
FailureOr<spirv::MemoryModel> memoryModel =
spirv::getMemoryModel(targetEnv);
if (failed(memoryModel))
return signalPassFailure();

// Map MemRef memory space to SPIR-V storage class first if requested.
if (mapMemorySpace) {
std::unique_ptr<ConversionTarget> target =
spirv::getMemorySpaceToStorageClassTarget(*context);
spirv::MemorySpaceToStorageClassMap memorySpaceMap =
spirv::mapMemorySpaceToVulkanStorageClass;
(memoryModel == spirv::MemoryModel::OpenCL)
Copy link
Member

Choose a reason for hiding this comment

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

Here we can also use the above targetEnvSupportsKernelCapability no? Something like:

spirv::MemorySpaceToStorageClassMap memorySpaceMap =
  targetEnvSupportsKernelCapability(gpuModule) ? ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

? spirv::mapMemorySpaceToOpenCLStorageClass
: spirv::mapMemorySpaceToVulkanStorageClass;
spirv::MemorySpaceToStorageClassConverter converter(memorySpaceMap);

RewritePatternSet patterns(context);
Expand All @@ -79,13 +125,6 @@ void GPUToSPIRVPass::runOnOperation() {
return signalPassFailure();
}

auto targetAttr = spirv::lookupTargetEnvOrDefault(gpuModule);
std::unique_ptr<ConversionTarget> target =
SPIRVConversionTarget::get(targetAttr);

SPIRVConversionOptions options;
options.use64bitIndex = this->use64bitIndex;
SPIRVTypeConverter typeConverter(targetAttr, options);
populateMMAToSPIRVCoopMatrixTypeConversion(typeConverter,
this->useCoopMatrixNV);

Expand All @@ -108,6 +147,31 @@ void GPUToSPIRVPass::runOnOperation() {
if (failed(applyFullConversion(gpuModule, *target, std::move(patterns))))
return signalPassFailure();
}

// In case of OpenCL, gpu.func in original gpu.module needs to replaced with
// an empty func.func with same arguments as gpu.func. And it also needs
// gpu.kernel attribute set.
module.walk([&](gpu::GPUModuleOp moduleOp) {
const mlir::spirv::TargetEnv &targetEnv =
getTargetEnvFromGPUModuleOp(moduleOp);
FailureOr<spirv::MemoryModel> memoryModel =
spirv::getMemoryModel(targetEnv);
if (failed(memoryModel))
return signalPassFailure();
if (memoryModel == spirv::MemoryModel::OpenCL) {
moduleOp.walk([&](gpu::GPUFuncOp funcOp) {
builder.setInsertionPoint(funcOp);
auto newFuncOp = builder.create<func::FuncOp>(
funcOp.getLoc(), funcOp.getName(), funcOp.getFunctionType());
auto entryBlock = newFuncOp.addEntryBlock();
builder.setInsertionPointToEnd(entryBlock);
builder.create<func::ReturnOp>(funcOp.getLoc());
newFuncOp->setAttr(gpu::GPUDialect::getKernelFuncAttrName(),
builder.getUnitAttr());
funcOp.erase();
});
}
});
}

} // namespace
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/Conversion/GPUToSPIRV/module-opencl.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module attributes {
// CHECK-SAME: {{%.*}}: !spirv.ptr<!spirv.array<12 x f32>, CrossWorkgroup>
// CHECK-NOT: spirv.interface_var_abi
// CHECK-SAME: spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [32, 4, 1]>
// CHECK-LABEL: func.func @basic_module_structure
// CHECK-SAME: attributes {gpu.kernel}
gpu.func @basic_module_structure(%arg0 : f32, %arg1 : memref<12xf32, #spirv.storage_class<CrossWorkgroup>>) kernel
attributes {spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [32, 4, 1]>} {
gpu.return
Expand Down Expand Up @@ -45,6 +47,8 @@ module attributes {
// CHECK-SAME: {{%.*}}: !spirv.ptr<!spirv.array<12 x f32>, CrossWorkgroup>
// CHECK-NOT: spirv.interface_var_abi
// CHECK-SAME: spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [32, 4, 1]>
// CHECK-LABEL: func.func @basic_module_structure
// CHECK-SAME: attributes {gpu.kernel}
gpu.func @basic_module_structure(%arg0 : f32, %arg1 : memref<12xf32, #spirv.storage_class<CrossWorkgroup>>) kernel
attributes {spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [32, 4, 1]>} {
gpu.return
Expand Down