MLIR  20.0.0git
GPUToSPIRVPass.cpp
Go to the documentation of this file.
1 //===- GPUToSPIRVPass.cpp - GPU to SPIR-V Passes --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a pass to convert a kernel function in the GPU Dialect
10 // into a spirv.module operation.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
27 #include "mlir/IR/PatternMatch.h"
28 
29 namespace mlir {
30 #define GEN_PASS_DEF_CONVERTGPUTOSPIRV
31 #include "mlir/Conversion/Passes.h.inc"
32 } // namespace mlir
33 
34 using namespace mlir;
35 
36 namespace {
37 /// Pass to lower GPU Dialect to SPIR-V. The pass only converts the gpu.func ops
38 /// inside gpu.module ops. i.e., the function that are referenced in
39 /// gpu.launch_func ops. For each such function
40 ///
41 /// 1) Create a spirv::ModuleOp, and clone the function into spirv::ModuleOp
42 /// (the original function is still needed by the gpu::LaunchKernelOp, so cannot
43 /// replace it).
44 ///
45 /// 2) Lower the body of the spirv::ModuleOp.
46 struct GPUToSPIRVPass final : impl::ConvertGPUToSPIRVBase<GPUToSPIRVPass> {
47  explicit GPUToSPIRVPass(bool mapMemorySpace)
48  : mapMemorySpace(mapMemorySpace) {}
49  void runOnOperation() override;
50 
51 private:
52  bool mapMemorySpace;
53 };
54 
55 void GPUToSPIRVPass::runOnOperation() {
56  MLIRContext *context = &getContext();
57  ModuleOp module = getOperation();
58 
59  SmallVector<Operation *, 1> gpuModules;
60  OpBuilder builder(context);
61 
62  auto targetEnvSupportsKernelCapability = [](gpu::GPUModuleOp moduleOp) {
63  Operation *gpuModule = moduleOp.getOperation();
64  auto targetAttr = spirv::lookupTargetEnvOrDefault(gpuModule);
65  spirv::TargetEnv targetEnv(targetAttr);
66  return targetEnv.allows(spirv::Capability::Kernel);
67  };
68 
69  module.walk([&](gpu::GPUModuleOp moduleOp) {
70  // Clone each GPU kernel module for conversion, given that the GPU
71  // launch op still needs the original GPU kernel module.
72  // For Vulkan Shader capabilities, we insert the newly converted SPIR-V
73  // module right after the original GPU module, as that's the expectation of
74  // the in-tree Vulkan runner.
75  // For OpenCL Kernel capabilities, we insert the newly converted SPIR-V
76  // module inside the original GPU module, as that's the expectaion of the
77  // normal GPU compilation pipeline.
78  if (targetEnvSupportsKernelCapability(moduleOp)) {
79  builder.setInsertionPointToStart(moduleOp.getBody());
80  } else {
81  builder.setInsertionPoint(moduleOp.getOperation());
82  }
83  gpuModules.push_back(builder.clone(*moduleOp.getOperation()));
84  });
85 
86  // Run conversion for each module independently as they can have different
87  // TargetEnv attributes.
88  for (Operation *gpuModule : gpuModules) {
89  spirv::TargetEnvAttr targetAttr =
91 
92  // Map MemRef memory space to SPIR-V storage class first if requested.
93  if (mapMemorySpace) {
95  targetEnvSupportsKernelCapability(
96  dyn_cast<gpu::GPUModuleOp>(gpuModule))
99  spirv::MemorySpaceToStorageClassConverter converter(memorySpaceMap);
100  spirv::convertMemRefTypesAndAttrs(gpuModule, converter);
101 
102  // Check if there are any illegal ops remaining.
103  std::unique_ptr<ConversionTarget> target =
105  gpuModule->walk([&target, this](Operation *childOp) {
106  if (target->isIllegal(childOp)) {
107  childOp->emitOpError("failed to legalize memory space");
108  signalPassFailure();
109  return WalkResult::interrupt();
110  }
111  return WalkResult::advance();
112  });
113  }
114 
115  std::unique_ptr<ConversionTarget> target =
116  SPIRVConversionTarget::get(targetAttr);
117 
119  options.use64bitIndex = this->use64bitIndex;
120  SPIRVTypeConverter typeConverter(targetAttr, options);
122 
123  RewritePatternSet patterns(context);
124  populateGPUToSPIRVPatterns(typeConverter, patterns);
126  patterns);
127 
128  // TODO: Change SPIR-V conversion to be progressive and remove the following
129  // patterns.
130  ScfToSPIRVContext scfContext;
131  populateSCFToSPIRVPatterns(typeConverter, scfContext, patterns);
132  mlir::arith::populateArithToSPIRVPatterns(typeConverter, patterns);
133  populateMemRefToSPIRVPatterns(typeConverter, patterns);
134  populateFuncToSPIRVPatterns(typeConverter, patterns);
135  populateVectorToSPIRVPatterns(typeConverter, patterns);
136 
137  if (failed(applyFullConversion(gpuModule, *target, std::move(patterns))))
138  return signalPassFailure();
139  }
140 
141  // For OpenCL, the gpu.func op in the original gpu.module op needs to be
142  // replaced with an empty func.func op with the same arguments as the gpu.func
143  // op. The func.func op needs gpu.kernel attribute set.
144  module.walk([&](gpu::GPUModuleOp moduleOp) {
145  if (targetEnvSupportsKernelCapability(moduleOp)) {
146  moduleOp.walk([&](gpu::GPUFuncOp funcOp) {
147  builder.setInsertionPoint(funcOp);
148  auto newFuncOp = builder.create<func::FuncOp>(
149  funcOp.getLoc(), funcOp.getName(), funcOp.getFunctionType());
150  auto entryBlock = newFuncOp.addEntryBlock();
151  builder.setInsertionPointToEnd(entryBlock);
152  builder.create<func::ReturnOp>(funcOp.getLoc());
153  newFuncOp->setAttr(gpu::GPUDialect::getKernelFuncAttrName(),
154  builder.getUnitAttr());
155  funcOp.erase();
156  });
157  }
158  });
159 }
160 
161 } // namespace
162 
163 std::unique_ptr<OperationPass<ModuleOp>>
164 mlir::createConvertGPUToSPIRVPass(bool mapMemorySpace) {
165  return std::make_unique<GPUToSPIRVPass>(mapMemorySpace);
166 }
static MLIRContext * getContext(OpFoldResult val)
static llvm::ManagedStatic< PassManagerOptions > options
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:215
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition: Operation.h:793
static std::unique_ptr< SPIRVConversionTarget > get(spirv::TargetEnvAttr targetAttr)
Creates a SPIR-V conversion target for the given target environment.
Type conversion from builtin types to SPIR-V types for shader interface.
static WalkResult advance()
Definition: Visitors.h:51
Type converter for converting numeric MemRef memory spaces into SPIR-V symbolic ones.
Definition: MemRefToSPIRV.h:48
An attribute that specifies the target version, allowed extensions and capabilities,...
A wrapper class around a spirv::TargetEnvAttr to provide query methods for allowed version/capabiliti...
Definition: TargetAndABI.h:29
void populateArithToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
std::unique_ptr< ConversionTarget > getMemorySpaceToStorageClassTarget(MLIRContext &)
Creates the target that populates legality of ops with MemRef types.
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
std::function< std::optional< spirv::StorageClass >(Attribute)> MemorySpaceToStorageClassMap
Mapping from numeric MemRef memory spaces into SPIR-V symbolic ones.
Definition: MemRefToSPIRV.h:26
void convertMemRefTypesAndAttrs(Operation *op, MemorySpaceToStorageClassConverter &typeConverter)
Converts all MemRef types and attributes in the op, as decided by the typeConverter.
std::optional< spirv::StorageClass > mapMemorySpaceToOpenCLStorageClass(Attribute)
Maps MemRef memory spaces to storage classes for OpenCL-flavored SPIR-V using the default rule.
std::optional< spirv::StorageClass > mapMemorySpaceToVulkanStorageClass(Attribute)
Maps MemRef memory spaces to storage classes for Vulkan-flavored SPIR-V using the default rule.
Include the generated interface declarations.
std::unique_ptr< OperationPass< ModuleOp > > createConvertGPUToSPIRVPass(bool mapMemorySpace=true)
Creates a pass to convert GPU kernel ops to corresponding SPIR-V ops.
LogicalResult applyFullConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Apply a complete conversion on the given operations, and all nested operations.
void populateFuncToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Func ops to SPIR-V ops.
Definition: FuncToSPIRV.cpp:90
void populateGpuWMMAToSPIRVCoopMatrixKHRConversionPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Collect a set of patterns to convert WMMA ops from GPU dialect to SPIRV, using the KHR Cooperative Ma...
void populateSCFToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, ScfToSPIRVContext &scfToSPIRVContext, RewritePatternSet &patterns)
Collects a set of patterns to lower from scf.for, scf.if, and loop.terminator to CFG operations withi...
Definition: SCFToSPIRV.cpp:439
void populateGPUToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating GPU Ops to SPIR-V ops.
Definition: GPUToSPIRV.cpp:732
void populateMMAToSPIRVCoopMatrixTypeConversion(SPIRVTypeConverter &typeConverter)
Adds MMAMatrixType conversions to SPIR-V cooperative matrix KHR type conversion to the type converter...
void populateMemRefToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating MemRef ops to SPIR-V ops.
void populateVectorToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Vector Ops to SPIR-V ops.