2020-10-02 09:59:58 +08:00
|
|
|
//===- func_builder.cpp ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is licensed under a pytorch-style license
|
|
|
|
// See frontends/pytorch/LICENSE for license information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "func_builder.h"
|
|
|
|
|
2021-02-02 09:59:42 +08:00
|
|
|
#include "op_builder.h"
|
|
|
|
|
2020-12-12 06:43:38 +08:00
|
|
|
#include "mlir-c/BuiltinAttributes.h"
|
|
|
|
#include "mlir-c/BuiltinTypes.h"
|
2020-11-21 09:03:23 +08:00
|
|
|
#include "mlir-c/Diagnostics.h"
|
2021-06-15 05:13:59 +08:00
|
|
|
#include "npcomp-c/TorchTypes.h"
|
2020-10-02 09:59:58 +08:00
|
|
|
|
|
|
|
using namespace torch_mlir;
|
|
|
|
|
|
|
|
std::unique_ptr<FuncBuilder>
|
2020-11-21 09:03:23 +08:00
|
|
|
FuncBuilder::createFunction(FuncBuilder::Inserter &inserter,
|
2020-12-15 00:42:42 +08:00
|
|
|
MlirLocation location, const std::string &name,
|
|
|
|
std::vector<MlirType> &inputTypes) {
|
2020-11-21 09:03:23 +08:00
|
|
|
auto context = mlirLocationGetContext(location);
|
2020-10-02 09:59:58 +08:00
|
|
|
// TODO: Create a dedicated API upstream for creating/manipulating func ops.
|
|
|
|
// (this is fragile and reveals details that are not guaranteed).
|
2020-12-15 00:42:42 +08:00
|
|
|
std::vector<MlirNamedAttribute> funcAttrs;
|
2020-12-15 06:30:51 +08:00
|
|
|
funcAttrs.push_back(toMlirNamedAttribute(
|
|
|
|
"type", mlirTypeAttrGet(mlirFunctionTypeGet(
|
|
|
|
context, inputTypes.size(), inputTypes.data(),
|
|
|
|
/*numResults=*/0, /*results=*/nullptr))));
|
|
|
|
funcAttrs.push_back(toMlirNamedAttribute(
|
|
|
|
"sym_name", mlirStringAttrGet(
|
|
|
|
context, mlirStringRefCreate(name.data(), name.size()))));
|
2020-10-02 09:59:58 +08:00
|
|
|
|
2020-11-24 11:20:26 +08:00
|
|
|
MlirOperationState state =
|
|
|
|
mlirOperationStateGet(toMlirStringRef("func"), location);
|
2020-10-02 09:59:58 +08:00
|
|
|
mlirOperationStateAddAttributes(&state, funcAttrs.size(), funcAttrs.data());
|
|
|
|
{
|
|
|
|
// Don't access these once ownership transferred.
|
|
|
|
MlirRegion newBodyRegion = mlirRegionCreate();
|
|
|
|
MlirBlock newEntryBlock =
|
|
|
|
mlirBlockCreate(inputTypes.size(), inputTypes.data());
|
|
|
|
mlirRegionInsertOwnedBlockAfter(newBodyRegion, {nullptr}, newEntryBlock);
|
|
|
|
mlirOperationStateAddOwnedRegions(&state, 1, &newBodyRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to re-lookup the region/block because we relinquished ownership above.
|
|
|
|
MlirOperation funcOp = mlirOperationCreate(&state);
|
|
|
|
MlirRegion bodyRegion = mlirOperationGetRegion(funcOp, 0);
|
|
|
|
MlirBlock entryBlock = mlirRegionGetFirstBlock(bodyRegion);
|
|
|
|
|
2020-11-21 09:03:23 +08:00
|
|
|
inserter(funcOp);
|
2020-10-02 09:59:58 +08:00
|
|
|
return std::unique_ptr<FuncBuilder>(new FuncBuilder(
|
2020-10-06 14:21:21 +08:00
|
|
|
context, funcOp, BlockBuilder(entryBlock, /*returnOp=*/{nullptr}, true)));
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:42:42 +08:00
|
|
|
void FuncBuilder::rewriteFuncReturnTypes(std::vector<MlirType> &resultTypes) {
|
2020-10-06 14:21:21 +08:00
|
|
|
// Get inputs from current function type.
|
2020-11-24 11:20:26 +08:00
|
|
|
MlirAttribute funcTypeAttr =
|
|
|
|
mlirOperationGetAttributeByName(funcOp, toMlirStringRef("type"));
|
2020-10-06 14:21:21 +08:00
|
|
|
assert(!mlirAttributeIsNull(funcTypeAttr) &&
|
|
|
|
"function missing 'type' attribute");
|
|
|
|
assert(mlirAttributeIsAType(funcTypeAttr) &&
|
|
|
|
"function type is not a TypeAttr");
|
|
|
|
MlirType funcType = mlirTypeAttrGetValue(funcTypeAttr);
|
2020-12-15 00:42:42 +08:00
|
|
|
std::vector<MlirType> inputTypes;
|
2020-10-06 14:21:21 +08:00
|
|
|
for (intptr_t i = 0, e = mlirFunctionTypeGetNumInputs(funcType); i < e; ++i) {
|
|
|
|
inputTypes.push_back(mlirFunctionTypeGetInput(funcType, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make new function type.
|
|
|
|
MlirType newFuncType =
|
|
|
|
mlirFunctionTypeGet(context, inputTypes.size(), inputTypes.data(),
|
|
|
|
resultTypes.size(), resultTypes.data());
|
|
|
|
MlirAttribute newFuncTypeAttr = mlirTypeAttrGet(newFuncType);
|
2020-11-24 11:20:26 +08:00
|
|
|
mlirOperationSetAttributeByName(funcOp, toMlirStringRef("type"),
|
|
|
|
newFuncTypeAttr);
|
2020-10-06 14:21:21 +08:00
|
|
|
(void)newFuncTypeAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirValue FuncBuilder::insertConstantOp(MlirOperation op) {
|
|
|
|
mlirBlockInsertOwnedOperationAfter(entryBlock.getBlock(), prevConstantOp, op);
|
|
|
|
prevConstantOp = op;
|
|
|
|
return mlirOperationGetResult(op, 0);
|
2020-10-02 09:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MlirValue FuncBuilder::lookupTensor(at::Tensor tensor) {
|
|
|
|
for (auto it = tensorValueMap.rbegin(), e = tensorValueMap.rend(); it != e;
|
|
|
|
++it) {
|
|
|
|
if (it->first.is_same(tensor))
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return {nullptr};
|
|
|
|
}
|
2020-10-06 14:21:21 +08:00
|
|
|
|
|
|
|
MlirValue FuncBuilder::getScalarConstant(MlirLocation loc, at::Scalar s) {
|
|
|
|
// Note that interpreter "scalars" match the Python semantics and are
|
|
|
|
// represented as one of double or int64_t, with a special tag for whether
|
|
|
|
// it should be interpreted as a bool.
|
|
|
|
if (s.isIntegral(/*includeBool=*/false)) {
|
2021-06-17 06:53:15 +08:00
|
|
|
MlirType t = npcompTorchIntTypeGet(context);
|
|
|
|
MlirAttribute value =
|
|
|
|
mlirIntegerAttrGet(mlirIntegerTypeGet(context, 64), s.to<int64_t>());
|
2021-06-16 03:42:51 +08:00
|
|
|
MlirOperation op = createMlirOperation(
|
|
|
|
"torch.constant.int", loc, t, toMlirNamedAttribute("value", value));
|
|
|
|
insertConstantOp(op);
|
|
|
|
return mlirOperationGetResult(op, 0);
|
2020-10-06 14:21:21 +08:00
|
|
|
}
|
|
|
|
if (s.isFloatingPoint()) {
|
2021-06-17 23:24:31 +08:00
|
|
|
MlirType t = npcompTorchFloatTypeGet(context);
|
|
|
|
MlirAttribute value = mlirFloatAttrDoubleGet(
|
|
|
|
context, mlirF64TypeGet(context), s.to<double>());
|
2021-06-16 03:42:51 +08:00
|
|
|
MlirOperation op = createMlirOperation(
|
|
|
|
"torch.constant.float", loc, t, toMlirNamedAttribute("value", value));
|
|
|
|
insertConstantOp(op);
|
|
|
|
return mlirOperationGetResult(op, 0);
|
2020-10-16 09:28:30 +08:00
|
|
|
}
|
|
|
|
if (s.isBoolean()) {
|
|
|
|
return getBoolConstant(loc, s.to<bool>());
|
2020-10-06 14:21:21 +08:00
|
|
|
}
|
|
|
|
// TODO: s.isComplex()
|
|
|
|
|
|
|
|
throw std::invalid_argument("TODO: Scalar of unknown kind");
|
|
|
|
}
|
2020-10-16 09:28:30 +08:00
|
|
|
|
|
|
|
MlirValue FuncBuilder::getBoolConstant(MlirLocation loc, bool v) {
|
2021-02-02 09:59:42 +08:00
|
|
|
return insertConstantOp(OpBuilder(context).createBoolConstant(loc, v));
|
2020-10-16 09:28:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-17 08:38:07 +08:00
|
|
|
MlirValue FuncBuilder::getNoneConstant(MlirLocation loc) {
|
2021-02-02 09:59:42 +08:00
|
|
|
return insertConstantOp(OpBuilder(context).createNoneConstant(loc));
|
2020-10-17 08:38:07 +08:00
|
|
|
}
|
|
|
|
|
2021-06-05 06:57:21 +08:00
|
|
|
MlirValue FuncBuilder::buildList(MlirLocation loc, MlirType elementType,
|
2020-12-15 00:42:42 +08:00
|
|
|
std::vector<MlirValue> &elements) {
|
2021-06-15 05:13:59 +08:00
|
|
|
MlirType resultType = npcompTorchListTypeGet(elementType);
|
2021-06-05 06:57:21 +08:00
|
|
|
OperationStateHolder state{"torch.prim.ListConstruct", loc};
|
2020-10-17 08:38:07 +08:00
|
|
|
mlirOperationStateAddResults(state, 1, &resultType);
|
|
|
|
mlirOperationStateAddOperands(state, elements.size(), elements.data());
|
|
|
|
MlirOperation op = state.createOperation();
|
2020-11-03 07:30:21 +08:00
|
|
|
entryBlock.insertBeforeTerminator(op);
|
|
|
|
return mlirOperationGetResult(op, 0);
|
2020-10-17 08:38:07 +08:00
|
|
|
}
|