13#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENUTILS_H_
14#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENUTILS_H_
82template <
class SubClass>
86 : retTypes(retTypes), params(params), genCall(genCall) {}
92 return genImplementation(retTypes, params, builder, loc);
95 std::string funcName = getMangledFuncName();
96 ModuleOp module = getParentOpOf<ModuleOp>(builder);
98 auto result = SymbolRefAttr::get(context, funcName);
99 auto func =
module.lookupSymbol<func::FuncOp>(result.getAttr());
105 func = func::FuncOp::create(
106 builder, loc, funcName,
107 FunctionType::get(context, params.getTypes(), retTypes));
115 genImplementation(retTypes, args, builder, loc);
116 func::ReturnOp::create(builder, loc,
result);
119 func::CallOp call = func::CallOp::create(builder, loc,
func, params);
120 return call.getResults();
124 template <
class OpTp>
130 std::string getMangledFuncName() {
131 return static_cast<SubClass *
>(
this)->getMangledFuncName();
136 OpBuilder &builder, Location loc) {
137 return static_cast<SubClass *
>(
this)->genImplementation(retTypes, params,
148Value
genCast(OpBuilder &builder, Location loc, Value value, Type dstTy);
163TypedAttr
getOneAttr(Builder &builder, Type tp);
168Value
genIsNonzero(OpBuilder &builder, Location loc, Value v);
174 SmallVectorImpl<Value> &dstShape,
175 ArrayRef<Value> srcShape, ArrayRef<Size> staticDstShape,
176 ArrayRef<ReassociationIndices> reassociation);
179void reshapeCvs(OpBuilder &builder, Location loc,
180 ArrayRef<ReassociationIndices> reassociation,
182 ValueRange dstSizes, SmallVectorImpl<Value> &dstCvs);
188FlatSymbolRefAttr
getFunc(ModuleOp module, StringRef name,
TypeRange resultType,
193func::CallOp
createFuncCall(OpBuilder &builder, Location loc, StringRef name,
205Value
genAlloca(OpBuilder &builder, Location loc, Value sz, Type tp);
210Value
genAlloca(OpBuilder &builder, Location loc,
unsigned sz, Type tp,
211 bool staticShape =
false);
233void sizesFromSrc(OpBuilder &builder, SmallVectorImpl<Value> &sizes,
234 Location loc, Value src);
237Operation *
getTop(Operation *op);
256 OpBuilder &builder, Location loc, SparseElementsAttr attr, AffineMap order,
263SmallVector<Value>
loadAll(OpBuilder &builder, Location loc,
size_t size,
264 Value mem,
size_t offsetIdx = 0,
265 Value offsetVal = Value());
272 size_t offsetIdx = 0, Value offsetVal = Value());
291 SmallVectorImpl<Value> &dimSizesValues,
292 Value &dimSizesBuffer);
296 ArrayRef<Value> dimSizesValues, Value dimSizesBuffer,
297 SmallVectorImpl<Value> &lvlSizesValues,
298 Value &dim2lvlBuffer,
299 Value &lvl2dimBuffer);
315 if (
auto ctp = dyn_cast<ComplexType>(tp)) {
316 auto zeroe = builder.
getZeroAttr(ctp.getElementType());
318 return complex::ConstantOp::create(builder, loc, tp, zeroa);
320 return arith::ConstantOp::create(builder, loc, tp, builder.
getZeroAttr(tp));
326 if (
auto ctp = dyn_cast<ComplexType>(tp)) {
327 auto zeroe = builder.
getZeroAttr(ctp.getElementType());
328 auto onee =
getOneAttr(builder, ctp.getElementType());
330 return complex::ConstantOp::create(builder, loc, tp, zeroa);
332 return arith::ConstantOp::create(builder, loc, tp,
getOneAttr(builder, tp));
367 return constantI32(builder, loc,
static_cast<uint32_t
>(action));
380 SparseTensorEncodingAttr enc) {
387 SparseTensorEncodingAttr enc) {
401 return constantI64(builder, loc,
static_cast<uint64_t
>(lt));
406 if (
auto complexAttr = dyn_cast<complex::NumberAttr>(attr)) {
407 Type tp = cast<ComplexType>(complexAttr.getType()).getElementType();
408 return complex::ConstantOp::create(
409 builder, loc, complexAttr.getType(),
410 builder.
getArrayAttr({FloatAttr::get(tp, complexAttr.getReal()),
411 FloatAttr::get(tp, complexAttr.getImag())}));
413 return arith::ConstantOp::create(builder, loc, cast<TypedAttr>(attr));
418 auto rtp = dyn_cast<RankedTensorType>(type);
419 return !rtp || rtp.getRank() == 0;
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
BlockArgListType getArguments()
This class is a general helper class for creating context-global objects like types,...
TypedAttr getZeroAttr(Type type)
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
ParentT getParentOfType()
Find the first parent operation of the given type, or nullptr if there is no ancestor 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...
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
static ConstantIntOp create(OpBuilder &builder, Location location, int64_t value, unsigned width)
FuncCallOrInlineGenerator(TypeRange retTypes, ValueRange params, bool genCall)
SmallVector< Value > genCallOrInline(OpBuilder &builder, Location loc)
A wrapper around RankedTensorType, which has three goals:
TypedAttr getOneAttr(Builder &builder, Type tp)
Generates a 1-valued attribute of the given type.
FlatSymbolRefAttr getFunc(ModuleOp module, StringRef name, TypeRange resultType, ValueRange operands, EmitCInterface emitCInterface)
Returns a function reference (first hit also inserts into module).
Value genAllocaScalar(OpBuilder &builder, Location loc, Type tp)
Generates an uninitialized temporary buffer with room for one value of the given type,...
Value constantIndex(OpBuilder &builder, Location loc, int64_t i)
Generates a constant of index type.
void foreachInSparseConstant(OpBuilder &builder, Location loc, SparseElementsAttr attr, AffineMap order, function_ref< void(ArrayRef< Value >, Value)> callback)
Iterate over a sparse constant, generates constantOp for value and coordinates.
Value constantZero(OpBuilder &builder, Location loc, Type tp)
Generates a 0-valued constant of the given type.
uint64_t Dimension
The type of dimension identifiers and dimension-ranks.
Value constantLevelTypeEncoding(OpBuilder &builder, Location loc, LevelType lt)
Generates a constant of the internal dimension level type encoding.
Value allocaBuffer(OpBuilder &builder, Location loc, ValueRange values)
Generates a temporary buffer, initializes it with the given contents, and returns it as type memref<?
Value constantPosTypeEncoding(OpBuilder &builder, Location loc, SparseTensorEncodingAttr enc)
Generates a constant of the internal type-encoding for position overhead storage.
OverheadType posTypeEncoding(SparseTensorEncodingAttr enc)
Returns the OverheadType for position overhead storage.
Value constantOne(OpBuilder &builder, Location loc, Type tp)
Generates a 1-valued constant of the given type.
OverheadType
Encoding of overhead types (both position overhead and coordinate overhead), for "overloading" @newSp...
Value constantI8(OpBuilder &builder, Location loc, int8_t i)
Generates a constant of i8 type.
Action
The actions performed by @newSparseTensor.
OverheadType crdTypeEncoding(SparseTensorEncodingAttr enc)
Returns the OverheadType for coordinate overhead storage.
TypedValue< BaseMemRefType > genToMemref(OpBuilder &builder, Location loc, Value tensor)
OverheadType overheadTypeEncoding(unsigned width)
Converts an overhead storage bitwidth to its internal type-encoding.
Value constantI1(OpBuilder &builder, Location loc, bool b)
Generates a constant of i1 type.
Value genIndexLoad(OpBuilder &builder, Location loc, Value mem, ValueRange s)
Generates a pointer/index load from the sparse storage scheme.
Value constantAction(OpBuilder &builder, Location loc, Action action)
Generates a constant of the given Action.
Value constantCrdTypeEncoding(OpBuilder &builder, Location loc, SparseTensorEncodingAttr enc)
Generates a constant of the internal type-encoding for coordinate overhead storage.
StringRef overheadTypeFunctionSuffix(OverheadType ot)
Convert OverheadType to its function-name suffix.
PrimaryType
Encoding of the elemental type, for "overloading" @newSparseTensor.
Value genValFromAttr(OpBuilder &builder, Location loc, Attribute attr)
bool isValidPrimaryType(Type elemTp)
Returns true if the given type is a valid sparse tensor element type supported by the runtime library...
PrimaryType primaryTypeEncoding(Type elemTp)
Converts a primary storage type to its internal type-encoding.
Operation * getTop(Operation *op)
Scans to top of generated loop.
Value createOrFoldSliceStrideOp(OpBuilder &builder, Location loc, Value tensor, Dimension dim)
Generates code to retrieve the slice slice for the sparse tensor slice, return a constant if the offs...
Type getOpaquePointerType(MLIRContext *ctx)
Returns the equivalent of void* for opaque arguments to the execution engine.
Value genMapBuffers(OpBuilder &builder, Location loc, SparseTensorType stt, ArrayRef< Value > dimSizesValues, Value dimSizesBuffer, SmallVectorImpl< Value > &lvlSizesValues, Value &dim2lvlBuffer, Value &lvl2dimBuffer)
Generates code to set up the buffer parameters for a map.
Value genIsNonzero(OpBuilder &builder, Location loc, Value v)
Generates the comparison v != 0 where v is of numeric type.
Value genReader(OpBuilder &builder, Location loc, SparseTensorType stt, Value tensor, SmallVectorImpl< Value > &dimSizesValues, Value &dimSizesBuffer)
Generates code that opens a reader and sets the dimension sizes.
Value genScalarToTensor(OpBuilder &builder, Location loc, Value elem, Type dstTp)
Add conversion from scalar to given type (possibly a 0-rank tensor).
bool isZeroRankedTensorOrScalar(Type type)
void deallocDenseTensor(OpBuilder &builder, Location loc, Value buffer)
Generates code to deallocate a dense buffer.
Value genAlloca(OpBuilder &builder, Location loc, Value sz, Type tp)
Generates an uninitialized temporary buffer of the given size and type, but returns it as type memref...
Value constantOverheadTypeEncoding(OpBuilder &builder, Location loc, unsigned width)
Generates a constant of the internal type-encoding for overhead storage.
SmallVector< Value > loadAll(OpBuilder &builder, Location loc, size_t size, Value mem, size_t offsetIdx=0, Value offsetVal=Value())
Loads size-many values from the memref, which must have rank-1 and size greater-or-equal to size.
void genReshapeDstShape(OpBuilder &builder, Location loc, SmallVectorImpl< Value > &dstShape, ArrayRef< Value > srcShape, ArrayRef< Size > staticDstShape, ArrayRef< ReassociationIndices > reassociation)
Computes the shape of destination tensor of a reshape operator.
Value constantI64(OpBuilder &builder, Location loc, int64_t i)
Generates a constant of i64 type.
void reshapeCvs(OpBuilder &builder, Location loc, ArrayRef< ReassociationIndices > reassociation, ValueRange srcSizes, ValueRange srcCvs, ValueRange dstSizes, SmallVectorImpl< Value > &dstCvs)
Reshape coordinates during a reshaping operation.
Value constantI16(OpBuilder &builder, Location loc, int16_t i)
Generates a constant of i16 type.
func::CallOp createFuncCall(OpBuilder &builder, Location loc, StringRef name, TypeRange resultType, ValueRange operands, EmitCInterface emitCInterface)
Creates a CallOp to the function reference returned by getFunc() in the builder's module.
Value genCast(OpBuilder &builder, Location loc, Value value, Type dstTy)
Add type casting between arith and index types when needed.
StringRef primaryTypeFunctionSuffix(PrimaryType pt)
Convert PrimaryType to its function-name suffix.
Value createOrFoldSliceOffsetOp(OpBuilder &builder, Location loc, Value tensor, Dimension dim)
Generates code to retrieve the slice offset for the sparse tensor slice, return a constant if the off...
Value constantI32(OpBuilder &builder, Location loc, int32_t i)
Generates a constant of i32 type.
Value constantPrimaryTypeEncoding(OpBuilder &builder, Location loc, Type elemTp)
Generates a constant of the internal type-encoding for primary storage.
void sizesFromSrc(OpBuilder &builder, SmallVectorImpl< Value > &sizes, Location loc, Value src)
Populates given sizes array from dense tensor or sparse tensor constant.
Type getOverheadType(Builder &builder, OverheadType ot)
Converts the internal type-encoding for overhead storage to an mlir::Type.
EmitCInterface
Shorthand aliases for the emitCInterface argument to getFunc(), createFuncCall(), and replaceOpWithFu...
Value allocDenseTensor(OpBuilder &builder, Location loc, RankedTensorType tensorTp, ValueRange sizes)
Generates code to allocate a buffer of the given type, and zero initialize it.
void storeAll(OpBuilder &builder, Location loc, Value mem, ValueRange vs, size_t offsetIdx=0, Value offsetVal=Value())
Stores all the values of vs into the memref mem, which must have rank-1 and size greater-or-equal to ...
Include the generated interface declarations.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
llvm::function_ref< Fn > function_ref
This enum defines all the sparse representations supportable by the SparseTensor dialect.