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

108 lines
3.1 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/Numpy/IR/NumpyOps.h"
2020-04-27 08:20:58 +08:00
using namespace mlir;
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;
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, getContext());
}
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 ndarray = type.cast<NdArrayType>();
auto dtype = ndarray.getOptionalDtype();
os << "ndarray<";
if (dtype)
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 optionalDtype, MLIRContext *context) {
return Base::get(context, NumpyTypes::NdArray, optionalDtype);
}
Type NdArrayType::getOptionalDtype() { return getImpl()->optionalDtype; }