Skip to content

Commit 22d6b76

Browse files
[mlir][sparse_tensor] Implement bufferization interface for foreach
This commit fixes a memory leak in `sparse_codegen_foreach.mlir`. The bufferization inserted a copy for the operand of `sparse_tensor.foreach` because it conservatively assumed that the op writes to the operand.
1 parent 162180d commit 22d6b76

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

mlir/lib/Dialect/SparseTensor/Transforms/BufferizableOpInterfaceImpl.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,35 @@ struct DisassembleOpInterface
187187
}
188188
};
189189

190+
struct ForeachOpInterface : public SparseBufferizableOpInterfaceExternalModel<
191+
ForeachOpInterface, sparse_tensor::ForeachOp> {
192+
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
193+
const AnalysisState &state) const {
194+
return true;
195+
}
196+
197+
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
198+
const AnalysisState &state) const {
199+
return false;
200+
}
201+
202+
AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
203+
const AnalysisState &state) const {
204+
return {};
205+
}
206+
207+
LogicalResult verifyAnalysis(Operation *op,
208+
const AnalysisState &state) const {
209+
// A more complex analysis (similar to scf.for) is needed if the op returns
210+
// a tensor. That tensor would have to be bufferized (not implemented yet).
211+
for (OpResult result : op->getResults()) {
212+
if (isa<TensorType>(result.getType()))
213+
return op->emitOpError("tensor results are not supported yet");
214+
}
215+
return success();
216+
}
217+
};
218+
190219
struct NumberOfEntriesOpInterface
191220
: public SparseBufferizableOpInterfaceExternalModel<
192221
NumberOfEntriesOpInterface, sparse_tensor::NumberOfEntriesOp> {
@@ -307,6 +336,7 @@ void mlir::sparse_tensor::registerBufferizableOpInterfaceExternalModels(
307336
NumberOfEntriesOpInterface>(*ctx);
308337
sparse_tensor::AssembleOp::attachInterface<AssembleOpInterface>(*ctx);
309338
sparse_tensor::DisassembleOp::attachInterface<DisassembleOpInterface>(*ctx);
339+
sparse_tensor::ForeachOp::attachInterface<ForeachOpInterface>(*ctx);
310340
sparse_tensor::ToCoordinatesBufferOp::attachInterface<
311341
ToCoordinatesBufferOpInterface>(*ctx);
312342
sparse_tensor::ToCoordinatesOp::attachInterface<ToCoordinatesOpInterface>(

0 commit comments

Comments
 (0)