torch-mlir/lib/Dialect/Numpy/IR/NumpyDialect.cpp

128 lines
3.8 KiB
C++
Raw Normal View History

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
//
//===----------------------------------------------------------------------===//
#include "npcomp/Dialect/Numpy/IR/NumpyDialect.h"
2020-04-30 09:20:42 +08:00
#include "mlir/IR/DialectImplementation.h"
#include "npcomp/Dialect/Basicpy/IR/BasicpyDialect.h"
#include "npcomp/Dialect/Numpy/IR/NumpyOps.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
NumpyDialect::NumpyDialect(MLIRContext *context)
: Dialect(getDialectNamespace(), context) {
addOperations<
#define GET_OP_LIST
#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>();
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:
// ndarray<?>
// ndarray<i32>
Type dtype = Basicpy::UnknownType::get(getContext());
2020-06-29 08:37:20 +08:00
if (parser.parseLess())
return Type();
if (failed(parser.parseOptionalQuestion())) {
// Specified dtype.
if (parser.parseType(dtype))
return Type();
}
if (parser.parseGreater())
return Type();
return NdArrayType::get(dtype);
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 {
switch (type.getKind()) {
case NumpyTypes::AnyDtypeType:
os << "any_dtype";
return;
2020-06-29 08:37:20 +08:00
case NumpyTypes::NdArray: {
auto unknownType = Basicpy::UnknownType::get(getContext());
2020-06-29 08:37:20 +08:00
auto ndarray = type.cast<NdArrayType>();
auto dtype = ndarray.getDtype();
2020-06-29 08:37:20 +08:00
os << "ndarray<";
if (dtype != unknownType)
2020-06-29 08:37:20 +08:00
os.printType(dtype);
else
os << "?";
os << ">";
return;
}
2020-04-30 09:20:42 +08:00
default:
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 {
using KeyTy = Type;
NdArrayTypeStorage(Type optionalDtype) : optionalDtype(optionalDtype) {}
bool operator==(const KeyTy &other) const { return optionalDtype == other; }
static llvm::hash_code hashKey(const KeyTy &key) {
return llvm::hash_combine(key);
}
static NdArrayTypeStorage *construct(TypeStorageAllocator &allocator,
const KeyTy &key) {
return new (allocator.allocate<NdArrayTypeStorage>())
NdArrayTypeStorage(key);
}
Type optionalDtype;
};
} // namespace detail
} // namespace Numpy
} // namespace NPCOMP
} // namespace mlir
NdArrayType NdArrayType::get(Type dtype) {
assert(dtype && "dtype cannot be null");
return Base::get(dtype.getContext(), NumpyTypes::NdArray, dtype);
2020-06-29 08:37:20 +08:00
}
2020-07-04 07:38:10 +08:00
bool NdArrayType::hasKnownDtype() {
return getDtype() != Basicpy::UnknownType::get(getContext());
}
Type NdArrayType::getDtype() { return getImpl()->optionalDtype; }
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());
}
return context.newArrayType(context.getIdentifier("!NdArray"), dtype);
}