xref: /llvm-project/mlir/include/mlir-c/Dialect/SparseTensor.h (revision 2a6b521b36fb538a49564323ecd457d7b08b1325)
1 //===-- mlir-c/Dialect/SparseTensor.h - C API for SparseTensor ----*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4 // Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef MLIR_C_DIALECT_SPARSETENSOR_H
11 #define MLIR_C_DIALECT_SPARSETENSOR_H
12 
13 #include "mlir-c/AffineMap.h"
14 #include "mlir-c/IR.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(SparseTensor, sparse_tensor);
21 
22 /// Dimension level types (and properties) that define sparse tensors.
23 /// See the documentation in SparseTensorAttrDefs.td for their meaning.
24 ///
25 /// These correspond to SparseTensorEncodingAttr::LevelType in the C++ API.
26 /// If updating, keep them in sync and update the static_assert in the impl
27 /// file.
28 typedef uint64_t MlirSparseTensorLevelType;
29 
30 enum MlirBaseSparseTensorLevelType {
31   MLIR_SPARSE_TENSOR_LEVEL_DENSE = 0x000000010000,
32   MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED = 0x000000020000,
33   MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU = 0x000000020001,
34   MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NO = 0x000000020002,
35   MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU_NO = 0x000000020003,
36   MLIR_SPARSE_TENSOR_LEVEL_SINGLETON = 0x000000040000,
37   MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU = 0x000000040001,
38   MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NO = 0x000000040002,
39   MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU_NO = 0x000000040003,
40   MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED = 0x000000080000,
41   MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NU = 0x000000080001,
42   MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NO = 0x000000080002,
43   MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NU_NO = 0x000000080003,
44   MLIR_SPARSE_TENSOR_LEVEL_N_OUT_OF_M = 0x000000100000,
45 };
46 
47 //===----------------------------------------------------------------------===//
48 // SparseTensorEncodingAttr
49 //===----------------------------------------------------------------------===//
50 
51 /// Checks whether the given attribute is a `sparse_tensor.encoding` attribute.
52 MLIR_CAPI_EXPORTED bool
53 mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr);
54 
55 /// Creates a `sparse_tensor.encoding` attribute with the given parameters.
56 MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet(
57     MlirContext ctx, intptr_t lvlRank,
58     MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl,
59     MlirAffineMap lvlTodim, int posWidth, int crdWidth);
60 
61 /// Returns the level-rank of the `sparse_tensor.encoding` attribute.
62 MLIR_CAPI_EXPORTED intptr_t
63 mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr);
64 
65 /// Returns a specified level-type of the `sparse_tensor.encoding` attribute.
66 MLIR_CAPI_EXPORTED MlirSparseTensorLevelType
67 mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl);
68 
69 /// Returns the dimension-to-level mapping of the `sparse_tensor.encoding`
70 /// attribute.
71 MLIR_CAPI_EXPORTED MlirAffineMap
72 mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr);
73 
74 /// Returns the level-to-dimension mapping of the `sparse_tensor.encoding`
75 /// attribute.
76 MLIR_CAPI_EXPORTED MlirAffineMap
77 mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr);
78 
79 /// Returns the position bitwidth of the `sparse_tensor.encoding` attribute.
80 MLIR_CAPI_EXPORTED int
81 mlirSparseTensorEncodingAttrGetPosWidth(MlirAttribute attr);
82 
83 /// Returns the coordinate bitwidth of the `sparse_tensor.encoding` attribute.
84 MLIR_CAPI_EXPORTED int
85 mlirSparseTensorEncodingAttrGetCrdWidth(MlirAttribute attr);
86 
87 MLIR_CAPI_EXPORTED unsigned
88 mlirSparseTensorEncodingAttrGetStructuredN(MlirSparseTensorLevelType lvlType);
89 
90 MLIR_CAPI_EXPORTED unsigned
91 mlirSparseTensorEncodingAttrGetStructuredM(MlirSparseTensorLevelType lvlType);
92 
93 MLIR_CAPI_EXPORTED MlirSparseTensorLevelType
94 mlirSparseTensorEncodingAttrBuildLvlType(
95     enum MlirBaseSparseTensorLevelType lvlType, unsigned n, unsigned m);
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #include "mlir/Dialect/SparseTensor/Transforms/Passes.capi.h.inc"
102 
103 #endif // MLIR_C_DIALECT_SPARSETENSOR_H
104