Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate temporary buffer in binary op conversion #130

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/common/transformations/src/transformations/mlir/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ static void prepareMLIRKernelWithoutWrapper(mlir::OwningOpRef<mlir::ModuleOp>& m

#else // Simplified default lowering to LLVM from LLVM tests

// Remove empty tensors to avoid converting them into temporary buffers.
pm.addPass(bufferization::createEmptyTensorEliminationPass());

pm.addPass(bufferization::createOneShotBufferizePass());
// TODO: Add deallocation pass/pipeline to avoid memory leaks.

// Cleanup after bufferization - possibly remove redundant copies.
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
pm.addNestedPass<func::FuncOp>(createCSEPass());

// Blanket-convert any remaining high-level vector ops to loops if any remain.
pm.addNestedPass<func::FuncOp>(createConvertVectorToSCFPass());
Expand Down Expand Up @@ -553,11 +561,15 @@ template <typename TargetOp>
struct ConvertBinary {
void operator()(ConversionContext& context, std::shared_ptr<ov::Node> node) {
auto loc = createLocation(context.context, node);
auto& builder = context.builder();
// TODO: Support broadcasts
const auto inputs = context.getInputs(node);
auto op = context.builder().create<TargetOp>(loc,
mlir::ValueRange{inputs[0], inputs[1]},
/* FIXME: Use linalg.fill or tensor.empty */ mlir::ValueRange{inputs[0]});
auto outType = cast<mlir::ShapedType>(inputs[0].getType());
// Named binary ops directly overwrite data in `outs` buffer so, there is no need to provide non-empty
// destination at the tensor-level.
// Use `tensor.empty` to avoid temporary buffer allocation and memcpy after bufferization.
auto empty = builder.create<mlir::tensor::EmptyOp>(loc, outType.getShape(), outType.getElementType());
adam-smnk marked this conversation as resolved.
Show resolved Hide resolved
auto op = builder.create<TargetOp>(loc, mlir::ValueRange{inputs[0], inputs[1]}, mlir::ValueRange{empty});
context.addOutputs(node, op);
}
};
Expand Down Expand Up @@ -606,8 +618,13 @@ mlir::OwningOpRef<mlir::ModuleOp> ngraph_to_mlir(MLIRContext* context,
auto tensor = conversion_context.nodeOutputMap.at(outputs[i]);
auto memref = func.getArgument(i + inputs.size());
auto loc = createLocation(context, outputs[i].get_node_shared_ptr());
auto materialize = block_builder.create<bufferization::MaterializeInDestinationOp>(loc, tensor, memref);
materialize.setWritable(true); // TODO: Can I set it as ctor argument above?
// TODO: Is restrict always guaranteed?
adam-smnk marked this conversation as resolved.
Show resolved Hide resolved
block_builder.create<bufferization::MaterializeInDestinationOp>(loc,
TypeRange{},
tensor,
memref,
/*restrict=*/true,
/*writable=*/true);
}

const auto retLoc = createLayerLocation(context, "output", "Output");
Expand Down
Loading