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 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 39 enum MlirSparseTensorLevelPropertyNondefault { 40 MLIR_SPARSE_PROPERTY_NON_UNIQUE = 0x0001, 41 MLIR_SPARSE_PROPERTY_NON_ORDERED = 0x0002, 42 MLIR_SPARSE_PROPERTY_SOA = 0x0004, 43 }; 44 45 //===----------------------------------------------------------------------===// 46 // SparseTensorEncodingAttr 47 //===----------------------------------------------------------------------===// 48 49 /// Checks whether the given attribute is a `sparse_tensor.encoding` attribute. 50 MLIR_CAPI_EXPORTED bool 51 mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr); 52 53 /// Creates a `sparse_tensor.encoding` attribute with the given parameters. 54 MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet( 55 MlirContext ctx, intptr_t lvlRank, 56 MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl, 57 MlirAffineMap lvlTodim, int posWidth, int crdWidth, 58 MlirAttribute explicitVal, MlirAttribute implicitVal); 59 60 /// Returns the level-rank of the `sparse_tensor.encoding` attribute. 61 MLIR_CAPI_EXPORTED intptr_t 62 mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr); 63 64 /// Returns a specified level-type of the `sparse_tensor.encoding` attribute. 65 MLIR_CAPI_EXPORTED MlirSparseTensorLevelType 66 mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl); 67 68 /// Returns a specified level-format of the `sparse_tensor.encoding` attribute. 69 MLIR_CAPI_EXPORTED enum MlirSparseTensorLevelFormat 70 mlirSparseTensorEncodingAttrGetLvlFmt(MlirAttribute attr, intptr_t lvl); 71 72 /// Returns the dimension-to-level mapping of the `sparse_tensor.encoding` 73 /// attribute. 74 MLIR_CAPI_EXPORTED MlirAffineMap 75 mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr); 76 77 /// Returns the level-to-dimension mapping of the `sparse_tensor.encoding` 78 /// attribute. 79 MLIR_CAPI_EXPORTED MlirAffineMap 80 mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr); 81 82 /// Returns the position bitwidth of the `sparse_tensor.encoding` attribute. 83 MLIR_CAPI_EXPORTED int 84 mlirSparseTensorEncodingAttrGetPosWidth(MlirAttribute attr); 85 86 /// Returns the coordinate bitwidth of the `sparse_tensor.encoding` attribute. 87 MLIR_CAPI_EXPORTED int 88 mlirSparseTensorEncodingAttrGetCrdWidth(MlirAttribute attr); 89 90 /// Returns the explicit value of the `sparse_tensor.encoding` attribute. 91 MLIR_CAPI_EXPORTED MlirAttribute 92 mlirSparseTensorEncodingAttrGetExplicitVal(MlirAttribute attr); 93 94 /// Returns the implicit value of the `sparse_tensor.encoding` attribute. 95 MLIR_CAPI_EXPORTED MlirAttribute 96 mlirSparseTensorEncodingAttrGetImplicitVal(MlirAttribute attr); 97 98 MLIR_CAPI_EXPORTED unsigned 99 mlirSparseTensorEncodingAttrGetStructuredN(MlirSparseTensorLevelType lvlType); 100 101 MLIR_CAPI_EXPORTED unsigned 102 mlirSparseTensorEncodingAttrGetStructuredM(MlirSparseTensorLevelType lvlType); 103 104 MLIR_CAPI_EXPORTED MlirSparseTensorLevelType 105 mlirSparseTensorEncodingAttrBuildLvlType( 106 enum MlirSparseTensorLevelFormat lvlFmt, 107 const enum MlirSparseTensorLevelPropertyNondefault *properties, 108 unsigned propSize, unsigned n, unsigned m); 109 110 #ifdef __cplusplus 111 } 112 #endif 113 114 #include "mlir/Dialect/SparseTensor/Transforms/Passes.capi.h.inc" 115 116 #endif // MLIR_C_DIALECT_SPARSETENSOR_H 117