Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit b0a0c25

Browse files
authored
[mlir][sparse] rename DimLevelType to LevelType (#73561)
The "Dim" prefix is a legacy left-over that no longer makes sense, since we have a very strict "Dimension" vs. "Level" definition for sparse tensor types and their storage.
1 parent 9c7c7b1 commit b0a0c25

File tree

3 files changed

+59
-60
lines changed

3 files changed

+59
-60
lines changed

mlir/include/mlir-c/Dialect/SparseTensor.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(SparseTensor, sparse_tensor);
2222
/// Dimension level types (and properties) that define sparse tensors.
2323
/// See the documentation in SparseTensorAttrDefs.td for their meaning.
2424
///
25-
/// These correspond to SparseTensorEncodingAttr::DimLevelType in the C++ API.
25+
/// These correspond to SparseTensorEncodingAttr::LevelType in the C++ API.
2626
/// If updating, keep them in sync and update the static_assert in the impl
2727
/// file.
28-
enum MlirSparseTensorDimLevelType {
29-
MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE = 4, // 0b00001_00
30-
MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED = 8, // 0b00010_00
31-
MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU = 9, // 0b00010_01
32-
MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NO = 10, // 0b00010_10
33-
MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU_NO = 11, // 0b00010_11
34-
MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON = 16, // 0b00100_00
35-
MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU = 17, // 0b00100_01
36-
MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NO = 18, // 0b00100_10
37-
MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU_NO = 19, // 0b00100_11
38-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED = 32, // 0b01000_00
39-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED_NU = 33, // 0b01000_01
40-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED_NO = 34, // 0b01000_10
41-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED_NU_NO = 35, // 0b01000_11
42-
MLIR_SPARSE_TENSOR_DIM_LEVEL_TWO_OUT_OF_FOUR = 64, // 0b10000_00
28+
enum MlirSparseTensorLevelType {
29+
MLIR_SPARSE_TENSOR_LEVEL_DENSE = 4, // 0b00001_00
30+
MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED = 8, // 0b00010_00
31+
MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU = 9, // 0b00010_01
32+
MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NO = 10, // 0b00010_10
33+
MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU_NO = 11, // 0b00010_11
34+
MLIR_SPARSE_TENSOR_LEVEL_SINGLETON = 16, // 0b00100_00
35+
MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU = 17, // 0b00100_01
36+
MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NO = 18, // 0b00100_10
37+
MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU_NO = 19, // 0b00100_11
38+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED = 32, // 0b01000_00
39+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NU = 33, // 0b01000_01
40+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NO = 34, // 0b01000_10
41+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NU_NO = 35, // 0b01000_11
42+
MLIR_SPARSE_TENSOR_LEVEL_TWO_OUT_OF_FOUR = 64, // 0b10000_00
4343
};
4444

4545
//===----------------------------------------------------------------------===//
@@ -53,15 +53,15 @@ mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr);
5353
/// Creates a `sparse_tensor.encoding` attribute with the given parameters.
5454
MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet(
5555
MlirContext ctx, intptr_t lvlRank,
56-
enum MlirSparseTensorDimLevelType const *lvlTypes, MlirAffineMap dimToLvl,
56+
enum MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl,
5757
MlirAffineMap lvlTodim, int posWidth, int crdWidth);
5858

5959
/// Returns the level-rank of the `sparse_tensor.encoding` attribute.
6060
MLIR_CAPI_EXPORTED intptr_t
6161
mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr);
6262

6363
/// Returns a specified level-type of the `sparse_tensor.encoding` attribute.
64-
MLIR_CAPI_EXPORTED enum MlirSparseTensorDimLevelType
64+
MLIR_CAPI_EXPORTED enum MlirSparseTensorLevelType
6565
mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl);
6666

6767
/// Returns the dimension-to-level mapping of the `sparse_tensor.encoding`

mlir/lib/Bindings/Python/DialectSparseTensor.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ using namespace mlir;
2323
using namespace mlir::python::adaptors;
2424

2525
static void populateDialectSparseTensorSubmodule(const py::module &m) {
26-
py::enum_<MlirSparseTensorDimLevelType>(m, "DimLevelType", py::module_local())
27-
.value("dense", MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE)
28-
.value("compressed24", MLIR_SPARSE_TENSOR_DIM_LEVEL_TWO_OUT_OF_FOUR)
29-
.value("compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED)
30-
.value("compressed_nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU)
31-
.value("compressed_no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NO)
32-
.value("compressed_nu_no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU_NO)
33-
.value("singleton", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON)
34-
.value("singleton_nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU)
35-
.value("singleton_no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NO)
36-
.value("singleton_nu_no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU_NO)
37-
.value("loose_compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED)
26+
py::enum_<MlirSparseTensorLevelType>(m, "LevelType", py::module_local())
27+
.value("dense", MLIR_SPARSE_TENSOR_LEVEL_DENSE)
28+
.value("compressed24", MLIR_SPARSE_TENSOR_LEVEL_TWO_OUT_OF_FOUR)
29+
.value("compressed", MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED)
30+
.value("compressed_nu", MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU)
31+
.value("compressed_no", MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NO)
32+
.value("compressed_nu_no", MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU_NO)
33+
.value("singleton", MLIR_SPARSE_TENSOR_LEVEL_SINGLETON)
34+
.value("singleton_nu", MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU)
35+
.value("singleton_no", MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NO)
36+
.value("singleton_nu_no", MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU_NO)
37+
.value("loose_compressed", MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED)
3838
.value("loose_compressed_nu",
39-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED_NU)
39+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NU)
4040
.value("loose_compressed_no",
41-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED_NO)
41+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NO)
4242
.value("loose_compressed_nu_no",
43-
MLIR_SPARSE_TENSOR_DIM_LEVEL_LOOSE_COMPRESSED_NU_NO);
43+
MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED_NU_NO);
4444

4545
mlir_attribute_subclass(m, "EncodingAttr",
4646
mlirAttributeIsASparseTensorEncodingAttr)
4747
.def_classmethod(
4848
"get",
49-
[](py::object cls, std::vector<MlirSparseTensorDimLevelType> lvlTypes,
49+
[](py::object cls, std::vector<MlirSparseTensorLevelType> lvlTypes,
5050
std::optional<MlirAffineMap> dimToLvl,
5151
std::optional<MlirAffineMap> lvlToDim, int posWidth, int crdWidth,
5252
MlirContext context) {
@@ -64,7 +64,7 @@ static void populateDialectSparseTensorSubmodule(const py::module &m) {
6464
"lvl_types",
6565
[](MlirAttribute self) {
6666
const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
67-
std::vector<MlirSparseTensorDimLevelType> ret;
67+
std::vector<MlirSparseTensorLevelType> ret;
6868
ret.reserve(lvlRank);
6969
for (int l = 0; l < lvlRank; ++l)
7070
ret.push_back(mlirSparseTensorEncodingAttrGetLvlType(self, l));

mlir/lib/CAPI/Dialect/SparseTensor.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,39 @@ MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(SparseTensor, sparse_tensor,
2020
mlir::sparse_tensor::SparseTensorDialect)
2121

2222
// Ensure the C-API enums are int-castable to C++ equivalents.
23-
static_assert(
24-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE) ==
25-
static_cast<int>(DimLevelType::Dense) &&
26-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED) ==
27-
static_cast<int>(DimLevelType::Compressed) &&
28-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU) ==
29-
static_cast<int>(DimLevelType::CompressedNu) &&
30-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NO) ==
31-
static_cast<int>(DimLevelType::CompressedNo) &&
32-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU_NO) ==
33-
static_cast<int>(DimLevelType::CompressedNuNo) &&
34-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON) ==
35-
static_cast<int>(DimLevelType::Singleton) &&
36-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU) ==
37-
static_cast<int>(DimLevelType::SingletonNu) &&
38-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NO) ==
39-
static_cast<int>(DimLevelType::SingletonNo) &&
40-
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU_NO) ==
41-
static_cast<int>(DimLevelType::SingletonNuNo),
42-
"MlirSparseTensorDimLevelType (C-API) and DimLevelType (C++) mismatch");
23+
static_assert(static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_DENSE) ==
24+
static_cast<int>(LevelType::Dense) &&
25+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED) ==
26+
static_cast<int>(LevelType::Compressed) &&
27+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU) ==
28+
static_cast<int>(LevelType::CompressedNu) &&
29+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NO) ==
30+
static_cast<int>(LevelType::CompressedNo) &&
31+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED_NU_NO) ==
32+
static_cast<int>(LevelType::CompressedNuNo) &&
33+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_SINGLETON) ==
34+
static_cast<int>(LevelType::Singleton) &&
35+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU) ==
36+
static_cast<int>(LevelType::SingletonNu) &&
37+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NO) ==
38+
static_cast<int>(LevelType::SingletonNo) &&
39+
static_cast<int>(MLIR_SPARSE_TENSOR_LEVEL_SINGLETON_NU_NO) ==
40+
static_cast<int>(LevelType::SingletonNuNo),
41+
"MlirSparseTensorLevelType (C-API) and LevelType (C++) mismatch");
4342

4443
bool mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr) {
4544
return isa<SparseTensorEncodingAttr>(unwrap(attr));
4645
}
4746

4847
MlirAttribute
4948
mlirSparseTensorEncodingAttrGet(MlirContext ctx, intptr_t lvlRank,
50-
MlirSparseTensorDimLevelType const *lvlTypes,
49+
MlirSparseTensorLevelType const *lvlTypes,
5150
MlirAffineMap dimToLvl, MlirAffineMap lvlToDim,
5251
int posWidth, int crdWidth) {
53-
SmallVector<DimLevelType> cppLvlTypes;
52+
SmallVector<LevelType> cppLvlTypes;
5453
cppLvlTypes.reserve(lvlRank);
5554
for (intptr_t l = 0; l < lvlRank; ++l)
56-
cppLvlTypes.push_back(static_cast<DimLevelType>(lvlTypes[l]));
55+
cppLvlTypes.push_back(static_cast<LevelType>(lvlTypes[l]));
5756
return wrap(SparseTensorEncodingAttr::get(unwrap(ctx), cppLvlTypes,
5857
unwrap(dimToLvl), unwrap(lvlToDim),
5958
posWidth, crdWidth));
@@ -71,9 +70,9 @@ intptr_t mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr) {
7170
return cast<SparseTensorEncodingAttr>(unwrap(attr)).getLvlRank();
7271
}
7372

74-
MlirSparseTensorDimLevelType
73+
MlirSparseTensorLevelType
7574
mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl) {
76-
return static_cast<MlirSparseTensorDimLevelType>(
75+
return static_cast<MlirSparseTensorLevelType>(
7776
cast<SparseTensorEncodingAttr>(unwrap(attr)).getLvlType(lvl));
7877
}
7978

0 commit comments

Comments
 (0)