
The old "pointer/index" names often cause confusion since these names clash with names of unrelated things in MLIR; so this change rectifies this by changing everything to use "position/coordinate" terminology instead. In addition to the basic terminology, there have also been various conventions for making certain distinctions like: (1) the overall storage for coordinates in the sparse-tensor, vs the particular collection of coordinates of a given element; and (2) particular coordinates given as a `Value` or `TypedValue<MemRefType>`, vs particular coordinates given as `ValueRange` or similar. I have striven to maintain these distinctions as follows: * "p/c" are used for individual position/coordinate values, when there is no risk of confusion. (Just like we use "d/l" to abbreviate "dim/lvl".) * "pos/crd" are used for individual position/coordinate values, when a longer name is helpful to avoid ambiguity or to form compound names (e.g., "parentPos"). (Just like we use "dim/lvl" when we need a longer form of "d/l".) I have also used these forms for a handful of compound names where the old name had been using a three-letter form previously, even though a longer form would be more appropriate. I've avoided renaming these to use a longer form purely for expediency sake, since changing them would require a cascade of other renamings. They should be updated to follow the new naming scheme, but that can be done in future patches. * "coords" is used for the complete collection of crd values associated with a single element. In the runtime library this includes both `std::vector` and raw pointer representations. In the compiler, this is used specifically for buffer variables with C++ type `Value`, `TypedValue<MemRefType>`, etc. The bare form "coords" is discouraged, since it fails to make the dim/lvl distinction; so the compound names "dimCoords/lvlCoords" should be used instead. (Though there may exist a rare few cases where is is appropriate to be intentionally ambiguous about what coordinate-space the coords live in; in which case the bare "coords" is appropriate.) There is seldom the need for the pos variant of this notion. In most circumstances we use the term "cursor", since the same buffer is reused for a 'moving' pos-collection. * "dcvs/lcvs" is used in the compiler as the `ValueRange` analogue of "dimCoords/lvlCoords". (The "vs" stands for "`Value`s".) I haven't found the need for it, but "pvs" would be the obvious name for a pos-`ValueRange`. The old "ind"-vs-"ivs" naming scheme does not seem to have been sustained in more recent code, which instead prefers other mnemonics (e.g., adding "Buf" to the end of the names for `TypeValue<MemRefType>`). I have cleaned up a lot of these to follow the "coords"-vs-"cvs" naming scheme, though haven't done an exhaustive cleanup. * "positions/coordinates" are used for larger collections of pos/crd values; in particular, these are used when referring to the complete sparse-tensor storage components. I also prefer to use these unabbreviated names in the documentation, unless there is some specific reason why using the abbreviated forms helps resolve ambiguity. In addition to making this terminology change, this change also does some cleanup along the way: * correcting the dim/lvl terminology in certain places. * adding `const` when it requires no other code changes. * miscellaneous cleanup that was entailed in order to make the proper distinctions. Most of these are in CodegenUtils.{h,cpp} Reviewed By: aartbik Differential Revision: https://reviews.llvm.org/D144773
114 lines
5.5 KiB
C++
114 lines
5.5 KiB
C++
//===- StorageBase.cpp - TACO-flavored sparse tensor representation -------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains method definitions for `SparseTensorStorageBase`.
|
|
// In particular we want to ensure that the default implementations of
|
|
// the "partial method specialization" trick aren't inline (since there's
|
|
// no benefit). Though this also helps ensure that we avoid weak-vtables:
|
|
// <https://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers>
|
|
//
|
|
// This file is part of the lightweight runtime support library for sparse
|
|
// tensor manipulations. The functionality of the support library is meant
|
|
// to simplify benchmarking, testing, and debugging MLIR code operating on
|
|
// sparse tensors. However, the provided functionality is **not** part of
|
|
// core MLIR itself.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/ExecutionEngine/SparseTensor/Storage.h"
|
|
|
|
using namespace mlir::sparse_tensor;
|
|
|
|
SparseTensorStorageBase::SparseTensorStorageBase( // NOLINT
|
|
uint64_t dimRank, const uint64_t *dimSizes, uint64_t lvlRank,
|
|
const uint64_t *lvlSizes, const DimLevelType *lvlTypes,
|
|
const uint64_t *lvl2dim)
|
|
: dimSizes(dimSizes, dimSizes + dimRank),
|
|
lvlSizes(lvlSizes, lvlSizes + lvlRank),
|
|
lvlTypes(lvlTypes, lvlTypes + lvlRank),
|
|
lvl2dim(lvl2dim, lvl2dim + lvlRank) {
|
|
// TODO: If we do get any nullptrs, I'm pretty sure these assertions
|
|
// will run too late (i.e., after copying things into vectors above).
|
|
// But since those fields are const I'm not sure there's any clean way
|
|
// to assert things before copying...
|
|
assert(dimSizes && "Got nullptr for dimension sizes");
|
|
assert(lvlSizes && "Got nullptr for level sizes");
|
|
assert(lvlTypes && "Got nullptr for level types");
|
|
assert(lvl2dim && "Got nullptr for level-to-dimension mapping");
|
|
// Validate dim-indexed parameters.
|
|
assert(dimRank > 0 && "Trivial shape is unsupported");
|
|
for (uint64_t d = 0; d < dimRank; ++d)
|
|
assert(dimSizes[d] > 0 && "Dimension size zero has trivial storage");
|
|
// Validate level-indexed parameters.
|
|
assert(lvlRank > 0 && "Trivial shape is unsupported");
|
|
for (uint64_t l = 0; l < lvlRank; ++l) {
|
|
assert(lvlSizes[l] > 0 && "Level size zero has trivial storage");
|
|
const auto dlt = lvlTypes[l]; // Avoid redundant bounds checking.
|
|
// We use `MLIR_SPARSETENSOR_FATAL` here instead of `assert` so that
|
|
// when this ctor is successful then all the methods can rely on the
|
|
// fact that each level-type satisfies one of these options (even
|
|
// when `NDEBUG` is true), thereby reducing the need to re-assert things.
|
|
if (!(isDenseDLT(dlt) || isCompressedDLT(dlt) || isSingletonDLT(dlt)))
|
|
MLIR_SPARSETENSOR_FATAL("unsupported level type: %d\n",
|
|
static_cast<uint8_t>(dlt));
|
|
}
|
|
}
|
|
|
|
// Helper macro for generating error messages when some
|
|
// `SparseTensorStorage<P,I,V>` is cast to `SparseTensorStorageBase`
|
|
// and then the wrong "partial method specialization" is called.
|
|
#define FATAL_PIV(NAME) \
|
|
MLIR_SPARSETENSOR_FATAL("<P,I,V> type mismatch for: " #NAME);
|
|
|
|
#define IMPL_NEWENUMERATOR(VNAME, V) \
|
|
void SparseTensorStorageBase::newEnumerator( \
|
|
SparseTensorEnumeratorBase<V> **, uint64_t, const uint64_t *, uint64_t, \
|
|
const uint64_t *) const { \
|
|
FATAL_PIV("newEnumerator" #VNAME); \
|
|
}
|
|
MLIR_SPARSETENSOR_FOREVERY_V(IMPL_NEWENUMERATOR)
|
|
#undef IMPL_NEWENUMERATOR
|
|
|
|
#define IMPL_GETPOSITIONS(PNAME, P) \
|
|
void SparseTensorStorageBase::getPositions(std::vector<P> **, uint64_t) { \
|
|
FATAL_PIV("getPositions" #PNAME); \
|
|
}
|
|
MLIR_SPARSETENSOR_FOREVERY_FIXED_O(IMPL_GETPOSITIONS)
|
|
#undef IMPL_GETPOSITIONS
|
|
|
|
#define IMPL_GETCOORDINATES(CNAME, C) \
|
|
void SparseTensorStorageBase::getCoordinates(std::vector<C> **, uint64_t) { \
|
|
FATAL_PIV("getCoordinates" #CNAME); \
|
|
}
|
|
MLIR_SPARSETENSOR_FOREVERY_FIXED_O(IMPL_GETCOORDINATES)
|
|
#undef IMPL_GETCOORDINATES
|
|
|
|
#define IMPL_GETVALUES(VNAME, V) \
|
|
void SparseTensorStorageBase::getValues(std::vector<V> **) { \
|
|
FATAL_PIV("getValues" #VNAME); \
|
|
}
|
|
MLIR_SPARSETENSOR_FOREVERY_V(IMPL_GETVALUES)
|
|
#undef IMPL_GETVALUES
|
|
|
|
#define IMPL_LEXINSERT(VNAME, V) \
|
|
void SparseTensorStorageBase::lexInsert(const uint64_t *, V) { \
|
|
FATAL_PIV("lexInsert" #VNAME); \
|
|
}
|
|
MLIR_SPARSETENSOR_FOREVERY_V(IMPL_LEXINSERT)
|
|
#undef IMPL_LEXINSERT
|
|
|
|
#define IMPL_EXPINSERT(VNAME, V) \
|
|
void SparseTensorStorageBase::expInsert(uint64_t *, V *, bool *, uint64_t *, \
|
|
uint64_t) { \
|
|
FATAL_PIV("expInsert" #VNAME); \
|
|
}
|
|
MLIR_SPARSETENSOR_FOREVERY_V(IMPL_EXPINSERT)
|
|
#undef IMPL_EXPINSERT
|
|
|
|
#undef FATAL_PIV
|