29 #define GEN_PASS_DEF_CONVERTGPUTOSPIRV
30 #include "mlir/Conversion/Passes.h.inc"
45 struct GPUToSPIRVPass final : impl::ConvertGPUToSPIRVBase<GPUToSPIRVPass> {
46 explicit GPUToSPIRVPass(
bool mapMemorySpace)
47 : mapMemorySpace(mapMemorySpace) {}
48 void runOnOperation()
override;
63 GPUToSPIRVPass::lookupTargetEnvInTargets(gpu::GPUModuleOp moduleOp) {
64 if (ArrayAttr targets = moduleOp.getTargetsAttr()) {
66 if (
auto spirvTargetEnvAttr = dyn_cast<spirv::TargetEnvAttr>(targetAttr))
67 return spirvTargetEnvAttr;
81 void GPUToSPIRVPass::runOnOperation() {
83 ModuleOp module = getOperation();
88 auto targetEnvSupportsKernelCapability = [
this](gpu::GPUModuleOp moduleOp) {
91 return targetEnv.allows(spirv::Capability::Kernel);
94 module.walk([&](gpu::GPUModuleOp moduleOp) {
103 if (targetEnvSupportsKernelCapability(moduleOp)) {
104 builder.setInsertionPointToStart(moduleOp.getBody());
106 builder.setInsertionPoint(moduleOp.getOperation());
108 gpuModules.push_back(builder.clone(*moduleOp.getOperation()));
113 for (
Operation *gpuModule : gpuModules) {
118 if (mapMemorySpace) {
120 targetEnvSupportsKernelCapability(
121 dyn_cast<gpu::GPUModuleOp>(gpuModule))
128 std::unique_ptr<ConversionTarget> target =
130 gpuModule->walk([&target,
this](
Operation *childOp) {
131 if (target->isIllegal(childOp)) {
132 childOp->emitOpError(
"failed to legalize memory space");
134 return WalkResult::interrupt();
140 std::unique_ptr<ConversionTarget> target =
144 options.use64bitIndex = this->use64bitIndex;
163 return signalPassFailure();
169 module.walk([&](gpu::GPUModuleOp moduleOp) {
170 if (targetEnvSupportsKernelCapability(moduleOp)) {
171 moduleOp.walk([&](gpu::GPUFuncOp funcOp) {
172 builder.setInsertionPoint(funcOp);
174 func::FuncOp::create(builder, funcOp.getLoc(), funcOp.getName(),
175 funcOp.getFunctionType());
176 auto entryBlock = newFuncOp.addEntryBlock();
177 builder.setInsertionPointToEnd(entryBlock);
178 func::ReturnOp::create(builder, funcOp.getLoc());
179 newFuncOp->setAttr(gpu::GPUDialect::getKernelFuncAttrName(),
180 builder.getUnitAttr());
189 std::unique_ptr<OperationPass<ModuleOp>>
191 return std::make_unique<GPUToSPIRVPass>(mapMemorySpace);
static MLIRContext * getContext(OpFoldResult val)
static llvm::ManagedStatic< PassManagerOptions > options
Attributes are known-constant values of operations.
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
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()
Type converter for converting numeric MemRef memory spaces into SPIR-V symbolic ones.
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...
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.
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.
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...
const FrozenRewritePatternSet & patterns
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...
void populateGPUToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating GPU Ops to SPIR-V ops.
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.