from mlir.ir import *
from mlir.dialects import llvm
def constructAndPrintInModule(f):
print("\nTEST:", f.__name__)
with Context(), Location.unknown():
module = Module.create()
with InsertionPoint(module.body):
f()
print(module)
return f
@constructAndPrintInModule
def testStructType():
print(llvm.StructType.get_literal([]))
i8, i32, i64 = tuple(map(lambda x: IntegerType.get_signless(x), [8, 32, 64]))
print(llvm.StructType.get_literal([i8, i32, i64]))
print(llvm.StructType.get_literal([i32]))
print(llvm.StructType.get_literal([i32, i32], packed=True))
literal = llvm.StructType.get_literal([i8, i32, i64])
assert len(literal.body) == 3
print(*tuple(literal.body))
assert literal.name is None
assert llvm.StructType.get_literal([i32]) == llvm.StructType.get_literal([i32])
assert llvm.StructType.get_literal([i32]) != llvm.StructType.get_literal([i64])
print(llvm.StructType.get_identified("foo"))
print(llvm.StructType.get_identified("bar"))
assert llvm.StructType.get_identified("foo") == llvm.StructType.get_identified(
"foo"
)
assert llvm.StructType.get_identified("foo") != llvm.StructType.get_identified(
"bar"
)
foo_struct = llvm.StructType.get_identified("foo")
print(foo_struct.name)
print(foo_struct.body)
assert foo_struct.opaque
foo_struct.set_body([i32, i64])
print(*tuple(foo_struct.body))
print(foo_struct)
assert not foo_struct.packed
assert not foo_struct.opaque
assert llvm.StructType.get_identified("foo") == foo_struct
bar_struct = llvm.StructType.get_identified("bar")
bar_struct.set_body([i32], packed=True)
print(bar_struct)
assert bar_struct.packed
foo_struct.set_body([i32, i64])
try:
foo_struct.set_body([])
except ValueError as e:
pass
else:
assert False, "expected exception not raised"
try:
bar_struct.set_body([i32])
except ValueError as e:
pass
else:
assert False, "expected exception not raised"
print(llvm.StructType.new_identified("foo", []))
assert llvm.StructType.new_identified("foo", []) != llvm.StructType.new_identified(
"foo", []
)
opaque = llvm.StructType.get_opaque("opaque")
print(opaque)
assert opaque.opaque
@constructAndPrintInModule
def testSmoke():
mat64f32_t = Type.parse(
"!llvm.struct<(f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32)>"
)
result = llvm.UndefOp(mat64f32_t)
@constructAndPrintInModule
def testPointerType():
ptr = llvm.PointerType.get()
print(ptr)
ptr_with_addr = llvm.PointerType.get(1)
print(ptr_with_addr)
@constructAndPrintInModule
def testConstant():
i32 = IntegerType.get_signless(32)
c_128 = llvm.mlir_constant(IntegerAttr.get(i32, 128))
print(c_128.owner)
@constructAndPrintInModule
def testIntrinsics():
i32 = IntegerType.get_signless(32)
ptr = llvm.PointerType.get()
c_128 = llvm.mlir_constant(IntegerAttr.get(i32, 128))
print(c_128.owner)
alloca = llvm.alloca(ptr, c_128, i32)
print(alloca.owner)
c_0 = llvm.mlir_constant(IntegerAttr.get(IntegerType.get_signless(8), 0))
print(c_0.owner)
result = llvm.intr_memset(alloca, c_0, c_128, False)
print(result)