1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
//===- 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 "mlir/Conversion/LLVMCommon/StructBuilder.h"
#include "mlir/Dialect/SparseTensor/IR/SparseTensorStorageLayout.h"
#include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
#include <optional>
using namespace mlir;
using namespace sparse_tensor;
namespace {
//===----------------------------------------------------------------------===//
// Helper methods.
//===----------------------------------------------------------------------===//
static SmallVector<Type, 4> getSpecifierFields(StorageSpecifierType tp) {
MLIRContext *ctx = tp.getContext();
auto enc = tp.getEncoding();
const Level lvlRank = enc.getLvlRank();
SmallVector<Type, 4> 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);
if (enc.isSlice()) {
// Extra fields are required for the slice information.
auto dimOffset = LLVM::LLVMArrayType::get(ctx, sizeType, lvlRank);
auto dimStride = LLVM::LLVMArrayType::get(ctx, sizeType, lvlRank);
result.push_back(dimOffset);
result.push_back(dimStride);
}
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;
constexpr uint64_t kDimOffsetPosInSpecifier = 2;
constexpr uint64_t kDimStridePosInSpecifier = 3;
class SpecifierStructBuilder : public StructBuilder {
private:
Value extractField(OpBuilder &builder, Location loc,
ArrayRef<int64_t> indices) const {
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 dimension sizes, all zero value for memory sizes.
static Value getInitValue(OpBuilder &builder, Location loc, Type structType,
Value source);
Value lvlSize(OpBuilder &builder, Location loc, Level lvl) const;
void setLvlSize(OpBuilder &builder, Location loc, Level lvl, Value size);
Value dimOffset(OpBuilder &builder, Location loc, Dimension dim) const;
void setDimOffset(OpBuilder &builder, Location loc, Dimension dim,
Value size);
Value dimStride(OpBuilder &builder, Location loc, Dimension dim) const;
void setDimStride(OpBuilder &builder, Location loc, Dimension dim,
Value size);
Value memSize(OpBuilder &builder, Location loc, FieldIndex fidx) const;
void setMemSize(OpBuilder &builder, Location loc, FieldIndex fidx,
Value size);
Value memSizeArray(OpBuilder &builder, Location loc) const;
void setMemSizeArray(OpBuilder &builder, Location loc, Value array);
};
Value SpecifierStructBuilder::getInitValue(OpBuilder &builder, Location loc,
Type structType, Value source) {
Value metaData = builder.create<LLVM::UndefOp>(loc, structType);
SpecifierStructBuilder md(metaData);
if (!source) {
auto memSizeArrayType =
cast<LLVM::LLVMArrayType>(cast<LLVM::LLVMStructType>(structType)
.getBody()[kMemSizePosInSpecifier]);
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);
} else {
// We copy non-slice information (memory sizes array) from source
SpecifierStructBuilder sourceMd(source);
md.setMemSizeArray(builder, loc, sourceMd.memSizeArray(builder, loc));
}
return md;
}
/// Builds IR extracting the pos-th offset from the descriptor.
Value SpecifierStructBuilder::dimOffset(OpBuilder &builder, Location loc,
Dimension dim) const {
return extractField(
builder, loc,
ArrayRef<int64_t>{kDimOffsetPosInSpecifier, static_cast<int64_t>(dim)});
}
/// Builds IR inserting the pos-th offset into the descriptor.
void SpecifierStructBuilder::setDimOffset(OpBuilder &builder, Location loc,
Dimension dim, Value size) {
insertField(
builder, loc,
ArrayRef<int64_t>{kDimOffsetPosInSpecifier, static_cast<int64_t>(dim)},
size);
}
/// Builds IR extracting the `lvl`-th level-size from the descriptor.
Value SpecifierStructBuilder::lvlSize(OpBuilder &builder, Location loc,
Level lvl) const {
// 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 pos-th stride from the descriptor.
Value SpecifierStructBuilder::dimStride(OpBuilder &builder, Location loc,
Dimension dim) const {
return extractField(
builder, loc,
ArrayRef<int64_t>{kDimStridePosInSpecifier, static_cast<int64_t>(dim)});
}
/// Builds IR inserting the pos-th stride into the descriptor.
void SpecifierStructBuilder::setDimStride(OpBuilder &builder, Location loc,
Dimension dim, Value size) {
insertField(
builder, loc,
ArrayRef<int64_t>{kDimStridePosInSpecifier, static_cast<int64_t>(dim)},
size);
}
/// Builds IR extracting the pos-th memory size into the descriptor.
Value SpecifierStructBuilder::memSize(OpBuilder &builder, Location loc,
FieldIndex fidx) const {
return extractField(
builder, loc,
ArrayRef<int64_t>{kMemSizePosInSpecifier, static_cast<int64_t>(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, static_cast<int64_t>(fidx)},
size);
}
/// Builds IR extracting the memory size array from the descriptor.
Value SpecifierStructBuilder::memSizeArray(OpBuilder &builder,
Location loc) const {
return builder.create<LLVM::ExtractValueOp>(loc, value,
kMemSizePosInSpecifier);
}
/// Builds IR inserting the memory size array into the descriptor.
void SpecifierStructBuilder::setMemSizeArray(OpBuilder &builder, Location loc,
Value array) {
value = builder.create<LLVM::InsertValueOp>(loc, value, array,
kMemSizePosInSpecifier);
}
} // 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());
switch (op.getSpecifierKind()) {
case StorageSpecifierKind::LvlSize: {
Value v = Base::onLvlSize(rewriter, op, spec, (*op.getLevel()));
rewriter.replaceOp(op, v);
return success();
}
case StorageSpecifierKind::DimOffset: {
Value v = Base::onDimOffset(rewriter, op, spec, (*op.getLevel()));
rewriter.replaceOp(op, v);
return success();
}
case StorageSpecifierKind::DimStride: {
Value v = Base::onDimStride(rewriter, op, spec, (*op.getLevel()));
rewriter.replaceOp(op, v);
return success();
}
case StorageSpecifierKind::CrdMemSize:
case StorageSpecifierKind::PosMemSize:
case StorageSpecifierKind::ValMemSize: {
auto enc = op.getSpecifier().getType().getEncoding();
StorageLayout layout(enc);
std::optional<unsigned> lvl;
if (op.getLevel())
lvl = (*op.getLevel());
unsigned idx =
layout.getMemRefFieldIndex(toFieldKind(op.getSpecifierKind()), lvl);
Value v = Base::onMemSize(rewriter, op, spec, idx);
rewriter.replaceOp(op, v);
return success();
}
}
llvm_unreachable("unrecognized specifer kind");
}
};
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 onDimOffset(OpBuilder &builder, SetStorageSpecifierOp op,
SpecifierStructBuilder &spec, Dimension d) {
spec.setDimOffset(builder, op.getLoc(), d, op.getValue());
return spec;
}
static Value onDimStride(OpBuilder &builder, SetStorageSpecifierOp op,
SpecifierStructBuilder &spec, Dimension d) {
spec.setDimStride(builder, op.getLoc(), d, 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 onDimOffset(OpBuilder &builder, GetStorageSpecifierOp op,
const SpecifierStructBuilder &spec, Dimension d) {
return spec.dimOffset(builder, op.getLoc(), d);
}
static Value onDimStride(OpBuilder &builder, GetStorageSpecifierOp op,
const SpecifierStructBuilder &spec, Dimension d) {
return spec.dimStride(builder, op.getLoc(), d);
}
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, adaptor.getSource()));
return success();
}
};
//===----------------------------------------------------------------------===//
// Public method for populating conversion rules.
//===----------------------------------------------------------------------===//
void mlir::populateStorageSpecifierToLLVMPatterns(TypeConverter &converter,
RewritePatternSet &patterns) {
patterns.add<StorageSpecifierGetOpConverter, StorageSpecifierSetOpConverter,
StorageSpecifierInitOpConverter>(converter,
patterns.getContext());
}
|