xref: /llvm-project/mlir/include/mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h (revision 206fad0e218e83799e49ca15545d997c6c5e8a03)
1 //===- MemRefToSPIRV.h - MemRef to SPIR-V Patterns --------------*- C++ -*-===//
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 // Provides patterns to convert MemRef dialect to SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_CONVERSION_MEMREFTOSPIRV_MEMREFTOSPIRV_H
14 #define MLIR_CONVERSION_MEMREFTOSPIRV_MEMREFTOSPIRV_H
15 
16 #include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
17 #include "mlir/Transforms/DialectConversion.h"
18 #include <memory>
19 
20 namespace mlir {
21 class SPIRVTypeConverter;
22 
23 namespace spirv {
24 /// Mapping from numeric MemRef memory spaces into SPIR-V symbolic ones.
25 using MemorySpaceToStorageClassMap =
26     std::function<std::optional<spirv::StorageClass>(Attribute)>;
27 
28 /// Maps MemRef memory spaces to storage classes for Vulkan-flavored SPIR-V
29 /// using the default rule. Returns std::nullopt if the memory space is unknown.
30 std::optional<spirv::StorageClass>
31     mapMemorySpaceToVulkanStorageClass(Attribute);
32 /// Maps storage classes for Vulkan-flavored SPIR-V to MemRef memory spaces
33 /// using the default rule. Returns std::nullopt if the storage class is
34 /// unsupported.
35 std::optional<unsigned> mapVulkanStorageClassToMemorySpace(spirv::StorageClass);
36 
37 /// Maps MemRef memory spaces to storage classes for OpenCL-flavored SPIR-V
38 /// using the default rule. Returns std::nullopt if the memory space is unknown.
39 std::optional<spirv::StorageClass>
40     mapMemorySpaceToOpenCLStorageClass(Attribute);
41 /// Maps storage classes for OpenCL-flavored SPIR-V to MemRef memory spaces
42 /// using the default rule. Returns std::nullopt if the storage class is
43 /// unsupported.
44 std::optional<unsigned> mapOpenCLStorageClassToMemorySpace(spirv::StorageClass);
45 
46 /// Type converter for converting numeric MemRef memory spaces into SPIR-V
47 /// symbolic ones.
48 class MemorySpaceToStorageClassConverter : public TypeConverter {
49 public:
50   explicit MemorySpaceToStorageClassConverter(
51       const MemorySpaceToStorageClassMap &memorySpaceMap);
52 
53 private:
54   MemorySpaceToStorageClassMap memorySpaceMap;
55 };
56 
57 /// Creates the target that populates legality of ops with MemRef types.
58 std::unique_ptr<ConversionTarget>
59 getMemorySpaceToStorageClassTarget(MLIRContext &);
60 
61 /// Converts all MemRef types and attributes in the op, as decided by the
62 /// `typeConverter`.
63 void convertMemRefTypesAndAttrs(
64     Operation *op, MemorySpaceToStorageClassConverter &typeConverter);
65 
66 } // namespace spirv
67 
68 /// Appends to a pattern list additional patterns for translating MemRef ops
69 /// to SPIR-V ops.
70 void populateMemRefToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,
71                                    RewritePatternSet &patterns);
72 
73 } // namespace mlir
74 
75 #endif // MLIR_CONVERSION_MEMREFTOSPIRV_MEMREFTOSPIRV_H
76