2020-04-27 08:20:58 +08:00
|
|
|
//===- NumpyDialect.cpp - Core numpy dialect --------------------*- 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-06-10 10:22:24 +08:00
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyDialect.h"
|
2020-07-05 11:33:13 +08:00
|
|
|
|
2020-04-30 09:20:42 +08:00
|
|
|
#include "mlir/IR/DialectImplementation.h"
|
2020-06-30 06:27:39 +08:00
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyDialect.h"
|
2020-06-10 10:22:24 +08:00
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyOps.h"
|
2020-07-05 11:33:13 +08:00
|
|
|
#include "npcomp/Typing/Support/CPAIrHelpers.h"
|
2020-08-28 06:09:10 +08:00
|
|
|
#include "llvm/ADT/TypeSwitch.h"
|
2020-04-27 08:20:58 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
2020-07-04 07:38:10 +08:00
|
|
|
using namespace mlir::NPCOMP;
|
2020-04-30 09:20:42 +08:00
|
|
|
using namespace mlir::NPCOMP::Numpy;
|
2020-04-27 08:20:58 +08:00
|
|
|
|
2020-08-28 06:09:10 +08:00
|
|
|
void NumpyDialect::initialize() {
|
2020-04-27 08:20:58 +08:00
|
|
|
addOperations<
|
|
|
|
#define GET_OP_LIST
|
2020-06-10 10:22:24 +08:00
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyOps.cpp.inc"
|
2020-04-27 08:20:58 +08:00
|
|
|
>();
|
2020-06-29 08:37:20 +08:00
|
|
|
addTypes<AnyDtypeType, NdArrayType>();
|
2021-04-23 09:11:40 +08:00
|
|
|
getContext()->loadDialect<Basicpy::BasicpyDialect>();
|
2020-04-30 09:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Type NumpyDialect::parseType(DialectAsmParser &parser) const {
|
|
|
|
StringRef keyword;
|
|
|
|
if (parser.parseKeyword(&keyword))
|
|
|
|
return Type();
|
|
|
|
|
|
|
|
if (keyword == "any_dtype")
|
|
|
|
return AnyDtypeType::get(getContext());
|
2020-06-29 08:37:20 +08:00
|
|
|
if (keyword == "ndarray") {
|
|
|
|
// Parse:
|
2020-07-06 08:34:03 +08:00
|
|
|
// ndarray<*:?>
|
|
|
|
// ndarray<*:i32>
|
|
|
|
// ndarary<[1,2,3]:i32>
|
|
|
|
// Note that this is a different syntax than the built-ins as the dialect
|
|
|
|
// parser is not general enough to parse a dimension list with an optional
|
|
|
|
// element type (?). The built-in form is also remarkably ambiguous when
|
|
|
|
// considering extending it.
|
2020-06-30 06:27:39 +08:00
|
|
|
Type dtype = Basicpy::UnknownType::get(getContext());
|
2020-07-06 08:34:03 +08:00
|
|
|
bool hasShape = false;
|
|
|
|
llvm::SmallVector<int64_t, 4> shape;
|
2020-06-29 08:37:20 +08:00
|
|
|
if (parser.parseLess())
|
|
|
|
return Type();
|
2020-07-06 08:34:03 +08:00
|
|
|
if (succeeded(parser.parseOptionalStar())) {
|
|
|
|
// Unranked.
|
|
|
|
} else {
|
|
|
|
// Parse dimension list.
|
|
|
|
hasShape = true;
|
|
|
|
if (parser.parseLSquare())
|
|
|
|
return Type();
|
|
|
|
for (bool first = true;; first = false) {
|
|
|
|
if (!first) {
|
|
|
|
if (failed(parser.parseOptionalComma())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (succeeded(parser.parseOptionalQuestion())) {
|
|
|
|
shape.push_back(-1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t dim;
|
|
|
|
auto optionalPr = parser.parseOptionalInteger(dim);
|
|
|
|
if (optionalPr.hasValue()) {
|
|
|
|
if (failed(*optionalPr))
|
|
|
|
return Type();
|
|
|
|
shape.push_back(dim);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (parser.parseRSquare()) {
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse colon dtype.
|
|
|
|
if (parser.parseColon()) {
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
|
2020-06-29 08:37:20 +08:00
|
|
|
if (failed(parser.parseOptionalQuestion())) {
|
|
|
|
// Specified dtype.
|
2020-07-06 08:34:03 +08:00
|
|
|
if (parser.parseType(dtype)) {
|
2020-06-29 08:37:20 +08:00
|
|
|
return Type();
|
2020-07-06 08:34:03 +08:00
|
|
|
}
|
2020-06-29 08:37:20 +08:00
|
|
|
}
|
2020-07-06 08:34:03 +08:00
|
|
|
if (parser.parseGreater()) {
|
2020-06-29 08:37:20 +08:00
|
|
|
return Type();
|
2020-07-06 08:34:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<ArrayRef<int64_t>> optionalShape;
|
|
|
|
if (hasShape)
|
|
|
|
optionalShape = shape;
|
|
|
|
auto ndarray = NdArrayType::get(dtype, optionalShape);
|
|
|
|
return ndarray;
|
2020-06-29 08:37:20 +08:00
|
|
|
}
|
2020-04-30 09:20:42 +08:00
|
|
|
|
|
|
|
parser.emitError(parser.getNameLoc(), "unknown numpy type: ") << keyword;
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumpyDialect::printType(Type type, DialectAsmPrinter &os) const {
|
2020-08-28 06:09:10 +08:00
|
|
|
TypeSwitch<Type>(type)
|
|
|
|
.Case<AnyDtypeType>([&](Type) { os << "any_dtype"; })
|
|
|
|
.Case<NdArrayType>([&](NdArrayType t) {
|
|
|
|
auto unknownType = Basicpy::UnknownType::get(getContext());
|
|
|
|
auto ndarray = type.cast<NdArrayType>();
|
|
|
|
auto shape = ndarray.getOptionalShape();
|
|
|
|
auto dtype = ndarray.getDtype();
|
|
|
|
os << "ndarray<";
|
|
|
|
if (!shape) {
|
|
|
|
os << "*:";
|
|
|
|
} else {
|
|
|
|
os << "[";
|
|
|
|
for (auto it : llvm::enumerate(*shape)) {
|
|
|
|
if (it.index() > 0)
|
|
|
|
os << ",";
|
|
|
|
if (it.value() < 0)
|
|
|
|
os << "?";
|
|
|
|
else
|
|
|
|
os << it.value();
|
|
|
|
}
|
|
|
|
os << "]:";
|
|
|
|
}
|
|
|
|
if (dtype != unknownType)
|
|
|
|
os.printType(dtype);
|
2020-07-06 08:34:03 +08:00
|
|
|
else
|
2020-08-28 06:09:10 +08:00
|
|
|
os << "?";
|
|
|
|
os << ">";
|
|
|
|
})
|
|
|
|
.Default([&](Type) { llvm_unreachable("unexpected 'numpy' type kind"); });
|
2020-04-27 08:20:58 +08:00
|
|
|
}
|
2020-06-29 08:37:20 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// Type and attribute detail
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
namespace mlir {
|
|
|
|
namespace NPCOMP {
|
|
|
|
namespace Numpy {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
struct NdArrayTypeStorage : public TypeStorage {
|
2020-07-06 08:34:03 +08:00
|
|
|
using KeyTy = std::pair<Type, llvm::Optional<ArrayRef<int64_t>>>;
|
|
|
|
NdArrayTypeStorage(Type dtype, int rank, const int64_t *shapeElements)
|
|
|
|
: dtype(dtype), rank(rank), shapeElements(shapeElements) {}
|
|
|
|
bool operator==(const KeyTy &key) const {
|
|
|
|
return key == KeyTy(dtype, getOptionalShape());
|
|
|
|
}
|
2020-06-29 08:37:20 +08:00
|
|
|
static llvm::hash_code hashKey(const KeyTy &key) {
|
2020-07-06 08:34:03 +08:00
|
|
|
if (key.second) {
|
|
|
|
return llvm::hash_combine(key.first, *key.second);
|
|
|
|
} else {
|
|
|
|
return llvm::hash_combine(key.first, -1);
|
|
|
|
}
|
2020-06-29 08:37:20 +08:00
|
|
|
}
|
|
|
|
static NdArrayTypeStorage *construct(TypeStorageAllocator &allocator,
|
|
|
|
const KeyTy &key) {
|
2020-07-06 08:34:03 +08:00
|
|
|
int rank = -1;
|
|
|
|
const int64_t *shapeElements = nullptr;
|
|
|
|
if (key.second.hasValue()) {
|
|
|
|
auto allocElements = allocator.copyInto(*key.second);
|
|
|
|
rank = key.second->size();
|
|
|
|
shapeElements = allocElements.data();
|
|
|
|
}
|
2020-06-29 08:37:20 +08:00
|
|
|
return new (allocator.allocate<NdArrayTypeStorage>())
|
2020-07-06 08:34:03 +08:00
|
|
|
NdArrayTypeStorage(key.first, rank, shapeElements);
|
2020-06-29 08:37:20 +08:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:34:03 +08:00
|
|
|
llvm::Optional<ArrayRef<int64_t>> getOptionalShape() const {
|
|
|
|
if (rank < 0)
|
|
|
|
return llvm::None;
|
|
|
|
return ArrayRef<int64_t>(shapeElements, rank);
|
|
|
|
}
|
|
|
|
|
|
|
|
Type dtype;
|
|
|
|
int rank;
|
|
|
|
const int64_t *shapeElements;
|
2020-06-29 08:37:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace Numpy
|
|
|
|
} // namespace NPCOMP
|
|
|
|
} // namespace mlir
|
|
|
|
|
2020-07-06 08:34:03 +08:00
|
|
|
NdArrayType NdArrayType::get(Type dtype,
|
|
|
|
llvm::Optional<ArrayRef<int64_t>> shape) {
|
2020-06-30 06:27:39 +08:00
|
|
|
assert(dtype && "dtype cannot be null");
|
2020-08-28 06:09:10 +08:00
|
|
|
return Base::get(dtype.getContext(), dtype, shape);
|
2020-06-29 08:37:20 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:28:30 +08:00
|
|
|
NdArrayType NdArrayType::getFromShapedType(ShapedType shapedType) {
|
|
|
|
llvm::Optional<ArrayRef<int64_t>> shape;
|
|
|
|
if (shapedType.hasRank())
|
|
|
|
shape = shapedType.getShape();
|
|
|
|
return get(shapedType.getElementType(), shape);
|
|
|
|
}
|
|
|
|
|
2020-07-04 07:38:10 +08:00
|
|
|
bool NdArrayType::hasKnownDtype() {
|
|
|
|
return getDtype() != Basicpy::UnknownType::get(getContext());
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:34:03 +08:00
|
|
|
Type NdArrayType::getDtype() { return getImpl()->dtype; }
|
|
|
|
|
|
|
|
llvm::Optional<ArrayRef<int64_t>> NdArrayType::getOptionalShape() {
|
|
|
|
return getImpl()->getOptionalShape();
|
|
|
|
}
|
2020-07-04 07:38:10 +08:00
|
|
|
|
2020-07-09 13:51:54 +08:00
|
|
|
TensorType NdArrayType::toTensorType() {
|
|
|
|
auto shape = getOptionalShape();
|
|
|
|
if (shape) {
|
|
|
|
return RankedTensorType::get(*shape, getDtype());
|
|
|
|
} else {
|
|
|
|
return UnrankedTensorType::get(getDtype());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 07:38:10 +08:00
|
|
|
Typing::CPA::TypeNode *
|
|
|
|
NdArrayType::mapToCPAType(Typing::CPA::Context &context) {
|
|
|
|
llvm::Optional<Typing::CPA::TypeNode *> dtype;
|
|
|
|
if (hasKnownDtype()) {
|
|
|
|
// TODO: This should be using a general mechanism for resolving the dtype,
|
|
|
|
// but we don't have that yet, and for NdArray, these must be primitives
|
|
|
|
// anyway.
|
|
|
|
dtype = context.getIRValueType(getDtype());
|
|
|
|
}
|
2020-07-06 08:45:45 +08:00
|
|
|
// Safe to capture an ArrayRef backed by type storage since it is uniqued.
|
|
|
|
auto optionalShape = getOptionalShape();
|
|
|
|
auto irCtor = [optionalShape](Typing::CPA::ObjectValueType *ovt,
|
|
|
|
llvm::ArrayRef<mlir::Type> fieldTypes,
|
|
|
|
MLIRContext *mlirContext,
|
|
|
|
llvm::Optional<Location>) {
|
2020-07-05 11:33:13 +08:00
|
|
|
assert(fieldTypes.size() == 1);
|
2020-07-06 08:45:45 +08:00
|
|
|
return NdArrayType::get(fieldTypes.front(), optionalShape);
|
2020-07-05 11:33:13 +08:00
|
|
|
};
|
|
|
|
return Typing::CPA::newArrayType(context, irCtor,
|
|
|
|
context.getIdentifier("!NdArray"), dtype);
|
2020-07-04 07:38:10 +08:00
|
|
|
}
|