2020-05-05 08:48:02 +08:00
|
|
|
//===- BasicpyDialect.cpp - Basic python 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/Basicpy/IR/BasicpyDialect.h"
|
2020-05-05 08:48:02 +08:00
|
|
|
#include "mlir/IR/DialectImplementation.h"
|
2020-06-10 10:22:24 +08:00
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyOps.h"
|
2020-08-28 06:09:10 +08:00
|
|
|
#include "llvm/ADT/TypeSwitch.h"
|
2020-05-05 08:48:02 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
2020-07-03 04:56:27 +08:00
|
|
|
using namespace mlir::NPCOMP;
|
2020-05-05 08:48:02 +08:00
|
|
|
using namespace mlir::NPCOMP::Basicpy;
|
|
|
|
|
2020-08-28 06:09:10 +08:00
|
|
|
void BasicpyDialect::initialize() {
|
2020-05-05 08:48:02 +08:00
|
|
|
addOperations<
|
|
|
|
#define GET_OP_LIST
|
2020-06-10 10:22:24 +08:00
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyOps.cpp.inc"
|
2020-05-05 08:48:02 +08:00
|
|
|
>();
|
2020-10-17 08:38:07 +08:00
|
|
|
addTypes<BoolType, BytesType, DictType, EllipsisType, ListType, NoneType,
|
|
|
|
SlotObjectType, StrType, TupleType, UnknownType>();
|
2020-06-07 12:24:28 +08:00
|
|
|
|
|
|
|
// TODO: Make real ops for everything we need.
|
|
|
|
allowUnknownOperations();
|
2020-05-05 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2020-11-24 11:20:26 +08:00
|
|
|
Operation *BasicpyDialect::materializeConstant(OpBuilder &builder,
|
|
|
|
Attribute value, Type type,
|
|
|
|
Location loc) {
|
|
|
|
// NumericConstantOp.
|
|
|
|
// Supports IntegerType (any signedness), FloatType and ComplexType.
|
|
|
|
if (type.isa<IntegerType>() || type.isa<FloatType>() ||
|
|
|
|
type.isa<ComplexType>())
|
|
|
|
return builder.create<NumericConstantOp>(loc, type, value);
|
|
|
|
|
|
|
|
// Bool (i1 -> !basicpy.BoolType).
|
|
|
|
if (type.isa<Basicpy::BoolType>()) {
|
|
|
|
auto i1Value = value.dyn_cast<IntegerAttr>();
|
|
|
|
if (i1Value && i1Value.getType().getIntOrFloatBitWidth() == 1)
|
|
|
|
return builder.create<BoolConstantOp>(loc, type, i1Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes.
|
|
|
|
if (type.isa<Basicpy::BytesType>()) {
|
|
|
|
if (auto strValue = value.dyn_cast<StringAttr>())
|
|
|
|
return builder.create<BytesConstantOp>(loc, type, strValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Str.
|
|
|
|
if (type.isa<Basicpy::StrType>()) {
|
|
|
|
if (auto strValue = value.dyn_cast<StringAttr>())
|
|
|
|
return builder.create<StrConstantOp>(loc, type, strValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-06 09:16:01 +08:00
|
|
|
Type BasicpyDialect::parseType(DialectAsmParser &parser) const {
|
|
|
|
StringRef keyword;
|
|
|
|
if (parser.parseKeyword(&keyword))
|
|
|
|
return Type();
|
|
|
|
|
2020-06-08 06:15:19 +08:00
|
|
|
if (keyword == "BoolType")
|
|
|
|
return BoolType::get(getContext());
|
2020-06-08 07:00:29 +08:00
|
|
|
if (keyword == "BytesType")
|
|
|
|
return BytesType::get(getContext());
|
2020-10-17 08:38:07 +08:00
|
|
|
if (keyword == "DictType")
|
|
|
|
return DictType::get(getContext());
|
2020-05-06 09:16:01 +08:00
|
|
|
if (keyword == "EllipsisType")
|
|
|
|
return EllipsisType::get(getContext());
|
2020-10-17 08:38:07 +08:00
|
|
|
if (keyword == "ListType")
|
|
|
|
return ListType::get(getContext());
|
2020-06-08 06:46:28 +08:00
|
|
|
if (keyword == "NoneType")
|
|
|
|
return NoneType::get(getContext());
|
2020-05-06 09:16:01 +08:00
|
|
|
if (keyword == "SlotObject") {
|
|
|
|
StringRef className;
|
|
|
|
if (parser.parseLess() || parser.parseKeyword(&className)) {
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallVector<Type, 4> slotTypes;
|
|
|
|
while (succeeded(parser.parseOptionalComma())) {
|
|
|
|
Type slotType;
|
|
|
|
if (parser.parseType(slotType))
|
|
|
|
return Type();
|
|
|
|
slotTypes.push_back(slotType);
|
|
|
|
}
|
|
|
|
if (parser.parseGreater())
|
|
|
|
return Type();
|
|
|
|
return SlotObjectType::get(StringAttr::get(className, getContext()),
|
|
|
|
slotTypes);
|
|
|
|
}
|
2020-06-08 06:46:28 +08:00
|
|
|
if (keyword == "StrType")
|
|
|
|
return StrType::get(getContext());
|
2020-10-17 08:38:07 +08:00
|
|
|
if (keyword == "TupleType")
|
|
|
|
return TupleType::get(getContext());
|
2020-06-08 06:46:28 +08:00
|
|
|
if (keyword == "UnknownType")
|
|
|
|
return UnknownType::get(getContext());
|
2020-05-06 09:16:01 +08:00
|
|
|
|
|
|
|
parser.emitError(parser.getNameLoc(), "unknown basicpy type");
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicpyDialect::printType(Type type, DialectAsmPrinter &os) const {
|
2020-08-28 06:09:10 +08:00
|
|
|
TypeSwitch<Type>(type)
|
|
|
|
.Case<BoolType>([&](Type) { os << "BoolType"; })
|
|
|
|
.Case<BytesType>([&](Type) { os << "BytesType"; })
|
2020-10-17 08:38:07 +08:00
|
|
|
.Case<DictType>([&](Type) { os << "DictType"; })
|
2020-08-28 06:09:10 +08:00
|
|
|
.Case<EllipsisType>([&](Type) { os << "EllipsisType"; })
|
2020-10-17 08:38:07 +08:00
|
|
|
.Case<ListType>([&](Type) { os << "ListType"; })
|
2020-08-28 06:09:10 +08:00
|
|
|
.Case<NoneType>([&](Type) { os << "NoneType"; })
|
|
|
|
.Case<SlotObjectType>([&](SlotObjectType slotObject) {
|
|
|
|
auto slotTypes = slotObject.getSlotTypes();
|
|
|
|
os << "SlotObject<" << slotObject.getClassName().getValue();
|
|
|
|
if (!slotTypes.empty()) {
|
|
|
|
os << ", ";
|
|
|
|
llvm::interleaveComma(slotTypes, os,
|
|
|
|
[&](Type t) { os.printType(t); });
|
|
|
|
}
|
|
|
|
os << ">";
|
|
|
|
})
|
|
|
|
.Case<StrType>([&](Type) { os << "StrType"; })
|
2020-10-17 08:38:07 +08:00
|
|
|
.Case<TupleType>([&](Type) { os << "TupleType"; })
|
2020-08-28 06:09:10 +08:00
|
|
|
.Case<UnknownType>([&](Type) { os << "UnknownType"; })
|
|
|
|
.Default(
|
|
|
|
[&](Type) { llvm_unreachable("unexpected 'basicpy' type kind"); });
|
2020-05-06 09:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// Type and attribute detail
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
namespace mlir {
|
|
|
|
namespace NPCOMP {
|
|
|
|
namespace Basicpy {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
struct SlotObjectTypeStorage : public TypeStorage {
|
|
|
|
using KeyTy = std::pair<StringAttr, ArrayRef<Type>>;
|
|
|
|
SlotObjectTypeStorage(StringAttr className, ArrayRef<Type> slotTypes)
|
|
|
|
: className(className), slotTypes(slotTypes) {}
|
|
|
|
bool operator==(const KeyTy &other) const {
|
|
|
|
return className == other.first && slotTypes == other.second;
|
|
|
|
}
|
|
|
|
static llvm::hash_code hashKey(const KeyTy &key) {
|
|
|
|
return llvm::hash_combine(key.first, key.second);
|
|
|
|
}
|
|
|
|
static SlotObjectTypeStorage *construct(TypeStorageAllocator &allocator,
|
|
|
|
const KeyTy &key) {
|
|
|
|
ArrayRef<Type> slotTypes = allocator.copyInto(key.second);
|
|
|
|
return new (allocator.allocate<SlotObjectTypeStorage>())
|
|
|
|
SlotObjectTypeStorage(key.first, slotTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringAttr className;
|
|
|
|
ArrayRef<Type> slotTypes;
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace Basicpy
|
|
|
|
} // namespace NPCOMP
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
|
|
StringAttr SlotObjectType::getClassName() { return getImpl()->className; }
|
|
|
|
ArrayRef<Type> SlotObjectType::getSlotTypes() { return getImpl()->slotTypes; }
|
|
|
|
unsigned SlotObjectType::getSlotCount() { return getImpl()->slotTypes.size(); }
|
|
|
|
|
|
|
|
SlotObjectType SlotObjectType::get(StringAttr className,
|
|
|
|
ArrayRef<Type> slotTypes) {
|
2020-08-28 06:09:10 +08:00
|
|
|
return Base::get(className.getContext(), className, slotTypes);
|
2020-05-06 09:16:01 +08:00
|
|
|
}
|
2020-07-03 04:56:27 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// CPA Interface Implementations
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2020-07-04 04:29:52 +08:00
|
|
|
Typing::CPA::TypeNode *
|
2020-07-03 04:56:27 +08:00
|
|
|
UnknownType::mapToCPAType(Typing::CPA::Context &context) {
|
|
|
|
return context.newTypeVar();
|
|
|
|
}
|