178 lines · python
1# RUN: %PYTHON %s | FileCheck %s2# This is just a smoke test that the dialect is functional.3 4from mlir.ir import *5from mlir.dialects import llvm6 7 8def constructAndPrintInModule(f):9 print("\nTEST:", f.__name__)10 with Context(), Location.unknown():11 module = Module.create()12 with InsertionPoint(module.body):13 f()14 print(module)15 return f16 17 18# CHECK-LABEL: testStructType19@constructAndPrintInModule20def testStructType():21 print(llvm.StructType.get_literal([]))22 # CHECK: !llvm.struct<()>23 24 i8, i32, i64 = tuple(map(lambda x: IntegerType.get_signless(x), [8, 32, 64]))25 print(llvm.StructType.get_literal([i8, i32, i64]))26 print(llvm.StructType.get_literal([i32]))27 print(llvm.StructType.get_literal([i32, i32], packed=True))28 literal = llvm.StructType.get_literal([i8, i32, i64])29 assert len(literal.body) == 330 print(*tuple(literal.body))31 assert literal.name is None32 # CHECK: !llvm.struct<(i8, i32, i64)>33 # CHECK: !llvm.struct<(i32)>34 # CHECK: !llvm.struct<packed (i32, i32)>35 # CHECK: i8 i32 i6436 37 assert llvm.StructType.get_literal([i32]) == llvm.StructType.get_literal([i32])38 assert llvm.StructType.get_literal([i32]) != llvm.StructType.get_literal([i64])39 40 print(llvm.StructType.get_identified("foo"))41 print(llvm.StructType.get_identified("bar"))42 # CHECK: !llvm.struct<"foo", opaque>43 # CHECK: !llvm.struct<"bar", opaque>44 45 assert llvm.StructType.get_identified("foo") == llvm.StructType.get_identified(46 "foo"47 )48 assert llvm.StructType.get_identified("foo") != llvm.StructType.get_identified(49 "bar"50 )51 52 foo_struct = llvm.StructType.get_identified("foo")53 print(foo_struct.name)54 print(foo_struct.body)55 assert foo_struct.opaque56 foo_struct.set_body([i32, i64])57 print(*tuple(foo_struct.body))58 print(foo_struct)59 assert not foo_struct.packed60 assert not foo_struct.opaque61 assert llvm.StructType.get_identified("foo") == foo_struct62 # CHECK: foo63 # CHECK: None64 # CHECK: i32 i6465 # CHECK: !llvm.struct<"foo", (i32, i64)>66 67 bar_struct = llvm.StructType.get_identified("bar")68 bar_struct.set_body([i32], packed=True)69 print(bar_struct)70 assert bar_struct.packed71 # CHECK: !llvm.struct<"bar", packed (i32)>72 73 # Same body, should not raise.74 foo_struct.set_body([i32, i64])75 76 try:77 foo_struct.set_body([])78 except ValueError as e:79 pass80 else:81 assert False, "expected exception not raised"82 83 try:84 bar_struct.set_body([i32])85 except ValueError as e:86 pass87 else:88 assert False, "expected exception not raised"89 90 print(llvm.StructType.new_identified("foo", []))91 assert llvm.StructType.new_identified("foo", []) != llvm.StructType.new_identified(92 "foo", []93 )94 # CHECK: !llvm.struct<"foo{{[^"]+}}95 96 opaque = llvm.StructType.get_opaque("opaque")97 print(opaque)98 assert opaque.opaque99 # CHECK: !llvm.struct<"opaque", opaque>100 101 typ = Type.parse('!llvm.struct<"zoo", (i32, i64)>')102 assert isinstance(typ, llvm.StructType)103 104 105# CHECK-LABEL: testSmoke106@constructAndPrintInModule107def testSmoke():108 mat64f32_t = Type.parse(109 "!llvm.struct<(f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32)>"110 )111 result = llvm.UndefOp(mat64f32_t)112 # CHECK: %0 = llvm.mlir.undef : !llvm.struct<(f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32)>113 114 115# CHECK-LABEL: testPointerType116@constructAndPrintInModule117def testPointerType():118 ptr = llvm.PointerType.get()119 # CHECK: !llvm.ptr120 print(ptr)121 122 ptr_with_addr = llvm.PointerType.get(1)123 # CHECK: !llvm.ptr<1>124 print(ptr_with_addr)125 126 typ = Type.parse("!llvm.ptr<1>")127 assert isinstance(typ, llvm.PointerType)128 129 130# CHECK-LABEL: testConstant131@constructAndPrintInModule132def testConstant():133 i32 = IntegerType.get_signless(32)134 c_128 = llvm.mlir_constant(IntegerAttr.get(i32, 128))135 # CHECK: %{{.*}} = llvm.mlir.constant(128 : i32) : i32136 print(c_128.owner)137 138 139# CHECK-LABEL: testIntrinsics140@constructAndPrintInModule141def testIntrinsics():142 i32 = IntegerType.get_signless(32)143 ptr = llvm.PointerType.get()144 c_128 = llvm.mlir_constant(IntegerAttr.get(i32, 128))145 # CHECK: %[[CST128:.*]] = llvm.mlir.constant(128 : i32) : i32146 print(c_128.owner)147 148 alloca = llvm.alloca(ptr, c_128, i32)149 # CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[CST128]] x i32 : (i32) -> !llvm.ptr150 print(alloca.owner)151 152 c_0 = llvm.mlir_constant(IntegerAttr.get(IntegerType.get_signless(8), 0))153 # CHECK: %[[CST0:.+]] = llvm.mlir.constant(0 : i8) : i8154 print(c_0.owner)155 156 result = llvm.intr_memset(alloca, c_0, c_128, False)157 # CHECK: "llvm.intr.memset"(%[[ALLOCA]], %[[CST0]], %[[CST128]]) <{isVolatile = false}> : (!llvm.ptr, i8, i32) -> ()158 print(result)159 160 161# CHECK-LABEL: testTranslateToLLVMIR162@constructAndPrintInModule163def testTranslateToLLVMIR():164 with Context(), Location.unknown():165 module = Module.parse(166 """\167 llvm.func @add(%arg0: i64, %arg1: i64) -> i64 { 168 %0 = llvm.add %arg0, %arg1 : i64 169 llvm.return %0 : i64 170 }171 """172 )173 # CHECK: define i64 @add(i64 %0, i64 %1) {174 # CHECK: %3 = add i64 %0, %1175 # CHECK: ret i64 %3176 # CHECK: }177 print(llvm.translate_module_to_llvmir(module.operation))178