xref: /llvm-project/mlir/lib/ExecutionEngine/SparseTensor/MapRef.cpp (revision d3af65358d5935dbf4710ebaa8f822e859b64cf4)
1 //===- MapRef.cpp - A dim2lvl/lvl2dim map reference wrapper ---------------===//
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/ExecutionEngine/SparseTensor/MapRef.h"
10 
11 mlir::sparse_tensor::MapRef::MapRef(uint64_t d, uint64_t l, const uint64_t *d2l,
12                                     const uint64_t *l2d)
13     : dimRank(d), lvlRank(l), dim2lvl(d2l), lvl2dim(l2d),
14       isPermutation(isPermutationMap()) {
15   if (isPermutation) {
16     for (uint64_t i = 0; i < dimRank; i++)
17       assert(lvl2dim[dim2lvl[i]] == i);
18   }
19 }
20 
21 bool mlir::sparse_tensor::MapRef::isPermutationMap() const {
22   if (dimRank != lvlRank)
23     return false;
24   std::vector<bool> seen(dimRank, false);
25   for (uint64_t i = 0; i < dimRank; i++) {
26     const uint64_t j = dim2lvl[i];
27     if (j >= dimRank || seen[j])
28       return false;
29     seen[j] = true;
30   }
31   return true;
32 }
33