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
9#include "mlir-c/Dialect/SparseTensor.h"
10#include "mlir-c/IR.h"
11#include "mlir/CAPI/AffineMap.h"
12#include "mlir/CAPI/Registration.h"
13#include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
14#include "mlir/Support/LLVM.h"
15
16using namespace llvm;
17using namespace mlir::sparse_tensor;
18
19MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(SparseTensor, sparse_tensor,
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) &&
30 static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED) ==
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
45bool mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr) {
46 return isa<SparseTensorEncodingAttr>(Val: unwrap(c: attr));
47}
48
49MlirAttribute mlirSparseTensorEncodingAttrGet(
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(N: lvlRank);
57 for (intptr_t l = 0; l < lvlRank; ++l)
58 cppLvlTypes.push_back(Elt: static_cast<LevelType>(lvlTypes[l]));
59
60 return wrap(cpp: SparseTensorEncodingAttr::get(
61 context: unwrap(c: ctx), lvlTypes: cppLvlTypes, dimToLvl: unwrap(c: dimToLvl), lvlToDim: unwrap(c: lvlToDim), posWidth,
62 crdWidth, explicitVal: unwrap(c: explicitVal), implicitVal: unwrap(c: implicitVal)));
63}
64
65MlirStringRef mlirSparseTensorEncodingAttrGetName(void) {
66 return wrap(ref: SparseTensorEncodingAttr::name);
67}
68
69MlirAffineMap mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr) {
70 return wrap(cpp: cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getDimToLvl());
71}
72
73MlirAffineMap mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr) {
74 return wrap(cpp: cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getLvlToDim());
75}
76
77intptr_t mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr) {
78 return cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getLvlRank();
79}
80
81MlirSparseTensorLevelType
82mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl) {
83 return static_cast<MlirSparseTensorLevelType>(
84 cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getLvlType(l: lvl));
85}
86
87enum MlirSparseTensorLevelFormat
88mlirSparseTensorEncodingAttrGetLvlFmt(MlirAttribute attr, intptr_t lvl) {
89 LevelType lt =
90 static_cast<LevelType>(mlirSparseTensorEncodingAttrGetLvlType(attr, lvl));
91 return static_cast<MlirSparseTensorLevelFormat>(lt.getLvlFmt());
92}
93
94int mlirSparseTensorEncodingAttrGetPosWidth(MlirAttribute attr) {
95 return cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getPosWidth();
96}
97
98int mlirSparseTensorEncodingAttrGetCrdWidth(MlirAttribute attr) {
99 return cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getCrdWidth();
100}
101
102MlirAttribute mlirSparseTensorEncodingAttrGetExplicitVal(MlirAttribute attr) {
103 return wrap(cpp: cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getExplicitVal());
104}
105
106MlirAttribute mlirSparseTensorEncodingAttrGetImplicitVal(MlirAttribute attr) {
107 return wrap(cpp: cast<SparseTensorEncodingAttr>(Val: unwrap(c: attr)).getImplicitVal());
108}
109
110MlirSparseTensorLevelType mlirSparseTensorEncodingAttrBuildLvlType(
111 enum MlirSparseTensorLevelFormat lvlFmt,
112 const enum MlirSparseTensorLevelPropertyNondefault *properties,
113 unsigned size, unsigned n, unsigned m) {
114
115 std::vector<LevelPropNonDefault> props;
116 props.reserve(n: size);
117 for (unsigned i = 0; i < size; i++)
118 props.push_back(x: static_cast<LevelPropNonDefault>(properties[i]));
119
120 return static_cast<MlirSparseTensorLevelType>(
121 *buildLevelType(lf: static_cast<LevelFormat>(lvlFmt), properties: props, n, m));
122}
123
124unsigned
125mlirSparseTensorEncodingAttrGetStructuredN(MlirSparseTensorLevelType lvlType) {
126 return getN(lt: static_cast<LevelType>(lvlType));
127}
128
129unsigned
130mlirSparseTensorEncodingAttrGetStructuredM(MlirSparseTensorLevelType lvlType) {
131 return getM(lt: static_cast<LevelType>(lvlType));
132}
133