|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +//===---------------- Fork.cpp - ONNX Operations -------------------------===// |
| 6 | +// |
| 7 | +// Copyright 2019-2024 The IBM Research Authors. |
| 8 | +// |
| 9 | +// ============================================================================= |
| 10 | +// |
| 11 | +// This file provides definition of ONNX dialect Fork operation. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "src/Dialect/ONNX/ONNXOps/OpHelper.hpp" |
| 16 | + |
| 17 | +using namespace mlir; |
| 18 | +using namespace onnx_mlir; |
| 19 | + |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +// ShapeHelper |
| 22 | +//===----------------------------------------------------------------------===// |
| 23 | + |
| 24 | +template <> |
| 25 | +LogicalResult ONNXParallelOpShapeHelper::computeShape() { |
| 26 | + ONNXParallelOp parallelOp = llvm::cast<ONNXParallelOp>(op); |
| 27 | + (void)parallelOp.inferShapes([](Region ®ion) {}); |
| 28 | + Operation *yieldOp = parallelOp.getBody().front().getTerminator(); |
| 29 | + for (unsigned i = 0; i < yieldOp->getNumOperands(); ++i) { |
| 30 | + DimsExpr outputDims; |
| 31 | + Value returnVal = yieldOp->getOperands()[i]; |
| 32 | + int64_t outRank = returnVal.getType().cast<ShapedType>().getRank(); |
| 33 | + for (int64_t j = 0; j < outRank; ++j) |
| 34 | + outputDims.emplace_back(createIE->getShapeAsDim(returnVal, j)); |
| 35 | + setOutputDims(outputDims, i); |
| 36 | + } |
| 37 | + return success(); |
| 38 | +} |
| 39 | + |
| 40 | +//===----------------------------------------------------------------------===// |
| 41 | +// Type Inference |
| 42 | +//===----------------------------------------------------------------------===// |
| 43 | + |
| 44 | +std::vector<Type> ONNXParallelOp::resultTypeInference() { |
| 45 | + Operation *terminator = getRegion().back().getTerminator(); |
| 46 | + auto bodyOutputTys = terminator->getOperandTypes(); |
| 47 | + |
| 48 | + std::vector<Type> resultTypes; |
| 49 | + for (auto [i, ty] : llvm::enumerate(bodyOutputTys)) { |
| 50 | + resultTypes.push_back(ty); |
| 51 | + } |
| 52 | + return resultTypes; |
| 53 | +} |
| 54 | + |
| 55 | +//===----------------------------------------------------------------------===// |
| 56 | +// Shape Inference |
| 57 | +//===----------------------------------------------------------------------===// |
| 58 | + |
| 59 | +LogicalResult ONNXParallelOp::inferShapes( |
| 60 | + std::function<void(Region &)> doShapeInference) { |
| 61 | + doShapeInference(getRegion()); |
| 62 | + for (auto [i, ty] : llvm::enumerate(resultTypeInference())) |
| 63 | + getResult(i).setType(ty); |
| 64 | + return success(); |
| 65 | +} |
| 66 | + |
| 67 | +//===----------------------------------------------------------------------===// |
| 68 | +// Builder: Refer to Async ExecuteOp |
| 69 | +//===----------------------------------------------------------------------===// |
| 70 | +void ONNXParallelOp::build(OpBuilder &builder, OperationState &result, |
| 71 | + TypeRange resultTypes, ValueRange operands, BodyBuilderFn bodyBuilder) { |
| 72 | + |
| 73 | + result.addOperands(operands); |
| 74 | + result.addTypes(resultTypes); |
| 75 | + |
| 76 | + // Add a body region with block arguments |
| 77 | + Region *bodyRegion = result.addRegion(); |
| 78 | + bodyRegion->push_back(new Block); |
| 79 | + Block &bodyBlock = bodyRegion->front(); |
| 80 | + for (Value operand : operands) { |
| 81 | + bodyBlock.addArgument(operand.getType(), operand.getLoc()); |
| 82 | + } |
| 83 | + |
| 84 | + // Create the default terminator if the builder is not provided and if the |
| 85 | + // expected result is empty. Otherwise, leave this to the caller |
| 86 | + // because we don't know which values to return from the execute op. |
| 87 | + if (resultTypes.empty() && !bodyBuilder) { |
| 88 | + OpBuilder::InsertionGuard guard(builder); |
| 89 | + builder.setInsertionPointToStart(&bodyBlock); |
| 90 | + builder.create<ONNXYieldOp>(result.location, ValueRange()); |
| 91 | + } else if (bodyBuilder) { |
| 92 | + OpBuilder::InsertionGuard guard(builder); |
| 93 | + builder.setInsertionPointToStart(&bodyBlock); |
| 94 | + bodyBuilder(builder, result.location, bodyBlock.getArguments()); |
| 95 | + } |
| 96 | +} |
0 commit comments