MLIR 22.0.0git
SparseTensor.cpp
Go to the documentation of this file.
1//===- Tensor.cpp - C API for SparseTensor dialect ------------------------===//
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
10#include "mlir-c/IR.h"
11#include "mlir/CAPI/AffineMap.h"
14#include "mlir/Support/LLVM.h"
15
16using namespace llvm;
17using namespace mlir::sparse_tensor;
18
20 mlir::sparse_tensor::SparseTensorDialect)
21
22// Ensure the C-API enums are int-castable to C++ equivalents.
23static_assert(
24 static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_DENSE) ==
25 static_cast<int>(LevelFormat::Dense) &&
26 static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED) ==
27 static_cast<int>(LevelFormat::Compressed) &&
28 static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_SINGLETON) ==
29 static_cast<int>(LevelFormat::Singleton) &&
31 static_cast<int>(LevelFormat::LooseCompressed) &&
32 static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_N_OUT_OF_M) ==
33 static_cast<int>(LevelFormat::NOutOfM),
34 "MlirSparseTensorLevelFormat (C-API) and LevelFormat (C++) mismatch");
35
36static_assert(static_cast<int>(MLIR_SPARSE_PROPERTY_NON_ORDERED) ==
37 static_cast<int>(LevelPropNonDefault::Nonordered) &&
38 static_cast<int>(MLIR_SPARSE_PROPERTY_NON_UNIQUE) ==
39 static_cast<int>(LevelPropNonDefault::Nonunique) &&
40 static_cast<int>(MLIR_SPARSE_PROPERTY_SOA) ==
41 static_cast<int>(LevelPropNonDefault::SoA),
42 "MlirSparseTensorLevelProperty (C-API) and "
43 "LevelPropertyNondefault (C++) mismatch");
44
46 return isa<SparseTensorEncodingAttr>(unwrap(attr));
47}
48
50 MlirContext ctx, intptr_t lvlRank,
51 MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl,
52 MlirAffineMap lvlToDim, int posWidth, int crdWidth,
53 MlirAttribute explicitVal, MlirAttribute implicitVal) {
54 SmallVector<LevelType> cppLvlTypes;
55
56 cppLvlTypes.reserve(lvlRank);
57 for (intptr_t l = 0; l < lvlRank; ++l)
58 cppLvlTypes.push_back(static_cast<LevelType>(lvlTypes[l]));
59
60 return wrap(SparseTensorEncodingAttr::get(
61 unwrap(ctx), cppLvlTypes, unwrap(dimToLvl), unwrap(lvlToDim), posWidth,
62 crdWidth, unwrap(explicitVal), unwrap(implicitVal)));
63}
64
65MlirAffineMap mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr) {
66 return wrap(cast<SparseTensorEncodingAttr>(unwrap(attr)).getDimToLvl());
67}
68
69MlirAffineMap mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr) {
70 return wrap(cast<SparseTensorEncodingAttr>(unwrap(attr)).getLvlToDim());
71}
72
73intptr_t mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr) {
74 return cast<SparseTensorEncodingAttr>(unwrap(attr)).getLvlRank();
75}
76
78mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl) {
79 return static_cast<MlirSparseTensorLevelType>(
80 cast<SparseTensorEncodingAttr>(unwrap(attr)).getLvlType(lvl));
81}
82
84mlirSparseTensorEncodingAttrGetLvlFmt(MlirAttribute attr, intptr_t lvl) {
85 LevelType lt =
87 return static_cast<MlirSparseTensorLevelFormat>(lt.getLvlFmt());
88}
89
91 return cast<SparseTensorEncodingAttr>(unwrap(attr)).getPosWidth();
92}
93
95 return cast<SparseTensorEncodingAttr>(unwrap(attr)).getCrdWidth();
96}
97
98MlirAttribute mlirSparseTensorEncodingAttrGetExplicitVal(MlirAttribute attr) {
99 return wrap(cast<SparseTensorEncodingAttr>(unwrap(attr)).getExplicitVal());
100}
101
102MlirAttribute mlirSparseTensorEncodingAttrGetImplicitVal(MlirAttribute attr) {
103 return wrap(cast<SparseTensorEncodingAttr>(unwrap(attr)).getImplicitVal());
104}
105
107 enum MlirSparseTensorLevelFormat lvlFmt,
108 const enum MlirSparseTensorLevelPropertyNondefault *properties,
109 unsigned size, unsigned n, unsigned m) {
110
111 std::vector<LevelPropNonDefault> props;
112 props.reserve(size);
113 for (unsigned i = 0; i < size; i++)
114 props.push_back(static_cast<LevelPropNonDefault>(properties[i]));
115
116 return static_cast<MlirSparseTensorLevelType>(
117 *buildLevelType(static_cast<LevelFormat>(lvlFmt), props, n, m));
118}
119
120unsigned
124
125unsigned
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)
intptr_t mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr)
Returns the level-rank of the sparse_tensor.encoding attribute.
MlirAffineMap mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr)
Returns the level-to-dimension mapping of the sparse_tensor.encoding attribute.
int mlirSparseTensorEncodingAttrGetCrdWidth(MlirAttribute attr)
Returns the coordinate bitwidth of the sparse_tensor.encoding attribute.
MlirAttribute mlirSparseTensorEncodingAttrGet(MlirContext ctx, intptr_t lvlRank, MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl, MlirAffineMap lvlToDim, int posWidth, int crdWidth, MlirAttribute explicitVal, MlirAttribute implicitVal)
Creates a sparse_tensor.encoding attribute with the given parameters.
MlirSparseTensorLevelType mlirSparseTensorEncodingAttrBuildLvlType(enum MlirSparseTensorLevelFormat lvlFmt, const enum MlirSparseTensorLevelPropertyNondefault *properties, unsigned size, unsigned n, unsigned m)
bool mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr)
Checks whether the given attribute is a sparse_tensor.encoding attribute.
MlirAffineMap mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr)
Returns the dimension-to-level mapping of the sparse_tensor.encoding attribute.
MlirAttribute mlirSparseTensorEncodingAttrGetExplicitVal(MlirAttribute attr)
Returns the explicit value of the sparse_tensor.encoding attribute.
int mlirSparseTensorEncodingAttrGetPosWidth(MlirAttribute attr)
Returns the position bitwidth of the sparse_tensor.encoding attribute.
unsigned mlirSparseTensorEncodingAttrGetStructuredN(MlirSparseTensorLevelType lvlType)
unsigned mlirSparseTensorEncodingAttrGetStructuredM(MlirSparseTensorLevelType lvlType)
MlirAttribute mlirSparseTensorEncodingAttrGetImplicitVal(MlirAttribute attr)
Returns the implicit value of the sparse_tensor.encoding attribute.
enum MlirSparseTensorLevelFormat mlirSparseTensorEncodingAttrGetLvlFmt(MlirAttribute attr, intptr_t lvl)
Returns a specified level-format of the sparse_tensor.encoding attribute.
MlirSparseTensorLevelType mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl)
Returns a specified level-type of the sparse_tensor.encoding attribute.
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
uint64_t MlirSparseTensorLevelType
Dimension level types (and properties) that define sparse tensors.
MlirSparseTensorLevelFormat
@ MLIR_SPARSE_TENSOR_LEVEL_N_OUT_OF_M
@ MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED
@ MLIR_SPARSE_TENSOR_LEVEL_DENSE
@ MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED
@ MLIR_SPARSE_TENSOR_LEVEL_SINGLETON
MlirSparseTensorLevelPropertyNondefault
@ MLIR_SPARSE_PROPERTY_NON_UNIQUE
@ MLIR_SPARSE_PROPERTY_NON_ORDERED
@ MLIR_SPARSE_PROPERTY_SOA
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
LevelFormat
This enum defines all supported storage format without the level properties.
Definition Enums.h:154
std::optional< LevelType > buildLevelType(LevelFormat lf, const std::vector< LevelPropNonDefault > &properties, uint64_t n=0, uint64_t m=0)
Definition Enums.h:402
uint64_t getN(LevelType lt)
Definition Enums.h:442
uint64_t getM(LevelType lt)
Definition Enums.h:443
LevelPropNonDefault
This enum defines all the nondefault properties for storage formats.
Definition Enums.h:204
This enum defines all the sparse representations supportable by the SparseTensor dialect.
Definition Enums.h:238
constexpr LevelFormat getLvlFmt() const
Get the LevelFormat of the LevelType.
Definition Enums.h:320