llvm-project/mlir/lib/Dialect/SparseTensor/Transforms/SparseStorageSpecifierToLLVM.cpp
wren romano 84cd51bb97 [mlir][sparse] Renaming "pointer/index" to "position/coordinate"
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
2023-03-06 12:23:33 -08:00

233 lines
9.2 KiB
C++

//===- SparseStorageSpecifierToLLVM.cpp - convert specifier to llvm -------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "CodegenUtils.h"
#include "SparseTensorStorageLayout.h"
#include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
#include <optional>
using namespace mlir;
using namespace sparse_tensor;
namespace {
//===----------------------------------------------------------------------===//
// Helper methods.
//===----------------------------------------------------------------------===//
static SmallVector<Type, 2> getSpecifierFields(StorageSpecifierType tp) {
MLIRContext *ctx = tp.getContext();
auto enc = tp.getEncoding();
const Level lvlRank = enc.getLvlRank();
SmallVector<Type, 2> result;
// TODO: how can we get the lowering type for index type in the later pipeline
// to be consistent? LLVM::StructureType does not allow index fields.
auto sizeType = IntegerType::get(tp.getContext(), 64);
auto lvlSizes = LLVM::LLVMArrayType::get(ctx, sizeType, lvlRank);
auto memSizes = LLVM::LLVMArrayType::get(ctx, sizeType,
getNumDataFieldsFromEncoding(enc));
result.push_back(lvlSizes);
result.push_back(memSizes);
return result;
}
static Type convertSpecifier(StorageSpecifierType tp) {
return LLVM::LLVMStructType::getLiteral(tp.getContext(),
getSpecifierFields(tp));
}
//===----------------------------------------------------------------------===//
// Specifier struct builder.
//===----------------------------------------------------------------------===//
constexpr uint64_t kLvlSizePosInSpecifier = 0;
constexpr uint64_t kMemSizePosInSpecifier = 1;
class SpecifierStructBuilder : public StructBuilder {
private:
Value extractField(OpBuilder &builder, Location loc,
ArrayRef<int64_t> indices) {
return genCast(builder, loc,
builder.create<LLVM::ExtractValueOp>(loc, value, indices),
builder.getIndexType());
}
void insertField(OpBuilder &builder, Location loc, ArrayRef<int64_t> indices,
Value v) {
value = builder.create<LLVM::InsertValueOp>(
loc, value, genCast(builder, loc, v, builder.getIntegerType(64)),
indices);
}
public:
explicit SpecifierStructBuilder(Value specifier) : StructBuilder(specifier) {
assert(value);
}
// Undef value for level-sizes, all zero values for memory-sizes.
static Value getInitValue(OpBuilder &builder, Location loc, Type structType);
Value lvlSize(OpBuilder &builder, Location loc, Level lvl);
void setLvlSize(OpBuilder &builder, Location loc, Level lvl, Value size);
Value memSize(OpBuilder &builder, Location loc, FieldIndex fidx);
void setMemSize(OpBuilder &builder, Location loc, FieldIndex fidx,
Value size);
};
Value SpecifierStructBuilder::getInitValue(OpBuilder &builder, Location loc,
Type structType) {
Value metaData = builder.create<LLVM::UndefOp>(loc, structType);
SpecifierStructBuilder md(metaData);
auto memSizeArrayType = structType.cast<LLVM::LLVMStructType>()
.getBody()[kMemSizePosInSpecifier]
.cast<LLVM::LLVMArrayType>();
Value zero = constantZero(builder, loc, memSizeArrayType.getElementType());
// Fill memSizes array with zero.
for (int i = 0, e = memSizeArrayType.getNumElements(); i < e; i++)
md.setMemSize(builder, loc, i, zero);
return md;
}
/// Builds IR extracting the `lvl`-th level-size from the descriptor.
Value SpecifierStructBuilder::lvlSize(OpBuilder &builder, Location loc,
Level lvl) {
// This static_cast makes the narrowing of `lvl` explicit, as required
// by the braces notation for the ctor.
return extractField(
builder, loc,
ArrayRef<int64_t>{kLvlSizePosInSpecifier, static_cast<int64_t>(lvl)});
}
/// Builds IR inserting the `lvl`-th level-size into the descriptor.
void SpecifierStructBuilder::setLvlSize(OpBuilder &builder, Location loc,
Level lvl, Value size) {
// This static_cast makes the narrowing of `lvl` explicit, as required
// by the braces notation for the ctor.
insertField(
builder, loc,
ArrayRef<int64_t>{kLvlSizePosInSpecifier, static_cast<int64_t>(lvl)},
size);
}
/// Builds IR extracting the `fidx`-th memory-size from the descriptor.
Value SpecifierStructBuilder::memSize(OpBuilder &builder, Location loc,
FieldIndex fidx) {
return extractField(builder, loc,
ArrayRef<int64_t>{kMemSizePosInSpecifier, fidx});
}
/// Builds IR inserting the `fidx`-th memory-size into the descriptor.
void SpecifierStructBuilder::setMemSize(OpBuilder &builder, Location loc,
FieldIndex fidx, Value size) {
insertField(builder, loc, ArrayRef<int64_t>{kMemSizePosInSpecifier, fidx},
size);
}
} // namespace
//===----------------------------------------------------------------------===//
// The sparse storage specifier type converter (defined in Passes.h).
//===----------------------------------------------------------------------===//
StorageSpecifierToLLVMTypeConverter::StorageSpecifierToLLVMTypeConverter() {
addConversion([](Type type) { return type; });
addConversion(convertSpecifier);
}
//===----------------------------------------------------------------------===//
// Storage specifier conversion rules.
//===----------------------------------------------------------------------===//
template <typename Base, typename SourceOp>
class SpecifierGetterSetterOpConverter : public OpConversionPattern<SourceOp> {
public:
using OpAdaptor = typename SourceOp::Adaptor;
using OpConversionPattern<SourceOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(SourceOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
SpecifierStructBuilder spec(adaptor.getSpecifier());
Value v;
if (op.getSpecifierKind() == StorageSpecifierKind::LvlSize) {
assert(op.getLevel().has_value());
v = Base::onLvlSize(rewriter, op, spec, op.getLevel().value());
} else {
auto enc = op.getSpecifier().getType().getEncoding();
StorageLayout layout(enc);
FieldIndex fidx =
layout.getMemRefFieldIndex(op.getSpecifierKind(), op.getLevel());
v = Base::onMemSize(rewriter, op, spec, fidx);
}
rewriter.replaceOp(op, v);
return success();
}
};
struct StorageSpecifierSetOpConverter
: public SpecifierGetterSetterOpConverter<StorageSpecifierSetOpConverter,
SetStorageSpecifierOp> {
using SpecifierGetterSetterOpConverter::SpecifierGetterSetterOpConverter;
static Value onLvlSize(OpBuilder &builder, SetStorageSpecifierOp op,
SpecifierStructBuilder &spec, Level lvl) {
spec.setLvlSize(builder, op.getLoc(), lvl, op.getValue());
return spec;
}
static Value onMemSize(OpBuilder &builder, SetStorageSpecifierOp op,
SpecifierStructBuilder &spec, FieldIndex fidx) {
spec.setMemSize(builder, op.getLoc(), fidx, op.getValue());
return spec;
}
};
struct StorageSpecifierGetOpConverter
: public SpecifierGetterSetterOpConverter<StorageSpecifierGetOpConverter,
GetStorageSpecifierOp> {
using SpecifierGetterSetterOpConverter::SpecifierGetterSetterOpConverter;
static Value onLvlSize(OpBuilder &builder, GetStorageSpecifierOp op,
SpecifierStructBuilder &spec, Level lvl) {
return spec.lvlSize(builder, op.getLoc(), lvl);
}
static Value onMemSize(OpBuilder &builder, GetStorageSpecifierOp op,
SpecifierStructBuilder &spec, FieldIndex fidx) {
return spec.memSize(builder, op.getLoc(), fidx);
}
};
struct StorageSpecifierInitOpConverter
: public OpConversionPattern<StorageSpecifierInitOp> {
public:
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(StorageSpecifierInitOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Type llvmType = getTypeConverter()->convertType(op.getResult().getType());
rewriter.replaceOp(op, SpecifierStructBuilder::getInitValue(
rewriter, op.getLoc(), llvmType));
return success();
}
};
//===----------------------------------------------------------------------===//
// Public method for populating conversion rules.
//===----------------------------------------------------------------------===//
void mlir::populateStorageSpecifierToLLVMPatterns(TypeConverter &converter,
RewritePatternSet &patterns) {
patterns.add<StorageSpecifierGetOpConverter, StorageSpecifierSetOpConverter,
StorageSpecifierInitOpConverter>(converter,
patterns.getContext());
}