|
| 1 | +//===- SparseStorageSpecifierToLLVM.cpp - convert specifier to llvm -------===// |
| 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 "CodegenUtils.h" |
| 10 | +#include "SparseTensorStorageLayout.h" |
| 11 | + |
| 12 | +#include "mlir/Dialect/SparseTensor/Transforms/Passes.h" |
| 13 | + |
| 14 | +using namespace mlir; |
| 15 | +using namespace sparse_tensor; |
| 16 | + |
| 17 | +static SmallVector<Type, 2> getSpecifierFields(StorageSpecifierType tp) { |
| 18 | + MLIRContext *ctx = tp.getContext(); |
| 19 | + auto enc = tp.getEncoding(); |
| 20 | + unsigned rank = enc.getDimLevelType().size(); |
| 21 | + |
| 22 | + SmallVector<Type, 2> result; |
| 23 | + auto indexType = tp.getSizesType(); |
| 24 | + auto dimSizes = LLVM::LLVMArrayType::get(ctx, indexType, rank); |
| 25 | + auto memSizes = LLVM::LLVMArrayType::get(ctx, indexType, |
| 26 | + getNumDataFieldsFromEncoding(enc)); |
| 27 | + result.push_back(dimSizes); |
| 28 | + result.push_back(memSizes); |
| 29 | + return result; |
| 30 | +} |
| 31 | + |
| 32 | +static Type convertSpecifier(StorageSpecifierType tp) { |
| 33 | + return LLVM::LLVMStructType::getLiteral(tp.getContext(), |
| 34 | + getSpecifierFields(tp)); |
| 35 | +} |
| 36 | + |
| 37 | +StorageSpecifierToLLVMTypeConverter::StorageSpecifierToLLVMTypeConverter() { |
| 38 | + addConversion([](Type type) { return type; }); |
| 39 | + addConversion([](StorageSpecifierType tp) { return convertSpecifier(tp); }); |
| 40 | +} |
| 41 | + |
| 42 | +constexpr uint64_t kDimSizePosInSpecifier = 0; |
| 43 | +constexpr uint64_t kMemSizePosInSpecifier = 1; |
| 44 | + |
| 45 | +class SpecifierStructBuilder : public StructBuilder { |
| 46 | +public: |
| 47 | + explicit SpecifierStructBuilder(Value specifier) : StructBuilder(specifier) { |
| 48 | + assert(value); |
| 49 | + } |
| 50 | + |
| 51 | + // Undef value for dimension sizes, all zero value for memory sizes. |
| 52 | + static Value getInitValue(OpBuilder &builder, Location loc, Type structType); |
| 53 | + |
| 54 | + Value dimSize(OpBuilder &builder, Location loc, unsigned dim); |
| 55 | + void setDimSize(OpBuilder &builder, Location loc, unsigned dim, Value size); |
| 56 | + |
| 57 | + Value memSize(OpBuilder &builder, Location loc, unsigned pos); |
| 58 | + void setMemSize(OpBuilder &builder, Location loc, unsigned pos, Value size); |
| 59 | +}; |
| 60 | + |
| 61 | +Value SpecifierStructBuilder::getInitValue(OpBuilder &builder, Location loc, |
| 62 | + Type structType) { |
| 63 | + Value metaData = builder.create<LLVM::UndefOp>(loc, structType); |
| 64 | + SpecifierStructBuilder md(metaData); |
| 65 | + auto memSizeArrayType = structType.cast<LLVM::LLVMStructType>() |
| 66 | + .getBody()[kMemSizePosInSpecifier] |
| 67 | + .cast<LLVM::LLVMArrayType>(); |
| 68 | + |
| 69 | + Value zero = constantZero(builder, loc, memSizeArrayType.getElementType()); |
| 70 | + // Fill memSizes array with zero. |
| 71 | + for (int i = 0, e = memSizeArrayType.getNumElements(); i < e; i++) |
| 72 | + md.setMemSize(builder, loc, i, zero); |
| 73 | + |
| 74 | + return md; |
| 75 | +} |
| 76 | + |
| 77 | +/// Builds IR inserting the pos-th size into the descriptor. |
| 78 | +Value SpecifierStructBuilder::dimSize(OpBuilder &builder, Location loc, |
| 79 | + unsigned dim) { |
| 80 | + return builder.create<LLVM::ExtractValueOp>( |
| 81 | + loc, value, ArrayRef<int64_t>({kDimSizePosInSpecifier, dim})); |
| 82 | +} |
| 83 | + |
| 84 | +/// Builds IR inserting the pos-th size into the descriptor. |
| 85 | +void SpecifierStructBuilder::setDimSize(OpBuilder &builder, Location loc, |
| 86 | + unsigned dim, Value size) { |
| 87 | + value = builder.create<LLVM::InsertValueOp>( |
| 88 | + loc, value, size, ArrayRef<int64_t>({kDimSizePosInSpecifier, dim})); |
| 89 | +} |
| 90 | + |
| 91 | +/// Builds IR extracting the pos-th memory size into the descriptor. |
| 92 | +Value SpecifierStructBuilder::memSize(OpBuilder &builder, Location loc, |
| 93 | + unsigned pos) { |
| 94 | + return builder.create<LLVM::ExtractValueOp>( |
| 95 | + loc, value, ArrayRef<int64_t>({kMemSizePosInSpecifier, pos})); |
| 96 | +} |
| 97 | + |
| 98 | +/// Builds IR inserting the pos-th memory size into the descriptor. |
| 99 | +void SpecifierStructBuilder::setMemSize(OpBuilder &builder, Location loc, |
| 100 | + unsigned pos, Value size) { |
| 101 | + value = builder.create<LLVM::InsertValueOp>( |
| 102 | + loc, value, size, ArrayRef<int64_t>({kMemSizePosInSpecifier, pos})); |
| 103 | +} |
| 104 | + |
| 105 | +template <typename Base, typename SourceOp> |
| 106 | +class SpecifierGetterSetterOpConverter : public OpConversionPattern<SourceOp> { |
| 107 | +public: |
| 108 | + using OpAdaptor = typename SourceOp::Adaptor; |
| 109 | + using OpConversionPattern<SourceOp>::OpConversionPattern; |
| 110 | + |
| 111 | + LogicalResult |
| 112 | + matchAndRewrite(SourceOp op, OpAdaptor adaptor, |
| 113 | + ConversionPatternRewriter &rewriter) const override { |
| 114 | + SpecifierStructBuilder spec(adaptor.getSpecifier()); |
| 115 | + Value v; |
| 116 | + if (op.getSpecifierKind() == StorageSpecifierKind::DimSize) { |
| 117 | + v = Base::onDimSize(rewriter, op, spec, |
| 118 | + op.getDim().value().getZExtValue()); |
| 119 | + } else { |
| 120 | + auto enc = op.getSpecifier().getType().getEncoding(); |
| 121 | + builder::StorageLayout layout(enc); |
| 122 | + Optional<unsigned> dim = std::nullopt; |
| 123 | + if (op.getDim()) |
| 124 | + dim = op.getDim().value().getZExtValue(); |
| 125 | + unsigned idx = layout.getMemRefFieldIndex(op.getSpecifierKind(), dim); |
| 126 | + v = Base::onMemSize(rewriter, op, spec, idx); |
| 127 | + } |
| 128 | + |
| 129 | + rewriter.replaceOp(op, v); |
| 130 | + return success(); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +struct StorageSpecifierSetOpConverter |
| 135 | + : public SpecifierGetterSetterOpConverter<StorageSpecifierSetOpConverter, |
| 136 | + SetStorageSpecifierOp> { |
| 137 | + using SpecifierGetterSetterOpConverter::SpecifierGetterSetterOpConverter; |
| 138 | + static Value onDimSize(OpBuilder &builder, SetStorageSpecifierOp op, |
| 139 | + SpecifierStructBuilder &spec, unsigned d) { |
| 140 | + spec.setDimSize(builder, op.getLoc(), d, op.getValue()); |
| 141 | + return spec; |
| 142 | + } |
| 143 | + |
| 144 | + static Value onMemSize(OpBuilder &builder, SetStorageSpecifierOp op, |
| 145 | + SpecifierStructBuilder &spec, unsigned i) { |
| 146 | + spec.setMemSize(builder, op.getLoc(), i, op.getValue()); |
| 147 | + return spec; |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +struct StorageSpecifierGetOpConverter |
| 152 | + : public SpecifierGetterSetterOpConverter<StorageSpecifierGetOpConverter, |
| 153 | + GetStorageSpecifierOp> { |
| 154 | + using SpecifierGetterSetterOpConverter::SpecifierGetterSetterOpConverter; |
| 155 | + static Value onDimSize(OpBuilder &builder, GetStorageSpecifierOp op, |
| 156 | + SpecifierStructBuilder &spec, unsigned d) { |
| 157 | + return spec.dimSize(builder, op.getLoc(), d); |
| 158 | + } |
| 159 | + static Value onMemSize(OpBuilder &builder, GetStorageSpecifierOp op, |
| 160 | + SpecifierStructBuilder &spec, unsigned i) { |
| 161 | + return spec.memSize(builder, op.getLoc(), i); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +struct StorageSpecifierInitOpConverter |
| 166 | + : public OpConversionPattern<StorageSpecifierInitOp> { |
| 167 | +public: |
| 168 | + using OpConversionPattern::OpConversionPattern; |
| 169 | + LogicalResult |
| 170 | + matchAndRewrite(StorageSpecifierInitOp op, OpAdaptor adaptor, |
| 171 | + ConversionPatternRewriter &rewriter) const override { |
| 172 | + Type llvmType = getTypeConverter()->convertType(op.getResult().getType()); |
| 173 | + rewriter.replaceOp(op, SpecifierStructBuilder::getInitValue( |
| 174 | + rewriter, op.getLoc(), llvmType)); |
| 175 | + return success(); |
| 176 | + } |
| 177 | +}; |
| 178 | + |
| 179 | +void mlir::populateStorageSpecifierToLLVMPatterns(TypeConverter &converter, |
| 180 | + RewritePatternSet &patterns) { |
| 181 | + patterns.add<StorageSpecifierGetOpConverter, StorageSpecifierSetOpConverter, |
| 182 | + StorageSpecifierInitOpConverter>(converter, |
| 183 | + patterns.getContext()); |
| 184 | +} |
0 commit comments