16#include "llvm/Support/FormatVariadic.h"
29 for (
auto type : types) {
45 if (kind == SparseTensorFieldKind::PosMemRef ||
46 kind == SparseTensorFieldKind::CrdMemRef ||
47 kind == SparseTensorFieldKind::ValMemRef) {
48 auto rtp = cast<ShapedType>(t);
50 rtp = RankedTensorType::get(rtp.getShape(), rtp.getElementType());
52 extraTypes->push_back(rtp);
67 for (
auto type : types) {
72 toVals.push_back(fromVals[idx++]);
76 auto rtp = cast<RankedTensorType>(type);
82 inputs.push_back(fromVals[idx++]);
88 if (kind == SparseTensorFieldKind::PosMemRef ||
89 kind == SparseTensorFieldKind::CrdMemRef ||
90 kind == SparseTensorFieldKind::ValMemRef) {
92 inputs.push_back(fromVals[idx++]);
93 }
else if (directOut) {
95 if (kind == SparseTensorFieldKind::PosMemRef)
96 mem = sparse_tensor::ToPositionsOp::create(builder, loc, inputs[0],
98 else if (kind == SparseTensorFieldKind::CrdMemRef)
99 mem = sparse_tensor::ToCoordinatesOp::create(builder, loc,
102 mem = sparse_tensor::ToValuesOp::create(builder, loc, inputs[0]);
103 toVals.push_back(mem);
105 ShapedType rtp = cast<ShapedType>(t);
106 rtp = RankedTensorType::get(rtp.getShape(), rtp.getElementType());
107 inputs.push_back(extraVals[extra++]);
108 retTypes.push_back(rtp);
117 auto a = sparse_tensor::AssembleOp::create(builder, loc, rtp, inputs);
118 toVals.push_back(a.getResult());
119 }
else if (!directOut) {
122 unsigned len = retTypes.size();
123 retTypes.append(cntTypes);
125 sparse_tensor::DisassembleOp::create(builder, loc, retTypes, inputs);
126 for (
unsigned i = 0; i < len; i++)
127 toVals.push_back(d.getResult(i));
171 SparseFuncAssembler(MLIRContext *context,
bool dO)
172 : OpRewritePattern(context), directOut(dO) {}
174 LogicalResult matchAndRewrite(func::FuncOp funcOp,
175 PatternRewriter &rewriter)
const override {
177 if (funcOp.isPrivate())
181 SmallVector<Type> inputTypes;
182 SmallVector<Type> outputTypes;
183 SmallVector<Type> extraTypes;
184 bool hasAnnotation =
false;
185 convTypes(hasAnnotation, funcOp.getArgumentTypes(), inputTypes,
nullptr,
187 convTypes(hasAnnotation, funcOp.getResultTypes(), outputTypes, &extraTypes,
195 auto orgName = funcOp.getName();
196 std::string wrapper = llvm::formatv(
"_internal_{0}", orgName).str();
198 funcOp.setName(wrapper);
203 Location loc = funcOp.getLoc();
204 ModuleOp modOp = funcOp->getParentOfType<ModuleOp>();
205 MLIRContext *context = modOp.getContext();
206 OpBuilder moduleBuilder(modOp.getBodyRegion());
207 unsigned extra = inputTypes.size();
208 inputTypes.append(extraTypes);
209 auto func = func::FuncOp::create(
210 moduleBuilder, loc, orgName,
211 FunctionType::get(context, inputTypes, outputTypes));
215 OpBuilder::InsertionGuard insertionGuard(rewriter);
216 Block *body = func.addEntryBlock();
220 SmallVector<Value> inputs;
226 auto org = SymbolRefAttr::get(context, wrapper);
227 auto call = func::CallOp::create(rewriter, loc, funcOp.getResultTypes(),
231 SmallVector<Value> outputs;
232 convVals(rewriter, loc, funcOp.getResultTypes(), call.getResults(),
233 body->
getArguments(), outputs, extra,
false, directOut);
234 func::ReturnOp::create(rewriter, loc, outputs);
237 if (funcOp->getDiscardableAttrOfType<UnitAttr>(
238 LLVM::LLVMDialect::getEmitCWrapperAttrName())) {
239 func->setDiscardableAttr(LLVM::LLVMDialect::getEmitCWrapperAttrName(),
240 UnitAttr::get(context));
242 funcOp->removeDiscardableAttr(
243 LLVM::LLVMDialect::getEmitCWrapperAttrName());
250 const bool directOut;
261 patterns.
add<SparseFuncAssembler>(patterns.
getContext(), directOut);
static void convTypes(bool &hasAnnotation, TypeRange types, SmallVectorImpl< Type > &convTypes, SmallVectorImpl< Type > *extraTypes, bool directOut)
static void convVals(OpBuilder &builder, Location loc, TypeRange types, ValueRange fromVals, ValueRange extraVals, SmallVectorImpl< Value > &toVals, unsigned extra, bool isIn, bool directOut)
BlockArgListType getArguments()
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class helps build Operations.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
A wrapper around RankedTensorType, which has three goals:
void foreachFieldAndTypeInSparseTensor(SparseTensorType, llvm::function_ref< bool(Type, FieldIndex, SparseTensorFieldKind, Level, LevelType)>)
unsigned FieldIndex
The type of field indices.
uint64_t Level
The type of level identifiers and level-ranks.
SparseTensorEncodingAttr getSparseTensorEncoding(Type type)
Convenience method to get a sparse encoding attribute from a type.
SparseTensorFieldKind
===-------------------------------------------------------------------—===// The sparse tensor storag...
Include the generated interface declarations.
void populateSparseAssembler(RewritePatternSet &patterns, bool directOut)
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
This enum defines all the sparse representations supportable by the SparseTensor dialect.