1//===- SparseTensorDescriptor.cpp -----------------------------------------===//
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 "SparseTensorDescriptor.h"
10#include "CodegenUtils.h"
11
12#include "mlir/Dialect/MemRef/IR/MemRef.h"
13#include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
14#include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
15#include "mlir/Transforms/DialectConversion.h"
16
17using namespace mlir;
18using namespace sparse_tensor;
19
20//===----------------------------------------------------------------------===//
21// Private helper methods.
22//===----------------------------------------------------------------------===//
23
24/// Constructs a nullable `LevelAttr` from the `std::optional<Level>`.
25static IntegerAttr optionalLevelAttr(MLIRContext *ctx,
26 std::optional<Level> lvl) {
27 return lvl ? IntegerAttr::get(type: IndexType::get(context: ctx), value: lvl.value())
28 : IntegerAttr();
29}
30
31// This is only ever called from `SparseTensorTypeToBufferConverter`,
32// which is why the first argument is `RankedTensorType` rather than
33// `SparseTensorType`.
34static std::optional<LogicalResult>
35convertSparseTensorType(RankedTensorType rtp, SmallVectorImpl<Type> &fields) {
36 const SparseTensorType stt(rtp);
37 if (!stt.hasEncoding())
38 return std::nullopt;
39
40 unsigned numFields = fields.size();
41 (void)numFields;
42 foreachFieldAndTypeInSparseTensor(
43 stt,
44 [&](Type fieldType, FieldIndex fieldIdx,
45 SparseTensorFieldKind /*fieldKind*/, Level /*lvl*/,
46 LevelType /*lt*/) -> bool {
47 assert(numFields + fieldIdx == fields.size());
48 fields.push_back(Elt: fieldType);
49 return true;
50 });
51 return success();
52}
53
54//===----------------------------------------------------------------------===//
55// The sparse tensor type converter (defined in Passes.h).
56//===----------------------------------------------------------------------===//
57
58static Value materializeTuple(OpBuilder &builder, RankedTensorType tp,
59 ValueRange inputs, Location loc) {
60 if (!getSparseTensorEncoding(type: tp))
61 // Not a sparse tensor.
62 return Value();
63 // Sparsifier knows how to cancel out these casts.
64 return genTuple(builder, loc, tp, values: inputs);
65}
66
67SparseTensorTypeToBufferConverter::SparseTensorTypeToBufferConverter() {
68 addConversion(callback: [](Type type) { return type; });
69 addConversion(callback&: convertSparseTensorType);
70
71 // Required by scf.for 1:N type conversion.
72 addSourceMaterialization(callback&: materializeTuple);
73}
74
75//===----------------------------------------------------------------------===//
76// StorageTensorSpecifier methods.
77//===----------------------------------------------------------------------===//
78
79Value SparseTensorSpecifier::getInitValue(OpBuilder &builder, Location loc,
80 SparseTensorType stt) {
81 return StorageSpecifierInitOp::create(
82 builder, location: loc, result: StorageSpecifierType::get(encoding: stt.getEncoding()));
83}
84
85Value SparseTensorSpecifier::getSpecifierField(OpBuilder &builder, Location loc,
86 StorageSpecifierKind kind,
87 std::optional<Level> lvl) {
88 return GetStorageSpecifierOp::create(
89 builder, location: loc, specifier, specifierKind: kind,
90 level: optionalLevelAttr(ctx: specifier.getContext(), lvl));
91}
92
93void SparseTensorSpecifier::setSpecifierField(OpBuilder &builder, Location loc,
94 Value v,
95 StorageSpecifierKind kind,
96 std::optional<Level> lvl) {
97 // TODO: make `v` have type `TypedValue<IndexType>` instead.
98 assert(v.getType().isIndex());
99 specifier = SetStorageSpecifierOp::create(
100 builder, location: loc, specifier, specifierKind: kind,
101 level: optionalLevelAttr(ctx: specifier.getContext(), lvl), value: v);
102}
103
104//===----------------------------------------------------------------------===//
105// SparseTensorDescriptor methods.
106//===----------------------------------------------------------------------===//
107
108Value sparse_tensor::SparseTensorDescriptor::getCrdMemRefOrView(
109 OpBuilder &builder, Location loc, Level lvl) const {
110 const Level cooStart = rType.getAoSCOOStart();
111 if (lvl < cooStart)
112 return getMemRefField(kind: SparseTensorFieldKind::CrdMemRef, lvl);
113
114 Value stride = constantIndex(builder, loc, i: rType.getLvlRank() - cooStart);
115 Value size = getCrdMemSize(builder, loc, lvl: cooStart);
116 size = arith::DivUIOp::create(builder, location: loc, lhs: size, rhs: stride);
117 return memref::SubViewOp::create(
118 builder, location: loc, source: getMemRefField(kind: SparseTensorFieldKind::CrdMemRef, lvl: cooStart),
119 /*offsets=*/ValueRange{constantIndex(builder, loc, i: lvl - cooStart)},
120 /*sizes=*/ValueRange{size},
121 /*step=*/strides: ValueRange{stride});
122}
123