mirror of https://github.com/llvm/torch-mlir
Add bytes constants.
parent
a1e6ff4ab7
commit
72499e0319
|
@ -22,6 +22,7 @@ namespace BasicpyTypes {
|
||||||
enum Kind {
|
enum Kind {
|
||||||
// Dialect types.
|
// Dialect types.
|
||||||
BoolType = TypeRanges::Basicpy,
|
BoolType = TypeRanges::Basicpy,
|
||||||
|
BytesType,
|
||||||
EllipsisType,
|
EllipsisType,
|
||||||
NoneType,
|
NoneType,
|
||||||
SlotObjectType,
|
SlotObjectType,
|
||||||
|
@ -49,6 +50,16 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// The type of the Python `bytes` values.
|
||||||
|
class BytesType : public Type::TypeBase<BytesType, Type> {
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
static bool kindof(unsigned kind) { return kind == BasicpyTypes::BytesType; }
|
||||||
|
static BytesType get(MLIRContext *context) {
|
||||||
|
return Base::get(context, BasicpyTypes::BytesType);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// The type of the Python `Ellipsis` value.
|
/// The type of the Python `Ellipsis` value.
|
||||||
class EllipsisType : public Type::TypeBase<EllipsisType, Type> {
|
class EllipsisType : public Type::TypeBase<EllipsisType, Type> {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -46,6 +46,14 @@ def Basicpy_BoolType : DialectType<Basicpy_Dialect,
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def Basicpy_BytesType : DialectType<Basicpy_Dialect,
|
||||||
|
CPred<"$_self.isa<::mlir::NPCOMP::Basicpy::BytesType>()">, "Bytes type">,
|
||||||
|
BuildableType<"$_builder.getType<::mlir::NPCOMP::Basicpy::BytesType>()"> {
|
||||||
|
let typeDescription = [{
|
||||||
|
Represents Python 'bytes' values.
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
def Basicpy_EllipsisType : DialectType<Basicpy_Dialect,
|
def Basicpy_EllipsisType : DialectType<Basicpy_Dialect,
|
||||||
CPred<"$_self.isa<::mlir::NPCOMP::Basicpy::EllipsisType>()">, "Ellipsis type">,
|
CPred<"$_self.isa<::mlir::NPCOMP::Basicpy::EllipsisType>()">, "Ellipsis type">,
|
||||||
BuildableType<"$_builder.getType<::mlir::NPCOMP::Basicpy::EllipsisType>()"> {
|
BuildableType<"$_builder.getType<::mlir::NPCOMP::Basicpy::EllipsisType>()"> {
|
||||||
|
|
|
@ -27,6 +27,21 @@ def Basicpy_BoolConstantOp : Basicpy_Op<"bool_constant", [
|
||||||
let assemblyFormat = "$value attr-dict";
|
let assemblyFormat = "$value attr-dict";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def Basicpy_BytesConstantOp : Basicpy_Op<"bytes_constant", [
|
||||||
|
ConstantLike, NoSideEffect]> {
|
||||||
|
let summary = "Constant bytes value";
|
||||||
|
let description = [{
|
||||||
|
A bytes value of BytesType. The value is represented by a StringAttr.
|
||||||
|
}];
|
||||||
|
let arguments = (ins
|
||||||
|
StrAttr:$value
|
||||||
|
);
|
||||||
|
let results = (outs
|
||||||
|
Basicpy_BytesType:$result
|
||||||
|
);
|
||||||
|
let assemblyFormat = "$value attr-dict";
|
||||||
|
}
|
||||||
|
|
||||||
def Basicpy_SlotObjectMakeOp : Basicpy_Op<"slot_object_make", [
|
def Basicpy_SlotObjectMakeOp : Basicpy_Op<"slot_object_make", [
|
||||||
NoSideEffect]> {
|
NoSideEffect]> {
|
||||||
let summary = "Creates an instance of a SlotObject type";
|
let summary = "Creates an instance of a SlotObject type";
|
||||||
|
|
|
@ -19,7 +19,7 @@ BasicpyDialect::BasicpyDialect(MLIRContext *context)
|
||||||
#define GET_OP_LIST
|
#define GET_OP_LIST
|
||||||
#include "npcomp/Dialect/Basicpy/BasicpyOps.cpp.inc"
|
#include "npcomp/Dialect/Basicpy/BasicpyOps.cpp.inc"
|
||||||
>();
|
>();
|
||||||
addTypes<BoolType, EllipsisType, NoneType, SlotObjectType, StrType,
|
addTypes<BoolType, BytesType, EllipsisType, NoneType, SlotObjectType, StrType,
|
||||||
UnknownType>();
|
UnknownType>();
|
||||||
|
|
||||||
// TODO: Make real ops for everything we need.
|
// TODO: Make real ops for everything we need.
|
||||||
|
@ -33,6 +33,8 @@ Type BasicpyDialect::parseType(DialectAsmParser &parser) const {
|
||||||
|
|
||||||
if (keyword == "BoolType")
|
if (keyword == "BoolType")
|
||||||
return BoolType::get(getContext());
|
return BoolType::get(getContext());
|
||||||
|
if (keyword == "BytesType")
|
||||||
|
return BytesType::get(getContext());
|
||||||
if (keyword == "EllipsisType")
|
if (keyword == "EllipsisType")
|
||||||
return EllipsisType::get(getContext());
|
return EllipsisType::get(getContext());
|
||||||
if (keyword == "NoneType")
|
if (keyword == "NoneType")
|
||||||
|
@ -69,6 +71,9 @@ void BasicpyDialect::printType(Type type, DialectAsmPrinter &os) const {
|
||||||
case BasicpyTypes::BoolType:
|
case BasicpyTypes::BoolType:
|
||||||
os << "BoolType";
|
os << "BoolType";
|
||||||
return;
|
return;
|
||||||
|
case BasicpyTypes::BytesType:
|
||||||
|
os << "BytesType";
|
||||||
|
return;
|
||||||
case BasicpyTypes::EllipsisType:
|
case BasicpyTypes::EllipsisType:
|
||||||
os << "EllipsisType";
|
os << "EllipsisType";
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -61,6 +61,14 @@ def joined_string_constant():
|
||||||
a = "I am" " still here"
|
a = "I am" " still here"
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
# CHECK-LABEL: func @bytes_constant
|
||||||
|
@import_global
|
||||||
|
def bytes_constant():
|
||||||
|
# CHECK: %[[A:.*]] = basicpy.bytes_constant "foobar"
|
||||||
|
# CHECK: basicpy.unknown_cast %[[A]]
|
||||||
|
a = b"foobar"
|
||||||
|
return a
|
||||||
|
|
||||||
# CHECK-LABEL: func @ellipsis
|
# CHECK-LABEL: func @ellipsis
|
||||||
@import_global
|
@import_global
|
||||||
def ellipsis():
|
def ellipsis():
|
||||||
|
|
|
@ -210,6 +210,8 @@ class ExpressionImporter(BaseNodeVisitor):
|
||||||
self.fctx.abort("unknown named constant '%r'" % (ast_node.value,))
|
self.fctx.abort("unknown named constant '%r'" % (ast_node.value,))
|
||||||
elif isinstance(ast_node, ast.Str):
|
elif isinstance(ast_node, ast.Str):
|
||||||
self.value = ir_h.basicpy_str_constant_op(ast_node.s).result
|
self.value = ir_h.basicpy_str_constant_op(ast_node.s).result
|
||||||
|
elif isinstance(ast_node, ast.Bytes):
|
||||||
|
self.value = ir_h.basicpy_bytes_constant_op(ast_node.s).result
|
||||||
elif isinstance(ast_node, ast.Ellipsis):
|
elif isinstance(ast_node, ast.Ellipsis):
|
||||||
self.value = ir_h.basicpy_singleton_op(ir_h.basicpy_EllipsisType).result
|
self.value = ir_h.basicpy_singleton_op(ir_h.basicpy_EllipsisType).result
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -50,11 +50,15 @@ class DialectHelper(_BaseDialectHelper):
|
||||||
def basicpy_bool_constant_op(self, value):
|
def basicpy_bool_constant_op(self, value):
|
||||||
c = self.context
|
c = self.context
|
||||||
ival = 1 if value else 0
|
ival = 1 if value else 0
|
||||||
attrs = c.dictionary_attr({
|
attrs = c.dictionary_attr({"value": c.integer_attr(self.i1_type, ival)})
|
||||||
"value": c.integer_attr(self.i1_type, ival)
|
|
||||||
})
|
|
||||||
return self.op("basicpy.bool_constant", [self.basicpy_BoolType], [], attrs)
|
return self.op("basicpy.bool_constant", [self.basicpy_BoolType], [], attrs)
|
||||||
|
|
||||||
|
def basicpy_bytes_constant_op(self, value):
|
||||||
|
c = self.context
|
||||||
|
attrs = c.dictionary_attr({"value": c.string_attr(value)})
|
||||||
|
return self.op("basicpy.bytes_constant", [self.basicpy_BytesType], [],
|
||||||
|
attrs)
|
||||||
|
|
||||||
def basicpy_singleton_op(self, singleton_type):
|
def basicpy_singleton_op(self, singleton_type):
|
||||||
return self.op("basicpy.singleton", [singleton_type], [])
|
return self.op("basicpy.singleton", [singleton_type], [])
|
||||||
|
|
||||||
|
@ -69,14 +73,12 @@ class DialectHelper(_BaseDialectHelper):
|
||||||
|
|
||||||
def basicpy_str_constant_op(self, value):
|
def basicpy_str_constant_op(self, value):
|
||||||
c = self.context
|
c = self.context
|
||||||
attrs = c.dictionary_attr({
|
attrs = c.dictionary_attr({"value": c.string_attr(value.encode("utf-8"))})
|
||||||
"value": c.string_attr(value.encode("utf-8"))
|
|
||||||
})
|
|
||||||
return self.op("basicpy.str_constant", [self.basicpy_StrType], [], attrs)
|
return self.op("basicpy.str_constant", [self.basicpy_StrType], [], attrs)
|
||||||
|
|
||||||
def basicpy_unknown_cast_op(self, result_type, operand):
|
def basicpy_unknown_cast_op(self, result_type, operand):
|
||||||
return self.op("basicpy.unknown_cast", [result_type], [operand])
|
return self.op("basicpy.unknown_cast", [result_type], [operand])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
|
@ -26,6 +26,11 @@ public:
|
||||||
return Basicpy::BoolType::get(
|
return Basicpy::BoolType::get(
|
||||||
&self.context->context);
|
&self.context->context);
|
||||||
})
|
})
|
||||||
|
.def_property_readonly("basicpy_BytesType",
|
||||||
|
[](BasicpyDialectHelper &self) -> PyType {
|
||||||
|
return Basicpy::BytesType::get(
|
||||||
|
&self.context->context);
|
||||||
|
})
|
||||||
.def_property_readonly("basicpy_EllipsisType",
|
.def_property_readonly("basicpy_EllipsisType",
|
||||||
[](BasicpyDialectHelper &self) -> PyType {
|
[](BasicpyDialectHelper &self) -> PyType {
|
||||||
return Basicpy::EllipsisType::get(
|
return Basicpy::EllipsisType::get(
|
||||||
|
|
Loading…
Reference in New Issue