1# RUN: toyc-ch3 %s -emit=ast 2>&1 | FileCheck %s 2 3# User defined generic function that operates on unknown shaped arguments. 4def multiply_transpose(a, b) { 5 return transpose(a) * transpose(b); 6} 7 8def main() { 9 # Define a variable `a` with shape <2, 3>, initialized with the literal value. 10 # The shape is inferred from the supplied literal. 11 var a = [[1, 2, 3], [4, 5, 6]]; 12 # b is identical to a, the literal array is implicitly reshaped: defining new 13 # variables is the way to reshape arrays (element count in literal must match 14 # the size of specified shape). 15 var b<2, 3> = [1, 2, 3, 4, 5, 6]; 16 17 # This call will specialize `multiply_transpose` with <2, 3> for both 18 # arguments and deduce a return type of <2, 2> in initialization of `c`. 19 var c = multiply_transpose(a, b); 20 # A second call to `multiply_transpose` with <2, 3> for both arguments will 21 # reuse the previously specialized and inferred version and return `<2, 2>` 22 var d = multiply_transpose(b, a); 23 # A new call with `<2, 2>` for both dimension will trigger another 24 # specialization of `multiply_transpose`. 25 var e = multiply_transpose(b, c); 26 # Finally, calling into `multiply_transpose` with incompatible shape will 27 # trigger a shape inference error. 28 var f = multiply_transpose(transpose(a), c); 29} 30 31 32# CHECK: Module: 33# CHECK-NEXT: Function 34# CHECK-NEXT: Proto 'multiply_transpose' @{{.*}}ast.toy:4:1 35# CHECK-NEXT: Params: [a, b] 36# CHECK-NEXT: Block { 37# CHECK-NEXT: Return 38# CHECK-NEXT: BinOp: * @{{.*}}ast.toy:5:25 39# CHECK-NEXT: Call 'transpose' [ @{{.*}}ast.toy:5:10 40# CHECK-NEXT: var: a @{{.*}}ast.toy:5:20 41# CHECK-NEXT: ] 42# CHECK-NEXT: Call 'transpose' [ @{{.*}}ast.toy:5:25 43# CHECK-NEXT: var: b @{{.*}}ast.toy:5:35 44# CHECK-NEXT: ] 45# CHECK-NEXT: } // Block 46# CHECK-NEXT: Function 47# CHECK-NEXT: Proto 'main' @{{.*}}ast.toy:8:1 48# CHECK-NEXT: Params: [] 49# CHECK-NEXT: Block { 50# CHECK-NEXT: VarDecl a<> @{{.*}}ast.toy:11:3 51# CHECK-NEXT: Literal: <2, 3>[ <3>[ 1.000000e+00, 2.000000e+00, 3.000000e+00], <3>[ 4.000000e+00, 5.000000e+00, 6.000000e+00]] @{{.*}}ast.toy:11:11 52# CHECK-NEXT: VarDecl b<2, 3> @{{.*}}ast.toy:15:3 53# CHECK-NEXT: Literal: <6>[ 1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00, 5.000000e+00, 6.000000e+00] @{{.*}}ast.toy:15:17 54# CHECK-NEXT: VarDecl c<> @{{.*}}ast.toy:19:3 55# CHECK-NEXT: Call 'multiply_transpose' [ @{{.*}}ast.toy:19:11 56# CHECK-NEXT: var: a @{{.*}}ast.toy:19:30 57# CHECK-NEXT: var: b @{{.*}}ast.toy:19:33 58# CHECK-NEXT: ] 59# CHECK-NEXT: VarDecl d<> @{{.*}}ast.toy:22:3 60# CHECK-NEXT: Call 'multiply_transpose' [ @{{.*}}ast.toy:22:11 61# CHECK-NEXT: var: b @{{.*}}ast.toy:22:30 62# CHECK-NEXT: var: a @{{.*}}ast.toy:22:33 63# CHECK-NEXT: ] 64# CHECK-NEXT: VarDecl e<> @{{.*}}ast.toy:25:3 65# CHECK-NEXT: Call 'multiply_transpose' [ @{{.*}}ast.toy:25:11 66# CHECK-NEXT: var: b @{{.*}}ast.toy:25:30 67# CHECK-NEXT: var: c @{{.*}}ast.toy:25:33 68# CHECK-NEXT: ] 69# CHECK-NEXT: VarDecl f<> @{{.*}}ast.toy:28:3 70# CHECK-NEXT: Call 'multiply_transpose' [ @{{.*}}ast.toy:28:11 71# CHECK-NEXT: Call 'transpose' [ @{{.*}}ast.toy:28:30 72# CHECK-NEXT: var: a @{{.*}}ast.toy:28:40 73# CHECK-NEXT: ] 74# CHECK-NEXT: var: c @{{.*}}ast.toy:28:44 75# CHECK-NEXT: ] 76 77