2020-05-07 09:24:51 +08:00
|
|
|
# Part of the LLVM Project, 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-05-07 13:44:12 +08:00
|
|
|
from _npcomp.dialect import BasicpyDialectHelper as _BaseDialectHelper
|
|
|
|
from _npcomp.mlir import ir
|
2020-05-07 09:24:51 +08:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"DialectHelper",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class DialectHelper(_BaseDialectHelper):
|
|
|
|
r"""Dialect helper for the Basicpy dialect.
|
|
|
|
|
|
|
|
>>> c = ir.MLIRContext()
|
|
|
|
>>> h = DialectHelper(c)
|
|
|
|
|
|
|
|
Dialect Types:
|
2020-05-07 10:07:50 +08:00
|
|
|
>>> h.basicpy_NoneType
|
2020-05-07 09:24:51 +08:00
|
|
|
!basicpy.NoneType
|
2020-05-07 10:07:50 +08:00
|
|
|
>>> h.basicpy_EllipsisType
|
2020-05-07 09:24:51 +08:00
|
|
|
!basicpy.EllipsisType
|
|
|
|
>>> h.basicpy_SlotObject_type(
|
2020-05-07 10:07:50 +08:00
|
|
|
... "foobar", h.basicpy_NoneType, h.basicpy_NoneType)
|
2020-05-07 09:24:51 +08:00
|
|
|
!basicpy.SlotObject<foobar, !basicpy.NoneType, !basicpy.NoneType>
|
|
|
|
|
|
|
|
singleton op:
|
|
|
|
>>> m = c.new_module()
|
|
|
|
>>> h.builder.insert_block_start(m.first_block)
|
2020-05-07 10:07:50 +08:00
|
|
|
>>> _ = h.basicpy_singleton_op(h.basicpy_NoneType)
|
2020-05-07 09:24:51 +08:00
|
|
|
>>> m.to_asm().strip()
|
|
|
|
'module {\n %0 = basicpy.singleton : !basicpy.NoneType\n}'
|
|
|
|
|
|
|
|
slot_object ops:
|
|
|
|
>>> m = c.new_module()
|
|
|
|
>>> h.builder.insert_block_start(m.first_block)
|
2020-05-07 10:07:50 +08:00
|
|
|
>>> v0 = h.basicpy_singleton_op(h.basicpy_NoneType).result
|
2020-05-07 09:24:51 +08:00
|
|
|
>>> slot_object = h.basicpy_slot_object_make_op("foobar", v0, v0).result
|
|
|
|
>>> _ = h.basicpy_slot_object_get_op(slot_object, 0)
|
|
|
|
>>> print(m.to_asm().strip())
|
|
|
|
module {
|
|
|
|
%0 = basicpy.singleton : !basicpy.NoneType
|
|
|
|
%1 = basicpy.slot_object_make(%0, %0) -> !basicpy.SlotObject<foobar, !basicpy.NoneType, !basicpy.NoneType>
|
|
|
|
%2 = basicpy.slot_object_get %1[0] : !basicpy.SlotObject<foobar, !basicpy.NoneType, !basicpy.NoneType>
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-06-08 05:29:39 +08:00
|
|
|
def basicpy_unknown_cast(self, result_type, operand):
|
|
|
|
return self.op("basicpy.unknown_cast", [result_type], [operand])
|
|
|
|
|
2020-06-08 06:15:19 +08:00
|
|
|
def basicpy_bool_constant_op(self, value):
|
|
|
|
c = self.context
|
|
|
|
ival = 1 if value else 0
|
|
|
|
attrs = c.dictionary_attr({
|
|
|
|
"value": c.integer_attr(self.i1_type, ival)
|
|
|
|
})
|
|
|
|
return self.op("basicpy.bool_constant", [self.basicpy_BoolType], [], attrs)
|
|
|
|
|
2020-05-07 09:24:51 +08:00
|
|
|
def basicpy_singleton_op(self, singleton_type):
|
|
|
|
return self.op("basicpy.singleton", [singleton_type], [])
|
|
|
|
|
|
|
|
def basicpy_slot_object_make_op(self, class_name, *slot_values):
|
|
|
|
c = self.context
|
|
|
|
class_name_attr = c.string_attr(class_name)
|
|
|
|
object_type = self.basicpy_SlotObject_type(class_name,
|
|
|
|
*[v.type for v in slot_values])
|
|
|
|
attrs = c.dictionary_attr({"className": class_name_attr})
|
|
|
|
return self.op("basicpy.slot_object_make", [object_type], slot_values,
|
|
|
|
attrs)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|