/// Creates allocation operation.
static Value createAllocation(OpBuilder &builder, Location loc, Type type,
- Value sz) {
+ Value sz, bool enableInit) {
auto memType = MemRefType::get({ShapedType::kDynamicSize}, type);
- return builder.create<memref::AllocOp>(loc, memType, sz);
+ Value buffer = builder.create<memref::AllocOp>(loc, memType, sz);
+ if (enableInit) {
+ Value fillValue =
+ builder.create<arith::ConstantOp>(loc, type, builder.getZeroAttr(type));
+ builder.create<linalg::FillOp>(loc, fillValue, buffer);
+ }
+ return buffer;
}
/// Creates allocation for each field in sparse tensor type. Note that
/// on the required capacities (see heuristic variable).
///
static void createAllocFields(OpBuilder &builder, Location loc, Type type,
- ValueRange dynSizes,
+ ValueRange dynSizes, bool enableInit,
SmallVectorImpl<Value> &fields) {
auto enc = getSparseTensorEncoding(type);
assert(enc);
// Per-dimension storage.
for (unsigned r = 0; r < rank; r++) {
if (isCompressedDim(rtp, r)) {
- fields.push_back(createAllocation(builder, loc, ptrType, heuristic));
- fields.push_back(createAllocation(builder, loc, idxType, heuristic));
+ fields.push_back(
+ createAllocation(builder, loc, ptrType, heuristic, enableInit));
+ fields.push_back(
+ createAllocation(builder, loc, idxType, heuristic, enableInit));
} else if (isSingletonDim(rtp, r)) {
- fields.push_back(createAllocation(builder, loc, idxType, heuristic));
+ fields.push_back(
+ createAllocation(builder, loc, idxType, heuristic, enableInit));
} else {
assert(isDenseDim(rtp, r)); // no fields
}
}
// The values array.
- fields.push_back(createAllocation(builder, loc, eltType, heuristic));
+ fields.push_back(
+ createAllocation(builder, loc, eltType, heuristic, enableInit));
assert(fields.size() == lastField);
// Initialize the storage scheme to an empty tensor. Initialized memSizes
// to all zeros, sets the dimSizes to known values and gives all pointer
: public OpConversionPattern<bufferization::AllocTensorOp> {
public:
using OpConversionPattern::OpConversionPattern;
+ SparseTensorAllocConverter(TypeConverter &typeConverter, MLIRContext *context,
+ bool enableInit)
+ : OpConversionPattern(typeConverter, context),
+ enableBufferInitialization(enableInit) {}
LogicalResult
matchAndRewrite(bufferization::AllocTensorOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// Construct allocation for each field.
Location loc = op.getLoc();
SmallVector<Value, 8> fields;
- createAllocFields(rewriter, loc, resType, adaptor.getOperands(), fields);
+ createAllocFields(rewriter, loc, resType, adaptor.getOperands(),
+ enableBufferInitialization, fields);
// Replace operation with resulting memrefs.
rewriter.replaceOp(op, genTuple(rewriter, loc, resType, fields));
return success();
}
+
+private:
+ bool enableBufferInitialization;
};
/// Sparse codegen rule for the dealloc operator.
/// Populates the given patterns list with conversion rules required for
/// the sparsification of linear algebra operations.
-void mlir::populateSparseTensorCodegenPatterns(TypeConverter &typeConverter,
- RewritePatternSet &patterns) {
+void mlir::populateSparseTensorCodegenPatterns(
+ TypeConverter &typeConverter, RewritePatternSet &patterns,
+ bool enableBufferInitialization) {
patterns.add<SparseReturnConverter, SparseCallConverter, SparseDimOpConverter,
SparseCastConverter, SparseTensorAllocConverter,
SparseTensorDeallocConverter, SparseTensorLoadConverter,
SparseToIndicesConverter, SparseToValuesConverter,
SparseConvertConverter, SparseNumberOfEntriesConverter>(
typeConverter, patterns.getContext());
+ patterns.add<SparseTensorAllocConverter>(typeConverter, patterns.getContext(),
+ enableBufferInitialization);
}
--- /dev/null
+// RUN: mlir-opt %s --sparse-tensor-codegen=enable-buffer-initialization=true --canonicalize --cse | FileCheck %s
+
+#SV = #sparse_tensor.encoding<{ dimLevelType = [ "compressed" ] }>
+
+// CHECK-LABEL: func @sparse_alloc_sparse_vector(
+// CHECK-SAME: %[[A:.*]]: index) ->
+// CHECK-SAME: memref<1xindex>, memref<3xindex>, memref<?xindex>, memref<?xindex>, memref<?xf64>
+// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
+// CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index
+// CHECK-DAG: %[[F0:.*]] = arith.constant 0.{{0*}}e+00 : f64
+// CHECK: %[[T0:.*]] = memref.alloc() : memref<1xindex>
+// CHECK: %[[T1:.*]] = memref.alloc() : memref<3xindex>
+// CHECK: %[[T2:.*]] = memref.alloc() : memref<16xindex>
+// CHECK: %[[T3:.*]] = memref.cast %[[T2]] : memref<16xindex> to memref<?xindex>
+// CHECK: linalg.fill ins(%[[C0]] : index) outs(%[[T2]] : memref<16xindex>)
+// CHECK: %[[T4:.*]] = memref.alloc() : memref<16xindex>
+// CHECK: %[[T5:.*]] = memref.cast %[[T4]] : memref<16xindex> to memref<?xindex>
+// CHECK: linalg.fill ins(%[[C0]] : index) outs(%[[T4]] : memref<16xindex>)
+// CHECK: %[[T6:.*]] = memref.alloc() : memref<16xf64>
+// CHECK: %[[T7:.*]] = memref.cast %[[T6]] : memref<16xf64> to memref<?xf64>
+// CHECK: linalg.fill ins(%[[F0]] : f64) outs(%[[T6]] : memref<16xf64>)
+// CHECK: linalg.fill ins(%[[C0]] : index) outs(%[[T1]] : memref<3xindex>)
+// CHECK: memref.store %[[A]], %[[T0]][%[[C0]]] : memref<1xindex>
+// CHECK: %[[P0:.*]] = sparse_tensor.push_back %[[T1]], %[[T3]]
+// CHECK: %[[P1:.*]] = sparse_tensor.push_back %[[T1]], %[[P0]]
+// CHECK: return %[[T0]], %[[T1]], %[[P1]], %[[T5]], %[[T7]] :
+func.func @sparse_alloc_sparse_vector(%arg0: index) -> tensor<?xf64, #SV> {
+ %0 = bufferization.alloc_tensor(%arg0) : tensor<?xf64, #SV>
+ %1 = sparse_tensor.load %0 : tensor<?xf64, #SV>
+ return %1 : tensor<?xf64, #SV>
+}