2020-04-27 08:20:58 +08:00
|
|
|
//===- NumpyOps.cpp - Core numpy dialect ops --------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "npcomp/Dialect/Numpy/NumpyOps.h"
|
2020-04-29 11:32:49 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/FunctionImplementation.h"
|
2020-04-27 08:20:58 +08:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2020-05-09 07:04:58 +08:00
|
|
|
#include "npcomp/Dialect/Basicpy/BasicpyDialect.h"
|
2020-04-27 08:20:58 +08:00
|
|
|
#include "npcomp/Dialect/Numpy/NumpyDialect.h"
|
|
|
|
|
|
|
|
namespace mlir {
|
2020-04-30 08:10:10 +08:00
|
|
|
namespace NPCOMP {
|
2020-04-30 09:20:42 +08:00
|
|
|
namespace Numpy {
|
2020-04-29 11:32:49 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BuildinUfuncOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseBuiltinUfuncOp(OpAsmParser &parser,
|
|
|
|
OperationState *result) {
|
|
|
|
// Parse the name as a symbol.
|
|
|
|
StringAttr nameAttr;
|
|
|
|
if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
|
|
|
|
result->attributes)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
if (failed(parser.parseOptionalAttrDict(result->attributes))) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printBuiltinUfuncOp(OpAsmPrinter &p, BuiltinUfuncOp op) {
|
|
|
|
p << op.getOperationName() << " ";
|
|
|
|
p.printSymbolName(op.getName());
|
|
|
|
p.printOptionalAttrDict(op.getAttrs(), {SymbolTable::getSymbolAttrName()});
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GenericUfuncOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseGenericUfuncOp(OpAsmParser &parser,
|
|
|
|
OperationState *result) {
|
|
|
|
Builder b(result->getContext());
|
|
|
|
|
|
|
|
// Parse the name as a symbol.
|
|
|
|
StringAttr nameAttr;
|
|
|
|
if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
|
|
|
|
result->attributes))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Parse the body of overloads.
|
|
|
|
if (parser.parseLParen())
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
SmallVector<Attribute, 4> overloadTypes;
|
|
|
|
for (bool first = true;; first = false) {
|
|
|
|
if (first) {
|
|
|
|
if (parser.parseOptionalKeyword("overload"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!first) {
|
|
|
|
if (parser.parseOptionalComma())
|
|
|
|
break;
|
|
|
|
if (parser.parseKeyword("overload"))
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
SmallVector<OpAsmParser::OperandType, 2> argNames;
|
|
|
|
SmallVector<Type, 2> argTypes;
|
|
|
|
SmallVector<Type, 1> resultTypes;
|
2020-05-09 11:42:40 +08:00
|
|
|
SmallVector<NamedAttrList, 1> unusedAttrs;
|
2020-04-29 11:32:49 +08:00
|
|
|
bool isVariadic = false;
|
|
|
|
if (::mlir::impl::parseFunctionSignature(parser, false, argNames, argTypes,
|
|
|
|
unusedAttrs, isVariadic,
|
|
|
|
resultTypes, unusedAttrs))
|
|
|
|
return failure();
|
|
|
|
overloadTypes.push_back(TypeAttr::get(
|
|
|
|
FunctionType::get(argTypes, resultTypes, result->getContext())));
|
|
|
|
auto *region = result->addRegion();
|
|
|
|
if (parser.parseRegion(*region, argNames, argTypes))
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.parseRParen())
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Parse 'attributes {...}'
|
|
|
|
if (parser.parseOptionalAttrDictWithKeyword(result->attributes))
|
|
|
|
return failure();
|
|
|
|
result->addAttribute(b.getIdentifier("overload_types"),
|
|
|
|
b.getArrayAttr(overloadTypes));
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printGenericUfuncOp(OpAsmPrinter &p, GenericUfuncOp op) {
|
|
|
|
p << op.getOperationName() << " @" << op.getName() << "(";
|
|
|
|
bool first = true;
|
|
|
|
for (auto it : llvm::enumerate(op.getRegions())) {
|
|
|
|
auto *region = it.value();
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
p << ", ";
|
|
|
|
if (region->empty()) {
|
|
|
|
p << "<<OVERLOAD_ERROR>>";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block &entryBlock = region->front();
|
|
|
|
p << "overload(";
|
|
|
|
if (it.index() >= op.overload_types().size()) {
|
|
|
|
p << "<<MISSING OVERLOAD TYPE>>";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TypeAttr tattr = op.overload_types()[it.index()].cast<TypeAttr>();
|
|
|
|
FunctionType overloadType = tattr.getValue().dyn_cast<FunctionType>();
|
|
|
|
if (!overloadType) {
|
|
|
|
p << "<<ILLEGAL OVERLOAD TYPE>>";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (overloadType.getNumInputs() != entryBlock.getNumArguments()) {
|
|
|
|
p << "<<OVERLOAD ARG MISMATCH>>";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto argTypes = entryBlock.getArgumentTypes();
|
|
|
|
for (unsigned i = 0, e = entryBlock.getNumArguments(); i < e; ++i) {
|
|
|
|
auto arg = entryBlock.getArgument(i);
|
|
|
|
if (i > 0)
|
|
|
|
p << ", ";
|
|
|
|
p.printOperand(arg);
|
|
|
|
p << ": ";
|
|
|
|
p.printType(overloadType.getInputs()[i]);
|
|
|
|
}
|
|
|
|
p << ")";
|
|
|
|
p.printArrowTypeList(overloadType.getResults());
|
|
|
|
p.printRegion(*region, false, true);
|
|
|
|
}
|
|
|
|
p << ")";
|
|
|
|
}
|
|
|
|
|
2020-04-27 08:55:15 +08:00
|
|
|
#define GET_OP_CLASSES
|
2020-04-27 08:20:58 +08:00
|
|
|
#include "npcomp/Dialect/Numpy/NumpyOps.cpp.inc"
|
2020-04-30 09:20:42 +08:00
|
|
|
} // namespace Numpy
|
2020-04-30 08:10:10 +08:00
|
|
|
} // namespace NPCOMP
|
2020-04-27 08:20:58 +08:00
|
|
|
} // namespace mlir
|