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_SPARSE_TENSOR_H 11 #define MLIR_C_DIALECT_SPARSE_TENSOR_H 12 13 #include "mlir-c/AffineMap.h" 14 #include "mlir-c/Registration.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 that define sparse tensors: 23 /// - MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE - dimension is dense, every 24 /// entry is stored 25 /// - MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED - dimension is sparse, 26 /// only nonzeros are stored. 27 /// - MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON - dimension contains single 28 /// coordinate, no siblings. 29 /// 30 /// These correspond to SparseTensorEncodingAttr::DimLevelType in the C++ API. 31 /// If updating, keep them in sync and update the static_assert in the impl 32 /// file. 33 enum MlirSparseTensorDimLevelType { 34 MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE, 35 MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED, 36 MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON, 37 }; 38 39 //===----------------------------------------------------------------------===// 40 // SparseTensorEncodingAttr 41 //===----------------------------------------------------------------------===// 42 43 /// Checks whether the given attribute is a sparse_tensor.encoding attribute. 44 MLIR_CAPI_EXPORTED bool 45 mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr); 46 47 /// Creates a sparse_tensor.encoding attribute with the given parameters. 48 MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet( 49 MlirContext ctx, intptr_t numDimLevelTypes, 50 enum MlirSparseTensorDimLevelType const *dimLevelTypes, 51 MlirAffineMap dimOrdering, int pointerBitWidth, int indexBitWidth); 52 53 /// Returns the number of dim level types in a sparse_tensor.encoding attribute. 54 MLIR_CAPI_EXPORTED intptr_t 55 mlirSparseTensorEncodingGetNumDimLevelTypes(MlirAttribute attr); 56 57 /// Returns a specified dim level type in a sparse_tensor.encoding attribute. 58 MLIR_CAPI_EXPORTED enum MlirSparseTensorDimLevelType 59 mlirSparseTensorEncodingAttrGetDimLevelType(MlirAttribute attr, intptr_t pos); 60 61 /// Returns the dimension ordering in a sparse_tensor.encoding attribute. 62 MLIR_CAPI_EXPORTED MlirAffineMap 63 mlirSparseTensorEncodingAttrGetDimOrdering(MlirAttribute attr); 64 65 /// Returns the pointer bit width in a sparse_tensor.encoding attribute. 66 MLIR_CAPI_EXPORTED int 67 mlirSparseTensorEncodingAttrGetPointerBitWidth(MlirAttribute attr); 68 69 /// Returns the index bit width in a sparse_tensor.encoding attribute. 70 MLIR_CAPI_EXPORTED int 71 mlirSparseTensorEncodingAttrGetIndexBitWidth(MlirAttribute attr); 72 73 #ifdef __cplusplus 74 } 75 #endif 76 77 #endif // MLIR_C_DIALECT_SPARSE_TENSOR_H 78