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
17extern "C" {
18#endif
19
20MLIR_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.
28typedef uint64_t MlirSparseTensorLevelType;
29
30enum MlirSparseTensorLevelFormat {
31 MLIR_SPARSE_TENSOR_LEVEL_DENSE = 0x000000010000,
32 MLIR_SPARSE_TENSOR_LEVEL_BATCH = 0x000000020000,
33 MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED = 0x000000040000,
34 MLIR_SPARSE_TENSOR_LEVEL_SINGLETON = 0x000000080000,
35 MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED = 0x000000100000,
36 MLIR_SPARSE_TENSOR_LEVEL_N_OUT_OF_M = 0x000000200000,
37};
38
39enum MlirSparseTensorLevelPropertyNondefault {
40 MLIR_SPARSE_PROPERTY_NON_UNIQUE = 0x0001,
41 MLIR_SPARSE_PROPERTY_NON_ORDERED = 0x0002,
42};
43
44//===----------------------------------------------------------------------===//
45// SparseTensorEncodingAttr
46//===----------------------------------------------------------------------===//
47
48/// Checks whether the given attribute is a `sparse_tensor.encoding` attribute.
49MLIR_CAPI_EXPORTED bool
50mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr);
51
52/// Creates a `sparse_tensor.encoding` attribute with the given parameters.
53MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet(
54 MlirContext ctx, intptr_t lvlRank,
55 MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl,
56 MlirAffineMap lvlTodim, int posWidth, int crdWidth);
57
58/// Returns the level-rank of the `sparse_tensor.encoding` attribute.
59MLIR_CAPI_EXPORTED intptr_t
60mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr);
61
62/// Returns a specified level-type of the `sparse_tensor.encoding` attribute.
63MLIR_CAPI_EXPORTED MlirSparseTensorLevelType
64mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl);
65
66/// Returns a specified level-format of the `sparse_tensor.encoding` attribute.
67MLIR_CAPI_EXPORTED enum MlirSparseTensorLevelFormat
68mlirSparseTensorEncodingAttrGetLvlFmt(MlirAttribute attr, intptr_t lvl);
69
70/// Returns the dimension-to-level mapping of the `sparse_tensor.encoding`
71/// attribute.
72MLIR_CAPI_EXPORTED MlirAffineMap
73mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr);
74
75/// Returns the level-to-dimension mapping of the `sparse_tensor.encoding`
76/// attribute.
77MLIR_CAPI_EXPORTED MlirAffineMap
78mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr);
79
80/// Returns the position bitwidth of the `sparse_tensor.encoding` attribute.
81MLIR_CAPI_EXPORTED int
82mlirSparseTensorEncodingAttrGetPosWidth(MlirAttribute attr);
83
84/// Returns the coordinate bitwidth of the `sparse_tensor.encoding` attribute.
85MLIR_CAPI_EXPORTED int
86mlirSparseTensorEncodingAttrGetCrdWidth(MlirAttribute attr);
87
88MLIR_CAPI_EXPORTED unsigned
89mlirSparseTensorEncodingAttrGetStructuredN(MlirSparseTensorLevelType lvlType);
90
91MLIR_CAPI_EXPORTED unsigned
92mlirSparseTensorEncodingAttrGetStructuredM(MlirSparseTensorLevelType lvlType);
93
94MLIR_CAPI_EXPORTED MlirSparseTensorLevelType
95mlirSparseTensorEncodingAttrBuildLvlType(
96 enum MlirSparseTensorLevelFormat lvlFmt,
97 const enum MlirSparseTensorLevelPropertyNondefault *properties,
98 unsigned propSize, unsigned n, unsigned m);
99
100#ifdef __cplusplus
101}
102#endif
103
104#include "mlir/Dialect/SparseTensor/Transforms/Passes.capi.h.inc"
105
106#endif // MLIR_C_DIALECT_SPARSETENSOR_H
107

source code of mlir/include/mlir-c/Dialect/SparseTensor.h